What does mixed-precision training change, why does fp16 need loss scaling, and why is bf16 often preferred?
Answer
Model answer, written in a candidate's voice
In short
- Runs most forward/backward compute (the matmuls) in low precision while keeping fp32 master weights, optimiser state and accumulators.
- fp16 has 5 exponent bits: tiny gradients underflow (below ~6e-8) and large values overflow (above 65,504); dynamic loss scaling scales the loss up before backward, unscales before the step, and skips steps with inf/NaN while lowering the scale.
- bf16 has fp32’s 8 exponent bits with fewer mantissa bits, so loss scaling is usually unnecessary.
- Keep numerically sensitive ops such as softmax, norms and the loss in fp32.
In full
Mixed precision keeps the model’s master weights, optimiser state and accumulators in fp32 while running most of the forward and backward compute, above all the matmuls, in fp16 or bf16, which is what buys the speed and memory savings. fp16 has only 5 exponent bits, so its range is narrow: values below about 6e-8 underflow to zero and values above 65,504 overflow to inf. Small gradients therefore vanish; dynamic loss scaling multiplies the loss by a large factor before backprop, then divides the gradients back down before the optimiser step. When an inf or NaN appears, that step is skipped and the scale is reduced, and after a run of clean steps the scale is raised again. bf16 keeps fp32’s 8 exponent bits, so it has fp32’s range with only 7 explicit mantissa bits; gradients usually stay in range and loss scaling is normally unnecessary, though the lower precision can still matter for long accumulations. The catch is that not every op should be low precision: softmax, layer/batch norm statistics, the loss and other sensitive reductions are commonly kept in fp32. In practice I would use bf16 when the hardware supports it, keep fp32 master weights and sensitive ops, and treat fp16 as needing careful scaling rather than assuming it is free.
What a strong answer covers
- Low-precision compute with fp32 master weights or accumulators.
- fp16 has a narrow exponent range, so gradients underflow; dynamic loss scaling fixes it.
- bf16 has fp32’s range with less mantissa, so usually no scaling is needed.
- Watch numerically sensitive ops (softmax, norms) kept in fp32.
Common mistakes
- casting everything including optimiser state to fp16
- ignoring overflow-induced NaNs.