Gradient Accumulation › Accumulation Steps: How Many?

Michael BrenndoerferJanuary 27, 20265 min read
Language AI HandbookAll chapters
Part 32 of 67Training Optimization
Chapter 236Gradient Accumulation › Accumulation Steps: How Many?
Reading section 4 of 14 · Accumulation Steps: How Many? · 5 minSection 4 of 14 · 5 min

Book contents

Section 4 of 14 · 5 min read

Gradient Accumulation › Accumulation Steps: How Many?One section · Sections in this chapter4 / 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 number of accumulation steps KK is a key design choice. It directly determines the effective batch size seen by the optimizer:

Beff=K×BmicroB_{\text{eff}} = K \times B_{\text{micro}}

where:

  • BeffB_{\text{eff}}: the effective batch size, the number of samples the optimizer "sees" per weight update
  • KK: the number of accumulation steps
  • BmicroB_{\text{micro}}: the micro-batch size, the number of samples in each forward-backward pass

If your GPU can process 8 samples per forward pass and you want an effective batch size of 256, you set K=32K = 32.

Out[3]:
Visualization
Line plot showing effective batch size vs accumulation steps for micro-batch sizes 4, 8, and 16.
Effective batch size as a function of accumulation steps K for micro-batch sizes 4, 8, and 16. Each line scales linearly, and the target of 256 samples is reached at K=64, K=32, and K=16 respectively. This shows how a smaller memory-limited micro-batch requires more sequential accumulation steps to preserve the same logical batch.

Choosing K for Memory Constraints

The primary driver for choosing KK is the target effective batch size from your training recipe. As discussed in the Large Batch Training chapter, large language models typically train with effective batch sizes ranging from 256 to 4 million tokens per step, depending on the model size and phase of training. If your target effective batch size is fixed by empirical best practices (such as the linear scaling rule for learning rates), then KK is determined by:

K=⌈BeffBmicro⌉K = \left\lceil \frac{B_{\text{eff}}}{B_{\text{micro}}} \right\rceil

where:

  • ⌈⋅⌉\lceil \cdot \rceil: the ceiling function, rounding up to the nearest integer
  • BeffB_{\text{eff}}: the target effective batch size from your training recipe
  • BmicroB_{\text{micro}}: the maximum micro-batch size that fits in GPU memory

A secondary consideration is the computational overhead of accumulation. Each micro-batch requires a complete forward and backward pass. With K=8K = 8 instead of K=1K = 1, you perform eight forward-backward passes per optimizer step rather than one. On a single GPU, this overhead is negligible in terms of floating-point operations (you perform exactly the same number of multiplications and additions), but it can affect memory bandwidth utilization and synchronization overhead in distributed settings.

A practical way to find the maximum micro-batch size is to start with a small batch (for instance, 1 or 2 samples) and double it until you hit an out-of-memory error. The last successful size is your BmicroB_{\text{micro}}. Then set K=Beff/BmicroK = B_{\text{eff}} / B_{\text{micro}}, rounding up.

The Effective Batch Size Trade-off

Increasing KK is not always beneficial beyond a certain point. Once the effective batch size exceeds the critical batch size for a given task, adding more samples per update produces diminishing returns in gradient quality while increasing the number of steps needed to reach the same number of epochs. The Large Batch Training chapter covers this trade-off in detail, including the Chinchilla and GPT-3 effective batch size recipes. The key point here is that gradient accumulation is a tool for reaching a target effective batch size, not an excuse to make that target arbitrarily large.

A useful mental model is that gradient quality saturates. With very few samples (say Bmicro=1B_{\text{micro}} = 1), the gradient from a single sample can point in nearly any direction because it reflects only one data point's contribution. As batch size grows, the gradient direction becomes more stable and more representative of the true loss landscape. But beyond a certain size, adding samples barely changes the direction. Gradient accumulation lets you climb out of the noisy, small-batch regime without requiring hardware that can hold the entire logical batch simultaneously.

To see this saturation quantitatively, consider the variance of the stochastic gradient estimator. If each sample contributes a gradient with variance σ2\sigma^2, the variance of the mean over BB independent samples is σ2/B\sigma^2 / B. Doubling BB from 64 to 128 halves the variance, a clear benefit. Doubling BB from 8192 to 16384 again halves the variance, but the remaining variance is already small. The signal-to-noise ratio of the gradient direction improves much faster at small batch sizes than at large ones. The concept of the critical batch size formalizes where this diminishing return becomes severe enough to matter in practice.