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 BB equally weighted examples, let gi=∇θℓi(θ)g_i=\nabla_\theta \ell_i(\theta) be the gradient of example ii‘s loss at the current weights θ\theta. The batch gradient is

gB=1B∑i=1Bgi.(1)g_B=\frac{1}{B}\sum_{i=1}^B g_i. \tag{1}

If the examples are drawn independently from the same distribution and the individual gradients have covariance Σ\Sigma, their covariance terms add:

Cov⁡(gB)=1B2∑i=1BCov⁡(gi)=ΣB.(2)\operatorname{Cov}(g_B)=\frac{1}{B^2}\sum_{i=1}^B\operatorname{Cov}(g_i)=\frac{\Sigma}{B}. \tag{2}

Under those assumptions, doubling the batch cuts the standard deviation of the gradient along any fixed direction by a factor of 2\sqrt{2}. 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, θ\theta. 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 gˉj\bar g_j be the average gradient for the eight sequences in microbatch jj, still treating all sequences equally. The gradient for the full batch is

g32=8gˉ1+8gˉ2+8gˉ3+8gˉ432=gˉ1+gˉ2+gˉ3+gˉ44.(3)g_{32}=\frac{8\bar g_1+8\bar g_2+8\bar g_3+8\bar g_4}{32} =\frac{\bar g_1+\bar g_2+\bar g_3+\bar g_4}{4}. \tag{3}

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 RR replicas each process bb sequences per microbatch and accumulate KK microbatches, the global batch size in sequences is

Bseq=R b K.(4)B_{\mathrm{seq}}=R\,b\,K. \tag{4}

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 SS occupies SS 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 njn_j count the valid targets in microbatch jj, and let Aj(θ)A_j(\theta) be the sum of their losses. Index jj across all passes and replicas. For N=∑jnj>0N=\sum_j n_j>0, the global mean loss and its gradient are

L(θ)=∑jAj(θ)N,∇θL=∑j∇θAj(θ)N.(5)L(\theta)=\frac{\sum_j A_j(\theta)}{N}, \qquad \nabla_\theta L=\frac{\sum_j\nabla_\theta A_j(\theta)}{N}. \tag{5}

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:

MicrobatchValid targetsAveraging the two microbatch meansAveraging all four targets
First31/61/6 per target1/41/4 per target
Second11/21/2 per target1/41/4 per target

The same tensor shape hid two different denominators. If you already have mean gradients, weight them by nj/Nn_j/N. 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 N/RN/R 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.