RiptideRiptide
Download Riptide SDKInstall Riptorch
Menu
Riptorch docs · Guide

Exact integer matmul

int8 and int16 matrix multiplies with no wraparound, and where they run.

PyTorch’s integer matmul wraps around

In stock PyTorch, int8 @ int8 returns int8. Every output is a sum of products of up to 127^2 each, so almost any real sum overflows 8 bits, and it wraps modulo 256 with no error and no warning. On a 10×64 @ 64×20 product with values spread over the int8 range, 200 of 200 outputs were wrong; int16 and int32 behave the same way. int8 @ uint8 raises.

Riptorch computes integer products exactly:

inputsRiptorch resultexact for
int8 @ int8, uint8 @ uint8, int8 @ uint8int32inner dimension below 2^{17}
int16 @ int16 (and uint16, mixed)int64every int16 input
int32 @ int32, int64 @ int64int64whenever the sum fits in int64

In practice

Example
import numpy as np
import riptorch as torch

rng = np.random.default_rng(1)
a8 = rng.integers(-128, 128, size=(4, 512), dtype=np.int8)
b8 = rng.integers(-128, 128, size=(512, 3), dtype=np.int8)

c = torch.from_numpy(a8) @ torch.from_numpy(b8)
exact = a8.astype(np.int64) @ b8.astype(np.int64)
print(c.dtype, "equals the exact int64 product:", bool((c.numpy() == exact).all()))
print("NumPy int8 @ int8 (wraps):", (a8 @ b8)[0].tolist(), "exact:", exact[0].tolist())

# int16 extremes: (-32768)^2 * 3 = 3,221,225,472 does not fit in 32 bits
w = torch.tensor([[-32768, -32768, -32768]], dtype=torch.int16)
print((w @ w.T).dtype, (w @ w.T).item())

# signed x unsigned 8-bit: stock PyTorch raises here
u = torch.tensor([[255], [255], [255]], dtype=torch.uint8)
m = torch.tensor([[-128, -128, -128]], dtype=torch.int8)
print("int8 @ uint8 ->", (m @ u).dtype, (m @ u).item())

print("matrix unit:", torch.matrix_unit(), torch.matrix_unit_modes()[:5])
print("integer matmuls run on:", torch.get_int_matmul())
Output, recorded on an Apple M1 Pro (CPU)
int32 equals the exact int64 product: True
NumPy int8 @ int8 (wraps): [-75, -14, 101] exact: [20149, 12274, -18075]
int64 3221225472
int8 @ uint8 -> int32 -97920
matrix unit: AMX ['s8s8_i32', 'u8u8_i32', 's8u8_i32', 'u8s8_i32', 's16s16_i32']
integer matmuls run on: auto

The int16 line shows why 16-bit inputs give int64: 3 \cdot 32768^2 = 3{,}221{,}225{,}472 doesn’t fit in 32 bits. NumPy’s own int8 @ int8 wraps exactly as PyTorch’s does.

Where it runs

  • The CPU’s matrix unit. Apple M4 and later have SME, M1 to M3 have AMX. torch.matrix_unit_modes() lists the input and output types this CPU multiplies on it, using every core. Other processors use portable kernels and give the same integers.
  • The GPU’s int8 matrix hardware. Large products go there automatically: from 2^{24} multiply-adds for 8-bit inputs and 2^{25} for 16-bit. Smaller products stay on the CPU, where a GPU round trip would cost more than the work.

The integers are identical on both sides, so the placement only changes speed. torch.set_int_matmul("cpu") or "gpu" pins it (RIPTORCH_INT_MATMUL from import); "auto" is the default.

16-bit and mixed-sign products run on the 8-bit hardware by splitting each number into 8-bit digits; How the exact maths works shows how that stays exact.

Uses

  • Quantised inference. int8 weights and activations multiplied exactly, then rescaled once.
  • Exact similarity scores. The dot products behind exact vector search.
  • Reproducible results. Integer sums don’t depend on the order of addition, so the result is the same on every device and every run.

Stock behaviour

torch.set_compat("pytorch") computes the exact product and then stores it in the input dtype, wrapping modulo 2^{\text{bits}}, which equals stock PyTorch entry for entry. int8 @ uint8 raises again.