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.
★☆☆☆☆ 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
format
underflow → 0
overflow
relative step
fp32
0.0%
0.0%
2^−23
bf16
0.0%
0.0%
2^−7
fp16
36.3%
0.0%
2^−10
fp8 E5M2
89.5%
0.0%
2^−2
fp8 E4M3
99.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. Using fewer bits per number makes computation…
2. If a number is too small for the format, it may become…
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. bf16 has the same range as…
2. Largest finite fp16 value:
3. Which fp8 variant has more range?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.
Three failure modes
Overflow: activations or logits above the format max become ∞, then NaN (e.g. ∞ − ∞ in softmax).
Underflow: gradients below the smallest subnormal become 0. Learning silently stops for those weights.
Swamping: an update ηg smaller than half an ulp of the weight is rounded away. In fp16, 1+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 S (e.g. 212–216) before backprop and divide the gradients by S in fp32. Dynamic loss scaling lowers S 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. Loss scaling fixes…
2. Weights are kept in fp32 because…
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=0 with e=⌊log2∣x∣⌋, clamped below at emin=1−bias, the spacing is 2e−m for m fraction bits. Round-to-nearest gives relative error at most 2−m−1 in the normal range. Below 2emin the spacing is fixed (subnormals), so relative error grows until values round to 0.
When does an update survive?
w+Δ rounds back to w if ∣Δ∣<21ulp(w)=2⌊log2∣w∣⌋−m−1. For w=1 in fp16, updates below 4.9×10−4 vanish. In fp32 the threshold is 6×10−8.
Why loss scaling is exact (almost)
Multiplying by a power of two only changes exponents. So fl(Sg)=Sfl(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−4. Since 3×10−4 is smaller, the update is lost.
📝 Quick check (Level 3)
0/3 answered
1. Scaling by a power of two adds rounding error?
2. Half-ulp of 1.0 in fp16 ≈
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−7–10−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 212.
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
P. Micikevicius et al. (2018). “Mixed precision training.” ICLR.
P. Micikevicius et al. (2022). “FP8 formats for deep learning.” arXiv:2209.05433.
D. Kalamkar et al. (2019). “A study of BFLOAT16 for deep learning training.” arXiv:1905.12322.
B. Jacob et al. (2018). “Quantization and training of neural networks for efficient integer-arithmetic-only inference.” CVPR.
B. Rouhani et al. (2023). “Microscaling data formats for deep learning.” arXiv:2310.10537.
w + Δ rounds to w whenever |Δ| is less than ½ ulp(w).
Practice
Show:
Level 0
Problem 1Level 0
How many bits do fp16 and bf16 each use?
16.
Problem 2Level 0
How many bits does an fp8 number use?
8.
Problem 3Level 0
A number too small for the format becomes ____.
Zero (underflow).
Level 1
Problem 4Level 1
Largest finite fp16 value?
65504.
Problem 5Level 1
How many fraction bits does bf16 have?
7.
Problem 6Level 1
Largest fp8 E4M3 value?
448.
Problem 7Level 1
bf16 relative step 2⁻⁷?
0.0078125.
Level 2
Problem 8Level 2
Smallest fp16 subnormal is 2⁻ᵏ. Find k.
1−15−10=−24, so k = 24.
Problem 9Level 2
In bf16, what does 1 + 0.001 round to?
1: the spacing at 1 is 2⁻⁷ ≈ 0.0078.
Problem 10Level 2
By how many decades does a loss scale of 2¹⁰ shift every gradient?
10log102≈3.01.
Problem 11Level 2
Which fp8 variant is typically used for gradients?
E5M2 (more range).
Problem 12Level 2
Master weights in mixed-precision training are kept in which format?
fp32.
Problem 13Level 2
Does bf16 training usually need loss scaling (yes/no)?
No. It has fp32’s range.
Level 3
Problem 14Level 3
Show that power-of-two loss scaling introduces no rounding error in the normal range.
Multiplying by 2k adds k to the exponent and leaves the fraction unchanged. So fl(2kx)=2kfl(x). ∎
Problem 15Level 3
Derive the threshold below which an update to w is lost.
Round-to-nearest maps w+Δ to w if ∣Δ∣<21ulp(w)=2⌊log2∣w∣⌋−m−1. ∎
Problem 16Level 3
Explain why softmax should be computed in fp32 (or with max-subtraction) in fp16 networks.
exp of logits above about 11 overflows fp16 (e^11 ≈ 59874 is near 65504). Subtracting the max and accumulating in fp32 avoids ∞/∞ = NaN. ∎
Problem 17Level 3
Half an ulp at w = 1 in fp16 is 2⁻ᵏ. Find k.
21⋅2−10=2−11, so k = 11.
Level 4
Problem 18Level 4
Which technique multiplies the loss before backprop to avoid gradient underflow?
Loss scaling.
Problem 19Level 4
Which fp8 variant is typically used for forward activations and weights?
E4M3.
Problem 20Level 4
In what precision do tensor cores accumulate fp16 matrix products?
fp32.
Coding practice
🐍 Code 1 (Level 1): format maximum
(2 − 2^−m)·2^bias.
🐍 Code 2 (Level 1): relative step
2^−m.
🐍 Code 3 (Level 2): smallest subnormal
2^(1 − bias − m).
🐍 Code 4 (Level 2): loss-scaled fp16 gradient
Scale, cast to fp16, unscale.
🐍 Code 5 (Level 3): does the update survive?
True if |Δ| ≥ ½ ulp(w) for m fraction bits.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
fp32, fp16, bf16, fp8: Numerical Stability in Deep Learning: topic test
10 questions, drawn at random from a bank of 16. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.