C.W.K.
Stream
Lesson 07 of 12 · published

Mixed Precision: BF16, FP16, FP8

~10 min · mixed-precision, bf16, fp8

Level 0Token
0 XP0/94 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Training in pure FP32 is wasteful for modern hardware. Mixed precision uses lower-precision floating-point types for the bulk of the computation while keeping a few critical operations in FP32 (or higher) for numerical stability. The result: roughly 2× memory savings and 2× throughput compared to FP32, with no measurable quality loss.

FormatBitsRangeUse case
FP3232±3.4 × 10³⁸Master weights, loss scaling, optimizer state
FP1616±65,504Forward/backward pass on older GPUs
BF1616±3.4 × 10³⁸ (same as FP32)Modern training (Llama 3.3, DeepSeek-V3, etc.)
FP8 (E4M3, E5M2)8LimitedFlash Attention 3, FP8 training experiments, inference

BF16 (Brain Floating Point) is the modern default for training. It has the same exponent range as FP32 — so no overflow problems — but only 8 bits of mantissa, which is enough precision for gradients and activations. FP16 has more mantissa precision but a much smaller range, making it prone to overflow without loss scaling. BF16 made the loss-scaling dance obsolete.

Code

BF16 mixed precision in PyTorch·python
import torch
from torch.cuda.amp import autocast

# On Ampere or later (A100, H100, etc):
with autocast(dtype=torch.bfloat16):
    logits = model(input_ids)
    loss = causal_lm_loss(logits, target_ids)

# Backward pass uses BF16 automatically
loss.backward()
optimizer.step()
optimizer.zero_grad()
# No GradScaler needed for BF16 — that was an FP16 trick.

External links

Exercise

Train the same tiny model in FP32, FP16 with GradScaler, and BF16 (autocast). Measure throughput (tokens/sec) and final loss. The BF16 numbers should be best on a modern GPU. Document the speedup and any quality differences.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.