Mixed-precision training
Explain bf16 matmuls with fp32 master weights and fp32 accumulation, and why fp16 also needs loss scaling. Identify which operations (softmax, norms, loss) are kept in higher precision.
Builds on: Floating point: fp32, fp16, bf16, int8, The LLM training loop
What the animation says
Why not all 16-bit?
Sixteen-bit numbers take half the memory of thirty-two-bit ones, and move half the data. GPU matrix units also run bf16 matrix multiplies many times faster, and those multiplies are nearly all of training's arithmetic. So why not store and compute everything in bf16? Because things break. Tiny weight updates vanish, and long sums lose precision. With fp16, tiny gradients even underflow to zero. Mixed precision patches each weak spot right where it breaks.
Updates that vanish
Take one weight, exactly 1.0, nudged down by 0.00005 every step. In fp32, the nudge lands and the weight ticks down. But bf16 keeps only about eight significant bits. Just below 1, its neighbouring values are about 0.004 apart. A nudge of 0.00005 is far less than half that gap, so it rounds back to 1.0. After a thousand steps, where does the bf16 weight end up?
Sums that stall
The bf16 weight never moved: still exactly 1.0. The fp32 weight reached about 0.95. So updates go to fp32 master weights. Sums break the same way. Add 0.01 to a bf16 running total, 4096 times. The true sum is 40.96. The bf16 total stalls at 4.0. At 4, bf16 values are 0.031 apart, more than twice 0.01, so every addition rounds away. That's why matrix hardware adds its products in an fp32 accumulator, and rounds only the final output.
One mixed-precision step
Here is one mixed-precision step. First, cast the fp32 master weights down to bf16. Forward and backward: matmuls read bf16, accumulate in fp32, and write bf16. Softmax, the norm layers and the loss are upcast to fp32. Their exps, logs and long sums amplify rounding error. Then the gradient is converted to fp32, and the update is applied to the master weights. Now the colours are gone. Tap the two boxes that run in fp32.
fp16 needs loss scaling
bf16 reaches about ten to the minus thirty-eight, so it needs no help. fp16 is narrower. Its smallest positive value is about six times ten to the minus eight. Its largest is 65504. A gradient of two times ten to the minus eight stores as zero. So multiply the loss by S, here 65536. By the chain rule, every gradient scales too: now about 0.0013. Divide by S in fp32 before the update, and the tiny value is back. Same S, but now a gradient of 4. What happens?
Skip, halve, retry
It overflows: 262144 is past 65504, so the gradient is inf. Whenever any gradient is inf or NaN, skip the step and halve S. S becomes two to the fifteenth. Four times that is 131072, still inf. Skip again. Keep halving until four times S fits. Once steps succeed, raise S again slowly. Tap the first scale where the step goes through.
Cheap precision for bulk, high where it counts
One mixed-precision training step
Loss scaling with real fp16 numbers
Watch bf16 lose updates and sums
import numpy as np
def bf16(x): # round fp32 to nearest bf16
b = np.asarray(x, np.float32).view(np.uint32)
b = (b + 0x7FFF + ((b >> 16) & 1)) & 0xFFFF0000
return b.astype(np.uint32).view(np.float32)
w16, w32, s16 = bf16(1.0), np.float32(1.0), bf16(0.0)
for _ in range(1000): # 1000 steps of -5e-5
w16 = bf16(w16 - np.float32(5e-5)) # bf16 weight
w32 = w32 - np.float32(5e-5) # fp32 master weight
for _ in range(4096): # 4096 terms of 0.01
s16 = bf16(s16 + np.float32(0.01)) # bf16 accumulator
print(w16, w32, s16) # 1.0 ~0.95 4.0
What to remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses