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 , and anything much smaller than that rounds to zero. Near the value 1, adjacent FP16 numbers are apart. A 32-bit float (FP32) has 23 mantissa bits, so the gap near 1 is about .
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 , 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.
- Start with a master copy. The weight is stored in FP32 as . The forward and backward passes use an FP16 copy of it, .
- Compute a small update. The gradient is and the learning rate is , so the update is . Applied in FP16, is rounded back to , because the nearest FP16 neighbour of 1.0 is about away and is less than half that gap. The update is lost.
- Apply the update to the master copy. In FP32 the gap near 1 is about , so is stored exactly enough. The change is kept.
- Let updates accumulate. After ten such steps . The FP16 copy used in the next forward pass is now , the nearest FP16 value to , so the change has reached the computation.
- Scale a tiny gradient. A raw gradient of is below , so it becomes zero in FP16. Multiply the loss by before backpropagation. Every gradient is then multiplied by by the chain rule, so the same gradient becomes , which FP16 can store (to about , the nearest multiple of ).
- Unscale before the update. Divide the stored gradient by in FP32 to recover , 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 has spacing between neighbours, and FP32 has spacing . The largest FP16 value is 65,504, the smallest normal one is , and subnormal values are multiples of .
Step 3 in symbols (the master update). Let be the learning rate and the gradient in FP32. The weight update is
and the forward pass uses . The update is kept as long as it is larger than half the FP16 spacing of on the scale of . The paper points out that when the ratio of a weight to its update exceeds , 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 be a scale factor and the loss. Backpropagation is run on . By the chain rule every gradient is multiplied by the same :
Choosing so that times the largest gradient stays below 65,504 avoids overflow, and the gradients are divided by before the update, so the update is unchanged.
Step 6 in symbols (unscaling and overflow). The unscaled gradient is , where is the stored scaled gradient. If any entry of 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
- Section 1, Introduction. Needs Floating-Point Numbers for FP16 and FP32, and GPU Memory Hierarchy for why memory bandwidth limits speed.
- Section 2, Related work. Needs Quantization to place the binarized and low-bit methods it compares against.
- Section 3.1, FP32 master copy. Needs Stochastic Gradient Descent for the optimizer step and Mixed-Precision Training for the complete recipe.
- Section 3.2, Loss scaling. Needs Backpropagation and Chain Rule: the whole argument is that the chain rule carries a scale factor through every gradient.
- Section 3.3, Arithmetic precision. Needs Batch Normalization and Softmax, which the paper keeps in FP32 for their large reductions.
- Section 4, Results. Needs Convolutional Neural Network for the classifiers, Adam Optimizer for the DCGAN training, and Learning Rate for the schedules the experiments reuse.
Check yourself
QuestionThe smallest positive FP16 value is . A gradient is . Does it survive FP16 storage without a scale factor? Give the reason.Show answer
QuestionA weight is 1.0 and the update is . Why is the update lost in FP16 but kept in FP32?Show answer
QuestionWhy does multiplying the loss by a scale factor leave the training update unchanged once the gradients are unscaled?Show answer
QuestionA model has 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
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