Gradient Accumulation › Gradient Accumulation for Memory

Michael BrenndoerferJanuary 27, 20266 min read
Language AI HandbookAll chapters
Part 32 of 67Training Optimization
Chapter 236Gradient Accumulation › Gradient Accumulation for Memory
Reading section 5 of 14 · Gradient Accumulation for Memory · 6 minSection 5 of 14 · 6 min

Book contents

Section 5 of 14 · 6 min read

Gradient Accumulation › Gradient Accumulation for MemoryOne section · Sections in this chapter5 / 14 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The memory savings from gradient accumulation are real and significant, but they apply only to a specific category of memory: activation memory. Understanding which types of memory are affected shows when accumulation helps.

Memory Taxonomy

GPU memory during training is occupied by several distinct components:

  • Model parameters: The weights and biases themselves. For a model with PP parameters stored in float32, this costs 4P4P bytes. In mixed precision training, a float16 copy costs 2P2P bytes.
  • Optimizer states: Adam maintains first and second moment estimates, costing roughly 8P8P bytes in float32 (two copies of the parameter count). This dominates for large models.
  • Gradient buffers: One gradient tensor per parameter, typically matching the parameter precision. Costs 4P4P bytes in float32 or 2P2P in float16.
  • Activation memory: Intermediate tensors computed during the forward pass and retained for the backward pass. Unlike the above, this scales with batch size. For a transformer, activation memory grows roughly as O(L⋅d⋅B)O(L \cdot d \cdot B) where LL is the number of layers, dd is the model dimension, and BB is the batch size.

Gradient accumulation reduces only activation memory, since at any point only one micro-batch is being processed. Parameters, optimizer states, and gradient buffers remain constant regardless of KK.

It is worth being specific about why activation memory scales with batch size. During the forward pass, the model computes intermediate representations at each layer: attention scores, attention outputs, feed-forward intermediate activations, and so on. These are not thrown away after the forward pass because the backward pass needs them to compute gradients via the chain rule. Each sample in the batch has its own set of these intermediate tensors. If you double the batch size, you double the number of samples in flight, and thus double the total activation storage. Gradient accumulation sidesteps this by processing one micro-batch at a time: the activations for micro-batch 1 are computed, used for the backward pass, and then freed before micro-batch 2 begins.

The gradient buffers, by contrast, are not freed between micro-batches. They must persist across the entire accumulation cycle so that gradients can be summed. The size of these buffers is proportional to the number of parameters, not the batch size. For a 125-million parameter model in float32, the gradient buffers occupy roughly 500 MB regardless of whether you accumulate 1 or 64 micro-batches.

Out[4]:
Visualization
Bar chart showing estimated GPU memory in GiB for parameters, Adam optimizer states, gradients, and activations.
Estimated GPU memory breakdown for a 125M-parameter model at micro-batch size 8 and sequence length 512. Activations are the largest component in this example and the only component that scales with micro-batch size; parameters, Adam states, and gradient buffers remain fixed during accumulation.
Out[5]:
Visualization
Line chart comparing estimated GPU memory with no accumulation against accumulation using micro-batches of eight.
Estimated total GPU memory for a 125M-parameter model as effective batch size increases. Processing the full logical batch at once makes activation memory grow linearly, while gradient accumulation with micro-batch size 8 keeps the memory footprint constant at about 3.6 GiB.

When Accumulation Helps Most

The benefit of accumulation is largest when activation memory is the binding constraint. For very large models, the optimizer state alone can exceed available memory, and accumulation provides no relief. In that regime, techniques like mixed precision training (covered in the Training Infrastructure part), ZeRO optimizer sharding, and activation checkpointing are necessary.

For models where activation memory dominates, gradient accumulation is immediately effective. A model that fits only 2 samples per forward pass can use K=64K = 64 to reach an effective batch size of 128, with the only cost being extra sequential forward-backward passes.

Activation Memory vs. Parameter Memory

For a GPT-2 medium model (345 million parameters), the parameter memory in float32 is approximately 1.3 GB. The optimizer state in Adam adds another 2.6 GB, for a total of nearly 4 GB that is independent of batch size. Activation memory at batch size 32 for sequence length 1024 can exceed 8 GB. Reducing the micro-batch to 4 drops activation memory to roughly 1 GB, and gradient accumulation with K=8K = 8 recovers the effective batch size of 32. Total memory drops from over 12 GB to under 6 GB, making training feasible on a single 8 GB GPU.

In practice, the combination of micro-batch size and accumulation steps is often the first knob to turn when a training run fails with an out-of-memory error. Before resorting to more complex techniques like activation checkpointing or model parallelism, halving the micro-batch size and doubling KK can free enough memory to continue training without any change to the effective batch size or optimizer behavior.