Skip to content

Mixed Precision Training

Definition

Mixed Precision Training — an approach where forward and backward passes are executed in 16-bit formats (FP16 or BF16), while the master copy of weights and optimizer updates are stored in FP32. This allows for reduced VRAM consumption and increased tensor core throughput.

Formats

Format Range Precision Features
FP32 wide 23-bit mantissa base precision
FP16 narrow 10-bit mantissa risk of overflow, requires loss scaling
BF16 like FP32 7-bit mantissa resilient to overflow

Loss Scaling

With FP16, small gradients are zeroed out due to limited range. Dynamic loss scaling compensates for this:

scaler = torch.cuda.amp.GradScaler()
with torch.cuda.amp.autocast(dtype=torch.float16):
    loss = model(batch)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()

BF16 does not require loss scaling due to a range comparable to FP32.

See also