﻿# Gradient accumulation needs a consistent token denominator

Gradient accumulation can match a larger token-weighted batch only when microbatch loss numerators and valid-token denominators are combined consistently.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/gradient-accumulation-token-denominator/
Language: en

Gradient accumulation divides a training update across several smaller forward and backward passes. It can reduce the need to hold all examples in memory simultaneously. But accumulation is not automatically equivalent to a larger batch: the scaling of each microbatch determines the objective whose gradients are actually added.

## Gradients add for a declared summed objective

Differentiation is linear over a sum of losses evaluated at the same parameters. Accumulating gradients from separate pieces can therefore reconstruct the gradient of that sum. The optimizer must not update the parameters between pieces if the intended comparison is a single large-batch update. Clearing gradients between pieces would also erase the desired accumulation. This mathematical statement concerns a specified objective and shared parameter state; stochastic layers and numerical rounding can still affect exact equality between execution paths.

## Unequal lengths expose a common weighting mistake

Suppose one microbatch contains 3 included targets and another contains 9. Giving their independently averaged losses equal weight gives the small microbatch disproportionate influence per token. For a global token mean, combine summed losses and divide by the total of 12 included targets, or apply equivalent weights to the separate means. The result is not obtained merely by dividing each mean by the number of accumulation steps. That shortcut assumes compatible denominators and can fail when sequence lengths or ignored-label counts differ.

## The optimizer step has its own boundary

Accumulation changes how a gradient is assembled; the optimizer update remains a separate event. Learning-rate schedules, gradient clipping and optimizer-state updates must follow the intended effective-batch boundary. Clipping each microbatch independently is generally different from clipping the assembled gradient because clipping is nonlinear. Weight decay applied at every microstep likewise changes the update sequence. Make these operations explicit in a training trace rather than treating an accumulation setting as a complete guarantee that every surrounding operation behaves like one larger update.

## Verify with a tiny deterministic fixture

Use a small model and a fixed set of valid targets. Compare the declared large-batch gradient with accumulated gradients before the optimizer update, recording each microbatch’s numerator and denominator. Keep stochastic behavior controlled and use a stated numerical tolerance. Then separately compare the resulting parameter update. This separates loss weighting from optimizer timing and floating-point differences. The fixture is especially valuable when changing collators or sequence packing, because occupancy changes can alter target counts while leaving the nominal accumulation count unchanged.

## Primary research and implementation references

- [Adam: A Method for Stochastic Optimization](https://arxiv.org/abs/1412.6980).

- [PyTorch: Automatic Mixed Precision Examples](https://docs.pytorch.org/docs/2.15/notes/amp_examples.html).

- [PyTorch: CrossEntropyLoss](https://docs.pytorch.org/docs/2.15/generated/torch.nn.CrossEntropyLoss.html).

## Related engineering context

[Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/); [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/).
