Inferensys

Glossary

Gradient Checkpointing

A memory optimization technique that trades compute for memory by discarding intermediate activations during the forward pass and recomputing them on demand during the backward pass.
Performance engineer optimizing AI latency on laptop, latency charts visible, technical optimization session.
MEMORY OPTIMIZATION

What is Gradient Checkpointing?

A memory optimization technique that trades compute for memory by discarding intermediate activations during the forward pass and recomputing them on demand during the backward pass.

Gradient checkpointing is a memory optimization technique that reduces the peak memory footprint of training deep neural networks by selectively discarding intermediate activations during the forward pass and recomputing them on demand during the backward pass. This explicit trade-off of additional computation for reduced memory enables the training of larger models or the use of larger batch sizes on hardware with limited GPU high-bandwidth memory (HBM).

During standard backpropagation, all intermediate activations from the forward pass are stored in memory because they are required to compute gradients. With gradient checkpointing, the user defines checkpoints—specific layers where activations are preserved—while activations for non-checkpointed layers are discarded. During the backward pass, when a discarded activation is needed, a mini-forward pass is re-executed from the nearest preceding checkpoint to recompute it. This can reduce memory consumption from O(n) to O(sqrt(n)) for a network with n layers, at the cost of approximately 33% additional forward computation.

Memory Optimization

Key Characteristics of Gradient Checkpointing

A compute-for-memory trade-off technique that enables training of large vision transformer models on limited hardware by strategically discarding and recomputing intermediate activations.

01

Core Mechanism: The Forward-Backward Trade

During a standard forward pass, all intermediate activations are stored in memory for the backward pass. Gradient checkpointing breaks this convention by discarding activations of designated layers. During backpropagation, when a discarded activation is needed for the gradient calculation, it is recomputed on-demand by re-running a segment of the forward pass. This transforms the memory footprint from O(n) to O(sqrt(n)) or O(log n) for the checkpointed segments, at the cost of approximately 33% additional forward computation.

02

Implementation in Vision Transformers

In Vision Transformer (ViT) architectures, checkpointing is typically applied to the MLP and self-attention blocks within each encoder layer. Frameworks like PyTorch implement this via torch.utils.checkpoint.checkpoint, which wraps a module segment as a no-gradient function. During the backward pass, the autograd engine triggers a re-computation of the wrapped segment with gradients enabled. This is particularly critical for Swin Transformers and Masked Autoencoders (MAE) processing large 3D medical volumes where activation memory can exceed 40GB per sample.

03

Memory Complexity Reduction

A standard n-layer network stores M activations. With optimal checkpointing at every sqrt(n) layers, the memory requirement drops to approximately O(sqrt(n) * M). For a ViT-Large model with 24 transformer blocks processing a 3D CT scan:

  • Without checkpointing: ~36GB activation memory
  • With selective checkpointing: ~8GB activation memory
  • Recomputation overhead: ~20-25% increase in training step time This enables training on a single NVIDIA A100 (80GB) that would otherwise require model parallelism across multiple GPUs.
~78%
Memory Reduction
~25%
Compute Overhead
04

Selective Checkpointing Strategies

Not all operations benefit equally from checkpointing. Optimal strategies target high-activation, low-compute operations:

  • Attention maps: Store QK^T matrices; they are memory-intensive but cheap to recompute
  • Linear projections: Checkpoint aggressively; matrix multiplications are fast to recompute
  • LayerNorm/GELU activations: Store these; they are computationally trivial but memory-light
  • Convolutional stem layers: Avoid checkpointing; early feature maps are small and recomputation is expensive Frameworks like DeepSpeed and FairScale offer automatic activation partitioning to apply this selectively.
05

Interaction with Mixed Precision Training

When combined with Automatic Mixed Precision (AMP) training, gradient checkpointing requires careful handling of precision casting. The recomputed forward pass must replicate the original precision state:

  • FP16 forward: Recomputation occurs in FP16 to match the original activation values
  • Master weights: Remain in FP32 and are not affected by checkpoint boundaries
  • Loss scaling: The gradient scaling factor is applied identically to recomputed gradients This combination is standard in medical imaging pipelines using nnUNet or MONAI with ViT backbones, enabling batch sizes of 4-8 on 3D volumes that would otherwise be limited to batch size 1.
06

Recomputation vs. Offloading Trade-off

Gradient checkpointing is one of three primary memory-saving strategies, each with distinct trade-offs:

  • Gradient Checkpointing: +25% compute, no CPU-GPU transfer overhead, deterministic
  • Activation Offloading: Moves activations to CPU RAM; +10% compute but high PCIe bandwidth dependency
  • Gradient Accumulation: Splits batch across micro-batches; no compute overhead but changes batch norm statistics For medical imaging with DICOM volumes, checkpointing is preferred over offloading when GPU compute is abundant but VRAM is constrained, as PCIe transfers become a bottleneck with high-resolution 3D data.
GRADIENT CHECKPOINTING

Frequently Asked Questions

Clear, technical answers to the most common questions about this critical memory optimization technique for training large vision transformer models on medical imaging data.

Gradient checkpointing is a memory optimization technique that trades increased computation for reduced memory consumption during neural network training. During the standard forward pass, a deep learning framework normally stores all intermediate activations required for the backward pass. Gradient checkpointing instead discards the activations of designated layers and recomputes them on demand during backpropagation. The technique segments the computational graph into checkpoints—typically at the boundaries of Transformer blocks or residual stages—and only stores the inputs to these segments. When the backward pass reaches a segment, it re-runs the forward computation for that segment to regenerate the necessary intermediate tensors. This reduces peak memory usage from O(n) to O(√n) or O(log n), where n is the number of layers, enabling the training of significantly larger models on memory-constrained hardware.

MEMORY EFFICIENCY COMPARISON

Gradient Checkpointing vs. Other Memory Optimization Techniques

A technical comparison of gradient checkpointing against alternative methods for reducing GPU memory footprint during deep learning training.

FeatureGradient CheckpointingMixed Precision TrainingModel Parallelism

Primary Mechanism

Discards intermediate activations; recomputes during backward pass

Uses FP16 for forward/backward; FP32 master weights

Partitions model layers across multiple accelerators

Memory Reduction Target

Activation memory

Activations, weights, gradients

All memory types (distributed)

Compute Overhead

~20-30% additional forward pass compute

Negligible; often faster due to tensor cores

Communication overhead between devices

Requires Multiple GPUs

Training Throughput Impact

Reduced throughput proportional to recomputation

Increased throughput on supported hardware

Scales near-linearly with device count

Implementation Complexity

Single API call or flag toggle

Few lines of code; framework-native

Significant architectural refactoring required

Numerical Stability Risk

None; exact recomputation

Loss scaling required; potential underflow

None inherent to partitioning

Compatibility with Other Techniques

Orthogonal; combines with mixed precision and parallelism

Orthogonal; combines with checkpointing and parallelism

Orthogonal; combines with checkpointing and mixed precision

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.