Gradient Accumulation › Gradient Accumulation for Memory
Language AI HandbookAll chapters 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 parameters stored in float32, this costs bytes. In mixed precision training, a float16 copy costs bytes.
- Optimizer states: Adam maintains first and second moment estimates, costing roughly 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 bytes in float32 or 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 where is the number of layers, is the model dimension, and 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 .
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.


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 to reach an effective batch size of 128, with the only cost being extra sequential forward-backward passes.
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 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 can free enough memory to continue training without any change to the effective batch size or optimizer behavior.