Gradient checkpointing and gradient accumulation are the two techniques that let you train a model that does not fit in memory. They attack different halves of the training memory bill — the activations stored for the backward pass, and the batch size held in flight — and both do it with the same bargain: spend extra compute or extra wall-clock time to buy back memory you do not have. Understanding them is the difference between "this model is too big for my GPU" and "this model trains fine, just a little slower."\n\nGradient checkpointing attacks activation memory by recomputing instead of storing. The backward pass needs the activations produced during the forward pass to compute each layer's gradient, so the naive approach stores every intermediate activation — a cost that grows linearly with network depth and sequence length, and which for large models dwarfs the memory used by the weights themselves. Checkpointing keeps only a sparse set of checkpoint activations and throws the rest away; when the backward pass needs a discarded activation, it recomputes it by re-running the forward pass from the nearest checkpoint. With checkpoints placed every square-root-of-depth layers, peak activation memory drops from order-n to order-square-root-of-n, at the price of roughly one extra forward pass — about 30% more compute for a large multiplicative cut in memory.\n\nGradient accumulation attacks batch memory by splitting a big batch into small pieces. A large batch stabilizes training and is often necessary for good results, but the whole batch's activations must fit in memory at once. Accumulation instead runs several small micro-batches through forward and backward one at a time, adding their gradients into a buffer without stepping the optimizer, and only applies a single weight update once all micro-batches have been processed. The effective batch size becomes the micro-batch size times the number of accumulation steps (times the number of data-parallel replicas), so you can reproduce the gradient of a giant batch using the memory footprint of a tiny one — you just pay for it in more sequential forward-backward passes per update.\n\nThe critical detail in accumulation is when you step. The optimizer update and the gradient zeroing must happen only after the final micro-batch, not every pass; stepping too early silently shrinks your effective batch. You also have to be careful with anything that computes statistics over the batch — BatchNorm sees only a micro-batch at a time, which is one more reason large-model training favors LayerNorm — and with loss normalization so the accumulated gradient matches the true large-batch average rather than its sum.\n\nThe two techniques compose, and they compose with everything else. A realistic large-model recipe stacks gradient checkpointing (to fit the activations), gradient accumulation (to reach the target batch size), mixed precision (to halve the bytes), and sharded data parallelism (to split the optimizer state) all at once. Each is an independent lever on a different part of the memory budget, and together they are what make training models far larger than any single device's memory possible.\n\n| Technique | What it saves | What it costs | The knob |\n|---|---|---|---|\n| Gradient checkpointing | Activation memory (order-n to order-sqrt-n) | ~1 extra forward pass (~30% compute) | Number / placement of checkpoints |\n| Gradient accumulation | Peak batch memory | More sequential passes per update | Accumulation steps K |\n| Effective batch | — | — | micro-batch x K x replicas |\n\n``svg\n\n``\n\nThe wrong way to see these is as obscure flags you flip when you get an out-of-memory error. The right way is to see the training memory budget as having distinct line items — weights, optimizer state, activations, and the batch — and to recognize that each has its own dedicated lever. Checkpointing pays compute to shrink the activation line; accumulation pays wall-clock to shrink the batch line; mixed precision shrinks the bytes; sharding splits the optimizer state. Read both techniques through a trade-compute-or-time-for-memory lens rather than a free-lunch lens, and fitting a large training run stops being guesswork and becomes an accounting exercise: find the line item that is too big, and pull the lever that shrinks it.
Related Topics
Explore 500+ Semiconductor & AI Topics
From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.