Skip to content

Precision

Where the arithmetic runs

  • MXU: matmul, bmm, convolutions. bf16 inputs (fp8 on newer chips such as v7x), float32 accumulation.
  • VPU: elementwise ops, activations, norms, softmax. float32 tensors compute in full float32.
  • bf16: float32's 8-bit exponent, 7 mantissa bits (epsilon \(2^{-7}\)): same range, two to three decimal digits.

float32 matmul precision

torch.set_float32_matmul_precision torch.tpu.Precision MXU passes Cost Relative error, random data
"medium" (TorchTPU default) DEFAULT 1 1x about 2e-3
"high" HIGH 3 about 3x
"highest" HIGHEST 6 about 6x float32 level
  • CUDA's default is "highest"; a CUDA script's "high" (chosen for speed) means 3 passes on TPU. Translate by intent: use "medium".
  • Scope it: with torch.tpu.precision(torch.tpu.Precision.HIGHEST): around the call to a compiled function, never inside it (it is a graph break; with fullgraph=True it raises).
  • Only float32 contractions are affected. bf16 matmuls take one pass in every mode.

Excess precision

torch.tpu.allow_excess_precision Compiled code
False (default) keeps the bf16 downcasts between fused ops: matches eager rounding
True may keep fused intermediates in float32: more accurate, differs from eager

The default is False (Manage numerical precision in TorchTPU). Set it at the top of the script, before any TPU work: it locks at the first compilation and a later set raises RuntimeError.

Mixed precision pattern

  • Weights and activations in bf16; torch.amp.autocast("tpu") (default dtype bf16; torch.tpu.get_amp_supported_dtype()).
  • float32 for gradient reduction, RoPE, optimizer state; take the accumulator explicitly with out_dtype=torch.float32 when a reduction follows.
  • Raise precision only for a layer with a demonstrated precision problem.

Tolerances (torch.testing.assert_close defaults)

dtype rtol atol
torch.float32 1.3e-6 1e-5
torch.bfloat16 1.6e-2 1e-5
torch.float16 1e-3 1e-5

For matmuls, bound the error by \(|A|\,|B|\), not by the output: \(|C_{ij} - \hat C_{ij}| \le (2u + K \cdot 2^{-24}) \sum_k |A_{ik}||B_{kj}|\), with \(u = 2^{-8}\) for bf16 inputs and \(K\) the contracting dimension.

Rules for parity

Situation Do
bf16 TPU against float32 CPU not a parity test: compare bf16 with bf16, or float32 under HIGHEST with float32
float32 result differs from GPU by about 1e-3 expected at "medium"; rerun under HIGHEST to check the hardware
compiled differs from eager in the last bits fusion order or excess precision; compare with a tolerance, not torch.equal
a few near-zero elements fail cancellation: use the $
training diverges after the port parity per layer first, then HIGHEST on the suspect layer
default-precision matmul matches CPU to 1e-7 it did not take one bf16 pass: check torch.get_float32_matmul_precision() and the device