16.4 Learning-rate schedules (cosine, WSD, Noam) and gradient clipping
With Adam and AdamW: bias correction and decoupled weight decay, the optimizer takes steps of about per coordinate, but it takes them with the same from the first step to the last. That fails at both ends of a run. At step 1, Adam’s second moment has seen one gradient, so its normalization is noisy, and a full-size step from random initial weights can push a Transformer into a region it never recovers from: the loss jumps and stays high. At the end, a constant rate keeps the weights bouncing around the minimum at a distance proportional to , and the loss stops falling. The fix is a schedule: start small, run at the peak, end small.
Concepts from other sections (10)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Second moment (,
exp_avg_sq): the EMA of the elementwise squared gradient. Read more: 16.3 Adam and AdamW: bias correction and decoupled weight decay - Optimizer protocol: the four methods
step,zero_grad,state_dict, andload_state_dictthat every optimizer exposes. Read more: 16.2 The Optimizer protocol: SGD, momentum, Nesterov, weight decay - Pure function: a function whose output depends only on its inputs and which mutates nothing. Read more: Chapter 4: Coding and Design Patterns in Software Engineering
- Descent direction: a direction with . Read more: 16.1 Convexity, smoothness, gradient descent, and Armijo line search
- fp16: a 16-bit format with 5 exponent bits and 10 mantissa bits; finer precision but a maximum of 65504 and a smallest subnormal of . Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing in AI Engineering
- Decoupled weight decay (AdamW): multiplying the weights by outside the gradient path. Read more: 16.3 Adam and AdamW: bias correction and decoupled weight decay
- backpropagation through time (BPTT): backpropagation over an RNN unrolled into one layer per timestep. Read more: Chapter 3: Neural Architectures and Their History in AI Engineering
- Stochastic gradient: a minibatch estimate of the full-loss gradient, unbiased for the average. Read more: 16.2 The Optimizer protocol: SGD, momentum, Nesterov, weight decay
- Scaling laws: empirical power laws relating loss to compute, data, and parameter count. Read more: Chapter 2: Deep Learning in AI Engineering
- loss scale: a power of two that multiplies the loss so that small fp16 gradients stay representable; divided out before the update. Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing in AI Engineering
A second failure is a single bad batch: one gradient a thousand times larger than usual, common in recurrent networks such as those of RNN language model with stateful TBPTT, which a normalized optimizer still follows for a step and momentum for many more. Gradient clipping bounds it. The train loop of Training loop and the autograd bigram in AI Engineering calls a schedule before every step and clips before every update, and the training spec of Capstone: TinyStories, owned end to end names both (schedule.name = "wsd", grad_clip = 1.0).
This section builds three schedules (cosine with warmup, warmup-stable-decay, and the Noam schedule of the 2017 Transformer) and global-norm clipping. It needs the geometric and arithmetic sequences of Sequences, geometric series, and frequency ladders and the optimizer protocol of The Optimizer protocol: SGD, momentum, Nesterov, weight decay, whose lr a schedule sets. The reference solution is course/solutions/python/tinyllm/optim/schedule.py, with cosine_with_warmup, wsd, noam, and clip_grad_norm_; the contract is course/contracts/py/tinyllm/optim/schedule.pyi, and the training spec’s schedule and grad_clip fields are defined in formats/train-spec.schema.json.
Learning objectives
- Explain why a schedule should be a pure function of the step, and fix the step convention that makes phase boundaries exact.
- Evaluate cosine-with-warmup, WSD, and Noam schedules by hand, including their values at every phase boundary.
- Derive where the Noam schedule peaks and how it decays.
- Implement global-norm gradient clipping that preserves the update direction, only shrinks, and leaves overflowed gradients for the caller to skip.
Notation
Section titled “Notation”The section uses these symbols, with their names in code:
| Symbol | Meaning | Type / shape |
|---|---|---|
| the step: the number of optimizer updates already taken, 0 for the first | int, step | |
| the learning rate used for update number | float | |
| the peak rate and the floor | float, lr_max, lr_min | |
| warmup length in steps | int, warmup | |
| the step at which the cosine reaches the floor | int, total | |
| WSD’s stable and decay lengths | int, stable, decay | |
| progress through a phase, in | float | |
| the Transformer’s model width (Noam) | int | |
| the gradient of parameter tensor | ndarray, p.grad | |
| the global norm, | float | |
| the clipping limit | float, max_norm |
A schedule is a function of the step
Section titled “A schedule is a function of the step”The train loop does opt.lr = schedule(t, ...) and then opt.step(), for Because the rate depends only on and the configuration, a run resumed from a checkpoint at step 5000 recomputes exactly the rate it would have used: no scheduler object has to be saved. The step is the number of updates already taken, the convention of HF’s LambdaLR schedulers, so the values equal HF’s when read the same way.
Every warmup ramps linearly from 0: for . Update 1 therefore runs at rate 0 and changes nothing except the optimizer’s moments, which is harmless and keeps the formulas exact at the phase boundaries.
Cosine decay with warmup
Section titled “Cosine decay with warmup”After warmup, the most common schedule follows half a period of a cosine from down to :
and for . At , gives ; at , gives ; at the rate is halfway. The curve is flat at both ends: its derivative in is , which is zero at and . So the peak lasts a while and the final steps change the weights very little. The weakness is : it must be fixed before training starts, and stopping early leaves the rate high.
Warmup, stable, decay
Section titled “Warmup, stable, decay”WSD (warmup-stable-decay, used by MiniCPM and many recent models) removes that weakness by splitting the run into three phases: the linear warmup over steps, a stable phase at for steps, and a short decay over the last steps, here a straight line:
and from on. The stable phase can be extended at will, and a decay can branch off any stable checkpoint, so one long run yields models at many lengths. Most of the loss improvement of a WSD run arrives during the short decay, which you can see in the capstone’s loss curve. HF’s get_wsd_schedule with a floor starts its warmup at the floor instead of 0; this one starts at 0 like the cosine, so the two agree from the end of warmup on.
The Noam schedule
Section titled “The Noam schedule”The 2017 Transformer used a schedule with no fixed end at all:
For the second term is smaller (because exactly when ), so the rate grows linearly; for it decays like . The two meet at , where , the peak. At the formula reads , and that is the value to return: Python raises ZeroDivisionError on 0 ** -0.5. The factor ties the peak to the width, so wider models get smaller rates. Encoder-decoder Transformer: post-LN and pre-LN, Noam, label smoothing trains with , : a peak of .
Clipping by the global norm
Section titled “Clipping by the global norm”Schedules control the step size on average; clipping controls it on the worst batch. Treat all gradients of the model as one long vector and take its Euclidean length, the global norm . If it exceeds the limit , scale every gradient by the same factor:
One factor for all tensors keeps the direction of the full gradient and only shortens it, so the clipped step is still a descent direction; clipping each tensor separately would change the direction. The is torch’s guard against dividing by a zero norm. Clipping never scales up: below the limit, the gradients are untouched. The function returns the norm before clipping, which the train loop logs, because a norm that keeps hitting the limit is the first sign of an unstable run.
If the norm is infinite or NaN (an overflow, common under fp16 in Mixed precision, loss scaling, gradient accumulation, and activation checkpointing), clipping cannot repair it, so the gradients are left alone and the caller skips the update. With Adam, clipping matters less for the step size, since Adam normalizes per coordinate, but it still bounds what one outlier batch writes into and .
Working it by hand
Section titled “Working it by hand”Take a cosine schedule with , , , . Its values over the first eight steps are:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|---|
| phase | warmup | warmup | after | |||||
| 0 | 0.5 | 1 | 0.55 | 0.1 | 0.1 |
For WSD with , , , , : steps 0 and 1 ramp (0, 0.5); steps 2 and 3 are stable (1, 1); the decay runs from step 4 (: 1) through steps 5, 6, 7 (: 0.8, 0.6, 0.4); step 8 and after are at the floor, 0.2.
For Noam with , , you have and . Then ; ; ; , the peak ; and , half the peak at four times the step.
For clipping, take two tensors with gradients and . Then . With the factor is , so the gradients become about and , and the call returns 13.
Designing the interface
Section titled “Designing the interface”The four functions have plain signatures:
def cosine_with_warmup(step: int, warmup: int, total: int, lr_max: float, lr_min: float) -> floatdef wsd(step: int, warmup: int, stable: int, decay: int, lr_max: float, lr_min: float) -> floatdef noam(step: int, d_model: int, warmup: int) -> floatdef clip_grad_norm_(params, max_norm: float) -> float # in place; returns the norm before clippingparams is any iterable of objects with a grad attribute (an ndarray or None), the same parameters AdamW steps. The schedules raise ValueError for a negative step, a warmup longer than total, a zero decay length, or lr_min outside ; clip_grad_norm_ raises it for max_norm <= 0. The trailing underscore follows torch: the function changes its argument in place.
The tests pin these functions down from several sides. Each hand-worked case above must come out exactly: the cosine row for steps 0 to 7, the WSD values for steps 0 to 9, the Noam values, and the clip of norm 13 to 6.5. Golden comparisons check the cosine against HF’s cosine schedules (with and without a floor and warmup) at every step, WSD against HF’s linear WSD including steps past the end, and clipping against torch.nn.utils.clip_grad_norm_ on four gradient sets, including None and float32, so runs are comparable with HF- and torch-trained baselines. A property check over random configurations demands that each schedule peaks exactly at the end of warmup, never exceeds the peak, never rises after it, never drops below the floor, and sits at the floor from the end on; that one check catches every phase-boundary off-by-one. The cosine must stay at the floor past total, for runs that train a little longer than planned. Noam must peak at at , keep constant after it, rise linearly before it, and return exactly 0 at step 0 with no exception.
For clipping, small gradients below the limit must be unchanged bit for bit. Clipping must be global and in place: one factor for all tensors, a norm after clipping equal to max_norm, and the same array objects, so the direction is kept and the optimizer sees the change. An inf gradient must return inf and change nothing. grad = None must be skipped and stay None, and no grads at all gives 0. Impossible configurations raise ValueError, so the error comes at the first step, not as a rising “decay”.
Building it step by step
Section titled “Building it step by step”The design rests on seven decisions.
- Pure functions of the step. A resumed run must get the same rate from the step count alone, so each schedule is a function of
stepand its configuration, with no hidden state to checkpoint. - Fix the step convention, then the boundaries follow.
stepis the number of updates already taken. Warmup islr_max * step / warmupforstep < warmup, so step 0 gives 0 and stepwarmupis the first step of the next phase, at exactlylr_max. - Phases as a chain of half-open intervals. Testing
step < warmup, thenstep < warmup + stable, thenstep < warmup + stable + decay, each<, assigns every step to exactly one phase, and the value at each boundary is the start of the next phase. Everything past the end is the floor, so overrunning the plan never raises the rate. - Progress is measured within the phase. Cosine progress is and WSD’s is , each running from 0 to 1 across its own phase only.
- Noam needs its own step 0. at is mathematically, but Python cannot evaluate
0 ** -0.5; return 0 explicitly. - Clip one vector, not many. The global norm is the length of all gradients concatenated; computing it in float64 avoids overflow and rounding for large models, and one scale factor for every tensor preserves the direction.
- Only shrink, and never repair an overflow. Scale only when the factor is below 1; if the norm is not finite, return it unchanged so the caller can skip the step.
The cosine schedule shows the pattern every schedule follows: validate, then walk the phases in order.
def _check_lrs(lr_max: float, lr_min: float) -> None: if not 0.0 <= lr_min <= lr_max: # a floor above the peak would "decay" upward raise ValueError(...)
def cosine_with_warmup(step, warmup, total, lr_max, lr_min) -> float: if step < 0 or warmup < 0 or total < 1 or warmup > total: raise ValueError(...) _check_lrs(lr_max, lr_min) if step < warmup: return lr_max * step / warmup # 0 at step 0; never divides by 0 here if step >= total: return lr_min # past the plan: stay at the floor progress = (step - warmup) / (total - warmup) # 0 at the end of warmup, < 1 before total return lr_min + (lr_max - lr_min) * 0.5 * (1.0 + math.cos(math.pi * progress))step < warmup is only reached when warmup > 0, so the division is safe, and total - warmup > 0 whenever the cosine branch runs (step < total and step >= warmup). Trace the hand example with , : step 1 gives ; step 3 has progress and gives ; step 6 hits step >= total and returns 0.1.
WSD adds one phase, and Noam needs only its special case:
def wsd(step, warmup, stable, decay, lr_max, lr_min) -> float: if step < 0 or warmup < 0 or stable < 0 or decay < 1: raise ValueError(...) _check_lrs(lr_max, lr_min) if step < warmup: return lr_max * step / warmup if step < warmup + stable: return lr_max # the stable phase starts after warmup if step < warmup + stable + decay: progress = (step - warmup - stable) / decay # 0 at the first decay step return lr_min + (lr_max - lr_min) * (1.0 - progress) # = lr_max - (lr_max - lr_min) p return lr_min
def noam(step, d_model, warmup) -> float: if step < 0 or d_model < 1 or warmup < 1: raise ValueError(...) if step == 0: return 0.0 # min(inf, 0); Python raises on 0 ** -0.5 return d_model**-0.5 * min(step**-0.5, step * warmup**-1.5)For WSD with , , : steps 2 and 3 return the peak, step 4 starts the decay at progress 0 (still 1), steps 5 to 7 give 0.8, 0.6, 0.4, and step 8 falls through to the floor. decay >= 1 is required because a zero-length decay would jump from peak to floor and the progress would divide by 0. For Noam with , : step 4 gives , with both branches of the min equal, which is why the peak is exactly at .
Clipping is the only function with side effects:
CLIP_EPS = 1e-6 # torch's guard against dividing by a zero norm
def clip_grad_norm_(params, max_norm: float) -> float: if not max_norm > 0.0: raise ValueError(f"max_norm must be > 0, got {max_norm}") grads = [p.grad for p in params if p.grad is not None] # skip unused parameters if not grads: return 0.0 # one norm over every tensor, accumulated in float64 total = math.sqrt(sum(float(np.sum(np.square(g, dtype=np.float64))) for g in grads)) if not math.isfinite(total): return total # inf or nan: leave grads, let the caller skip coef = max_norm / (total + CLIP_EPS) if coef < 1.0: # only ever shrink for g in grads: g *= coef # in place: the optimizer reads these arrays return total # the norm before clipping, for loggingTrace the hand example: gradients and give ; coef , so both arrays are scaled by about and the function returns 13. g *= coef keeps each array’s identity and dtype (a float32 gradient stays float32), which is what makes the change visible to the optimizer. np.square(g, dtype=np.float64) squares in float64, so a float32 gradient with entries near does not overflow to inf before the sum.
The full solution is in schedule.py.
Why this design
Section titled “Why this design”Each schedule is per step. Clipping is one pass over all gradients to compute the norm and, when it clips, one more to scale; memory is beyond a float64 square per tensor.
Functions beat scheduler objects for this purpose. torch’s lr_scheduler classes hold state that must be saved and restored with the checkpoint. A pure function of step needs only the step count, which the optimizer state already stores. The cost is that composing phases is done with if chains rather than by stacking scheduler objects.
HF’s step convention, reading the rate for update from updates already taken, matches HF’s LambdaLR schedules value for value, so baselines trained with HF are comparable. The price is one update at rate 0 at the very start, which changes nothing but Adam’s moments.
Global clipping beats per-tensor clipping. Per-tensor clipping is simpler to fuse but changes the update direction; global clipping keeps the direction and only shortens the step, so the clipped update is still a descent direction.
Skipping beats repairing non-finite norms. Clipping an infinite norm gives a factor of 0 and silently zeroes the update; returning the infinite norm lets the loop (or the grad scaler of mixed-precision training) detect the overflow and skip the step.
What can go wrong
Section titled “What can go wrong”Schedule bugs are almost all off-by-one errors at phase boundaries; clipping bugs change the direction or hide overflows. The common ones:
| Pitfall | Symptom | How the solution avoids it |
|---|---|---|
decaying to 0 and ignoring lr_min | the last part of the run does nothing; WSD and cosine runs end at different floors | both decays are written lr_min + (lr_max - lr_min) * ... |
measuring cosine progress over total instead of total - warmup | the floor is never reached; the rate at total is above lr_min | progress = (step - warmup) / (total - warmup) |
warmup from step + 1 | the rate reaches the peak one step early and every value differs from HF | lr_max * step / warmup |
letting the cosine run past total | the rate climbs back toward lr_max after the planned end | if step >= total: return lr_min |
| counting WSD’s stable phase from step 0 | the decay starts warmup steps early | the boundaries are warmup + stable and warmup + stable + decay |
| evaluating Noam’s formula at step 0 | ZeroDivisionError on the first update | if step == 0: return 0.0 |
| clipping each tensor by its own norm | the update direction changes; small layers are never clipped while large ones always are | one total over all gradients and one coef |
scaling small gradients up to max_norm | every step has the same length; early training diverges | scales only if coef < 1.0 |
| clipping an infinite norm | max_norm / inf = 0 zeroes the update and the overflow goes unnoticed | returns early if not math.isfinite(total) |
Where this leads
Section titled “Where this leads”These functions finish the optimizer toolkit that the rest of the series trains with. In AI Engineering, train_step(..., clip=...) in Training loop and the autograd bigram clips, then the loop sets opt.lr from the spec’s schedule and steps; AdamW reads that opt.lr on every step, and its decay scales with it. Clipping keeps truncated backpropagation through time stable in RNN language model with stateful TBPTT. The Noam schedule drives Encoder-decoder Transformer: post-LN and pre-LN, Noam, label smoothing, GPT: the decoder-only language model sets its learning rate from these schedules, and the TinyStories capstone uses WSD with decay_frac and grad_clip = 1.0 from formats/train-spec.schema.json. Mixed precision, loss scaling, gradient accumulation, and activation checkpointing clips before every update and skips steps whose norm is not finite.
Further reading
Section titled “Further reading”- Loshchilov and Hutter, “SGDR: Stochastic Gradient Descent with Warm Restarts” (2017); Vaswani et al., “Attention Is All You Need” (2017), section 5.3; Hägele et al., “Scaling Laws and Compute-Optimal Training Beyond Fixed Training Durations” (2024); Pascanu, Mikolov, and Bengio, “On the difficulty of training recurrent neural networks” (2013), section 3.2.
torch.optim.lr_scheduler(LambdaLR,SequentialLR,CosineAnnealingWarmRestarts;torch/optim/lr_scheduler.py) provides stateful schedulers that compose phases and save their own state.- HF
get_wsd_schedule(transformers/optimization.py) adds cosine and 1-sqrt decay shapes and other warmup shapes. - Schedule-free AdamW averages the iterates instead of decaying the rate, so no
totalis needed: Defazio et al., “The Road Less Scheduled” (2024). - torch’s
foreachnorms and FSDP’sclip_grad_norm_(torch/nn/utils/clip_grad.py;torch/distributed/fsdp) use one fused kernel per dtype and reduce the norm across data-parallel shards. - AMP’s
GradScaler(torch/amp/grad_scaler.py) lowers the loss scale and skips the step when gradients overflow.
Key terms
Section titled “Key terms”- Learning-rate schedule: a function from the step number to the learning rate.
- Warmup: a linear ramp from 0 to the peak rate over the first steps.
- Cosine decay: half a cosine period from to over a fixed horizon .
- Warmup-stable-decay (WSD): warmup, a long constant phase at the peak, and a short final decay.
- Noam schedule: , linear rise then inverse-square-root decay.
- Global norm: the Euclidean length of all gradients concatenated into one vector.
- Gradient clipping: rescaling all gradients by one factor when their global norm exceeds a limit.
Summary
Section titled “Summary”- A schedule is a pure function from the step number to a learning rate, so a resumed run gets the same rate from the step count alone.
- Warmup ramps the rate linearly from 0 while Adam’s second moment is still unreliable; cosine decay then lowers it smoothly to a floor at a fixed
total. - Warmup-stable-decay holds the peak rate and decays only at the end, so one run can be stopped and annealed at any length.
- The Noam schedule rises linearly and decays like , peaking at .
- Global-norm clipping rescales all gradients by one factor when their joint norm exceeds a limit: it caps the step size, keeps the direction, and leaves non-finite norms for the caller to skip.
Review questions
Section titled “Review questions”-
Why does warmup return
lr_max * step / warmuprather thanlr_max * (step + 1) / warmup?Answer
stepcounts the updates already taken, so the peak should arrive exactly atstep == warmup, the first step of the next phase. Withstep + 1the ramp reaches the peak one step early, and every value of the run is shifted by one relative to HF’s schedules. -
Why is cosine progress divided by
total - warmupand nottotal?Answer
The cosine phase runs from step
warmupto steptotal, a length oftotal - warmup. Dividing bytotalmakes progress reach only(total - warmup)/total < 1at the end, so the rate attotalis still above the floor and then jumps down tolr_min. -
Why does
noamspecial-case step 0?Answer
The formula at is , but Python evaluates
0 ** -0.5by raisingZeroDivisionError. Returning 0 explicitly gives the mathematically correct value for the first update without the exception. -
Why must clipping use one factor for all tensors?
Answer
Scaling every tensor by the same factor multiplies the full gradient vector by a positive scalar, which keeps its direction and only shortens it. Clipping each tensor by its own norm rescales the components differently, so the combined update points in a different direction; small layers would never be clipped while large ones always would.
-
What would happen if
clip_grad_norm_clipped when the norm is infinite?Answer
max_norm / infis 0, so every gradient would be multiplied by 0: the update silently does nothing (or, with a NaN norm, every gradient becomes NaN). Returning the non-finite norm without touching the gradients lets the caller see the overflow and skip the step.