Search

Search papers and lessons

Mixed Precision Training

Paulius Micikevicius, Sharan Narang, Jonah Alben et al. · 2017

Micikevicius et al. (2017) train networks mostly in 16-bit floats, keep a 32-bit copy of the weights and scale the loss, matching 32-bit accuracy with about half the memory.

On this page

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

TL;DR

Training a neural network in 16-bit floating point halves the memory and can run faster on hardware built for it, but small numbers are lost and training can diverge. Micikevicius and colleagues showed that three techniques fix this: keep a 32-bit master copy of the weights, scale the loss so that small gradients stay representable, and accumulate products in 32 bits. Across image, detection, speech, translation, language-model and GAN experiments, the resulting models matched 32-bit training without changing their architecture or hyperparameters.

Builds on: Floating-Point Numbers, Backpropagation, Stochastic Gradient Descent, Mixed-Precision Training

Why it mattered

Models kept growing through the late 2010s, and training them was limited by memory and arithmetic throughput. The paper turned 16-bit training from a research curiosity into a routine recipe. Its three techniques became a widely used approach on GPUs with 16-bit tensor units, and the same ideas, a high-precision master copy and scaled gradients, appear in later low-precision training work.

The paper was also an early demonstration on large, real workloads. Earlier low-precision studies mostly used small convolutional networks or binarized weights and often left gradients in 32 bits. This paper trained AlexNet-scale to Resnet-50 classifiers, Faster R-CNN and SSD detectors, English and Mandarin speech models of over 100 million parameters, and a 3-by-1024 LSTM translator, and it kept the same hyperparameters for each.

The problem

A 16-bit float (FP16) has 1 sign bit, 5 exponent bits and 10 mantissa bits. Its largest value is 65,504. Its smallest positive value is 2−24≈6×10−82^{-24} \approx 6 \times 10^{-8}, and anything much smaller than that rounds to zero. Near the value 1, adjacent FP16 numbers are 2−10≈0.000982^{-10} \approx 0.00098 apart. A 32-bit float (FP32) has 23 mantissa bits, so the gap near 1 is about 2−23≈0.000000122^{-23} \approx 0.00000012.

Two things go wrong when a whole network is trained in FP16. Gradients are often tiny, so many of them become zero before the optimizer sees them. And a weight update that is small compared with the weight it changes can be rounded away completely. The paper measured both: in the speech model about 5 percent of weight gradients had exponents below −24-24, and in the multibox detector many activation gradients fell below FP16's smallest values and became zero.

How it works

Take one weight, one learning rate and one gradient, and follow them through FP16 and FP32. The goal is to keep the update, which is learning rate times gradient, from disappearing.

  1. Start with a master copy. The weight is stored in FP32 as w32=1.0w_{32} = 1.0. The forward and backward passes use an FP16 copy of it, w16=1.0w_{16} = 1.0.
  2. Compute a small update. The gradient is 0.0010.001 and the learning rate is 0.10.1, so the update is 10−410^{-4}. Applied in FP16, 1.0+0.00011.0 + 0.0001 is rounded back to 1.01.0, because the nearest FP16 neighbour of 1.0 is about 0.000980.00098 away and 0.00010.0001 is less than half that gap. The update is lost.
  3. Apply the update to the master copy. In FP32 the gap near 1 is about 0.000000120.00000012, so w32=1.0001w_{32} = 1.0001 is stored exactly enough. The change is kept.
  4. Let updates accumulate. After ten such steps w32=1.001w_{32} = 1.001. The FP16 copy used in the next forward pass is now 1.00097661.0009766, the nearest FP16 value to 1.0011.001, so the change has reached the computation.
  5. Scale a tiny gradient. A raw gradient of 10−810^{-8} is below 2−24≈6×10−82^{-24} \approx 6 \times 10^{-8}, so it becomes zero in FP16. Multiply the loss by 10241024 before backpropagation. Every gradient is then multiplied by 10241024 by the chain rule, so the same gradient becomes 1.024×10−51.024 \times 10^{-5}, which FP16 can store (to about 1.025×10−51.025 \times 10^{-5}, the nearest multiple of 2−242^{-24}).
  6. Unscale before the update. Divide the stored gradient by 10241024 in FP32 to recover 10−810^{-8}, check it for overflow, and then apply it to the master copy.

Summarised as a loop, one training step does the following. The figure is for one layer, after Figure 1 of the paper.

flowchart LR
  A["FP32 master weights"] --> B["Cast to FP16"]
  B --> C["Forward pass in FP16"]
  C --> D["Scaled loss times s"]
  D --> E["Backward pass in FP16"]
  E --> F["Unscale gradients divide by s"]
  F --> G["Optimizer step in FP32"]
  G --> A

The math

Storage. A normalised FP16 number with exponent ee has spacing 2e−102^{e-10} between neighbours, and FP32 has spacing 2e−232^{e-23}. The largest FP16 value is 65,504, the smallest normal one is 2−142^{-14}, and subnormal values are multiples of 2−242^{-24}.

Step 3 in symbols (the master update). Let η\eta be the learning rate and g32g_{32} the gradient in FP32. The weight update is

w32←w32−η g32,w_{32} \leftarrow w_{32} - \eta \, g_{32},

and the forward pass uses w16=round16(w32)w_{16} = \mathrm{round}_{16}(w_{32}). The update is kept as long as it is larger than half the FP16 spacing of ww on the scale of ww. The paper points out that when the ratio of a weight to its update exceeds 2048=2112048 = 2^{11}, an FP16 addition shifts the update's bits out of the mantissa and the update can be lost entirely.

Step 5 in symbols (loss scaling). Let ss be a scale factor and L\mathcal{L} the loss. Backpropagation is run on sLs\mathcal{L}. By the chain rule every gradient is multiplied by the same ss:

∇θ(sL)=s ∇θL.\nabla_\theta (s\mathcal{L}) = s \, \nabla_\theta \mathcal{L}.

Choosing ss so that ss times the largest gradient stays below 65,504 avoids overflow, and the gradients are divided by ss before the update, so the update is unchanged.

Step 6 in symbols (unscaling and overflow). The unscaled gradient is g=1s⋅g~g = \tfrac{1}{s} \cdot \widetilde{g}, where g~\widetilde{g} is the stored scaled gradient. If any entry of g~\widetilde{g} is infinite or not a number, the paper's suggested option is to skip that update, because an overflowed gradient would damage the weights permanently.

Memory. Weights, activations and gradients take 2 bytes each in FP16, against 4 in FP32. The master copy adds 4 bytes per weight, so weight storage rises by half relative to pure FP32 training. Activations dominate training memory, though, and they are halved, so the total is roughly halved.

Results

The paper compares FP32 baselines with mixed precision (MP) on the same architectures and hyperparameters.

  • Classification (ILSVRC12, top-1). Baseline and MP accuracies: AlexNet 56.77 and 56.93, VGG-D 65.40 and 65.43, GoogLeNet 68.33 and 68.43, Inception v2 70.03 and 70.02, Inception v3 73.85 and 74.13, Resnet-50 75.92 and 76.04. No loss scaling was needed for these networks.
  • Detection (Pascal VOC 2007 mAP). Faster R-CNN reaches 69.1 in FP32, 68.6 in MP without loss scaling and 69.7 with it. Multibox SSD reaches 76.9 in FP32, diverges in MP without loss scaling, and reaches 77.1 with a scale factor of 8.
  • Speech (character error rate, lower is better). English DeepSpeech 2 goes from 2.20 to 1.99, and Mandarin from 15.82 to 15.01. The authors note that the half-precision storage may act as a regularizer.
  • Translation. On English-to-French with a 3-layer LSTM, MP with loss scaling matches FP32, while MP without scaling degrades slightly.
  • Language modelling. The bigLSTM model on the one-billion-word benchmark diverges in FP16 without loss scaling after 300,000 iterations. A scale factor of 128 matches the FP32 perplexity curve.
  • GANs. DCGAN faces are visually comparable between FP32 and MP, with no loss scaling needed.
  • Speed. On Volta GPUs, DeepBench operations that are limited by memory or arithmetic run 2 to 6 times faster than FP32. Full-network speedups depend on framework support, which was still early in 2017.

The limits are worth stating. The speedups in the paper come from operations, not full networks, and the language and GAN results are qualitative or limited to one model. Loss-scale selection was manual; the authors suggested automating it by detecting overflow and adjusting.

Reading guide

Check yourself

QuestionThe smallest positive FP16 value is 2−242^{-24}. A gradient is 2×10−82 \times 10^{-8}. Does it survive FP16 storage without a scale factor? Give the reason.Show answer
No. 2−242^{-24} is about 5.96×10−85.96 \times 10^{-8}, so the value halfway to the next representable number is about 2.98×10−82.98 \times 10^{-8}. Since 2×10−82 \times 10^{-8} is below that, it rounds to zero in FP16 without a scale factor.
QuestionA weight is 1.0 and the update is 2×10−42 \times 10^{-4}. Why is the update lost in FP16 but kept in FP32?Show answer
The FP16 spacing near 1 is about 0.000980.00098, so an update of 0.00020.0002 is less than half that gap and rounds back to 1.0. The FP32 spacing near 1 is about 1.2×10−71.2 \times 10^{-7}, far smaller than 0.00020.0002, so the update is stored.
QuestionWhy does multiplying the loss by a scale factor leave the training update unchanged once the gradients are unscaled?Show answer
The chain rule multiplies every gradient by the same factor ss, and dividing the stored gradients by ss before the optimizer step cancels it, so the update matches what FP32 training would compute, apart from rounding.
QuestionA model has NN parameters. Compared with pure FP32, how does FP16 mixed precision change the weight storage, and why does the paper say total training memory is roughly halved?Show answer
The FP16 copy takes 2 bytes per weight and the FP32 master takes 4, so weight storage rises from 4 bytes to 6 bytes per weight, a 50 percent increase. Activations dominate training memory and are stored in FP16, so they are halved, and the total is roughly halved.

Sources

  • Micikevicius, Narang, Alben, Diamos, Elsen, Garcia, Ginsburg, Houston, Kuchaiev, Venkatesh and Wu, "Mixed Precision Training", arXiv:1710.03740. https://arxiv.org/abs/1710.03740

Read the full paper

Paulius Micikevicius, Sharan Narang, Jonah Alben et al., 2017. arXiv:1710.03740