Numerical Methods and Floating Point
Overview
Section titled “Overview”- Primary references: Goldberg, What Every Computer Scientist Should Know About Floating-Point Arithmetic (free); Higham, Accuracy and Stability of Numerical Algorithms (SIAM, 2nd ed.)
- Supplementary: Trefethen and Bau, Numerical Linear Algebra (SIAM); Muller et al., Handbook of Floating-Point Arithmetic; the OCP Microscaling Formats (MX) v1.0 spec (free); Micikevicius et al., FP8 Formats for Deep Learning (free)
- Prerequisites: Precalculus, Calculus 1 and Calculus 2 (Taylor series), Linear Algebra
- Estimated time: 3 to 4 weeks at 10 to 12 h/week; in the course, Pass 2 (M09.1, M09.2) and Pass 6 (M09.3 to M09.6)
Key Takeaways
Section titled “Key Takeaways”- A float is a sign, an exponent, and a mantissa, and every operation rounds to the nearest representable value (round to nearest, ties to even). The gap between neighbours, one ULP, grows with magnitude.
- Most numerical bugs are cancellation or overflow, and both have standard cures: subtract the maximum before
exp(log-sum-exp), sum in pairs or with compensation (Kahan). - Tolerances are derived, not guessed. A dot product of length in a format with unit roundoff is off by at most about ; tests should assert that bound, not
1e-5. - Low precision is an encoding problem. bf16, fp16, fp8, and MX formats differ in how many bits go to range vs precision; quantization picks a scale so values land where the format is dense.
- Transcendentals in C are polynomials plus range reduction, and
rsqrtis Newton’s method with a good first guess.
How to Study
Section titled “How to Study”Read Goldberg once quickly, then Higham chapters 1 to 4 with a Python session: reproduce every cancellation example with numpy.float32. Decompose floats by hand (struct.pack) until reading bits is easy. In the course, M09.1 and M09.2 come in Pass 2 because autograd needs stable softmax; M09.3 to M09.6 come in Pass 6 with the inference engine and the C kernels.
Concepts & Techniques
Section titled “Concepts & Techniques”Core Insight
Section titled “Core Insight”Real numbers do not fit in 32 bits, so every computation is an approximation, and the job is to know how far off it is. Floating point fails in a few predictable ways (overflow, underflow, cancellation, accumulation of rounding error), each with a known algorithmic fix and a provable error bound. The same bound that explains why a kernel is accurate becomes the tolerance its test asserts.
1. IEEE-754 anatomy and rounding
Section titled “1. IEEE-754 anatomy and rounding”Key ideas:
- Layout: f32 is 1 sign, 8 exponent, 23 mantissa bits; bf16 keeps f32’s exponent with 7 mantissa bits; fp16 has 5 and 10.
- Rounding to nearest even through a
uint32view is how bf16 is emulated in numpy (M09.1, used byL7.9weight loading andL11.1mixed precision).
2. Stable numerics
Section titled “2. Stable numerics”Key ideas:
- Log-sum-exp: with ; softmax and log-softmax follow, finite at , and a fully masked row gives zeros.
- Summation: Kahan compensation and pairwise summation keep long reductions (perplexity over tokens) accurate (
M09.2).
3. Error analysis and tolerance budgets
Section titled “3. Error analysis and tolerance budgets”Key ideas:
- Unit roundoff and bound the error of accumulated operations.
- Condition number separates a bad problem from a bad algorithm.
- Call sites:
M09.3gives the learner’s own differential tests their bounds,L8.5the quantization error budget, and optionalL9.1the standalone C matmul parity bound.
4. Low-precision formats and kernels in C
Section titled “4. Low-precision formats and kernels in C”Key ideas:
- fp8 E4M3 and E5M2, and MX block formats with a shared E8M0 scale per 32 values (
M09.4, used byL8.5,L9.4, and the KV format v2 migration). - Fast inverse square root: a bit-level first guess plus two Newton steps (
M09.5, used bytl_rmsnorm_f32). expf: range reduction , a minimax polynomial for , and a scale by (M09.6, used by softmax, attention, and SiLU kernels).
Course modules
Section titled “Course modules”| Module | Topic | Kind | Pass |
|---|---|---|---|
S-M09a | Floating point and stable numerics problem set | solve | 2 |
S-M09b | Error analysis and low-precision problem set | solve | 6 |
M09.1 | IEEE-754 anatomy, ULP, RNE rounding, bf16/fp16 emulation | build | 2 |
M09.2 | Stable numerics: LSE, softmax, Kahan and pairwise sums | build | 2 |
M09.3 | Error analysis, condition numbers, tolerance budgets | build | 6 |
M09.4 | FP8 E4M3/E5M2, MXFP4/MXFP8 with E8M0 block scales; C conversions | build | 6 |
M09.5 | Fixed-point iteration, fast inverse sqrt in C | build | 6 |
M09.6 | Polynomial approximation and range reduction: expf in C | build | 6 |
M09.7 | Iterative solvers: conjugate gradient | build | optional |
Chapters
Section titled “Chapters”Connections to Other Tracks
Section titled “Connections to Other Tracks”| Track | Connection |
|---|---|
| Calculus 2 | Taylor series behind polynomial approximation |
| Matrix Calculus and Autodiff | VJPs that need stable softmax and normalization |
| tinyllm Part 8 | quantization and its error budget |
| tinyllm Part 9 | the C kernels that call tl_expf and the fast rsqrt |
| LLM Systems: quantization | production quantization formats as depth |