Inferensys

Glossary

Gradient Checkpointing

Gradient Checkpointing is a memory optimization technique for training deep neural networks that trades compute for memory by selectively saving only a subset of layer activations during the forward pass and recomputing the others during the backward pass.
ML engineer managing model training cluster on laptop, GPU utilization visible, technical deep learning setup.
MEMORY OPTIMIZATION TECHNIQUE

What is Gradient Checkpointing?

Gradient Checkpointing is a fundamental technique for training deep neural networks on memory-constrained hardware.

Gradient Checkpointing is a memory optimization technique for training deep neural networks that trades compute for memory by selectively saving only a subset of layer activations during the forward pass and recomputing the others during the backward pass. This strategic recomputation dramatically reduces the peak memory required to store intermediate results, enabling the training of models that are significantly larger than the available GPU or NPU memory. The technique is a form of time-memory trade-off, where increased computational cost is accepted to alleviate a critical memory bottleneck.

The process works by designating specific layers as checkpoints. During the forward pass, only the outputs of these checkpoint layers are stored in memory. During the backward pass, when gradients for a non-checkpoint layer are needed, the forward computation is re-executed from the nearest upstream checkpoint. This is managed by the autograd engine in frameworks like PyTorch. It is particularly crucial for training large Transformer models with long sequence lengths, where activation memory scales quadratically with context size, and is a key enabler for hardware-aware model optimization on specialized accelerators like NPUs.

MEMORY OPTIMIZATION TECHNIQUE

Key Characteristics of Gradient Checkpointing

Gradient Checkpointing is a memory-for-compute trade-off technique that enables the training of deeper neural networks by strategically managing which intermediate activations are stored during the forward pass.

01

Core Trade-Off: Memory vs. Compute

Gradient Checkpointing fundamentally trades increased computation for reduced memory consumption. During the standard backpropagation algorithm, all intermediate layer activations from the forward pass must be stored to compute gradients, leading to O(n) memory complexity with network depth (n). Checkpointing reduces this to O(√n) by storing only a subset of activations (checkpoints) and recomputing the non-checkpointed activations on-demand during the backward pass. This recomputation is the source of the ~30% typical compute overhead.

02

Selective Activation Storage

Not all layer outputs are treated equally. The technique involves a strategic policy for selecting which activations to save as checkpoints. Common policies include:

  • Uniform: Save every k-th layer's output.
  • Dynamic (Optimal): Use a dynamic programming algorithm to minimize total recomputation cost for a given memory budget.
  • Heuristic-based: Prioritize layers with large output tensors or high computational cost to recompute. The saved checkpoints act as 'anchor points' from which the forward pass can be re-executed in segments during backpropagation.
03

Recomputation on Demand

This is the computational engine of the technique. During the backward pass, when gradients for a non-checkpointed layer are needed, the system:

  1. Identifies the nearest upstream checkpoint.
  2. Re-executes the forward pass from that checkpoint to the required layer.
  3. Computes the local gradients using these freshly recomputed activations.
  4. Discards the recomputed activations after use. This process eliminates the need to store the entire activation history, but requires the forward pass to be run multiple times for different segments of the network.
04

Implementation in Frameworks

Modern deep learning frameworks provide built-in support, abstracting the complexity.

  • PyTorch: torch.utils.checkpoint.checkpoint and torch.utils.checkpoint.checkpoint_sequential wrap model segments. It uses a custom autograd Function that reruns the forward pass in the backward hook.
  • TensorFlow: tf.recompute_grad decorator or the tf.contrib.layers.recompute_grad function (in older TF 1.x).
  • JAX: jax.checkpoint (formerly jax.remat) is a core transformation that performs gradient checkpointing. These APIs allow developers to annotate which parts of their model should use checkpointing, often at the level of individual transformer blocks or residual groups.
05

Primary Use Case: Large Models & Long Sequences

Checkpointing is indispensable in specific high-memory scenarios:

  • Training Very Deep Networks: Enables models with hundreds or thousands of layers that would otherwise exceed GPU memory.
  • Long Sequence Processing: Critical for Transformer models with long context windows, where the self-attention mechanism's memory footprint grows quadratically with sequence length. Checkpointing the attention computation is often combined with memory-efficient attention algorithms like Flash Attention.
  • Large Batch Sizes: Allows for increased batch sizes within a fixed memory budget, which can improve training stability and utilization. It is a standard technique in large language model (LLM) and large vision model training pipelines.
06

Interaction with Parallelism Strategies

Gradient Checkpointing interacts closely with distributed training methods:

  • Pipeline Parallelism: Essential for making pipeline bubbles manageable. Checkpointing is applied within each pipeline stage to reduce the memory footprint of activations stored for the backward pass across micro-batches.
  • Model & Tensor Parallelism: While these split the model parameters, the per-device activation memory can still be prohibitive. Checkpointing is used in conjunction to train even larger models.
  • ZeRO Optimizer Stages: Used with ZeRO Stage 2 (partitioned gradients) and Stage 3 (partitioned parameters) to further push the boundaries of model size by reducing activation memory, which ZeRO does not address.
COMPARISON

Gradient Checkpointing vs. Other Memory Optimization Techniques

A technical comparison of memory reduction strategies for training large neural networks, highlighting trade-offs between memory, compute, and implementation complexity.

Technique / FeatureGradient CheckpointingActivation RecomputationModel ParallelismZero Redundancy Optimizer (ZeRO)

Primary Goal

Reduce peak activation memory

Reduce peak activation memory

Distribute model parameters across devices

Eliminate memory redundancy in data-parallel training

Memory Savings Source

Activations

Activations

Model Parameters & Activations

Optimizer States, Gradients, Parameters

Compute Overhead

High (33-50% extra FLOPs)

Very High (2x forward passes)

Moderate (Communication latency)

Low to Moderate (Communication volume)

Communication Overhead

None (single device)

None (single device)

High (inter-device per layer)

Moderate (inter-device per optimizer step)

Implementation Complexity

Moderate (framework support)

Low (manual checkpoint placement)

High (model partitioning logic)

High (integration with training framework)

Transparency to Model Code

High (often automatic)

Low (requires manual code changes)

Low (requires model restructuring)

High (often automatic via optimizer wrapper)

Best Suited For

Single-device training of very deep networks

Extreme memory constraints on a single device

Models too large for any single device's memory

Large-scale data-parallel training across many devices

Typical Memory Reduction

√(n) vs. n layers (e.g., 10x for 100 layers)

Up to ~n vs. n layers

Linear with number of devices

Up to 8x for Adam optimizer states

IMPLEMENTATION SUPPORT

Frameworks and Libraries Supporting Gradient Checkpointing

Gradient checkpointing is implemented as a core memory optimization feature within major deep learning frameworks and specialized libraries, enabling the training of models that exceed single-device memory capacity.

GRADIENT CHECKPOINTING

Frequently Asked Questions

Gradient Checkpointing is a memory-for-compute trade-off technique essential for training large neural networks on hardware with limited memory, such as Neural Processing Units (NPUs). This FAQ addresses its core mechanisms, trade-offs, and implementation.

Gradient Checkpointing is a memory optimization technique for training deep neural networks that trades increased computational cost for reduced memory consumption by selectively saving only a subset of layer activations during the forward pass and recomputing the non-saved activations during the backward pass.

During standard backpropagation, all intermediate activations from the forward pass must be stored to compute gradients, leading to memory usage that scales linearly with model depth and batch size. Gradient Checkpointing strategically designates certain layers as checkpoints. Only the activations at these checkpoints are stored; the activations for the layers between checkpoints are discarded after the forward pass and must be recomputed during the backward pass when they are needed for gradient calculation. This reduces peak memory usage from O(n) to O(√n) in a naive implementation, where n is the number of layers, enabling the training of models that would otherwise exceed accelerator memory limits.

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.