Gradient Accumulation
Train at a batch size you can't fit, one micro-batch at a time. Scale the loss, hold the optimizer step, and let the gradients pile up.
Learning Objectives
- Derive the effective batch identity: `effective_batch = micro_batch * accum_steps`.
- Implement loss-per-micro-batch scaling so the accumulated gradient matches a single full-batch backward.
- Skip optimizer synchronization until the last micro-batch (sync-on-last-step).
- Read a throughput against effective batch curve and explain the diminishing return.
The Problem
Gradient accumulation means running several forward and backward passes before a single optimizer step, so the gradients sum inside the parameter buffers and the update behaves as if you had used one large batch. The effective batch is the micro-batch size times the number of accumulation steps.
Say you want an effective batch of 512 because the loss curve is smoother and the optimizer step makes more sense at that scale. The accelerator on the desk holds 32 examples before it runs out of memory. Doubling the batch is not an option. Halving the model is not an option. The trick the field reached for in 2017 and kept using is to run 16 backward passes, let the gradients accumulate inside the parameter buffers, and only step the optimizer when the count reaches the target.
The risk is that the loss is no longer the same number it was at the bigger batch. The cross entropy of 16 mini-batches summed naively is 16 times the loss of one full batch. Without scaling, the gradient direction is correct but the magnitude is wrong, and the optimizer step is 16 times too big. The fix is one division, and it is easy to forget.
The Concept
The contract has four parts:
- Divide each micro-batch's loss by
accum_stepsbeforebackward(). PyTorch sums gradients intoparam.gradby default, and the division brings the running sum back to the right scale. - Fire the optimizer step once per effective batch, after the last micro-batch's backward. Stepping mid-accumulation skews every parameter the rest of the run depends on.
- Advance the optimizer's state (momentum buffers, Adam moments) once per effective step, not once per micro-batch. Otherwise the exponential moving averages see the wrong frequency and burn through the schedule.
- On a single device this is bookkeeping. On a multi-rank cluster the same pattern wraps the non-final micro-batches in a
no_synccontext that skips the gradient all-reduce, so the last micro-batch reduces the full accumulated gradient in one pass instead of paying the network cost N times.
The equivalence proof in code
loss = criterion(model(x_full), y_full)
loss.backward()
opt.step()is equivalent to
for x, y in chunks(x_full, y_full, n):
scaled = criterion(model(x), y) / n
scaled.backward()
opt.step()up to floating point summation order. The accumulated gradient buffer at the end of the loop is the same tensor a single full-batch backward would produce. equivalence_check returns the max absolute gradient and parameter differences, and main() asserts both stay under 1e-4.
Where the cost goes
Each micro-batch costs one forward and one backward, so accumulation trades memory for time. The throughput curve in outputs/accum-curve.json shows what happens as the effective batch grows at fixed micro-batch:
There is no free lunch. Doubling accum_steps doubles the wall time per optimizer step. What changes is the variance of the gradient estimate: at the same wall budget you take fewer optimizer steps, but each one is averaged over more samples. The literature treats large batch and small batch as different optimization problems. The point here is mechanical, not statistical.
Build It
code/main.py is the runnable artifact. It does three things.
Step 1: equivalence check
equivalence_check() builds two copies of the same network with the same seed. One sees a 16-sample batch in one forward pass. The other sees four 4-sample chunks with the loss divided by four. The function compares the gradient buffers before the optimizer step and the parameters after. It returns max_grad_diff, max_param_diff and params_init_match, and main() asserts both diffs are under 1e-4.
Step 2: sync-on-last-step pattern
train_one_optimizer_step walks the micro-batches. Every micro-batch except the last enters no_sync_context(model). On a single process the context is a no-op; on DDP it is where the gradient all-reduce gets skipped. The bookkeeping is the same either way. A sync_counter increments on each synced backward (the last micro-batch, or every micro-batch if no_sync_until_last is False), so with the default it grows by one per effective step, not N, and accumulates across num_steps.
Step 3: the throughput curve
sweep_effective_batches runs the same model with a fixed micro-batch and a list of accumulation steps. For each setting it logs:
samples_per_sec: total samples seen divided by wall timemedian_step_ms: 50th percentile per effective stepsync_calls: collective points exercisedavg_loss: average across the sweep's optimizer steps
The output lands in outputs/accum-curve.json and is reusable from a notebook.
Run it:
python3 code/main.pyThe script prints the equivalence diff, then the sweep table, then the JSON path. Exit code zero.
Use It
In production training, gradient accumulation lives behind one knob. PyTorch's pattern is accumulation_steps = effective_batch // (micro_batch * world_size). That is the general formula for a multi-rank run; the demo here is a single process, so world_size is 1. The frameworks you are not allowed to use here wrap the same loop with the same steps: scale the loss, skip sync on non-final micros, accumulate, step once.
Three patterns show up in practice:
- Pick the micro-batch size to saturate device memory. Smaller wastes accelerator cycles, and larger crashes.
- Pick the effective batch from a learning rate schedule. Large effective batches need scaled learning rates and warmup, which is the linear scaling rule people have talked about since 2017.
- Treat the accumulation count as the bridge between the two. It is a knob you can tune at runtime without rewriting the data loader.
Ship It
outputs/skill-gradient-accumulation.md captures the recipe so a peer can drop it into a new repo: scale loss by accum_steps, skip optimizer sync on non-final micros, step the optimizer once per effective batch, and log throughput against effective batch as JSON so the trade is visible.
Exercises
- Re-run the sweep with
--num-steps 100and plot samples per second against effective batch. Where does the curve flatten? - Add a wrong scaling variant (no division) and show the parameter diff at step 1 against the reference.
- Swap SGD for AdamW and confirm the optimizer state advances once per effective step, not once per micro-batch.
- Introduce a real
DistributedDataParallelwrapper and route theno_sync_contextto its method. Confirm the number of all-reduces per effective batch drops from N to 1, which is whatsync_callscounts whenno_sync_until_lastis on. - Modify the equivalence check to compare two different micro splits (2 by 8 vs 4 by 4) and explain any tolerance you need to relax.
Key Terms
| Term | What people say | What it actually means |
|---|---|---|
| Micro batch | The batch you forward | The slice that fits in memory in a single forward pass |
| Accum steps | Backward passes per step | Number of backwards summed before one optimizer step |
| Effective batch | The batch | Micro batch times accum steps, times world size when training is data parallel (the demo is single process) |
| Loss scaling | Divide by N | Per-micro-batch division so summed gradients match full batch |
| Sync on last | Skip the rest | Only run the gradient collective on the last backward in the window |
My bet is that the missing division is the mistake worth guarding against first, because it fails quietly: the gradient direction is right and only the step size is wrong. I'd run the no-division variant from the exercises early, so you see the oversized parameter step for yourself instead of taking my word for it.
I'd also treat the equivalence check as the gate for everything after it. If the gradients from one 16-sample batch and four 4-sample chunks don't match under 1e-4, the throughput curve is measuring a different training run than the one you think you have.
One thing I'd keep in mind when you read the sweep: on a single process the no_sync context is a no-op, so nothing is actually skipped here. The sync_calls column counts the one synced backward per effective step, which is where the all-reduce would fire on a cluster. The saving only becomes real once you wire in DDP, which is what the DDP exercise asks for.
Further Reading
- PyTorch docs on
DistributedDataParallel.no_syncfor the production version of the sync-on-last-step trick. - Goyal et al., 2017, on linear scaling for large batch training, the canonical reason to care about effective batch.
- PyTorch issue tracker on gradient accumulation interactions with mixed precision unscaling.
- Phase 19 lessons 42 to 45 cover the model, data loader, optimizer, and trainer scaffolding this lesson assumes.
- Phase 19 lesson 47 covers checkpoint and resume so a long accumulation run survives a wallclock cap.
Code
- main.py (python, 354 lines)
- test_main.py (python, 105 lines)