Gradient checkpointing

From wenmin-wu/ds-skills · View interactive version

Gradient checkpointing is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Trades compute for memory by recomputing intermediate activations during backprop instead of storing them, reducing memory from O(n) to O(sqrt(n)).

How to use this skill

Skill file

Gradient Checkpointing

Overview

Large transformer models store all intermediate activations for backpropagation, consuming massive GPU memory. Gradient checkpointing discards most activations during the forward pass and recomputes them on-the-fly during backward. This reduces activation memory from O(n_layers) to O(sqrt(n_layers)) at the cost of ~30% slower training. Essential for fine-tuning large models on limited GPU memory.

Quick Start

from transformers import AutoModel

model = AutoModel.from_pretrained("microsoft/deberta-v3-base")
model.gradient_checkpointing_enable()

# Now train as usual — memory usage drops significantly
# Pair with mixed precision for maximum memory savings

Workflow

  1. Load your pretrained transformer model
  2. Call model.gradient_checkpointing_enable() before training
  3. Train as normal — PyTorch handles activation recomputation automatically
  4. Optionally combine with mixed precision (torch.cuda.amp) for further savings

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.