How does mixed precision training work, and why can it be unstable?
Written by the mlprep team · Updated
Explain mixed precision training. Why does it speed up deep learning, and what numerical problems do you need to handle?
The goal#
Mixed precision uses lower-precision formats for most tensor operations while keeping sensitive parts in higher precision. This improves throughput and reduces memory.
Modern GPUs have specialized hardware for FP16/BF16 matrix multiplies. Lower precision also halves activation and gradient memory, which can allow larger batches or models.
FP16 vs BF16#
FP16 has limited exponent range. Small gradients can underflow to zero, and large values can overflow. BF16 has fewer mantissa bits but a much larger exponent range, closer to FP32. BF16 is often more stable for large model training.
Loss scaling#
With FP16, gradients can be tiny. Loss scaling multiplies the loss by a large value before backpropagation, making gradients representable. Before the optimizer step, gradients are unscaled.
Dynamic loss scaling adjusts the scale: increase it when training is stable, decrease it when overflow occurs.
What stays in FP32#
Typically, master weights, optimizer states, reductions, softmax-sensitive operations, and some normalization statistics may stay in FP32 or use careful kernels. Optimizer states are especially important: Adam keeps moment estimates that can lose accuracy in low precision.
Senior/staff considerations#
Mixed precision is a system-level optimization. The failure modes look like ML bugs:
- NaN loss from overflow
- no learning from gradient underflow
- unstable softmax or attention logits
- different convergence from FP32 baseline
- silent quality regressions after switching precision
Validation should compare loss curves, final metrics, NaN/overflow counts, gradient norms, and throughput. For production training, track hardware type because FP16/BF16 support differs across GPUs and TPUs.
Common traps#
- Saying mixed precision is just "make everything FP16." That is unsafe.
- Ignoring optimizer state precision.
- Assuming speedup is automatic. If the workload is input-pipeline-bound or uses unsupported ops, gains may be small.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Mixed precision speeds training by using FP16/BF16 for many tensor operations while preserving stability with FP32 where needed. FP16 often needs loss scaling; BF16 is more numerically forgiving. Monitor overflows, NaNs, convergence, and quality against a baseline.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- Why is BF16 often more stable than FP16?
- What is dynamic loss scaling?
- Which parts of Adam training are risky to store only in low precision?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions