Floating-point precision: set_matmul
torch.set_matmul(mode) sets how floating-point matrix multiplies are computed. It covers matmul, linear layers, convolutions and the attention backward, and you can change it between steps (compiled programs are cached per mode).
| mode | what it does | use it for |
|---|---|---|
"fp32" (default) | float32 inputs and accumulation on the fastest hardware, with fused multiply-adds | training and inference |
"bf16", "fp16" | each multiply rounds its inputs to 16 bits and accumulates in float32; weights, gradients and optimizer state stay float32 | mixed-precision speed |
"exact" | the reference order: multiply, then add, in a fixed order, no fused multiply-add | bit-for-bit reproducible results |
"fast" (Apple GPU) | scaled half-precision multiplies that never overflow, float32 accumulation | speed with float32-level accuracy |
"high" (Apple GPU) | float32 as two half-precision pieces: about twice float32’s accuracy at about float32’s speed | accuracy-sensitive work |
Here are the CPU modes on a 64×1024 @ 1024×64 product, against a float64 reference:
import numpy as np
import riptorch as torch
rng = np.random.default_rng(2)
A = rng.standard_normal((64, 1024)).astype(np.float32)
B = rng.standard_normal((1024, 64)).astype(np.float32)
ref = A.astype(np.float64) @ B.astype(np.float64)
a, b = torch.from_numpy(A), torch.from_numpy(B)
for mode in ["fp32", "bf16", "fp16", "exact"]:
torch.set_matmul(mode)
c = (a @ b).numpy().astype(np.float64)
err = np.abs(c - ref).max() / np.abs(ref).max()
print(f"{mode:6} max relative error {err:.1e}")
torch.set_matmul("exact")
first, second = (a @ b).numpy(), (a @ b).numpy()
print("exact: the same bits run to run:", first.tobytes() == second.tobytes())
torch.set_matmul("fp32")
print("back to", torch.get_matmul())fp32 max relative error 1.3e-06 bf16 max relative error 2.2e-03 fp16 max relative error 3.7e-04 exact max relative error 1.3e-06 exact: the same bits run to run: True back to fp32
RIPTORCH_MATMUL=bf16 (or any mode) sets it from import; torch.get_matmul() reads it back.
Using the GPU and the CPU together: RIPTORCH_HYBRID
On Apple silicon the GPU and the CPU’s matrix unit share memory. With RIPTORCH_HYBRID=1, a large float32, bfloat16 or float16 matrix multiply is split between them: the CPU computes the last rows of the output while the GPU computes the rest, at the same time.
| variable | default | meaning |
|---|---|---|
RIPTORCH_HYBRID | off | 1 turns the split on |
RIPTORCH_HYBRID_SHARE | 0.22 | the share of output rows the CPU computes |
RIPTORCH_HYBRID_MIN | 2^{30} multiply-adds | the smallest product that is split |
The split is never used for "exact" (the two sides’ rounding differs), for batched products or for a transposed first operand.
Integer matmul placement: set_int_matmul
Exact integer products (int8, uint8, int16, uint16) run on the GPU’s int8 hardware for large products and on the CPU’s matrix unit for small ones. torch.set_int_matmul("auto" | "gpu" | "cpu") or RIPTORCH_INT_MATMUL changes that; the integers are identical either way. See Exact integer matmul.
Choosing the device: RIPTORCH_DEVICE
| value | effect |
|---|---|
unset or auto | the GPU when there is a usable one, else the CPU (with one line saying why) |
cpu | everything on the CPU |
gpu, metal or mps | the Apple GPU; an error if there isn’t a usable one (in a virtual machine, for example) |
amd | an AMD Instinct GPU on Linux |
.to("mps"), .cuda() and device= arguments are accepted as in PyTorch. On Apple silicon there is one memory, so moving a tensor between devices doesn’t copy it.
float64
Apple GPUs have no float64 hardware. By default (set_float64("auto")), float64 work runs in IEEE float64 on the CPU. set_float64("fast") runs it on the GPU in double-float arithmetic (two float32 values per number, about 48 significant bits) wherever that is faster. RIPTORCH_FLOAT64 sets it from import.
Threads and the compiled-program cache
| variable | meaning |
|---|---|
RIPTORCH_NUM_THREADS | CPU threads (default: OMP_NUM_THREADS, else the performance cores) |
RIPTORCH_CPU_CACHE_DIR | where compiled CPU programs are kept between runs |
RIPTORCH_CPU_CACHE_MB | the cache’s size cap (default 512; 0 keeps nothing on disk) |
Every environment variable is listed on Riptorch extras.