Contents
Figure 1: Mixed Precision Training Explained: FP16 vs BF16 vs FP8 — the same 16 bits, spent two different ways, and one of them quietly deletes your gradients
Training a large model in full 32-bit floats wastes memory and time. Switch everything to 16 bits, and your loss turns into NaN by step 200.
The middle path is mixed precision: use low-precision numbers where they are safe and high precision where they are not. This post covers how the formats differ, shows the two failures with numbers I measured on this machine, and explains how FP16, BF16, and FP8 each fit into a training recipe.
Why Use Lower Precision at All?
Smaller numbers buy three things: less memory per tensor, less data to move, and faster matrix multiplies on tensor cores built for the narrower format. Recent GPUs add dedicated low-precision hardware — the H100 introduced FP8 support, which roughly doubles matrix-multiply throughput over BF16 on the same silicon.
The catch is that fewer bits means a coarser, narrower number system. Training breaks in specific, predictable ways, and every technique below exists to stop one of them.
Anatomy of a Floating-Point Number
Every float splits its bits into three parts: a sign, an exponent that sets the range, and a mantissa that sets the precision. Here are the formats you will meet:
| Format | Sign / Exponent / Mantissa | Max value | Machine epsilon |
|---|---|---|---|
| FP32 | 1 / 8 / 23 | ~3.4e38 | ~1.2e-7 |
| FP16 | 1 / 5 / 10 | 65,504 | 0.00098 |
| BF16 | 1 / 8 / 7 | ~3.4e38 | 0.0078 |
| FP8 E4M3 | 1 / 4 / 3 | 448 | 0.125 |
| FP8 E5M2 | 1 / 5 / 2 | 57,344 | 0.25 |
I checked the FP16 and BF16 values with NumPy:
fp16 max: 65504.0 eps: 0.000977 tiny: 6.104e-05
bf16 eps: 0.0078125 (8 exponent bits, so max matches fp32)
The pattern jumps out: FP16 and BF16 have the same total width but spend it differently. FP16 favours precision with 10 mantissa bits. BF16 keeps FP32's 8 exponent bits and sacrifices precision for range.
The same value looks different in each format. I stored 0.3952 in both:
fp32: 0.3952
fp16: 0.395263671875
bf16: 0.39453125
BF16 lands noticeably further from the original, which previews its tradeoff.
Problem 1: Overflow and Underflow in FP16
FP16 has a narrow range. Its maximum is 65,504, its smallest normal value is about 6.1e-5, and subnormals reach down to roughly 6e-8. Neural network gradients often fall below that floor. I tried a tiny gradient:
import numpy as np
g = np.float32(1e-8)
print(np.float16(g)) # 0.0
fp16(1e-8): 0.0
That gradient silently turned into zero, so the weight never learned from it. Overflow goes the other direction: np.float16(70000.0) returns inf.
The Fix: Loss Scaling
Loss scaling shifts the gradient distribution into FP16's representable range. Multiply the loss by a large constant before the backward pass, so every gradient scales up by the same factor, then divide the gradients back down before the optimizer step. Using only powers of two keeps the numerics exact:
S = 1024.0
scaled = np.float16(g * S) # 1.025e-05
print(np.float32(scaled) / S) # 1.0011718e-08
scaled fp16: 1.025e-05
recovered: 1.0011718e-08
Notice the recovered value reads 1.0011718e-08, not exactly 1e-08. The scaled number landed in FP16's coarse subnormal zone, so some precision was lost — but the gradient survived, which is the difference between a slow step and no step at all.
NVIDIA's mixed-precision recipe names two components: choosing which operations run in FP16, and dynamic loss scaling, which avoids both overflow and underflow by adjusting the scale during training [1]. The usual implementation backs off the scale when it detects infinities and skips that optimizer step, which PyTorch's GradScaler does for you [3].
Problem 2: Small Updates Vanish
Low precision has a second failure that loss scaling does not fix. Adding a small update to a large weight can round away to nothing. Take a weight of 1.0 and an update of 1e-4:
w16 = np.float16(1.0)
w32 = np.float32(1.0)
for _ in range(1000):
w16 = np.float16(w16 + np.float16(1e-4))
w32 = np.float32(w32 + np.float32(1e-4))
single step, fp32: 1.0001
after 1000 updates: fp16 -> 1.0 fp32 -> 1.1000166
After a thousand updates the FP16 weight has not moved at all, while FP32 reached 1.1. The updates are not slow — they are exactly zero, because 1.0 + 1e-4 needs a mantissa bit that FP16 does not have.
That is why mixed precision keeps a master copy of the weights in FP32 [1]. The forward and backward passes use low-precision copies for speed, but the optimizer updates the FP32 master weights, where small changes survive. Every framework's AMP implementation does this for you — you do not write the copy yourself.
BF16: Range Over Precision
BF16 (brain float) takes a different design stance. It keeps FP32's exponent width, so its range matches FP32. In my test, 70,000 overflows to infinity in FP16 but stays finite in BF16:
70000 -> fp16: inf bf16: 70144.0
Look closely: BF16 returned 70144, not 70000, because its mantissa is coarse. That is the tradeoff: you get range with little rounding precision.
In practice this means BF16 training usually runs without loss scaling, because gradients rarely underflow or overflow — which removes a whole class of training-loop bugs. The cost is resolution: BF16 has 7 mantissa bits against FP16's 10, so 0.3952 becomes 0.39453 rather than 0.39526.
BF16 needs hardware support, which arrived with Ampere and later GPUs; the format itself is specified in the bfloat16 study that validated it for training [5].
BF16 is the sensible default for most LLM training today: fewer knobs and fewer surprises. It still needs FP32 master weights for the reason shown above.
FP8: Two Formats, Per-Tensor Scaling
FP8 pushes further. The format comes in two variants, each suited to a different job [2]:
- E4M3: 4 exponent bits, 3 mantissa bits, maximum 448. More precision, less range.
- E5M2: 5 exponent bits, 2 mantissa bits, maximum 57,344, with infinities and NaN. More range, less precision.
NVIDIA's guidance maps them to the training passes. Forward activations and weights need more precision, so E4M3 fits the forward pass. Gradients tolerate precision loss better but need dynamic range, so E5M2 fits the backward pass. Transformer Engine's "hybrid" recipe does exactly that [4].
Scaling Gets Harder
A single global loss scale cannot work for FP8. The dynamic range of FP8 is enough for any one activation or gradient tensor, but not for all of them at once. So FP8 training gives each tensor its own FP32 scaling factor. Two strategies pick it:
- Just-in-time scaling: compute the scale from the tensor's maximum absolute value (
amax) as you produce it. That requires extra passes over the data, which erodes the speed gain. - Delayed scaling: use the
amaxvalues from previous iterations. That preserves full speed, but you store a history of maxima.
Blackwell GPUs added MXFP8, which assigns a separate scaling factor to each block of 32 consecutive values, stored as a power of two. That finer granularity reduces the dynamic-range burden [4].
Beyond that sits NVFP4, a 4-bit format (E2M1, magnitudes up to ±6) with one scale per 16 elements, plus techniques like stochastic rounding and random Hadamard transforms to tame outliers. The NVFP4 recipe also recommends running the last few layers in higher precision, because they are more sensitive.
FP8 is demanding. Not every operation is safe in FP8, and the library wraps only supported modules in an autocast context. Transformer Engine's Linear layers also require tensor dimensions divisible by 16 [4]. Treat FP8 as an advanced option for teams with the hardware and engineering time.
What Stays in Higher Precision
Even in "low precision" training, certain operations keep wide formats:
- Optimizer states and master weights: FP32.
- Accumulations inside matmuls: tensor cores accumulate in FP32 even when the inputs are FP16 or BF16, which is why a 16-bit multiply does not lose the sum.
- Softmax and other exponentials, normalisation statistics, and the loss: computed in FP32, because these operations are sensitive to both range and resolution.
NVIDIA's guidance says matrix multiplies, convolutions, and normalisation layers are safe in low precision, while exponentials need high precision [1]. Autocast systems apply these rules for you, which brings us to the code.
Using It in PyTorch
I ran the autocast pattern on CPU with bf16 to confirm the API behaves as described — the tensors are cast, and the parameters and gradients stay FP32:
torch 2.15.0+cpu
autocast out dtype: torch.bfloat16
grad dtype: torch.float32 | param dtype: torch.float32
The GPU training loops look like this [3]:
import torch
# BF16: no loss scaling needed
for x, y in loader:
opt.zero_grad(set_to_none=True)
with torch.autocast("cuda", dtype=torch.bfloat16):
loss = loss_fn(model(x), y)
loss.backward()
opt.step()
# FP16: use a gradient scaler
scaler = torch.amp.GradScaler("cuda")
for x, y in loader:
opt.zero_grad(set_to_none=True)
with torch.autocast("cuda", dtype=torch.float16):
loss = loss_fn(model(x), y)
scaler.scale(loss).backward()
scaler.unscale_(opt)
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
scaler.step(opt)
scaler.update()
Gradient clipping needs scaler.unscale_ first, or you would clip gradients that are still multiplied by the loss scale. The model's parameters stay in FP32 throughout, and autocast handles the casting.
For FP8, NVIDIA's Transformer Engine provides a DelayedScaling recipe with a hybrid format (E4M3 forward, E5M2 backward) and an fp8_autocast context manager [4]. Its documentation notes that the backward call must happen outside the autocast region, and that sequence lengths often need padding to a multiple of 16.
Which Format Should You Pick?
| Situation | Choice |
|---|---|
| Modern NVIDIA GPU, standard training | BF16 with autocast |
| Older GPU without BF16 support | FP16 with dynamic loss scaling |
| Maximum throughput on H100 or newer | Evaluate FP8 via Transformer Engine |
| Blackwell training at huge scale | MXFP8 and NVFP4, carefully |
| Debugging weird numerics | Drop to FP32 first, then reintroduce one step at a time |
| Inference only | Different rules — INT8 or FP8 weight quantisation |
Common Pitfalls
- Using FP16 without loss scaling. Gradients underflow quietly, and the loss curve just looks flat.
- Keeping weights in low precision. Small updates vanish, so keep FP32 master weights.
- Clipping gradients before unscaling. Your clip threshold then means something else entirely.
- Assuming BF16 needs no care. Its coarse mantissa can hurt sensitive reductions.
- Treating NaN as bad luck. It signals an overflow or an unstable operation, so investigate the source.
- Casting everything by hand. Use autocast so sensitive ops stay in higher precision.
- Comparing runs without equal seeds and settings. Precision effects hide behind run-to-run noise.
Best Book to Read
| Cover | Book | Description | Get it |
|---|---|---|---|
![]() |
Deep Learning | chapter 4 on numerical computation covers overflow, underflow, and conditioning, which is the theory behind every failure in this post. | View on Amazon |
As an Amazon Associate I earn from qualifying purchases. Links on this page are affiliate links, and I may earn a commission at no extra cost to you.
Read chapter 4 after this post. It explains why underflow deletes gradients rather than merely rounding them, and that framing makes the loss-scaling trick feel inevitable instead of arbitrary.
Wrapping Up
FP16 offers precision but a narrow range, so it needs loss scaling. BF16 trades precision for FP32's range and simplifies training. FP8 squeezes out more throughput with two formats and per-tensor or per-block scaling. All of them keep an FP32 master copy of the weights and run sensitive operations in higher precision.
The two failures to remember are concrete: 1e-8 becomes 0.0 in FP16, and a thousand updates of 1e-4 move a weight of 1.0 by exactly nothing.
Run the small NumPy experiments above, especially the thousand-update test. Watching an FP16 weight refuse to move teaches the master-weights rule better than any paragraph.
Frequently Asked Questions
Should I use BF16 or FP16 for training?
BF16 on any Ampere or newer GPU. It keeps FP32's eight exponent bits, so gradients rarely underflow or overflow and you can skip loss scaling entirely. FP16 needs dynamic loss scaling and a careful eye on overflow, which is one more thing to debug.
Why does FP16 training need loss scaling?
FP16's smallest normal value is about 6.1e-5, and many gradients fall below that, so they round to zero and the weight never updates. Multiplying the loss by a large power of two before the backward pass shifts every gradient up into the representable range, and dividing back afterwards restores the original scale.
Why keep an FP32 master copy of the weights?
Because small updates vanish in low precision. Adding 1e-4 to a weight of 1.0 rounds back to exactly 1.0 in FP16, so a thousand updates move the weight not at all. The optimizer updates an FP32 copy where that change survives, and the low-precision copy is regenerated for each forward pass.
Does BF16 lose accuracy because it has only 7 mantissa bits?
It has coarser resolution, not less range. A value like 0.3952 becomes 0.39453 in BF16, and 70000 becomes 70144. That rounding is usually harmless for training because gradients and activations tolerate relative error far better than they tolerate overflow, but sensitive reductions still run in FP32.
Is FP8 worth using?
Only if you have Hopper or newer hardware and engineering time to spare. FP8 needs per-tensor or per-block scaling, two different formats for forward and backward passes, and padding constraints in the library. It is a throughput optimisation on top of a training recipe that already works in BF16.
Sources
[1]Micikevicius et al., Mixed Precision Training — loss scaling, master weights, and which operations keep FP32: arxiv.org[2]Micikevicius et al., FP8 Formats for Deep Learning — the E4M3 and E5M2 definitions and their intended roles: arxiv.org[3]PyTorch, Automatic Mixed Precision —autocast,GradScaler, and the unscale-before-clip requirement: pytorch.org[4]NVIDIA, Transformer Engine documentation — hybrid FP8 recipes, delayed scaling, MXFP8, and the divisibility-by-16 constraint: docs.nvidia.com[5]Kalamkar et al., A Study of BFLOAT16 for Deep Learning Training — the validation of bf16 as a training format: arxiv.org
