# Mixed precision training explained: FP16, BF16 and FP8



Learn how to use FP16, BF16, and FP8 to double your training speed and reduce VRAM usage with practical PyTorch examples and hardware tips.



Training · 2026-08-10 · 4 min read



Modern deep learning models are growing exponentially in size, but our GPU memory remains a finite resource. If you’ve ever hit an `OutOfMemory` (OOM) error or felt that your training loops were sluggish, mixed precision training is the first optimization you should reach for.

In this guide, we will break down the mechanics of FP16, BF16, and the emerging FP8 format, and show you how to implement them to maximize throughput on your GPU instances.

## The Numerical Landscape: FP32 vs. The Rest

Traditionally, deep learning used **FP32 (Single Precision)**. It uses 32 bits to represent a number: 1 bit for the sign, 8 for the exponent, and 23 for the mantissa (precision). While accurate, it is computationally expensive and memory-intensive.

Mixed precision training works by performing most operations in lower precision formats while keeping a "master copy" of weights in FP32 to ensure the model eventually converges.

### 1. FP16 (Half Precision)
FP16 uses 16 bits: 5 for the exponent and 10 for the mantissa.
*   **Pros:** Significant speedups (up to 2-3x) and 50% less memory usage.
*   **Cons:** Small dynamic range. It is prone to "gradient underflow," where small gradients become zero, causing the model to stop learning. To fix this, we use **Loss Scaling**.

### 2. BF16 (Brain Floating Point)
Developed to solve FP16’s limitations, BF16 also uses 16 bits but allocates them differently: 8 bits for the exponent (same as FP32) and 7 for the mantissa.
*   **Pros:** It handles large dynamic ranges without loss scaling. It is the "gold standard" for LLM training on modern hardware (NVIDIA Ampere architecture and newer).
*   **Cons:** Slightly less precise than FP16, but usually negligible for deep learning.

### 3. FP8 (The New Frontier)
Introduced with the NVIDIA Hopper (H100) architecture, FP8 drops to just 8 bits. It comes in two variants: E4M3 (for weights/activations) and E5M2 (for gradients).
*   **Pros:** Massive throughput increases for inference and large-scale pre-training.
*   **Cons:** Requires specialized hardware and sophisticated scaling logic.

## Implementing Mixed Precision in Python

Modern frameworks like PyTorch make switching to mixed precision remarkably easy using the `torch.cuda.amp` module (Automatic Mixed Precision).

Here is a practical snippet for implementing FP16 with Loss Scaling:

```python
import torch
import torch.nn as nn
from torch.cuda.amp import autocast, GradScaler

# Define model and optimizer
model = MyComplexModel().cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)

# Initialize the GradScaler for FP16
scaler = GradScaler()

for data, target in dataloader:
    optimizer.zero_grad()
    
    # Runs the forward pass in FP16
    with autocast(dtype=torch.float16):
        output = model(data.cuda())
        loss = criterion(output, target.cuda())

    # Scales loss, calls backward(), and unscales gradients for optimizer
    scaler.scale(loss).backward()
    
    # scaler.step() first unscales the gradients of the optimizer's assigned params.
    # If gradients contain infs or NaNs, optimizer.step() is skipped.
    scaler.step(optimizer)
    
    # Updates the scale for next iteration
    scaler.update()
```

If you are using an A100 or H100, you can often simplify this further by using `dtype=torch.bfloat16` inside `autocast` and removing the `GradScaler`, as BF16 is robust enough to handle gradients without scaling.

## Practical Tips for ML Engineers

1.  **Check your Hardware:** FP16 is supported on almost all modern GPUs, but BF16 requires NVIDIA Ampere (A100, A30, A10) or newer. If you try to run BF16 on a T4 or V100, it will emulate it in FP32, resulting in a massive performance penalty.
2.  **Batch Size Matters:** Because mixed precision reduces memory usage by half, you should double your batch size to fully saturate the GPU’s Tensor Cores.
3.  **Mind the Tensor Cores:** For maximum efficiency, ensure your layer dimensions (input/output features) are multiples of 8 (or 16/32 for FP8). This allows the hardware to utilize specialized matrix math units.
4.  **Watch the Loss:** If you see `NaN` losses immediately with FP16, your loss scaling might be failing. Try switching to BF16 if your hardware supports it.

## Running at Scale on Podstack

When you're ready to move from local experimentation to heavy-duty training, Podstack provides the infrastructure to run these workloads with zero friction. Whether you need a single A100 for a few minutes or a cluster of H100s for a week, the per-second billing ensures you only pay for the compute you actually use.

To get started, simply set your API key and deploy your training script as a job:

```text
Podstack does not ship a command-line interface. Use the console, the Python package `podstack-sdk`, or the API at https://podstack.ai/api/v1 with a `ps_live_` key.
```

Because Podstack treats GPU functions as first-class citizens, you can also wrap your inference logic in a serverless endpoint. By using FP16 or FP8 for your inference functions, you can significantly reduce latency and lower your cost per request, as Podstack only bills for the active execution time of the function.

## Conclusion

Mixed precision is no longer an "advanced" technique; it is a necessity for modern ML. By understanding the trade-offs between FP16, BF16, and FP8, you can drastically reduce training time and hardware costs. 

Start by auditing your current training scripts—switching just a few lines of code to enable `autocast` on a Podstack A100 could cut your bill in half while doubling your iteration speed.
