Skip to content
LearnAI
PART 3 · TRAINING LLMS/ Making training work

3.5Mixed precision: FP32, BF16, FP8

How a number is stored in 32, 16 or 8 bits, why training moved from FP32 to BF16 to FP8, and the tricks (master weights, loss scaling, block scaling) that keep it stable.

⏱ 14 minAdvanced

Why bits matter

Every number in a neural network, whether weight, activation or gradient, is stored in some floating-point format. Fewer bits per number pays off three ways. Memory: a 70B-parameter model’s weights take 280 GB in FP32, 140 GB in 16-bit and 70 GB in 8-bit. Bandwidth: moving data between GPU memory and compute units is often the real bottleneck, and half the bits means half the traffic. And speed: GPU tensor cores roughly double their throughput each time the format halves. An H100, for instance, is rated for about twice as many dense FP8 operations per second as BF16.

But fewer bits means coarser numbers. Training involves adding tiny updates to weights, gradients spanning many orders of magnitude, and long sums that accumulate rounding error. Push precision too low, carelessly, and training silently stalls or blows up with NaNs. This lesson is about how far you can push it, and the tricks that make it work. In the previous lesson you met the 16 bytes per parameter of mixed-precisionMixed-precision trainingDoing most math in a low-precision format (FP16, BF16 or FP8) while keeping a high-precision (FP32) master copy of the weights and sensitive operations. training; now you’ll see where each of those bytes comes from.

ANALOGY· Scientific notation with a tiny notebook

Imagine writing every number in scientific notation, like 6.02×10236.02 \times 10^{23}, but your notebook only has room for a few characters. You can spend them on the exponent (how big or small a number you can write at all) or on the digits (how precisely you can write it). Write "6.0×10236.0 \times 10^{23}" and you lose precision; allow only one exponent digit and you can’t write 102310^{23} at all.

The exponent digits are the exponent bits that set a format’s range. The digits before the "×\times" are the mantissa bits that set its precision. BF16 is a notebook that spends generously on the exponent; FP16 spends more on digits; FP8 has almost no room for either.

Anatomy of a float

A floating-point number has three fields: a sign bit, an exponent ee and a mantissa (or fraction) ff. For normal numbers the value is

(−1)sign×(1.f)2×2 e−bias.(-1)^{\text{sign}} \times (1.f)_2 \times 2^{\,e - \text{bias}} .

The leading “1.” is implicit, so it’s free. The exponent field picks a power of two, setting the scale; the mantissa picks one of 2M2^{M} evenly spaced values between that power of two and the next. So the relative spacing between neighboring numbers is about 2−M2^{-M} everywhere: a format with 7 mantissa bits has roughly 2–3 significant decimal digits, wherever the number sits in its range. When the exponent field is all zeros you get subnormals, which trade precision for a gentle underflow toward zero.

formatsign / exponent / mantissamaxsmallest normalrel. precision 2−M2^{-M}
FP321 / 8 / 23≈3.4×1038\approx 3.4 \times 10^{38}≈1.2×10−38\approx 1.2 \times 10^{-38}1.2×10−71.2 \times 10^{-7}
FP161 / 5 / 1065,5046.1×10−56.1 \times 10^{-5}9.8×10−49.8 \times 10^{-4}
BF16BF16 (bfloat16)A 16-bit floating-point format with the same exponent range as FP32 but less precision, widely used for training.1 / 8 / 7≈3.4×1038\approx 3.4 \times 10^{38}≈1.2×10−38\approx 1.2 \times 10^{-38}7.8×10−37.8 \times 10^{-3}
FP8FP88-bit floating-point formats (E4M3, E5M2) used to speed up training and inference on recent GPUs. E4M31 / 4 / 34482−6≈0.0162^{-6} \approx 0.0160.125
FP8 E5M21 / 5 / 257,3442−14≈6.1×10−52^{-14} \approx 6.1 \times 10^{-5}0.25

Look at FP16 versus BF16. Both use 16 bits, but FP16 can’t represent anything above 65,504 or (normally) below about 6×10−56 \times 10^{-5}, while BF16 has FP32’s entire range, because it is literally the top 16 bits of an FP32 number. BF16 pays with precision: 7 mantissa bits instead of 10.

INTERACTIVEBit-layout explorer
Scale before casting (loss scaling)
FP32 1 + 8 + 23 bits= 0.100000001
error 1.490116e-9 (0.0000015%)max 3.402823e+38min normal 1.175494e-38min subnormal 1.401298e-45ε = 2⁻23 ≈ 1.192093e-7
FP16 1 + 5 + 10 bits= 0.0999755859
error -2.441406e-5 (0.024%)max 65504min normal 6.103516e-5min subnormal 5.960464e-8ε = 2⁻10 ≈ 0.0009765625
BF16 1 + 8 + 7 bits= 0.100097656
error 9.765625e-5 (0.098%)max 3.389531e+38min normal 1.175494e-38min subnormal 9.18355e-41ε = 2⁻7 ≈ 0.0078125
FP8 E4M3 1 + 4 + 3 bits= 0.1015625
error 0.0015625 (1.6%)max 448min normal 0.015625min subnormal 0.001953125ε = 2⁻3 ≈ 0.125
FP8 E5M2 1 + 5 + 2 bits= 0.09375
error -0.00625 (6.3%)max 57344min normal 6.103516e-5min subnormal 1.525879e-5ε = 2⁻2 ≈ 0.25

■ sign · ■ exponent · ■ mantissa. Click any bit to flip it. Values are rounded to nearest (ties to even).

Type a number (or pick a preset) and see how each format stores it, what value actually comes back, and the rounding error. Click individual bits to flip them. Try 0.1, 65504 vs 70000, 448 vs 500, and 1e-8 with and without loss scaling.

Things to try:

  1. 0.1 is not exactly representable in any binary format. FP32 is off by about 1.5×10−91.5 \times 10^{-9}; BF16 by 10−410^{-4}; FP8 E4M3 lands on 0.1016.
  2. 70000 overflows FP16 to infinity but is fine in BF16 (rounded to 70,144). 500 overflows E4M3 (which has no infinity, so the cast produces NaN) but fits E5M2.
  3. 1e-8 underflows to zero in FP16 and both FP8 formats. Now choose ×2¹⁶: the scaled number survives in FP16, and dividing back recovers it. That’s loss scaling in one click.
  4. Flip the top exponent bit of a BF16 number and watch it jump by a factor of 21282^{128}. Flip the lowest mantissa bit and it barely moves.

Mixed-precision training

The 2017 paper “Mixed Precision Training” (Micikevicius et al.) established the recipe that still underlies everything:

Compute in low precision, keep a master copy in FP32. Forward and backward matrix multiplications run in 16-bit, but the optimizer updates an FP32 copy of every weight. Why? Updates are tiny relative to weights. In BF16 the gap between 1.0 and the next representable number is 2−7≈0.00782^{-7} \approx 0.0078, so adding an update of 0.001 to a weight of 1.0 rounds straight back to 1.0. Thousands of such updates would vanish without a trace. In FP32 they accumulate correctly, and the 16-bit working copy is refreshed from the master after each step.

Accumulate in FP32. A matrix multiplication sums thousands of products. Tensor cores multiply low-precision inputs but accumulate the sums in FP32. Numerically sensitive operations such as softmax, normalization layers, the loss and large reductions also run in FP32.

Scale the loss (for FP16). Many gradient values are smaller than FP16’s smallest normal number (6×10−56 \times 10^{-5}) and underflow to zero. Loss scalingLoss scalingMultiplying the loss by a large factor before backpropagation so tiny FP16 gradients don't underflow to zero, then dividing the gradients back before the update. multiplies the loss by a large factor SS (say 2162^{16}) before backpropagation; by the chain rule every gradient is multiplied by SS too, lifting them into FP16’s range. Before the optimizer step, the gradients are divided by SS in FP32. Dynamic loss scaling starts with a large SS, halves it (and skips the step) whenever an overflow produces Inf or NaN, and slowly increases it again otherwise.

BF16 made life simpler. With FP32’s range, gradients rarely underflow, so loss scaling is unnecessary, and BF16 has become the default training format on hardware that supports it (Google TPUs, NVIDIA from the A100 onward). Its lower precision turned out to matter less than FP16’s limited range, as long as master weights and accumulations stay in FP32.

FP8: the next halving

FP8 doubles tensor-core throughput again, but with only 8 bits the range problem returns with a vengeance. E4M3 covers roughly 2−92^{-9} to 448 (including subnormals); E5M2 trades a mantissa bit for range up to 57,344. A common recipe uses E4M3 for weights and activations in the forward pass and E5M2 for gradients, whose values span a wider range.

The key trick is scaling. Before casting a tensor to FP8, divide it by a scale factor so its largest value lands near the format’s maximum, and remember the scale to undo it after the matmul. With a single scale per tensor, though, one outlier ruins everything. LLM activations famously contain a few channels with values hundreds of times larger than the rest. The per-tensor scale has to accommodate the outlier, which pushes all the ordinary values down toward zero, where FP8 has almost no precision.

Where the bytes go

Putting it together, here’s where each number in a BF16 mixed-precision run lives. The working weights are BF16 (2 bytes per parameter) and feed the matmuls. Activations saved for the backward pass are mostly BF16, and FP8 recipes store some of them in FP8 too. Gradients come out of the backward pass in BF16 (2 bytes), sometimes accumulated in FP32 across micro-batches. The optimizer holds the FP32 master weights (4 bytes) and Adam’s two FP32 moments (4 + 4 bytes). That’s the 16 bytes per parameter from the distributed training lesson.

Notice what FP8 does and doesn’t change. It speeds up the matrix multiplications and shrinks some activations and communication, but the optimizer state still dominates memory. That’s why labs also shrink the optimizer state, for example by keeping Adam’s moments in BF16 (as DeepSeek-V3 did) or in 8-bit formats, and why an optimizer with a single momentum buffer (like Muon) is attractive. Precision is a budget you spend tensor by tensor: aggressive where errors average out (big matmuls), conservative where they accumulate (weight updates, long sums, normalization statistics).

A final practical note: low precision rarely fails gracefully. A run that works for a week can hit a single overflow that propagates NaNs through the whole network, or a slow drift that only shows up as worse loss. That’s why large runs monitor gradient norms and loss spikes closely, and keep frequent checkpoints to roll back to.

Below 8 bits: MXFP4 and NVFP4

The block-scaling idea is now built into hardware formats. The Open Compute Project’s microscaling (MX) formats group values into blocks of 32 that share one 8-bit power-of-two scale. MXFP4 stores each element as a 4-bit float (E2M1: 1 sign, 2 exponent and 1 mantissa bit, so the only magnitudes are 0, 0.5, 1, 1.5, 2, 3, 4 and 6). NVIDIA’s NVFP4 uses smaller blocks of 16 elements with a more precise FP8 (E4M3) block scale, plus a second FP32 scale per tensor. Recent GPUs such as NVIDIA’s Blackwell generation support these formats natively.

So far, 4-bit formats are mostly used for inference: OpenAI’s open-weight gpt-oss models, for example, ship their MoE weights in MXFP4 (see the quantization lesson). Training in FP4 is an active research area: NVIDIA, for example, has reported pretraining a 12B-parameter model on 10 trillion tokens with NVFP4 (arXiv 2509.25149). Treat such results as early and provisional.

MATH CORNER · optionalEncoding 0.1 by hand, and why updates vanish

Encode 0.1 in BF16 (bias 127, 7 mantissa bits).

  1. Normalize: 0.1=1.6×2−40.1 = 1.6 \times 2^{-4}. Sign bit 0.
  2. Exponent field: −4+127=123=011110112-4 + 127 = 123 = 01111011_2.
  3. Mantissa: 0.6×27=76.80.6 \times 2^7 = 76.8, which rounds to 77=1001101277 = 1001101_2.
  4. Decode: (1+77/128)×2−4=1.6015625/16=0.10009765625(1 + 77/128) \times 2^{-4} = 1.6015625 / 16 = 0.10009765625.
formatexponent fieldmantissastored valuerelative error
BF16−4+127=123-4 + 127 = 123round(0.6⋅27)=77\text{round}(0.6 \cdot 2^7) = 770.10009770.098%
FP8 E4M3−4+7=3-4 + 7 = 3round(0.6⋅23)=5\text{round}(0.6 \cdot 2^3) = 50.10156251.6%

Swamping. Adding x+δx + \delta in a format with MM mantissa bits loses δ\delta entirely when ∣δ∣<12⋅2−M∣x∣|\delta| < \tfrac{1}{2} \cdot 2^{-M} |x| (half the spacing between neighbors). For BF16 at x=1x = 1: anything below 2−8≈0.00392^{-8} \approx 0.0039 disappears. A typical update η⋅step≈10−4\eta \cdot \text{step} \approx 10^{-4} to 10−310^{-3} is well below that, which is why the master weights must be FP32 (whose threshold at x=1x = 1 is about 6×10−86 \times 10^{-8}).

Block scaling for FP8. For a block XX, pick s=max⁡∣X∣/448s = \max|X| / 448, store fp8(X/s)\text{fp8}(X / s) and the scalar ss. A matmul of two scaled blocks becomes (sAsB)⋅(A~B~)(s_A s_B) \cdot (\tilde A \tilde B), with A~B~\tilde A \tilde B computed on FP8 tensor cores and accumulated in higher precision. Smaller blocks mean each scale fits its values more tightly.

In code

In PyTorch, BF16 mixed precision is one context manager; FP16 adds a gradient scaler:

import torch

model = model.cuda()                     # parameters (the master weights) stay FP32
opt = torch.optim.AdamW(model.parameters(), lr=3e-4)

# BF16: autocast runs matmuls in bf16, keeps sensitive ops in fp32. No scaling needed.
with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
    loss = model(x, y)
loss.backward(); opt.step(); opt.zero_grad()

# FP16: same, plus dynamic loss scaling
scaler = torch.amp.GradScaler("cuda")
with torch.autocast(device_type="cuda", dtype=torch.float16):
    loss = model(x, y)
scaler.scale(loss).backward()            # gradients are multiplied by S
scaler.step(opt)                         # unscales; skips the step on Inf/NaN
scaler.update()                          # adjusts S for next time
opt.zero_grad()

FP8 training needs library support beyond plain PyTorch, for example NVIDIA’s Transformer Engine or torchao, which manage the per-tensor or per-block scales for you.

COMMON MISCONCEPTIONS
  • “BF16 is just a less accurate FP16.” BF16 has less precision than FP16 (7 vs 10 mantissa bits) but vastly more range (the same as FP32). For training, range matters more, which is why BF16 rarely needs loss scaling.
  • “Mixed precision means the model is stored in 16 bits.” During training, an FP32 master copy of the weights, and FP32 optimizer states, are kept; 16-bit copies are used for the heavy compute. That’s why training state is ~16 bytes per parameter.
  • “FP8 training just casts everything to 8 bits.” It casts the big matrix multiplications, with carefully managed scale factors (per tensor or per block), higher-precision accumulation, and sensitive layers left in BF16/FP32.
  • “Loss scaling changes what the model learns.” Gradients are divided by the same factor before the update, so the math is unchanged; the scaling only protects small values from underflowing while they’re in FP16.

Check your understanding

0/5 answered
  1. Q1.FP16 and BF16 both use 16 bits. What's the key trade-off between them?

  2. Q2.Why does mixed-precision training keep an FP32 master copy of the weights?

  3. Q3.What does loss scaling protect against in FP16 training?

  4. Q4.Why did DeepSeek-V3 use 1×128 tile and 128×128 block scales for FP8 instead of one scale per tensor?

  5. Q5.A value of 500 is cast to FP8 E4M3 without scaling. What happens?

Further reading

Progress is saved in this browser only.
Type to search across all lessons and glossary terms.