What Is Mixed-Precision Arithmetic? How Dtypes Differ Across Compute Stages
Mixed precision assigns different dtypes to inputs, multiplication, accumulation, outputs, and weight updates. Learn why FP32 accumulation cannot undo rounded inputs and how to validate quality, training stability, and performance.
What Is Mixed-Precision Arithmetic? How Dtypes Differ Across Compute Stages
Mixed-precision arithmetic assigns different numerical precisions to stages of one workload instead of processing every value in one format. Lower precision can reduce data movement or the cost of some operations, while a wider format can be kept for sums or updates that are more sensitive to error. But information lost when an input is rounded to a lower precision does not return when the result is accumulated in FP32. Check the dtype at each stage and the final task quality separately.
Lowering one dtype is not the same as mixed precision
Saying “the model runs in FP16” does not describe its full computation path. Weight and activation storage, operation inputs, product accumulation, output storage, gradients, and optimizer state are separate choices. A matrix operation can accept FP16 inputs, accumulate partial sums in FP32, and convert the result back to FP16 for storage. Another operation may use FP32 because it needs more dynamic range or stable reduction behavior.
| Stage | Dtype to identify | What it changes |
|---|---|---|
| Storage | Weights, activations, optimizer state | Memory footprint and data traffic |
| Operation inputs | Values passed to multiplication or convolution | Representable range and input rounding |
| Multiply and accumulate | Internal product precision and partial-sum dtype | Loss of small terms, accumulated error, and overflow risk |
| Output storage | Format used to write a result | Precision and memory size passed to the next stage |
| Training update | Gradient, master-weight, and optimizer-state formats | Whether small updates survive and training remains stable |
Therefore, seeing “FP32 accumulation” is not enough to conclude that the computation matches an FP32-input calculation. If inputs were rounded to FP16 before multiplication, accumulation cannot reconstruct those input differences. The combination also depends on the hardware, framework, kernel, and settings. PyTorch 2.15 AMP documentation explains that autocast can choose a dtype per operation because operators have different performance and range requirements.
Why lower-precision inputs can use a wider accumulator
A dot product multiplies pairs of values and adds the products. Computing products on a lower-precision path and keeping partial sums in FP32 can reduce some accumulation error or range problems. But if inputs were converted to lower precision first, the products already use approximations. FP32 accumulation sums those approximations in a wider format; it does not guarantee the answer from the original inputs. NVIDIA’s mixed-precision training guidance and a 2018 research paper describe FP16 arithmetic with FP32 accumulation as one specific design.
The following is an arithmetic illustration, not a GPU performance measurement. Around 2048, adjacent FP16 values are spaced by 2. Assuming round-to-nearest, ties-to-even, 2049 converts to 2048. The exact dot product of vectors [2049, -2048] and [1, 1] is 1. If the first vector is converted to FP16 first, it becomes [2048, -2048], and the FP32 accumulated result is 0. The CUDA Math API half-conversion reference documents round-to-nearest-even conversion.
| Calculation path | First product | Second product | FP32 accumulated result |
|---|---|---|---|
| Reference calculation with original inputs | 2049×1 = 2049 | -2048×1 = -2048 | 1 |
| FP16-rounded inputs, then FP32 accumulation | 2048×1 = 2048 | -2048×1 = -2048 | 0 |
The example shows how input rounding can remove a small residual when large terms cancel. It does not mean every such calculation causes a practical problem. Use the real input distribution and task metric to determine sensitivity. The mixed-precision training paper evaluates a recipe combining FP16 weights, activations, and gradients with FP32 accumulation, master-weight updates, and loss scaling. Its results are not a guarantee of the same accuracy or speed for every model or device.
Inference and training must preserve different state
For inference, evaluate whether lower-precision weights and intermediate activations reduce memory or compute cost while preserving output quality and latency on representative service inputs. Some sensitive layers or post-processing steps can remain at higher precision.
Training updates weights every iteration. A small gradient may underflow to zero in a lower-precision format, or an update may disappear when rounded into the stored weight. In FP16 training, loss scaling multiplies the loss so backpropagated gradients move into a representable range, then gradients are unscaled before the optimizer update. If the scale is too large, gradients can become Inf or NaN; a dynamic scaler can skip that update and lower the scale. PyTorch warns that GradScaler does not stabilize every model and that some BF16-pretrained models can overflow in FP16 because its maximum finite value is 65,504.
Another option is to keep an FP32 master copy of weights. A lower-precision copy is used for forward and backward passes, while updates are applied to the FP32 copy before it is converted again for the next iteration. This is not a separate copy required by every mixed-precision framework. PyTorch autocast generally applies operation-specific casts without requiring the model parameters to be converted wholesale to half precision. Other training implementations may keep weights and optimizer state in different combinations. NVIDIA Transformer Engine 2.21 performance documentation explains that memory use changes with the placement of master weights and lower-precision copies.
| Training state | Common option to evaluate | Risk to check |
|---|---|---|
| Matrix inputs and activations | Lower precision such as FP16/BF16 when supported | Input rounding, overflow, or underflow |
| Partial sums and large reductions | Wider format such as FP32 | Whether the actual library path uses the intended dtype |
| Small FP16 gradients | Loss scaling, followed by unscaling | Overflow or skipped updates during scale adjustment |
| Master weights and optimizer state | FP32 or another format, depending on implementation | Extra memory and preservation of small updates |
PyTorch AMP provides operation-specific casting
In PyTorch 2.15 AMP, the documented pattern wraps forward and loss computation in an autocast region rather than converting every model parameter to FP16 first. Autocast can run some operators at lower precision and others at FP32 according to operator-specific policies. The documented pattern runs backward outside the autocast region.
In outline, prepare the model and optimizer, autocast the forward pass and loss, and, for FP16 training, scale the loss before backpropagation. At the optimizer step, gradients are unscaled; updates containing Inf or NaN can be skipped, and the scale is adjusted for a later iteration. This is a summary of the PyTorch 2.15 documentation flow, not a recipe executed or validated here. Confirm API arguments and device support in the documentation for the version you use. PyTorch AMP examples
Operations such as gradient clipping need unscaled gradients, so call the scaler’s unscale operation before clipping. A BF16 path may not need the same GradScaler procedure used to prevent FP16 underflow, but model compatibility and quality still require validation. PyTorch also documents different supported dtypes and autocast operator lists by device.
Judge memory and speed from the full state and bottleneck
Lower-precision storage can reduce bytes per element and data movement. Some devices can also execute supported lower-precision operations at higher throughput. However, weights, gradients, optimizer state, and saved activations do not necessarily all switch to lower precision during training. Retaining FP32 master weights or Adam state can make total memory savings smaller than the element-size ratio suggests. Transformer Engine documentation notes that FP8 training does not always use less memory than BF16/FP16; the result depends on the storage strategy.
Performance also depends on more than theoretical format throughput. Small batches, conversions for unsupported operators, data movement, scale calculation, and kernel selection can determine end-to-end time. Interpret speed or memory claims only for the specific device, library, model, and input shape measured. After changing formats, verify the kernel actually selected and measure full-workload latency and throughput.
Validate five stages before adopting a configuration
**Set a baseline and acceptance range.** Record current inference or validation metrics, an acceptable error range, and the memory and latency to measure.
**Map the computation path.** Identify dtypes for input storage, operation inputs, multiplication, accumulation, outputs, gradients, master weights, and optimizer state, alongside device and framework versions.
**Look for numerical anomalies on representative inputs.** Compare loss, intermediate activations, gradients, and outputs with the reference path for Inf/NaN, small values rounded to zero, and large cancellation errors.
**Evaluate task quality end to end.** For inference, measure quality on the real input distribution. For training, compare the full convergence curve and final validation metrics, not only a few iterations. Test keeping higher precision only in operations where the discrepancy begins.
**Measure full cost and recovery options.** On the target device, measure peak memory, throughput, and latency. For FP16 overflow, evaluate BF16 or FP32 for sensitive operations; for underflow, consider supported loss scaling, then recheck quality with the changed configuration.
Find the stage where values first diverge
If loss becomes NaN or training becomes unstable, locate the first point where Inf or NaN appears. If small gradients underflow to zero in FP16, inspect loss scaling and the order of unscaling. If large activations or gradients exceed FP16’s range, do not simply keep increasing the scale; compare BF16 or FP32 for the specific operation. Large reductions and normalization statistics may also need wider precision, depending on the implementation.
If outputs are finite but task quality falls below the baseline, compare intermediate values by layer to find where the error first grows sharply. Identify whether it begins at input casting, multiplication, accumulation, or output conversion before choosing a different dtype assignment. Finally, repeat quality and performance measurements under the same conditions and record the passing configuration and version. The absence of NaN in one batch does not establish long-run training stability or service quality.
References
[PyTorch 2.15, Automatic Mixed Precision](https://docs.pytorch.org/docs/2.15/amp.html)
[PyTorch 2.15, Automatic Mixed Precision Examples](https://docs.pytorch.org/docs/2.15/notes/amp_examples.html)
[Micikevicius et al., Mixed Precision Training, ICLR 2018](https://openreview.net/pdf?id=r1gs9JgRZ)
[NVIDIA, Train With Mixed Precision](https://docs.nvidia.com/deeplearning/performance/mixed-precision-training/index.html)
[NVIDIA Transformer Engine 2.21, Performance Considerations](https://docs.nvidia.com/deeplearning/transformer-engine/features/low_precision_training/performance_considerations/performance_considerations.html)
[NVIDIA CUDA Math API 13.5, Half Precision Conversion](https://docs.nvidia.com/cuda/cuda-math-api/cuda_math_api/group__CUDA__MATH____HALF__MISC.html)