Mixed Precision Training

SkillDocs & knowledge

Uses PyTorch AMP autocast and GradScaler for FP16 training, halving memory usage and speeding up training on modern GPUs.

Available today. Use it from your connected AI after setup.

Connect ahel once, and every AI you use reads what you have installed.

Then ask your AI: use the Mixed Precision Training skill

What this skill tells your AI

The instructions your AI receives, as published by wenmin-wu/ds-skills in skills/cv/mixed-precision-training/SKILL.md and read by ahel’s review.

Overview

Run forward pass in FP16 (half precision) while keeping master weights in FP32. This halves GPU memory for activations and enables larger batch sizes or image resolutions. PyTorch's autocast handles dtype selection per operation; GradScaler prevents underflow in FP16 gradients.

Quick Start

import torch
from torch.cuda.amp import autocast, GradScaler

scaler = GradScaler()

for images, labels in train_loader:
    images, labels = images.cuda(), labels.cuda()
    optimizer.zero_grad()

    with autocast():
        logits = model(images)
        loss = criterion(logits, labels)

    scaler.scale(loss).backward()
    scaler.unscale_(optimizer)
    torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
    scaler.step(optimizer)
    scaler.update()

Workflow

  1. Create GradScaler() before training loop
  2. Wrap forward pass + loss in autocast() context
  3. Call scaler.scale(loss).backward() instead of loss.backward()
  4. Optionally unscale before gradient clipping
  5. scaler.step(optimizer) and scaler.update() replace optimizer.step()

Key Decisions

  • Memory savings: ~40-50% reduction in activation memory → can double batch size
  • Speed: 1.5-2x faster on Volta/Ampere GPUs (V100, A100, RTX 30xx+)
  • Accuracy: Virtually identical to FP32 — safe for almost all CV tasks
  • Gradient clipping: Must unscale_ before clip_grad_norm_ to clip in FP32 space
  • When to skip: Very small models where memory isn't a bottleneck

References

Signals

GitHub stars
60
Forks
4
Last commit
Apr 2026
Advanced
Catalog kind
skill
Gateway key
cv-mixed-precision-training
Source
github.com/wenmin-wu/ds-skills