Build a Mixed-Precision Training Pipeline with Dynamic Loss Scaling
distributed-systems performance cost-optimization
AI System Design Deep Dive
Mixed-Precision Training Pipeline
fp8 saves up to $792,000 on one run, and a loss scaler decides how much of that you keep.
Think of the gain knob on a mixing desk. Turn it too low and the quiet instruments disappear under the hiss. Turn it too high and the loud ones clip into a flat, ugly buzz. A good engineer sets the gain per channel, watches the meters, and backs off the instant a peak lights the red LED. Mixed-precision training is gain staging for tensors: every number in the step lives in a format with a fixed usable range, and the pipeline is the set of knobs and meters that keeps each tensor inside it.
Here are the numbers. We keep the 70.55B parameter model, the 512 H100 GPUs and the TP=8 x PP=4 x DP=16 layout from the last four posts. The bf16 run takes 36.0 days at 8.7 s per step and costs $3.04M, which is $2.03 per million tokens over 1.5T tokens. An H100 SXM peaks at about 989 TFLOPS dense in bf16 and about 1,979 TFLOPS dense in fp8, so the tensor cores can do twice the work per second if we feed them the narrower format. The catch is range. fp16 spans 40 bits of dynamic range from its smallest subnormal to its maximum, bf16 spans 261, fp8 E4M3 only 17.8 and E5M2 31.8 (computed from the bit layouts in the policy section).
A naive pipeline fails in four places. Casting the loss to fp16 and calling backward flushes 2.23 percent of the gradient elements to zero in our synthetic tensor, and those are the small elements that matter late in training. Folding the 1/N_global normalization of post 34 into the loss without a scale is worse: with N = 4,194,304 the factor is 2^-22 = 2.4e-7 and 100 percent of the elements flush to zero in fp16 (measured below). A static loss scale that works for the first 10,000 steps overflows on the first loss spike. And a delayed fp8 scale set from yesterday’s activation maximum clips 1.2 percent of a tensor the moment an outlier token raises the maximum 12x, which multiplies the quantization error of that tensor by 8.
The economics decide how much of this we build. In a throughput model calibrated to the 8.7 s step, fp8 on the linear layers cuts the run to between 30.2 and 26.6 days and saves between $487,000 and $792,000. One skipped step costs 8.7 s x $0.9785 per second = $8.51, so a scaler that skips 131 steps in a run costs $1,111. Precision is the cheapest large lever in the whole stack, and the one with the least forgiving failure modes. We need to solve for precision assignment, scale management, and failure containment simultaneously. Post 32 gave the basic rule (halve on overflow, double after a clean streak, skip globally) and post 34 gave the fp32 accumulator and the norm guard. This post builds the layer that decides which tensor lives in which format, keeps every scale correct, and falls back before a bad step reaches the optimizer.
Requirements and Constraints
Functional requirements
- Assign every parameter tensor and activation to
fp32,bf16orfp8through an explicit, versioned policy, withbf16as the safe default. - Run the linear layers of 76 of the 80 transformer blocks in
fp8(E4M3forward,E5M2backward) with per-tensor scales. - Provide a dynamic loss scaler for
fp16mode that works with the token-weighted1/N_globalfactor and with thefp32main_gradhook from post 34. - Detect overflow, saturation and non-finite values per step and per layer, and make one identical decision on all 512 ranks.
- Fall back from
fp8tobf16per layer, then globally, without restarting the job.
Non-functional requirements
- Throughput: at least 1.3x kernel speedup on the
fp8linear layers, which is 7.307 s per step, 30.25 days in the model below. - Cost: cost per million tokens under $1.75 with
fp8(against $2.03 inbf16), and under $1,200 spent on skipped steps over the run. - Quality: validation loss within 0.5 percent of a
bf16reference at the same token count, checked every 1B tokens. - Numerical safety: zero silent flush-to-zero in the gradient path, a saturation fraction under 0.5 percent per
fp8tensor, and zero applied steps with non-finite gradients. - Capacity: under 45 GB of parameter and optimizer state per GPU, inside the 72 GB budget with 11.4 GB of stage 0 activations and 10 GB of workspace.
- Overhead: scale and saturation bookkeeping under 1 ms per step on the critical path, and one 1-element all-reduce for the overflow flag.
Constraints
- Hardware: 8 H100 per node (80 GB HBM3), 64
p5.48xlargenodes at $55.04 per hour in us-east-1 (third-party trackers, October 2026), $3,522.56 per cluster hour, $0.9785 per second. - Software: PyTorch with NVIDIA Transformer Engine for the
fp8GEMMs. TheDelayedScalingrecipe takesmargin,fp8_format,amax_history_lenandamax_compute_algo, and recent releases also ship a per-tensor current scaling recipe andMXFP8BlockScaling(NVIDIA Transformer Engine documentation, releases 2.14 to 2.18). - Plain data parallelism,
fp32master weights, 18 bytes per parameter of live state from post 34, step 8.7 s, 357,628 steps at constant batch. - Out of scope:
fp4, MXFP8 (it targets Blackwell tensor cores, not H100), the layout, the bubble, the all-reduce and the learning rate schedule, which earlier posts fixed.
The requirements split by tensor lifetime. Weights and optimizer state need precision, activations need range, and gradients need both. Each class gets its own format and its own meter, and that split drives every component below.
High-Level Architecture
The pipeline has seven components. The Precision Policy maps every module to a mode. The Quantizer casts tensors to bf16 or fp8 and keeps an amax history per tensor. The fp8 GEMMs are Transformer Engine kernels that multiply in fp8 and accumulate in fp32. The Loss Scaler multiplies the loss by S / N_global in fp16 mode and holds the scale state. The Norm Guard from post 34 unscales, checks finiteness and decides apply, clip or skip. The Precision Guard watches saturation per layer and flips a layer to bf16 when it trips. The Scale State table on the control plane persists S, the clean streak and every event.
A step runs like this. The fp32 master weights are cast to bf16 once per step, and to fp8 inside each Transformer Engine module on the way into the GEMM. Forward activations are quantized to E4M3 with the scale from the amax history. Backward gradients are quantized to E5M2, which trades a mantissa bit for range because gradients span more binades than activations. The accumulator, the reducer and the Norm Guard from post 34 run unchanged, with one addition: the global gradient norm that the Norm Guard already computes doubles as the overflow detector, because an inf or nan anywhere makes the norm non-finite.
After the norm comes one decision. The 512 ranks reduce a single 1-element flag with MAX, every rank calls scaler.update with the same flag, and the scale stays identical everywhere without being communicated. A finite step applies AdamW. A non-finite step is skipped, halves S, and costs $8.51 in wasted compute.
The most important architectural decision is to reuse the global gradient norm as the overflow detector. It makes the found_inf flag free, keeps one decision point for clipping and skipping, and means the scaler adds zero collectives beyond the single 1-element MAX.
The Precision Policy
The policy has one job: say which format each tensor uses, in a way a reviewer can read in one screen and a test can enforce.
The non-obvious part is that the cheap choice is the wrong default. Putting everything in fp8 is not 2x faster, it is a different training run that may not converge. The safe default is bf16 everywhere with fp32 for the state that accumulates error, and fp8 is an opt-in for the modules where measurement says it holds. In our model, the 80 blocks hold 855.6M parameters each (attention 151.0M, FFN 704.6M), the embedding and output head hold 2.10B, and the total is 70.55B. Keeping the first two and last two blocks plus the embedding and head in bf16 leaves 65.03B parameters (92.2 percent) in fp8 and 5.52B in bf16.
# Precision policy builder and FLOP accounting for the 70B model (ran: policy.py)
LAYERS, HID, FFN, KV_HEADS, HEAD_DIM, VOCAB, SEQ = 80, 8192, 28672, 8, 128, 128_256, 4096
attn = HID * HID * 2 + HID * KV_HEADS * HEAD_DIM * 2 # q, o, k, v
ffn = 3 * HID * FFN # gate, up, down
per_layer = attn + ffn
embed_head = 2 * VOCAB * HID
total = LAYERS * per_layer + embed_head
print("params per layer %.1fM total %.2fB" % (per_layer / 1e6, total / 1e9))
BF16_EDGE_LAYERS = 2 # first 2 and last 2 layers stay bf16
fp8_layers = LAYERS - 2 * BF16_EDGE_LAYERS
fp8_params = fp8_layers * per_layer
print("fp8 params %.2fB (%.1f%%) bf16 params %.2fB" % (fp8_params / 1e9, 100 * fp8_params / total, (total - fp8_params) / 1e9))
gemm_flops_tok = 6 * total # fwd + bwd, linear layers incl. embed/head
attn_flops_tok = 6 * SEQ * HID * LAYERS # causal QK^T and AV, fwd + bwd
all_flops = gemm_flops_tok + attn_flops_tok
fp8_flops = 6 * fp8_params
print("GFLOP per token: linear %.1f attention %.1f fp8 eligible %.1f" % (gemm_flops_tok / 1e9, attn_flops_tok / 1e9, fp8_flops / 1e9))
print("fp8 eligible share of FLOPs %.1f%%" % (100 * fp8_flops / all_flops))
Output of that run:
params per layer 855.6M total 70.55B
fp8 params 65.03B (92.2%) bf16 params 5.52B
GFLOP per token: linear 423.3 attention 16.1 fp8 eligible 390.2
fp8 eligible share of FLOPs 88.8%
So 88.8 percent of the FLOPs can run in fp8. The attention score and value matmuls stay in bf16 (3.7 percent of FLOPs), as do the softmax, the RMSNorm and the residual stream. For the step-time model we use 78 percent of compute time as fp8-eligible, 10 points below the FLOP share, because casts, amax reductions and elementwise kernels take time that does not shrink.
The four formats differ in range far more than in anything else (computed from the bit layouts in formats.py):
| Format | Max | Min normal | Min subnormal | Range in bits |
|---|---|---|---|---|
fp16 |
65,504 | 6.10e-5 | 5.96e-8 | 40.0 |
bf16 |
3.39e38 | 1.18e-38 | 9.18e-41 | 261.0 |
fp8 E4M3 |
448 | 0.0156 | 0.00195 | 17.8 |
fp8 E5M2 |
57,344 | 6.10e-5 | 1.53e-5 | 31.8 |
Premium Content
Unlock the full article along with everything else in the archive — all in one place.