Batch size in tokens and gradient accumulation
Measure batch size in tokens (B·T) and simulate a large batch with gradient accumulation over microbatches. Explain the critical batch size beyond which a bigger batch stops reducing the number of steps needed.
Builds on: The LLM training loop
What the animation says
Count predictions, not sequences
Your training loop averages the loss over a batch, then takes one optimizer step. But how big is a batch, really? Draw it as a grid. Each row is a sequence. Each column is a position in that sequence. Every cell is one next-token prediction, and every prediction adds its own term to the gradient. So the size that matters is rows times columns: the number of tokens. Two batches. A has thirty-two sequences of five hundred twelve tokens. B has eight sequences of four thousand ninety-six. Which gives more gradient terms?
Average it in pieces
B wins: about thirty-three thousand predictions against sixteen thousand. Count tokens, not sequences. Now say the batch you want doesn't fit in memory. A gradient is an average, so you can compute it in chunks. Split sixty-four rows into four equal microbatches. They run one after another, and each gives its own mean gradient. Divide each one by four, add them up, and you get exactly the full-batch gradient. Only then, one optimizer step. This loop forgot something. Tap the line that makes the gradient four times too big.
A million tokens on a small GPU
Now a real target: about one million tokens per step, with sequences of two thousand forty-eight tokens. One million divided by two thousand forty-eight is five hundred twelve sequences. But only eight fit on the GPU at once. Five hundred twelve divided by eight gives sixty-four microbatches. Each microbatch loss is divided by sixty-four, and the optimizer steps once, after all of them. Same update as the big batch, just slower. Say a one-million-token batch needs twenty thousand steps to reach your target loss. Make it four million. How many steps now?
Why bigger batches stop helping
Twelve thousand five hundred. Quadrupling the batch cut barely more than a third of the steps. Why? A batch gradient is the true direction plus noise, and more tokens shrink that noise. While noise dominates, steps must stay cautious, so doubling the batch nearly halves the steps. But the gradient is only a local slope. The loss surface curves away from it, so steps can't grow much, however precisely you measure. The crossover is the critical batch size. Past it, extra tokens just polish a direction that's already accurate.
Steps vs tokens: the trade
An empirical fit: steps equal the minimum steps, times one plus the critical batch over your batch. Take ten thousand minimum steps and a critical batch of one million tokens. A quarter million per batch needs fifty thousand steps. One million needs twenty thousand. Four million, twelve thousand five hundred. Now multiply by batch size to count tokens: twelve and a half billion, twenty billion, fifty billion. Below the critical batch, you waste steps. Above it, you waste tokens, and so compute.
Count predictions; average them in pieces
Why bigger batches stop helping
The formulas
1M tokens on a small GPU
Accumulated gradient = full-batch gradient
import numpy as np
rng = np.random.default_rng(0)
X = rng.normal(size=(64, 3)); y = rng.normal(size=64); w = np.ones(3)
def grad(Xb, yb): # gradient of mean squared error
return 2 * Xb.T @ (Xb @ w - yb) / len(yb)
full = grad(X, y)
k = 4; acc = np.zeros(3)
for Xm, ym in zip(np.split(X, k), np.split(y, k)):
acc += grad(Xm, ym) / k # like (loss / k).backward()
print(np.allclose(full, acc)) # True: then one optimizer step
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses