Mixed Precision Training
Mixed precision training is a technique that uses low-precision floating point (FP16 or BF16) to speed up training while preserving model accuracy. It can make training 1.5-3x faster and cut memory use by 30-50%, and it is a standard technique in large-model training.
Why Low Precision Works
The core operations of deep learning (matrix multiplication, convolution) are not sensitive to numerical precision: small errors in weights and activations usually do not change the final result. But some operations are sensitive: gradient accumulation, loss computation, and weight updates. The idea of mixed precision is to use low precision where speed matters and high precision where accuracy matters.
The Three Pitfalls of FP16
Overflow: FP16 has a dynamic range of only about 6x10^4, so gradients easily exceed the upper limit and become inf. Underflow: gradients smaller than 6x10^-8 become 0, losing information from small gradients.
The fix is loss scaling: after the forward pass computes the loss, multiply it by a large factor (such as 65536); the gradients from backpropagation are scaled up accordingly, avoiding underflow, and divided back before the weight update. Dynamic loss scaling adjusts the factor automatically depending on whether overflow occurs.
The Advantage of BF16
BF16 (Brain Floating Point) is a format Google designed for TPUs. Its exponent width matches FP32 (8 bits), and only the mantissa shrinks from 23 to 7 bits. This means its dynamic range is identical to FP32, it needs almost no loss scaling, and training is more stable. The price is lower precision, which matters little for deep learning. Since A100/H100, BF16 has become the first choice for large-model training.
Implementation Details
In practice you need master weights: keep a copy of the weights in FP32, accumulate gradients in FP32 on each update, then cast down for the forward pass. Otherwise rounding error in the low-precision weights accumulates over many updates.
Applications
All large-scale model training. PyTorch's torch.cuda.amp provides automatic mixed precision (AMP), which takes only a few lines of code to enable. Frameworks such as DeepSpeed and Megatron build more aggressive optimizations on top.