Inferensys

Glossary

Gradient Checkpointing

Gradient checkpointing is a memory optimization technique that trades compute for memory during neural network training by selectively discarding and recomputing intermediate activations, enabling the training of larger models with limited GPU memory.
ML engineer managing model training cluster on laptop, GPU utilization visible, technical deep learning setup.
EFFICIENT MODEL ARCHITECTURES

What is Gradient Checkpointing?

A memory optimization technique for training deep neural networks.

Gradient checkpointing is a memory-for-compute trade-off technique in deep learning that reduces GPU memory consumption during training by selectively discarding intermediate activations from the forward pass and recomputing them on-demand during the backward pass. This enables the training of significantly larger models or the use of longer sequences within a fixed memory budget, such as on edge hardware, at the cost of increased computational overhead.

The technique works by strategically saving only a subset of activations, known as checkpoints, during the forward pass. During backpropagation, the discarded activations are recomputed from the nearest checkpoint. This reduces peak memory usage from O(n) to O(√n) with respect to the number of layers, a critical optimization for small language model engineering and other memory-constrained environments, though it typically increases training time by 20-30%.

EFFICIENT MODEL ARCHITECTURES

Key Characteristics of Gradient Checkpointing

Gradient checkpointing is a memory optimization technique that trades compute for memory by selectively discarding and recomputing intermediate activations during the backward pass of neural network training.

01

Core Trade-Off: Compute for Memory

The fundamental principle of gradient checkpointing is a time-memory trade-off. Instead of storing all intermediate activations from the forward pass—which consumes O(n) memory for n layers—the technique strategically discards most of them. During the backward pass, these discarded activations are recomputed on-demand from the nearest stored checkpoint. This reduces peak memory usage from O(n) to O(√n), enabling the training of models that are 2-4x larger on the same hardware, at the cost of approximately 30-40% more compute time for the extra forward passes.

02

Selective Checkpoint Placement

Not all activations are checkpointed. The placement of checkpoints is a critical optimization problem. Common strategies include:

  • Uniform Placement: Checkpointing every k-th layer (e.g., every 5 layers). Simple but suboptimal.
  • Optimal Dynamic Programming: Algorithms that determine the checkpoint schedule to minimize total recomputation cost for a fixed memory budget, considering the varying computational cost of different layers.
  • Manual Heuristics: Targeting memory-intensive layers (e.g., early transformer blocks with large sequence lengths). The goal is to store just enough activations to keep the recomputation cost manageable while staying within the memory limit.
03

Implementation in Autodiff Frameworks

Gradient checkpointing is implemented by intercepting the automatic differentiation (autodiff) engine. In PyTorch, this is done via torch.utils.checkpoint.checkpoint. The function wraps a segment of the model:

  • Forward Pass: The wrapped segment runs, but its outputs are stored, not its internal activations. A custom autograd Function is recorded.
  • Backward Pass: When gradients are requested, this custom function re-executes the wrapped segment in a no-grad context to recompute the internal activations, then performs the backward pass through the segment with gradients enabled. Frameworks like TensorFlow and JAX have similar APIs (tf.recompute_grad, jax.checkpoint).
04

Activation Recomputation Process

The backward pass becomes a series of recomputation sweeps. Starting from the output and moving backward:

  1. The system loads the nearest stored checkpoint activation.
  2. It re-executes the forward pass for the subsequent non-checkpointed layers.
  3. With the full set of activations now temporarily available for that segment, it computes the gradients for those layers.
  4. The recomputed activations are discarded, and the process repeats for the next segment. This creates a wavefront of computation where memory is repeatedly allocated and freed for small segments, keeping the peak usage low. The process is automatic but adds significant computational overhead.
05

Interaction with Mixed Precision Training

Gradient checkpointing is often used in conjunction with mixed precision training (using FP16/BF16). This combination is powerful but requires care:

  • Checkpoint Precision: Typically, checkpoints are stored in FP32 (full precision) even when forward/backward passes use FP16. This prevents a buildup of quantization error during the chain of recomputations, which can degrade training stability.
  • Recomputation Precision: The recomputation forward pass is usually performed in the same lower precision (FP16) as the original forward pass for speed.
  • Memory Savings: Storing checkpoints in FP32 doubles their memory footprint compared to FP16, but this is offset by not storing all other activations. The net effect is still a substantial memory reduction.
06

Use Cases and Limitations

Primary Use Cases:

  • Training very large models (e.g., LLMs, diffusion models) on memory-constrained hardware.
  • Processing extremely long sequences in transformers, where activation memory is the primary bottleneck.
  • Enabling larger batch sizes for better hardware utilization and training stability.

Key Limitations:

  • Compute Overhead: Adds 30-40% more FLOPs, increasing wall-clock training time.
  • Implementation Complexity: Requires careful segmentation of the model; not all model architectures (e.g., those with complex control flow) are easily checkpointable.
  • I/O Bottlenecks: On systems with slow CPU-GPU data transfer, the overhead can be higher. It is less beneficial for models where parameter memory, not activation memory, is the limiting factor.
MEMORY-COMPUTE TRADEOFFS

Gradient Checkpointing vs. Related Techniques

A comparison of techniques for managing memory and computational load during the training of large neural networks, focusing on the trade-offs inherent in each approach.

Feature / MetricGradient CheckpointingFull Activation StorageMicro-BatchingCPU Offloading

Primary Objective

Reduce peak GPU memory usage

Maximize training speed

Fit large batches into memory

Train models larger than GPU memory

Memory Reduction Mechanism

Discard & recompute intermediate activations

Store all activations

Split batch into smaller sequential chunks

Move optimizer states/gradients/params to CPU RAM

Computational Overhead

~30-40% extra forward pass compute

None

Minimal (slight pipeline bubble)

High (significant CPU-GPU transfer latency)

Implementation Complexity

Medium (selective layer checkpointing)

Low (default PyTorch/TF behavior)

Low (data loader logic)

High (manual tensor management)

Maximum Model Size (Relative)

2-4x larger

Baseline (1x)

Limited by micro-batch size

10x+ larger (bound by system RAM)

Best Suited For

Memory-bound single GPU training

Compute-bound scenarios with ample VRAM

Pipeline parallelism in multi-GPU setups

Extreme model sizes on limited GPU hardware

Impact on Training Speed

Slower (due to recomputation)

Fastest

Slightly slower (pipeline overhead)

Slowest (bandwidth-limited)

Framework Support

Native in PyTorch (torch.utils.checkpoint), TF

Default in all frameworks

Manual implementation or pipeline parallel libs

Libraries like DeepSpeed (ZeRO-Offload)

GRADIENT CHECKPOINTING

Frequently Asked Questions

Gradient checkpointing is a critical memory optimization technique for training large neural networks. It strategically trades compute for memory, enabling the training of models that would otherwise exceed available GPU memory. This FAQ addresses its core mechanisms, trade-offs, and practical implementation.

Gradient checkpointing is a memory optimization technique for neural network training that trades compute for memory by selectively discarding intermediate activations during the forward pass and recomputing them during the backward pass. In standard backpropagation, all intermediate layer outputs (activations) are stored in memory to compute gradients. Gradient checkpointing designates certain layers as checkpoints. During the forward pass, only the activations at these checkpoints are retained; the non-checkpoint activations are discarded. During the backward pass, when gradients for a non-checkpointed segment are needed, the network re-executes the forward pass for that segment, starting from the nearest stored checkpoint, to regenerate the required activations. This process reduces peak memory usage from O(n) to O(√n) with respect to the number of layers, at the cost of approximately one additional forward pass per checkpointed segment.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.