RiptideRiptide
Download Riptide SDKInstall Riptorch
Menu
Riptorch docs · Guide

Switch in one line

What changes when you replace import torch, how to check where your model runs, and how to get stock behaviour back.

The one-line change

Riptorch implements PyTorch’s Python API. Change the import and keep the rest of your code:

import riptorch as torch          # was: import torch
from riptorch import nn           # was: from torch import nn
import riptorch.nn.functional as F

Models, optimizers, state_dict, torch.save and torch.load, autograd and the data loaders keep working as they are. If a library you use does import torch itself, import Riptorch first: it registers itself under that name, and torch.is_torch_alias() tells you when that happened.

Check what you’re running on

Four calls tell you what you’ve got. The outputs on this page were recorded on an Apple M1 Pro with RIPTORCH_DEVICE=cpu, so backend() names the CPU; on a Mac with its GPU available it names the Metal GPU.

Example
import riptorch as torch

print(torch.__version__)
print(torch.backend())
print(torch.matrix_unit())
print(torch.get_compat())
print(len(torch.improvements()), "defaults differ from stock PyTorch")
Output, recorded on an Apple M1 Pro (CPU)
2.15.0+riptorch
riptide cpu (10 cores, clang)
AMX
riptorch
52 defaults differ from stock PyTorch
  • torch.backend() is the engine that runs large operations.
  • torch.matrix_unit() is the CPU’s matrix unit: "SME" on M4 and later, "AMX" on M1 to M3, "none" elsewhere.
  • torch.get_compat() is "riptorch" unless you switched to stock behaviour.
  • torch.improvements() lists every default that differs from stock PyTorch, each with its measurement.

A training loop, unchanged

Ordinary PyTorch code runs as it is. After it runs, torch.realizer_report() says where the work went: how many GPU programs, how many CPU programs, and why a graph stayed off the GPU when it did (a small graph where a GPU round trip costs more than the work, for example, or float64).

Example
import riptorch as torch
from riptorch import nn

torch.manual_seed(0)
model = nn.Sequential(nn.Linear(16, 32), nn.ReLU(), nn.Linear(32, 4))
opt = torch.optim.AdamW(model.parameters(), lr=1e-2)
x = torch.randn(64, 16)
y = torch.randint(0, 4, (64,))

for step in range(5):
    opt.zero_grad()
    loss = nn.functional.cross_entropy(model(x), y)
    loss.backward()
    opt.step()
    print(f"step {step}: loss {loss.item():.4f}")

print(torch.realizer_report())
Output, recorded on an Apple M1 Pro (CPU)
step 0: loss 1.4076
step 1: loss 1.3634
step 2: loss 1.3250
step 3: loss 1.2897
step 4: loss 1.2568
cpu realizer (clang): 36.9 ms in graphs; programs 3.1% (5 runs), program builds 96.9%, interpreter 0.0% (0 graphs), reference interpreter 0.0% (0 graphs)

Riptorch builds a compiled program for each new graph shape and caches it, so the first steps of a new model include build time. Later steps reuse the programs.

What changes: correct by default

Where stock PyTorch gives a wrong or degraded answer, Riptorch gives the correct one. The two you’ll meet first:

  • Integer matrix multiplies are exact. PyTorch keeps the input dtype for int8 @ int8, so any sum that doesn’t fit wraps around silently. Riptorch returns int32 for 8-bit inputs and int64 for 16- and 32-bit inputs, and the result is the exact integer.
  • Sums that PyTorch rounds badly are rounded once. index_add with repeated indices, for example, adds in float64 and rounds once, so [1e8, 1, -1e8] at one index gives 1.0 where stock float32 gives 0.0.
Example
import riptorch as torch

a = torch.tensor([[100, -100, 127]], dtype=torch.int8)
b = torch.tensor([[127], [-128], [127]], dtype=torch.int8)
print("default:", a @ b)            # exact: 100*127 + 100*128 + 127*127 = 41629

torch.set_compat("pytorch")
print("pytorch:", a @ b)            # stock behaviour: int8 result, wrapped modulo 256
torch.set_compat("riptorch")

x = torch.zeros(2)
x.index_add_(0, torch.tensor([0, 0, 0]), torch.tensor([1e8, 1.0, -1e8]))
print(x)                            # the exact sum, 1.0 (stock float32 gives 0.0)

e = torch.improvements("index_add")
print(e["headline"])
print("switch back:", e["compat"])
Output, recorded on an Apple M1 Pro (CPU)
default: tensor([[41629]], dtype=riptorch.int32)
pytorch: tensor([[-99]], dtype=riptorch.int8)
tensor([1., 0.])
repeated indices summed in float64 and rounded once: [1e8, 1, -1e8] at one index gives 1.0, stock's float32 sum gives 0.0
switch back: torch.set_compat("pytorch")

There are 52 such defaults in all: the full list, grouped by area, is on Riptorch extras. Each entry says what stock PyTorch does, what Riptorch does and how it was measured.

Getting stock behaviour back

torch.set_compat("pytorch") switches to stock PyTorch’s behaviour wherever it can be reproduced: integer products keep the input dtype and wrap, int8 @ uint8 raises, float64 runs on the CPU. Set RIPTORCH_COMPAT=pytorch to have it from import. torch.set_compat("riptorch") switches back.

Use it to check that a difference you see between Riptorch and PyTorch comes from one of these defaults, or to reproduce a result recorded with stock PyTorch bit for bit where integer wraparound was part of it.

What isn’t there yet

  • Every public PyTorch name exists, so imports never fail. A part that isn’t implemented yet raises NotImplementedError saying what’s missing; the reference marks those entries “Different from PyTorch”.
  • NVIDIA GPUs (CUDA) aren’t supported yet: on an NVIDIA machine, Riptorch runs on the CPU. Platforms and install notes are on the Riptorch page.