Search

Search papers and lessons

Mixed-Precision Training

Level 4 · 11 min read

Mixed-precision training runs most arithmetic in 16-bit floats for speed and memory, keeps a 32-bit master copy of the weights, and scales the loss so tiny gradients do not vanish.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Linear algebra
  • Probability
  • Neural network basics
  • Optimizers
  • ReadReady to readNot yet

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 ww, one input xx, and a target tt. The model computes y=wxy = wx and the loss is L=(y−t)2L = (y - t)^2. The numbers are w=0.5w = 0.5, x=2x = 2, t=1.5t = 1.5, so y=1y = 1, L=0.25L = 0.25, and the gradient is ∂L/∂w=2(y−t)x=−2\partial L / \partial w = 2(y - t)x = -2. Learning rate η=0.1\eta = 0.1.

  1. Keep a 32-bit master copy of the weights. The master holds w=0.5w = 0.5 in fp32. It is the only copy the optimizer ever changes. Check: 0.50.5 is exact in every format.
  2. Make a 16-bit working copy. The forward pass uses the fp16 copy, w16=0.5w_{16} = 0.5. Check: 0.50.5 is exact in fp16 too, so nothing is lost here.
  3. Run the forward pass in 16 bits. y=w16x=1y = w_{16} x = 1. Check: 0.5×2=10.5 \times 2 = 1, still exact.
  4. Compute the loss. L=(1−1.5)2=0.25L = (1 - 1.5)^2 = 0.25. 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.
  5. Scale the loss and run the backward pass. Multiply the loss by a scale SS, here S=1024S = 1024, a power of two so the multiplication is exact. The gradient of SLS L is SS times the gradient of LL, so every gradient is 1024×(−2)=−20481024 \times (-2) = -2048. Check: −2048-2048 is exact in fp16. In real training, this step is what keeps small gradients from becoming zero.
  6. Unscale in fp32. Divide the scaled gradient by SS after copying it to fp32: −2048/1024=−2-2048 / 1024 = -2. Check: this is the true gradient from step 0.
  7. Update the master weight in fp32. w←w−η×(−2)=0.5+0.2=0.7w \leftarrow w - \eta \times (-2) = 0.5 + 0.2 = 0.7. Check: the master is now 0.70.7 in fp32, and the next step will cast it to a fresh fp16 copy, which stores 0.70.7 as about 0.70020.7002. 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

Definition: Mixed-precision training step

Keep master weights WW in fp32. Each step computes a fp16 copy W16=fp16⁡(W)W_{16} = \operatorname{fp16}(W), runs the forward and backward passes with W16W_{16} to get the scaled gradient G16=fp16⁡ ⁣(S⋅∇WL)G_{16} = \operatorname{fp16}\!\big(S \cdot \nabla_W L\big), and then updates W←W−η GW \leftarrow W - \eta\,G in fp32, where G=G16/SG = G_{16} / S is the unscaled gradient. Here SS is the loss scale, a power of two, and η\eta 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 (−1)s 2e−15 (1+f/1024)(-1)^{s}\, 2^{e - 15}\,(1 + f/1024), with a 5-bit exponent ee (bias 15) and a 10-bit fraction ff. This follows the scheme in Floating-Point Numbers. Three numbers matter here:

largest=(2−2−10)⋅215=65504,smallest normal=2−14≈6.1×10−5,smallest positive=2−24≈6.0×10−8.\text{largest} = (2 - 2^{-10}) \cdot 2^{15} = 65504, \qquad \text{smallest normal} = 2^{-14} \approx 6.1 \times 10^{-5}, \qquad \text{smallest positive} = 2^{-24} \approx 6.0 \times 10^{-8}.

The spacing between neighbouring fp16 numbers near 11 is 2−10≈0.000982^{-10} \approx 0.00098. 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 W16=fp16⁡(W)W_{16} = \operatorname{fp16}(W), the rounding of each master weight to the nearest fp16 number. The rounding error is at most half the spacing, which near 11 is about 0.000490.00049. Nothing in the forward pass uses the master directly.

Step 5 in symbols: scaling the loss

The chain rule gives ∇W(SL)=S ∇WL\nabla_W (S L) = S\, \nabla_W L. 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 3×10−83 \times 10^{-8}, half the smallest positive fp16 number, round to zero in fp16. With S=216S = 2^{16}, a gradient of 10−810^{-8} becomes 6.55×10−46.55 \times 10^{-4}, which is well inside the representable range.

Step 7 in symbols: the update

The update is W←W−η GW \leftarrow W - \eta\,G, computed in fp32. A change of 10−910^{-9} to a weight of size 0.0010.001 is still visible in fp32, whose spacing near 0.0010.001 is about 10−1010^{-10}. In fp16 the same change would be lost.

Worked examples

Example
Example 1: accumulating in fp16 (tiny)

Start with 1.01.0 in fp16 and add 0.00040.0004 one thousand times. The exact answer is 1.41.4.

  1. The spacing near 11 is 2−10=0.0009772^{-10} = 0.000977, so half the spacing is 0.0004880.000488.
  2. The first addition gives 1.00041.0004. Since 0.0004<0.0004880.0004 < 0.000488, it rounds to 1.01.0.
  3. Every later addition does the same, because each partial sum is still 1.01.0 before the addition.
  4. The final value is 1.01.0, not 1.41.4. Check: 1000×0.0004=0.41000 \times 0.0004 = 0.4, so the exact sum is 1.41.4, and the fp16 result is off by 0.40.4.
  5. 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.
Example
Example 2: a gradient that underflows (standard)

A weight has master value 0.0010.001, learning rate η=0.1\eta = 0.1, and a true gradient of g=10−8g = 10^{-8}.

  1. Without scaling, the fp16 gradient is fp16⁡(10−8)\operatorname{fp16}(10^{-8}). The smallest positive fp16 number is 2−24=5.96×10−82^{-24} = 5.96 \times 10^{-8}, and half of it is 2.98×10−82.98 \times 10^{-8}. Since 10−8<2.98×10−810^{-8} < 2.98 \times 10^{-8}, the gradient rounds to 00.
  2. The update is 0.001−0.1×0=0.0010.001 - 0.1 \times 0 = 0.001. Nothing changed. Check: the weight should have moved by 10−910^{-9}.
  3. With scale S=216=65536S = 2^{16} = 65536, the scaled gradient is 65536×10−8=6.55×10−465536 \times 10^{-8} = 6.55 \times 10^{-4}. This is representable in fp16, with a spacing near 6.55×10−46.55 \times 10^{-4} of about 4.8×10−74.8 \times 10^{-7}, so it is stored as about 6.552×10−46.552 \times 10^{-4}.
  4. Copy to fp32 and divide by SS: 6.552×10−4/65536=1.0×10−86.552 \times 10^{-4} / 65536 = 1.0 \times 10^{-8}. Check: this matches the true gradient to within about 0.02 percent.
  5. Update in fp32: 0.001−0.1×10−8=0.0009999999990.001 - 0.1 \times 10^{-8} = 0.000999999999. The fp32 spacing near 0.0010.001 is about 1.2×10−101.2 \times 10^{-10}, so the change is recorded.
Example
Example 3: memory for a 7-billion-parameter model (realistic)

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 7×1097 \times 10^9 parameters and use decimal gigabytes, 1 GB=1091\ \text{GB} = 10^9 bytes.

  1. Bytes per parameter: 2+2+4+4+4=162 + 2 + 4 + 4 + 4 = 16.
  2. Total for weights, gradients and optimizer state: 7×109×16=1.12×10117 \times 10^9 \times 16 = 1.12 \times 10^{11} bytes.
  3. Convert to gigabytes: 1.12×1011/109=1121.12 \times 10^{11} / 10^9 = 112 GB. Check: the same model with fp32 weights, gradients and Adam states would use 4×4=164 \times 4 = 16 bytes per parameter, also 112 GB.
  4. 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.
  5. Try it: how many gigabytes does the same 16-bytes-per-parameter budget need for a model with 13×10913 \times 10^9 parameters? Answer: 13×109×16/109=20813 \times 10^9 \times 16 / 10^9 = 208 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 11 cannot be moved by a step of 0.00010.0001 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 6550465504, 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 22 is 2−9=0.001952^{-9} = 0.00195. What does 2.0+0.00052.0 + 0.0005 become in fp16?Show answer

Half the spacing is 0.000980.00098. Since 0.0005<0.000980.0005 < 0.00098, the sum rounds to 2.02.0. 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.

Sources