Pretraining is a long run of small steps
A language model is pretrained by showing it text and nudging every weight so that the next token becomes a little more likely. The loss is the mean next-token cross-entropy over a batch, , and an optimizer turns its gradient into a change of the weights, once per batch, for thousands of steps. The four optimizer tabs of this explainer differ only in that rule.
Every tab trains the same small GPT here: 4 pre-LayerNorm blocks of width 256 with 4 heads, a context of 256 tokens and an output head tied to the token embedding, 16,081,664 parameters in all. Each run starts from the same initial weights (seed 0) and sees the same 2,000 batches of 32 windows from TinyStories, short stories written in simple English, tokenized with GPT-2's tokenizer. Only the optimizer changes, so the loss curves at the bottom of the page compare like with like.
The stochastic gradient step
The plainest rule moves each weight against its gradient: with on batch . The batch gradient is a noisy estimate of the gradient over all the data, which is why this is stochastic gradient descent (SGD); Robbins and Monro showed in 1951 that such noisy steps still converge when the step size shrinks suitably.
The learning rate follows the schedule all four runs share: a linear warmup over the first 100 steps to the peak , then a cosine decay to a tenth of it,
The toolbar picks one of the recorded steps; the facts above the One SGD step panel give its and the loss on that step's batch before and after the update.
Momentum
Each batch gradient points a slightly different way. Momentum keeps a buffer of past gradients and steps along it instead:
with and here, PyTorch's convention. A gradient seen steps ago still counts with weight , so the buffer averages roughly the last gradients: directions that persist add up to about 10 times one gradient, and noise that flips sign cancels. Polyak introduced this "heavy ball" in 1964.
The One SGD step panel draws a 64 × 64 corner of one real weight matrix, block 2's attention value projection , at the chosen step: the batch gradient, the buffer (with last step's buffer drawn small above its operation), the step and the new weights. Click any cell to have its numbers worked out below the panel against the recorded ones. The same weight over every step follows eight of these weights through all 2,000 steps. Gradients that kept agreeing would build a buffer ten times the size of one of them; here they mostly cancel, and over the whole run the eight buffers have an RMS only 1.33 times their gradients'. The gradients also swing: over steps 500 to 1,000 each is anticorrelated with the gradient two steps later (correlation −0.60 on average), as when momentum carries a weight past the bottom of a valley and back.
One step size for every weight
SGD has no per-weight scale: a weight whose gradients are small takes small steps. In a transformer the gradient sizes of different tensors differ by orders of magnitude (the token embedding is touched only through the rows of tokens in the batch; LayerNorm gains see sums over many positions), so one learning rate is too large for some tensors and too small for others. The Every weight tensor panel shows the step each group of tensors took relative to its size, ‖Δθ‖ / ‖θ‖, for all four optimizers at the same step. SGD's bars spread over the widest range: at step 1,000 its groups' relative steps span a factor of 96 from the smallest (the LayerNorms) to the largest, against 20 for AdamW, 20 for Lion and 16 for Muon.
This is the usual explanation for why SGD trains transformers worse than Adam. Zhang and colleagues traced it to heavy-tailed gradient noise in attention models; Kunstner and colleagues found that the gap remains even with full-batch gradients and that sign descent, which ignores gradient sizes as Lion does, closes most of it. In this run SGD reaches a validation loss of 3.059 after 2,000 steps, against 1.908 for AdamW, 2.190 for Lion and 1.729 for Muon.
Choosing the learning rate
Every optimizer has its own scale of good learning rates, so each was swept: 500-step runs with the same warmup, data and seed at peak rates a factor of two apart, keeping the one with the lowest validation loss for the full run. The Learning-rate sweep chart shows the four sweeps together; SGD's best peak was 0.25, far above the others because its steps are not normalized. Short sweeps tend to favour slightly larger rates than a longer run would, so the chosen peaks are good rather than optimal.
Where it came from
Stochastic approximation (Robbins and Monro, 1951) and the heavy ball (Polyak, 1964) became the default way to train neural networks; Sutskever and colleagues showed in 2013 that momentum with a good initialization trains deep networks that had been thought to need second-order methods, and SGD with momentum trained the convolutional networks of the 2010s. Transformers moved to adaptive methods. The AdamW tab adds a per-weight scale, Lion keeps only the sign of a momentum, and Muon orthogonalizes the momentum of each matrix.
Papers
- A Stochastic Approximation Method Noisy gradient steps with a decaying step size converge: the origin of stochastic gradient descent.
- Some methods of speeding up the convergence of iteration methods The heavy-ball method, the momentum buffer this tab records.
- On the importance of initialization and momentum in deep learning SGD with momentum and a careful initialization trains deep and recurrent networks well; the momentum form PyTorch uses.
- Why are Adaptive Methods Good for Attention Models? Heavy-tailed gradient noise in attention models as a reason SGD trails Adam on transformers.
- Noise Is Not the Main Factor Behind the Gap Between SGD and Adam on Transformers, but Sign Descent Might Be The gap persists with full batches; sign-based updates close most of it, linking SGD, Adam and Lion.
- TinyStories: How Small Can Language Models Be and Still Speak Coherent English? The dataset all four optimizer runs train on: synthetic short stories in a small vocabulary.