Foundations to Frontier
Tests
A. Mathematical Foundations › A8 Numerical Methods and Scientific Computing

fp32, fp16, bf16, fp8: Numerical Stability in Deep Learning

Modern neural networks train and run in reduced precision to save memory, bandwidth and energy. This topic explains the trade-off between exponent bits (range) and fraction bits (precision) in fp32, fp16, bf16 and the two fp8 formats. You will see why gradients underflow in fp16, why updates vanish against large weights, and how mixed-precision training (fp32 master weights, loss scaling, higher-precision accumulation) fixes both. You will emulate every format in NumPy and reproduce a training run that stalls in pure fp16 and recovers with loss scaling.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Smaller numbers, faster chips

Imagine measuring everything with a ruler that has fewer marks. It’s lighter and quicker to read, but less precise, and it might be too short for long things or too coarse for tiny ones. AI chips do the same with numbers. Using 16 or even 8 bits instead of 32 makes computing several times faster and cheaper. The trick is making sure the numbers you care about still fit on the ruler.

Will your gradients fit? Number formats vs gradient magnitudes
fp32bf16fp16fp8 E5M2fp8 E4M31e-451e-301e-151e01e151e30
formatunderflow → 0overflowrelative step
fp320.0%0.0%2^−23
bf160.0%0.0%2^−7
fp1636.3%0.0%2^−10
fp8 E5M289.5%0.0%2^−2
fp8 E4M399.6%0.0%2^−3
fp16 has 10 fraction bits but only a 5-bit exponent, so small gradients fall below its smallest subnormal (6×10⁻⁸) and become zero. Multiplying the loss by 2^k shifts every gradient up by k·0.3 decades; the optimizer divides it back out in fp32. bf16 keeps fp32's 8-bit exponent (no scaling needed) at the cost of precision. fp8 needs per-tensor scaling factors to fit at all.
Why engineers care

Onboard AI on rovers, drones and satellites runs on power-limited hardware: Jetson modules, Edge TPUs, radiation-tolerant FPGAs. That hardware is fastest in 16-bit or 8-bit arithmetic. Large models are trained in bf16 or fp8 on GPU clusters. Knowing when low precision breaks a computation is essential for both.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Using fewer bits per number makes computation…
  2. 2. If a number is too small for the format, it may become…
  3. 3. Which formats do AI chips favour for speed?

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The formats

Format Exponent Fraction Relative step Max Smallest subnormal
fp32 8 23 1.2e−7 3.4e38 1.4e−45
bf16 8 7 7.8e−3 3.4e38 9.2e−41
fp16 5 10 9.8e−4 65504 6.0e−8
fp8 E5M2 5 2 0.25 57344 1.5e−5
fp8 E4M3 4 3 0.125 448 2.0e−3
  • Exponent bits set the range; fraction bits set the precision.
  • bf16 is fp32 with the bottom 16 bits removed: the same range, coarse precision.
  • fp16 is more precise than bf16 but has a much narrower range.
  • fp8: E4M3 is used for weights and activations, E5M2 for gradients (Micikevicius et al., 2022).
📝 Quick check (Level 1)
0/3 answered
  1. 1. bf16 has the same range as…
  2. 2. Largest finite fp16 value:
  3. 3. Which fp8 variant has more range?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Three failure modes

  1. Overflow: activations or logits above the format max become ∞, then NaN (e.g. ∞ − ∞ in softmax).
  2. Underflow: gradients below the smallest subnormal become 0. Learning silently stops for those weights.
  3. Swamping: an update ηg\eta g smaller than half an ulp of the weight is rounded away. In fp16, 1+10−4=11 + 10^{-4} = 1.

Mixed-precision training (Micikevicius et al., 2018)

  • fp32 master weights: forward and backward passes run in fp16 or bf16, but updates are applied to an fp32 copy.
  • Loss scaling: multiply the loss by SS (e.g. 2122^{12}–2162^{16}) before backprop and divide the gradients by SS in fp32. Dynamic loss scaling lowers SS whenever an overflow appears.
  • fp32 accumulation: matrix-multiply sums are accumulated in fp32 (tensor cores do this). Reductions such as softmax, layer norm and loss run in fp32.

bf16 usually needs no loss scaling, thanks to its range. fp8 needs per-tensor (or per-block) scaling factors, from delayed amax history or microscaling (MX) formats.

🐍 Exercise 1: emulating low-precision formats
Write a generic quantizer for (exponent bits, fraction bits) with subnormals and saturation. Check that it reproduces IEEE fp16 exactly. Then compare range, underflow, precision and the fp8 maxima across formats.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Loss scaling fixes…
  2. 2. Weights are kept in fp32 because…
  3. 3. Dynamic loss scaling reduces S when…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Rounding to a format

For x≠0x\ne0 with e=⌊log⁡2∣x∣⌋e = \lfloor\log_2\lvert x\rvert\rfloor, clamped below at emin⁡=1−biase_{\min} = 1 - \text{bias}, the spacing is 2e−m2^{e - m} for mm fraction bits. Round-to-nearest gives relative error at most 2−m−12^{-m-1} in the normal range. Below 2emin⁡2^{e_{\min}} the spacing is fixed (subnormals), so relative error grows until values round to 0.

When does an update survive?

w+Δw + \Delta rounds back to ww if ∣Δ∣<12ulp⁡(w)=2⌊log⁡2∣w∣⌋−m−1\lvert\Delta\rvert < \frac12\operatorname{ulp}(w) = 2^{\lfloor\log_2\lvert w\rvert\rfloor - m - 1}. For w=1w = 1 in fp16, updates below 4.9×10−44.9\times10^{-4} vanish. In fp32 the threshold is 6×10−86\times10^{-8}.

Why loss scaling is exact (almost)

Multiplying by a power of two only changes exponents. So fl⁡(Sg)=Sfl⁡(g)\operatorname{fl}(Sg) = S\operatorname{fl}(g) whenever both are in the normal range, and the scaling adds no rounding error. It just moves values out of the subnormal and underflow region.

Exercise 3.1: does the update 3×10⁻⁴ survive on a weight of 1.0 in fp16?

Half an ulp at 1.0 is 2−11≈4.9×10−42^{-11}\approx4.9\times10^{-4}. Since 3×10−43\times10^{-4} is smaller, the update is lost.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Scaling by a power of two adds rounding error?
  2. 2. Half-ulp of 1.0 in fp16 ≈
  3. 3. Below the normal range, relative error…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Reproducing mixed-precision training

A linear model whose gradients sit around 10−710^{-7}–10−1010^{-10} (typical late in training, or for weights with weak signal) is trained four ways:

  • in fp32;
  • in pure fp16, with weights and gradients both in fp16;
  • in mixed precision, with fp16 gradients and fp32 master weights but no scaling;
  • in mixed precision with a loss scale of 2122^{12}.

Without scaling, most gradients underflow to zero and the loss stalls about 4000× above the fp32 result. With loss scaling plus fp32 master weights, the run is indistinguishable from fp32.

🐍 Exercise 2 (research): loss scaling rescues fp16 training
Train a regression model with tiny gradients in fp32, pure fp16, mixed precision without loss scaling, and mixed precision with loss scaling. Compare the final losses.

Low precision in AI, robotics and aerospace

  • Training at scale: mixed precision (Micikevicius et al., 2018), bf16 on TPUs (Kalamkar et al., 2019), and fp8 training of large transformers (Micikevicius et al., 2022; DeepSeek-V3, 2024).
  • Edge inference: int8 and fp8 quantized perception networks on Jetson and Coral-class hardware, with post-training quantization or quantization-aware training (Jacob et al., 2018).
  • Space hardware: FPGA and radiation-tolerant processors favour fixed-point and low-precision arithmetic; precision is chosen per layer.
  • Numerical safety: keep softmax, normalization, loss and covariance updates in fp32. Watch for NaN propagation with inf/NaN checks.

References

  1. P. Micikevicius et al. (2018). “Mixed precision training.” ICLR.
  2. P. Micikevicius et al. (2022). “FP8 formats for deep learning.” arXiv:2209.05433.
  3. D. Kalamkar et al. (2019). “A study of BFLOAT16 for deep learning training.” arXiv:1905.12322.
  4. B. Jacob et al. (2018). “Quantization and training of neural networks for efficient integer-arithmetic-only inference.” CVPR.
  5. B. Rouhani et al. (2023). “Microscaling data formats for deep learning.” arXiv:2310.10537.
  6. DeepSeek-AI (2024). “DeepSeek-V3 technical report.” arXiv:2412.19437.
  7. N. Higham, T. Mary (2022). “Mixed precision algorithms in numerical linear algebra.” Acta Numerica, 31.
  8. IEEE (2019). IEEE Standard for Floating-Point Arithmetic, IEEE 754-2019.