TL;DR
Mixed-precision training trains a neural network with most of its arithmetic in 16-bit floating-point numbers, which are faster and take half the memory of 32-bit ones. Its weights are still kept as a 32-bit master copy, because a 16-bit copy cannot absorb small updates. The loss is multiplied by a large scale before the backward pass, so that tiny gradients stay representable, and the scale is divided back out before the update.
Builds on: Floating-Point Numbers, Backpropagation, Stochastic Gradient Descent · Leads to: Mixed Precision Training
Where it's used
Training large language models on GPUs. Modern GPUs have dedicated hardware for matrix multiplication in 16-bit precision. Most of the time in training a transformer is spent in those multiplications, so running them in 16 bits gives a large speedup, and the activations take half the memory. Many large models use some form of it.
Image classification and detection. The original paper by Micikevicius and colleagues (Mixed Precision Training, 2017) trained image and language models with this recipe and matched the accuracy of 32-bit training on the same schedules. That result is what made the method standard.
Training on TPUs. Google's TPUs use a related 16-bit format, bfloat16, which keeps the exponent range of 32-bit numbers. Training there often works with bfloat16 arithmetic and fp32 weights, and it needs less loss scaling than the fp16 recipe.
How it works
Take one weight , one input , and a target . The model computes and the loss is . The numbers are , , , so , , and the gradient is . Learning rate .
- Keep a 32-bit master copy of the weights. The master holds in fp32. It is the only copy the optimizer ever changes. Check: is exact in every format.
- Make a 16-bit working copy. The forward pass uses the fp16 copy, . Check: is exact in fp16 too, so nothing is lost here.
- Run the forward pass in 16 bits. . Check: , still exact.
- Compute the loss. . The loss is a single number and is kept in fp32, so the scale used in the next step is applied to a well-defined value.
- Scale the loss and run the backward pass. Multiply the loss by a scale , here , a power of two so the multiplication is exact. The gradient of is times the gradient of , so every gradient is . Check: is exact in fp16. In real training, this step is what keeps small gradients from becoming zero.
- Unscale in fp32. Divide the scaled gradient by after copying it to fp32: . Check: this is the true gradient from step 0.
- Update the master weight in fp32. . Check: the master is now in fp32, and the next step will cast it to a fresh fp16 copy, which stores as about . That rounding is why the master is kept in fp32 at all.
flowchart LR A[fp32 master weights] --> B[Cast to fp16 copy] B --> C[Forward pass in fp16] C --> D[Loss in fp32] D --> E[Scale loss by S] E --> F[Backward pass in fp16] F --> G[Copy grads to fp32 and divide by S] G --> H[Optimizer updates fp32 master] H --> A
The math
Definition
Keep master weights in fp32. Each step computes a fp16 copy , runs the forward and backward passes with to get the scaled gradient , and then updates in fp32, where is the unscaled gradient. Here is the loss scale, a power of two, and is the learning rate.
The definition says three things: a precise copy of the weights is kept, most arithmetic is done on a rounded copy, and the scale is put in before the rounding and taken out after it. The rest of this section explains each.
Step 1 in symbols: the 16-bit format
A normal fp16 number is , with a 5-bit exponent (bias 15) and a 10-bit fraction . This follows the scheme in Floating-Point Numbers. Three numbers matter here:
The spacing between neighbouring fp16 numbers near is . A value is rounded to the nearest representable one, so any change smaller than half that spacing disappears.
Step 2 in symbols: the working copy
The working copy is , the rounding of each master weight to the nearest fp16 number. The rounding error is at most half the spacing, which near is about . Nothing in the forward pass uses the master directly.
Step 5 in symbols: scaling the loss
The chain rule gives . So scaling the loss scales every gradient by the same factor, and the backward pass is the same computation with bigger numbers. Gradients below about , half the smallest positive fp16 number, round to zero in fp16. With , a gradient of becomes , which is well inside the representable range.
Step 7 in symbols: the update
The update is , computed in fp32. A change of to a weight of size is still visible in fp32, whose spacing near is about . In fp16 the same change would be lost.
Worked examples
Start with in fp16 and add one thousand times. The exact answer is .
- The spacing near is , so half the spacing is .
- The first addition gives . Since , it rounds to .
- Every later addition does the same, because each partial sum is still before the addition.
- The final value is , not . Check: , so the exact sum is , and the fp16 result is off by .
- Interpretation: this is why the sum is accumulated in fp32 in mixed-precision training. The gradient of a weight is a sum over a batch, and in fp16 most of its small contributions would be lost.
A weight has master value , learning rate , and a true gradient of .
- Without scaling, the fp16 gradient is . The smallest positive fp16 number is , and half of it is . Since , the gradient rounds to .
- The update is . Nothing changed. Check: the weight should have moved by .
- With scale , the scaled gradient is . This is representable in fp16, with a spacing near of about , so it is stored as about .
- Copy to fp32 and divide by : . Check: this matches the true gradient to within about 0.02 percent.
- Update in fp32: . The fp32 spacing near is about , so the change is recorded.
A common mixed-precision setup with the Adam optimizer (Adam Optimizer) stores, per parameter: an fp16 weight (2 bytes), an fp16 gradient (2 bytes), an fp32 master weight (4 bytes), and two fp32 Adam states (4 bytes each). That is 16 bytes per parameter. Take a model with parameters and use decimal gigabytes, bytes.
- Bytes per parameter: .
- Total for weights, gradients and optimizer state: bytes.
- Convert to gigabytes: GB. Check: the same model with fp32 weights, gradients and Adam states would use bytes per parameter, also 112 GB.
- So the optimizer state is not where mixed precision saves memory. Its savings come from the activations, which are stored in fp16, and from faster matrix multiplications.
- Try it: how many gigabytes does the same 16-bytes-per-parameter budget need for a model with parameters? Answer: GB, before activations.
Common mistakes and tradeoffs
It is tempting to think that training in fp16 only means casting the model. In fact the master copy must stay in fp32, because updates smaller than the fp16 spacing are lost. Example 1 shows the same effect for sums, and a weight near cannot be moved by a step of in fp16 at all.
It is tempting to think that loss scaling changes the answer. In fact it changes only the intermediate numbers. Unscaling in fp32 restores the true gradient up to rounding, so the update is the same as if no scale had been used, except that the small gradients survive.
It is tempting to think that fp16 is always safe for the forward pass. In fact fp16 overflows above , so large activations become infinite, and the scale must be lowered when that happens. Some implementations skip the scale for bfloat16, which has the same range as fp32 but only 7 fraction bits, so it trades precision for range.
Use mixed precision when the model is large enough that matrix multiplications dominate, and the hardware has fast low-precision units. It is a poor fit for tiny models, for hardware without fast fp16 arithmetic, and for computations with large sums of small numbers that cannot be accumulated in fp32. Alternatives include bfloat16 training, which is often simpler, full fp32 training when memory allows, and Quantization of a finished model for inference, which uses much lower precision than training ever does.
History
Half-precision numbers were standardised in the 2008 revision of IEEE 754. Before 2017, most neural network training ran in 32-bit floats. In 2017 Micikevicius and colleagues showed that a fp32 master copy, a loss scale and fp32 accumulation were enough to train large image and language models in fp16 with no loss of accuracy on the schedules they tested (Mixed Precision Training). The idea caught on quickly, and it became the default in deep-learning frameworks. Bfloat16, a format with more range and less precision, was developed at Google for its TPUs and later adopted by other hardware.
Recap
- Most arithmetic runs in fp16 because it is faster and smaller, and the matrix multiplications gain the most.
- A fp32 master copy holds the weights, because fp16 cannot represent small updates.
- The loss is scaled up before the backward pass so that small gradients survive, and the scale is divided out in fp32 before the update.
- Sums are accumulated in fp32, because adding small numbers to a large fp16 sum loses them.
- Read next: Mixed Precision Training for the original experiments, and Quantization for the lower-precision formats used after training.
Check yourself
QuestionRecall: why does the optimizer update a fp32 master copy instead of the fp16 weights used in the forward pass?Show answer
An update smaller than half the fp16 spacing at that weight's size rounds back to the old value, so it is lost. The fp32 master has a much finer spacing, so small accumulated updates are kept, and a fresh fp16 copy is made from it each step.
QuestionComputation: the fp16 spacing near is . What does become in fp16?Show answer
Half the spacing is . Since , the sum rounds to . The addition has no effect.
QuestionWhy: why scale the loss rather than rescaling each gradient by hand?Show answer
By the chain rule, scaling the loss multiplies every gradient by the same factor in one place, with no extra work for each layer. Doing it on the loss also means the backward pass runs with the scaled numbers throughout, which is what prevents underflow. The divide by the same factor happens once in fp32 before the update.
QuestionApplied: bfloat16 is often trained without loss scaling. Why might that be safe?Show answer
Bfloat16 has the same 8-bit exponent as fp32, so its range is about the same, and tiny gradients do not underflow the way they do in fp16, which has only a 5-bit exponent. It has fewer fraction bits, so the rounding error per step is larger, but the range problem that loss scaling solves is mostly absent.