FP16 is a format with 1 sign bit, 5 exponent bits, and 10 mantissa bits, and the nonzero magnitudes it can represent range from about 2−24 to 65,504 (right of the figure above). FP32 has 8 exponent bits and 23 mantissa bits, so it can represent a much wider range at much finer granularity. Each of the three techniques of mixed precision training addresses one of three problems that arise from this difference.
| Technique | Problem it prevents | Example shown in the paper |
|---|
| FP32 master weights | Update values so small that they become 0; updates so small relative to the weights that adding them does not change the weights | In a Mandarin speech recognition model, updating FP16 weights directly reduced accuracy by 80% relative |
| Loss scaling | Small gradients falling below 2−24 and becoming 0 | SSD diverged without loss scaling; with a factor of 8 it matched FP32 accuracy |
| FP32 accumulation | Rounding errors accumulating when sums of products are accumulated in FP16 | Some models did not reach FP32 training accuracy without FP32 accumulation |
There are two reasons FP32 master weights are needed. One is that if an update value, the gradient multiplied by the learning rate, is smaller than 2−24, it becomes 0 in FP16. In the paper's speech recognition model, about 5% of the weight gradients had exponents smaller than −24. The other is that if a weight is 2,048 or more times larger than the update value, the update is truncated when aligning the exponents during FP16 addition, so the weight does not change. This is because FP16 has only 10 mantissa bits. Adding in FP32 avoids both problems.
The reason loss scaling is needed is that many gradient values are skewed toward the small end, below the range representable in FP16. In the activation gradients the paper collected while training SSD (object detection), the upper part of FP16's representable range was almost unused, while many values fell below the smallest representable magnitude and became 0. Values smaller than 2−27 were irrelevant to training, whereas values in [2−27,2−24) needed to be preserved, and a factor of 8 (raising the exponent by 3) was enough.
The factor S can be made large without harm as long as the maximum gradient multiplied by it does not exceed 65,504. Beyond that, values become inf or NaN, and updating with them corrupts the weights. The paper showed two approaches: choosing a fixed factor (values from 8 to 32K were used) and skipping the update whenever an overflow is detected. PyTorch's GradScaler adjusts this automatically.
- The initial factor is 216.
- When inf/NaN appears, that update is skipped and the factor is multiplied by 0.5.
- After 2,000 consecutive steps without inf/NaN, the factor is doubled.
FP32 accumulation treats operations differently depending on their type. For matrix multiplications (dot products), FP16 products are summed in FP32, and large reductions such as batch normalization statistics and softmax are computed in FP32. Element-wise operations (such as activation functions) are bound by memory transfer speed, so they can be computed in either FP16 or FP32.