Transformer Training — Presentation 05

Learning Rate, Schedules and Batch Size

The single most important number in a run, and how it is found: a learning-rate sweep, warmup, cosine and warmup–stable–decay schedules, and the batch size, with the critical batch and the gradient noise scale measured on the model.

LR sweep Warmup Cosine WSD Critical batch Gradient noise scale Linear scaling
Sweep → Warmup → Peak → Decay
00

Topics We’ll Cover

Live companion and sources

This deck follows chapter 6, learning rate, batch size and schedules, where you can run the sweep yourself. Measured numbers: results.md §6a–6d, every setting three data seeds, mean ± standard deviation.

01

Why the Learning Rate Matters Most

The gradient gives a direction; the learning rate \(\eta\) decides the step. Too small and the model crawls; too large and the steps overshoot. In between is a band where it trains well.

Too small: 0.001

Ends 2,000 steps at 2.948, still on deck 01’s plateau.

In the band: 0.01

2.277 ± 0.090; 0.03 is indistinguishable (2.280 ± 0.107).

Too large: 1

Ends at 2.906; recomputed on another computer, one of its seeds blew up.

None of these runs diverged outright in the recorded results: gradient clipping and Adam’s normalised step usually keep such a run finite; it just learns little. Plain SGD and momentum have no such safety net (deck 07 blows one up with a single bad batch).

02

The Learning-Rate Sweep

Train the same model for the same number of steps at a ladder of learning rates a factor of about 3 apart, and keep the best. AdamW, warmup 50 then cosine to a tenth of the peak, 2,000 steps, clipping at 1.0, three seeds each (results.md §6a):

2 2.25 2.5 2.75 3 0.001 0.003 0.01 0.03 0.1 0.3 1 peak learning rate (log scale) final validation loss AdamW, mean ± sd over 3 seeds
Final validation loss against peak learning rate (results.md §6a). Best mean: 0.01.

Every optimiser and every method has its own best rate, so a fair comparison tunes the learning rate for each one separately: deck 04 compared optimisers each at the best of its own three rates, and deck 12 does the same for distributed-training methods, because a method that changes what the optimiser sees changes the rate it wants.

03

Warmup

$$\eta_t = \eta\,\frac{t+1}{W} \qquad (t < W)$$

Most runs start with a few hundred to a few thousand steps of linearly rising learning rate. Early on the gradients are large and change fast, and Adam’s running averages are built from only a few steps, so a full-size first step can throw the weights somewhere bad. Warmup was popularised for large-batch training by Goyal et al. (2017) and is essential for post-LN transformers (Xiong et al. 2020; deck 06).

Measured: this model doesn’t need it

This tiny pre-LN model at a peak of 0.01 trains fine without warmup: the run that starts at the full rate is at 2.382 at step 1,000 against 2.640 with 50 warmup steps, and ends at 2.159 ± 0.045 against 2.246 ± 0.025. Warmup is insurance that this model happens not to need.

04

Decay: Cosine and Warmup–Stable–Decay

At the end of a run a smaller learning rate lets the weights settle into a valley instead of bouncing across it. The cosine schedule (Loshchilov & Hutter 2016) decays smoothly from the start; LLaMA used it down to 10% of the peak (Touvron et al. 2023, §2.3). Its drawback: the run’s length must be fixed in advance. WSD (Hu et al. 2024, studied by Hägele et al. 2024) holds the peak and decays only at the end, so a run can be extended from any checkpoint of the stable phase.

$$\eta_t = \eta_{\min} + \tfrac12(\eta - \eta_{\min})\Big(1 + \cos \tfrac{\pi (t-W)}{T-W}\Big)$$
05

Four Schedules, Measured

AdamW at peak learning rate 0.01, 2,000 steps, three seeds, warmup 50 where used, WSD decaying over the last 20% to a tenth of the peak (results.md §6b):

ScheduleFinal validation loss (mean ± sd)Seeds 1, 2, 3
constant2.159 ± 0.0452.111, 2.165, 2.201
warmup + constant2.246 ± 0.0252.275, 2.238, 2.227
warmup + cosine2.277 ± 0.0902.299, 2.354, 2.178
WSD2.224 ± 0.0382.264, 2.220, 2.189

WSD is the same run as warmup + constant until step 1,600; then its decay pulls it below. The cosine run ends where it does because a 2,000-step run that is still improving at its end gains little from spending its last steps at a small learning rate. Decay pays off in long runs that are close to converged; this toy isn’t, and the spreads overlap.

06

Batch Size and the Critical Batch

Each step’s gradient is an average over the batch: a noisy estimate of the true gradient. A bigger batch gives a better estimate, so the same progress needs fewer steps, but each step costs more. McCandlish et al. (2018) model the trade-off: doubling the batch halves the steps well below a critical batch size, and barely helps above it.

$$S = S_{\min}\Big(1 + \frac{B_{\text{crit}}}{B}\Big)$$
300 500 1,000 2,000 3,000 4 8 16 32 64 batch (windows of 8 characters, log scale) steps to validation loss 2.6 measured, mean ± sdfit: S = S_min (1 + B_crit / B)
Each point at the better of two learning rates, three seeds (results.md §6c). Fit RMS error 0.077 in log steps.

Steps to a validation loss of 2.6 at five batch sizes, each at the better of two learning rates, three seeds (results.md §6c). From 4 to 64 windows the steps fall from 1,950 to 625, far less than the 16-fold the batch grew. The fit gives \(B_{\text{crit}}\) = 10.4 windows, \(S_{\min}\) = 592 steps.

07

The Gradient Noise Scale

McCandlish et al.’s quick estimate of the critical batch is the gradient noise scale: the gradient’s variance over its squared size (their eq. 2.9), the batch at which noise is as large as signal.

$$B_{\text{simple}} = \frac{\operatorname{tr}(\Sigma)}{|G|^2}$$
Steptr(Σ)|G|2Bsimple (windows)
01.6e+005.8e-012.8
2501.9e+003.1e-0262.8
5001.3e+001.0e-02126.4
1,0002.3e+002.8e-0279.5
1,5009.0e+005.6e-02161.4
2,0001.2e+012.6e-02455.7

Measured from 256 single-window gradients along the default run (results.md §6d). It agrees with the fitted 10 windows only to within about an order of magnitude, which is the precision McCandlish et al. claim for it. The trend is the useful part: as the loss falls, the gradient’s signal shrinks relative to its noise, so later in training a bigger batch is worth more, which is why some large runs grow their batch during training.

08

The Linear Scaling Rule

The linear scaling rule (Goyal et al. 2017): when you multiply the batch by \(k\), multiply the learning rate by \(k\) too, up to a limit, since \(k\) small steps in about the same direction add up to one \(k\)-times larger step.

Where it holds

SGD, below the critical batch, after warmup (Goyal et al. trained ImageNet with large batches this way, with a warmup).

Where it weakens

Adam’s normalised step: the step size no longer scales with the gradient. Large language-model runs therefore tune the batch size and learning rate together, and the measured best rate here changed between batch sizes (results.md §6c).

Batch size is also where parallelism meets optimisation: more data-parallel workers means a bigger batch per step (deck 10), and the critical batch says how far that helps.

09

What to Take Away