Topic 2 of 6 · 5 min read
Mixed precision with bf16 and fp8
Goal: Pick a precision setting for a stated GPU and diagnose a NaN loss caused by precision.
Mixed precision runs most math in 16-bit or 8-bit numbers for speed and memory, and keeps the fragile parts, such as master weights and the loss, in 32-bit.
Check yourself · 1 of 3
What is the main reason bf16 is easier to use than fp16 for training?
Why it matters
Modern GPUs have tensor cores that run 16-bit matrix math much faster than 32-bit, and 16-bit values take half the memory. Almost every real training run uses it. The price is a smaller range or precision per number, and when that range runs out your loss turns into nan at step 4,000 after hours of training.
The formats
- fp32 has a wide range and fine precision. It is the safe baseline and the slowest.
- fp16 has fine precision but a narrow range. Small gradients can underflow to zero, and large values overflow to
inf. It needs loss scaling: multiply the loss by a large factor before the backward pass so gradients stay representable, then divide it out. - bf16 has the same range as fp32 but coarser precision. It rarely overflows, so it needs no loss scaling. It works on Ampere-generation NVIDIA GPUs and newer, and on recent AMD and Google TPU hardware.
- fp8 halves memory again and is faster on the newest GPUs such as H100. It needs per-tensor scaling and library support, and is still a specialist setting.
How to choose
- GPU supports bf16: use bf16 autocast. This is the default for fine-tuning.
- Older GPU such as a V100 or T4 with no bf16: use fp16 with a gradient scaler.
- Try fp8 only on hardware and a library built for it, and compare the loss curve with a bf16 run first.
import torch
with torch.autocast("cuda", dtype=torch.bfloat16):
loss = model(**batch).loss
loss.backward()
optimizer.step()
Before and after
Weak: fp16 on an RTX 3090 with no scaler, loss reaches nan after 900 steps. The fix: switch to bf16, or add torch.amp.GradScaler and check that it skips steps on overflow.
What goes wrong
- fp16 without loss scaling. Gradients underflow, learning stalls or the loss goes
nan. - Reductions in low precision. Softmax, layer norm and loss sums should run in fp32. Autocast does this for known ops, but custom kernels may not.
- Blaming the data first. A NaN is not always bad data. Check whether it appears at the same step across seeds and precision settings.
Examples
Diagnosing a NaN from a log
step 800 loss 1.92 scale 65536
step 850 loss 1.88 scale 65536
step 900 loss nan scale 65536
This is fp16 with a fixed loss scale. At 65536 the gradients overflowed to inf. A dynamic scaler halves the scale after an overflow and skips the step. Switching to bf16 removes the problem on a GPU that supports it.
Choosing for a T4
A T4 has no bf16 support. The plan is fp16 autocast with GradScaler, gradient clipping at 1.0, and a short fp32 run to compare early loss values.
Key points
- Mixed precision keeps master weights and sensitive ops in fp32 while the heavy matrix math runs in 16-bit or 8-bit.
- bf16 has fp32's range, so it needs no loss scaling and is the default on Ampere-class GPUs and newer.
- fp16 has a narrow range and needs a gradient scaler to avoid underflow and overflow.
- fp8 is a specialist option that needs supporting hardware, library support and a baseline comparison.
- A NaN loss is a precision suspect first: rerun in bf16 or fp32 for a few hundred steps to see if it disappears.
Common mistakes
- Using fp16 on a bf16-capable GPU and then fighting NaNs that bf16 would not have produced.
- Setting a fixed loss scale instead of a dynamic scaler, so one overflow ruins the run.
- Skipping the baseline: switching to fp8 with no bf16 curve to compare against.
- Treating a NaN as proof of bad data without testing precision first.
Check yourself · 2 of 3
You train on a T4, which has no bf16 support. Which setting is the sensible default?
Check yourself · 3 of 3
A run in fp16 shows loss nan at step 900. What is a good first test?
Practice items, not counted towards your mastery.
Start here
- The Ultra-Scale Playbook: Training LLMs on GPU Clusters (opens in a new tab)BookFree
An interactive guide to data, tensor, pipeline and ZeRO parallelism with measured trade-offs.
- Getting Started with Fully Sharded Data Parallel (FSDP) (opens in a new tab)DocsFree
A runnable FSDP setup that shows how parameters, gradients and optimizer state are sharded.
- ZeRO: Memory Optimizations Toward Training Trillion Parameter Models (opens in a new tab)PaperFree
Breaks down the bytes per parameter for weights, gradients and Adam states, and how sharding removes the copies.
More for this topic
- Mixed Precision Training (opens in a new tab)PaperFree
The original paper on half-precision training with an fp32 master copy and loss scaling.
- PyTorch automatic mixed precision examples (opens in a new tab)DocsFree
Working autocast and GradScaler code, including gradient clipping and accumulation.