Solutions#
Common problems in checking numerical code, each with code that solves it using ExactRef.
Testing:
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 across 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 and float16 accumulators, float8 conversions 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.
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 near the top of the range, overflow from a poor order, and NaN the inputs 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.