Skip to content
Back to skills

Mixed Precision Training

ASecurity

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

  • 61 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 12, 2026
toolspythongit

Works with

  • cli

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill mixed-precision-training --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Mixed Precision Training?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Mixed Precision Training
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-mixed-precision-training/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-mixed-precision-training)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: cv-mixed-precision-training
description: >
  Uses PyTorch AMP autocast and GradScaler for FP16 training, halving memory usage and speeding up training on modern GPUs.
---
# Mixed Precision Training

## 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

```python
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

- RANZCR CLiP - Catheter and Line Position Challenge (Kaggle)
- Source: [ranzcr-resnext50-32x4d-starter-training](https://www.kaggle.com/code/yasufuminakama/ranzcr-resnext50-32x4d-starter-training)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.