2 · FP16 and BF16: range versus precision
Core · minutes 06–16. Start with this prediction: “Both formats store 16 bits. Do they represent the same numbers?” Ask learners to defend their answer before showing the table.
What the bits buy
A floating-point value uses a sign, an exponent and a fraction. The exponent controls the scale of representable numbers. Fraction bits control how closely values are spaced at a given scale. More range does not mean more detail near one.
| Format | Sign | Exponent | Stored fraction | Largest finite (approx.) | Smallest normal (approx.) | Gap above 1 |
|---|---|---|---|---|---|---|
| FP32 | 1 | 8 | 23 | 3.403×10³⁸ | 1.175×10⁻³⁸ | 2⁻²³ ≈ 0.000000119 |
| FP16 | 1 | 5 | 10 | 65,504 | 6.104×10⁻⁵ | 2⁻¹⁰ ≈ 0.000977 |
| BF16 | 1 | 8 | 7 | 3.390×10³⁸ | 1.175×10⁻³⁸ | 2⁻⁷ = 0.0078125 |
The fraction column excludes the implicit leading bit in normal values. torch.finfo(dtype).tiny reports the smallest normal value, not the smallest subnormal. Subnormal availability and actual operation behaviour depend on the execution path; hardware may flush small results.
Compare the table above; the interactive representation examples load with JavaScript.
Three numbers, three different lessons
For direct PyTorch casts with round-to-nearest representation:
| Input | FP16 cast | BF16 cast | Interpretation |
|---|---|---|---|
| 1.001 | 1.0009765625 | 1.0 | FP16 retains finer detail here |
| 100,000 | infinity | 99,840 | BF16 has much wider range |
| 0.00000001 | 0.0 | about 1.00117×10⁻⁸ | FP16 loses this tiny value |
These are number-representation examples, not measurements of complete training behaviour. AMP does not cast every operation or every stored tensor to the same dtype. A FP32 reduction may avoid overflow that a direct FP16 cast would cause.
Why gradients can disappear
Backpropagation multiplies and accumulates quantities. Small gradients may fall below FP16's representable range. Gradient scaling multiplies the loss by a scale before backward so intermediate gradients are larger. Before clipping and the optimizer step, the scaler unscales gradients.
Scaling does not create extra fraction bits. It does not correct an invalid loss, fix a mismatched target, or repair overflow that already happened in the forward calculation. If an FP16 gradient check finds nonfinite values, GradScaler can skip the optimizer update and lower its scale for the next attempt.
BF16 has an FP32-like exponent range, which often removes the need for loss scaling. In this lesson BF16 uses autocast without a scaler. BF16 still rounds more coarsely near one and still requires numerical and quality checks.
Which policy should we try?
Begin with a working FP32 reference. On CUDA hardware with native BF16 support, BF16 autocast is a reasonable first low-precision experiment because of its range. On compatible CUDA hardware without native BF16 support, use FP16 autocast with GradScaler. Preserve FP32 where required for numerically sensitive calculations.
The notebook explicitly checks CUDA availability and native BF16 support. CPU FP32 is the universal teaching route here; CPU BF16 execution illustrates the API without promising acceleration. Requesting CUDA FP16 on CPU raises an actionable error instead of relabelling an FP32 run as AMP.
Repair the claim
“A BF16 run is more accurate because BF16 represents larger numbers.”
Suggested response
Wider range reduces certain overflow and underflow problems. Accuracy depends on the task, optimization and rounding effects as well. BF16 has fewer fraction bits than FP16, so it is coarser near one. Compare held-out quality under a fixed protocol rather than inferring it from the exponent alone.
Notebook handoff: in setup and handoffs, run 01 · FP16 versus BF16 immediately before 02 · AMP. Let learners predict all three casts first.
References: PyTorch AMP, NVIDIA floating-point formats, AMD BF16 format.