Solutions#
Common problems in checking numerical code, each with code that solves it
using ExactRef. For your inputs, ExactRef gives the exact answer rounded once
and which outputs a correct implementation can produce, so most solutions end
by checking your output with covers().
Testing:
Find a bug in a matmul backend: a backend whose matmul passes
assert_close, and what ExactRef finds wrong with it.Replace hand-picked test tolerances:
allcloseandassert_closereject correct answers and accept real bugs.Test a custom kernel: check a GPU, Triton, Metal, or C++ kernel at your model's shapes, in any order of additions.
Check a PyTorch operation across dtypes: float32, float16, and bfloat16 on any device, including the roundings PyTorch adds.
Check low-precision kernels: bfloat16 accumulators, float8 conversions and products, and TF32 inputs.
Accept nondeterministic reductions: atomics and split-K give a different answer each run; check that each one is correct.
Check gradients and custom backward kernels: a custom
autograd.Functionor a fused backward.Check fused attention kernels: online softmax, bfloat16 weights, and other FlashAttention methods.
Check an FFT library: spectra at every length, including primes, and a tighter check from the library's plan.
Investigating:
Compare the accuracy of two implementations: measure errors exactly, in ulps.
Find the step that loses accuracy: check each intermediate of a computation with several steps.
Decide whether inf or NaN is legitimate: overflow the exact answer has, overflow from a poor order of additions, and NaN the inputs do or do not force.
Beyond Python tests:
Generate golden reference values: test vectors with bounds, for hardware, compilers, and other languages.
Learn and teach floating-point rounding: exact values, ties, rounding modes, small formats, and the order of additions.