The reduction is a choice, and it moves the learning rate
shape▲▲△The second assumption of this chapter is that the reduction is a plain mean over examples. Show that summing instead of averaging changes the effective learning rate, quantify it for two batch sizes, and identify a case where the choice varies within a training run.
Hint
The gradient of a sum is times the gradient of a mean.
Solution
Step 1 — the two reductions.
Step 2 — the gradients. Differentiation is linear, so the factor passes straight through:
Step 3 — the update. With learning rate :
Summing rather than averaging is exactly training at learning rate . Not approximately, and not “roughly like a bigger step” — identically.
Step 4 — quantify. At and , the effective rate under summation is ; at it is . The same code, the same , and an eightfold difference in effective rate purely from the batch size. A configuration tuned at diverges at , and the reduction is nowhere in the hyperparameter file.
Step 5 — the case that varies within a run. This is the part worth remembering, because it is invisible.
Consider token-level cross-entropy over variable-length sequences with padding, as in I.4.B07. Suppose the implementation sums the per-token losses and divides by the batch size rather than by the token count:
Then a batch of long sequences has more tokens contributing to the same denominator, so its gradient is larger. With :
A batch averaging tokens per sequence contributes about token losses over a denominator of — an effective per-token weight of .
A batch averaging tokens contributes losses over the same denominator — an effective weight of .
A tenfold swing in effective learning rate, batch to batch, driven entirely by the sequence lengths that happened to be sampled. And length-bucketing — grouping similar lengths together for throughput — makes it systematic rather than random: early buckets of short sequences train at one rate and later buckets of long ones at another.
Step 6 — the correct reduction, and the check. Divide by the number of valid tokens:
with the mask. Then the denominator tracks the numerator and the effective per-token weight is regardless of lengths or padding.
The check is one line: log the denominator. If it varies across steps by more than the padding fraction should allow, the reduction is wrong. Nothing else in the run will tell you.
Why this belongs with the losses rather than with the engineering. The reduction does not appear in the loss’s formula and is not part of its definition — which is exactly why it is assumed rather than stated, and why the assumption is worth writing down as this chapter does.