How much data should go into one training step? With a larger batch, each gradient averages over more examples. With a smaller batch, you get more updates from the same data. Once you’ve picked a size, there’s a second question: how much of that batch can your GPU process at once?
What batch size changes
A training batch is the data used to compute one optimizer update. For a batch of equally weighted examples, let be the gradient of example ‘s loss at the current weights . The batch gradient is
If the examples are drawn independently from the same distribution and the individual gradients have covariance , their covariance terms add:
Under those assumptions, doubling the batch cuts the standard deviation of the gradient along any fixed direction by a factor of . The average is less sensitive to which examples happened to land in the batch.
Take the same stream of examples and double the batch size: you get half as many full updates if the stream divides evenly. Each gradient averages more examples, but the model holds its weights fixed for more of the stream. What you put in the batch matters, too. If the gradients are positively correlated, averaging them removes less noise. A thousand targets from a single passage don’t necessarily give you the same spread of training signals as a thousand targets drawn from different texts.
Microbatch: what fits in one pass
Say you want 32 sequences per update, but your GPU only has room for the activations of eight at a time. You can run eight sequences through forward and backward, free the activations from that pass, and move on to the next eight. You keep the gradient from each pass.
Each group of eight is a microbatch. The microbatch size is what one model replica processes in a forward and backward pass. Splitting the batch reduces how many activations need to be in memory at once, while the parameters and the accumulated gradient stay in memory across passes.
Gradient accumulation: one update after several passes
Run all four microbatches at the same weights, . For each one, backpropagation adds its gradient to the buffer. After the last pass, the optimizer uses the combined gradient once. That’s gradient accumulation.
Let be the average gradient for the eight sequences in microbatch , still treating all sequences equally. The gradient for the full batch is
This is why dividing each mean loss by the number of accumulation steps works in this case. Each mean represents the same number of loss terms. If you update the weights after each group of eight instead, the second group already sees different weights. That’s four smaller steps.
With data parallelism, multiple replicas process different portions of the same training batch. If replicas each process sequences per microbatch and accumulate microbatches, the global batch size in sequences is
Two replicas doing two passes of eight still give 32 sequences per update. Two GPUs working together on the same replica count only once.
Token averaging: use the full batch denominator
In language modeling, it’s common to average the loss over valid prediction targets. A sequence of length occupies input positions, but padding and masked labels don’t contribute to the loss. Two microbatches with the same number of sequences can therefore have different numbers of targets.
Let count the valid targets in microbatch , and let be the sum of their losses. Index across all passes and replicas. For , the global mean loss and its gradient are
Suppose two microbatches each hold one sequence, padded to the same length. The first has three valid targets; the second has one. Here’s how much weight each target gets:
| Microbatch | Valid targets | Averaging the two microbatch means | Averaging all four targets |
|---|---|---|---|
| First | 3 | per target | per target |
| Second | 1 | per target | per target |
The same tensor shape hid two different denominators. If you already have mean gradients, weight them by . If a microbatch has no valid targets, its loss sum contributes zero; there’s no local mean to compute. A short final accumulation window uses its actual target count, too.
The same issue comes up across GPUs. If each replica accumulates its own gradient sum and the reducer averages those sums, as in standard PyTorch DDP, divide the result by to get the global mean. Apply gradient clipping to that combined gradient. Clipping each piece first can change their relative weights.
Splitting preserves the mathematical gradient as long as the loss terms stay the same. If changing the packing lets a token attend to a different document, it changes the loss for that token. Even with the same objective, floating-point addition in a different order can give slightly different numbers.