7 routes42 sections324 units

Unit 3.6 · about 5 h

190 / 324

Schedules, gradient accumulation and clipping

Schedules, batch size and clipping jointly set the update scale. Each one silently changes what the learning rate means, so they must be reasoned about as one mechanism.

  • implement
  • measure

You can now

  • Implement linear warmup followed by cosine decay with the scheduler stepped in the right order.
  • Implement gradient accumulation that matches a full-batch gradient up to floating-point order, and state the effective batch size.
  • Apply global-norm clipping after unscaling in mixed precision, and run an LR range test to set bounds.

Covers

  • Warmup, cosine schedules and schedule order
  • Gradient accumulation and effective batch size
  • Gradient clipping by global norm
  • Learning-rate range tests

Study, in this order

6 steps

  1. Schedules and warmup
    D2L - optimization and regularization slice

    Schedulers and Policies: factor, multi-factor, cosine with formula, and warmup

  2. Gradient clipping by global norm
    D2L - optimization and regularization slice

    Gradient Clipping: Lipschitz bound, the \(\min(1, \theta/\|g\|)\) projection, and its limits

    Depth · Read the Gradient Clipping section only. Skip the RNN implementation around it.

  3. Clip API semantics
    PyTorch - optimization and automatic mixed precision examples

    clip_grad_norm_ page: global-norm definition over concatenated per-parameter norms, in-place edit, returned total norm, error_if_nonfinite

  4. Scaler order with clipping
    PyTorch - optimization and automatic mixed precision examples

    Adding GradScaler and Inspecting/modifying gradients: autocast forward, scale, backward, unscale, clip, step, update, zero_grad

  5. LR range test
    Cyclical Learning Rates for Training Neural Networks (Smith, 2017)

    Abstract and range-test description: linearly increasing the rate for a few epochs to read reasonable bounds

  6. HOML: PyTorch schedulers and gradient clipping
    Hands-On Machine Learning with Scikit-Learn and PyTorch

    Chapter 11, Gradient Clipping (printed pp. 382-383) and Learning Rate Scheduling from Cosine Annealing through 1cycle Scheduling (pp. 400-405)

    Depth · The warm-up loop on p. 402 calls warmup_scheduler.step() at the start of each epoch. On PyTorch 2.14 that trains at 40, 70 and 100 percent of the rate and never at the 10 percent the text promises; step at the end of each epoch to get 10, 40, 70, 100.

Focus on

  • Cosine decay sets \(\eta_t\) from a closed form with fixed \(T\). Stepping the scheduler per step versus per epoch changes the curve, so record which one the code does.
  • Accumulation adds \(K\) microbatch gradients and steps once. Scaling each microbatch's mean loss by \(1/K\) reproduces the full-batch mean gradient when the microbatches are equal in size and nothing updates between them. Batch-norm statistics and dropout masks still differ, and the summation order changes, so agreement is to floating-point tolerance.
  • Scaled gradients must be unscaled before clipping. Clipping scaled values applies the threshold in the wrong units; the recipe unscales first so max_norm keeps its unscaled meaning.
  • The range test raises the rate linearly and reads bounds off the loss: flat start, steady fall, then divergence. It sets a search interval, not an optimum.

Reproduce closed-book

Closed-book, write the cosine formula and evaluate it at halfway: \(\eta_0\) 0.3, \(\eta_T\) 0.01, \(T\) 20, \(t\) 10 gives 0.155. Write the clipping rescale \(\min(1, \theta/\|g\|)\) and apply it to per-param norms 3 and 4 with \(\theta\) 1: total 5, scale 0.2. Write the accumulation average that reproduces a batch-32 mean from four microbatches of 8.

Traps

  • Calling scheduler.step() before optimizer.step(). PyTorch warns because the first scheduled rate is skipped; assert the rate used on step one. Forgetting zero_grad at the start of each accumulation cycle silently adds the previous cycle's gradients.
  • Claiming accumulation equals large-batch training. Batch-norm running statistics update per microbatch and dropout draws differ, so the match is statistical, not bitwise.
  • Treating warmup as a universal fix. The Tuning Playbook shows instabilities that need clipping or architecture change; warmup only shortens the risky early phase.

Practice

TrenTorch: Gradient Accumulation

Implement compute_output_head_gradient, accumulate_gradients and train_with_gradient_accumulation; the tests compare the accumulated update with one full-batch step

Average the microbatch gradients, hold the weights fixed until every microbatch is done, then step once. Then check your PyTorch loop, which divides each microbatch loss by \(K\), against a full-batch reference within floating-point tolerance.

TrenTorch: Warmup and Cosine Decay

Implement linear_warmup_lr, cosine_decay_lr and warmup_cosine_lr, with the decay step counted from the end of warmup

Check the closed form first: base 0.3, minimum 0.01, 20 decay steps, step 10 gives 0.155.

D2L Learning-Rate Scheduling: Exercises

Exercises 1 and 4 (best fixed-rate model, warmup length); run the cosine comparison of exercise 3 on Fashion-MNIST instead of ImageNet

Self-test

  1. Cosine schedule \(\eta_0\) 0.3, \(\eta_T\) 0.01, \(T\) 20. What is \(\eta\) at \(t\) 10 and \(t\) 20?

  2. Four microbatches of 8 with mean-reduced losses give microbatch gradients \(a\), \(b\), \(c\) and \(d\). What update matches batch-32 SGD, and when does it fail to match?

  3. Where does clipping go in a GradScaler step, and what breaks if it moves before unscaling?

  4. Range-test loss falls slowly, then steeply, then explodes. How do you read it?

Completion criterion

Stop when

Your accumulated run matches the full-batch gradient within floating-point tolerance, your clipped AMP order passes the unscale check, and your range test names defensible bounds.

Mark passed, then open 3.7 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

Also in this section

  • Understanding Deep Learning - fitting, gradients and regularization
    Simon J. D. Princeprimarytheory
    Use Chapters 6-9 and targeted parts of Chapter 20 for optimization, initialization, measurement, regularization and why training works. Re-derive the update rules you keep in the trainer.
  • Deep-ML candidate - optimization and normalization problems
    Deep-MLoptionalpractice
    After the platform admission check, use at most one of 146 Momentum, 169 AdamW, 197 Global-Norm Clipping, 109 LayerNorm or 115 BatchNorm as a changed-prompt drill.
  • TrenTorch - training mechanics questions
    TrenTorchsupportpractice
    Solve only the questions the units name: Weight Initialization, Layer Normalization, Label Smoothing, Gradient Accumulation, and Warmup and Cosine Decay. Read the tests first; they run in the browser IDE.
  • PyTorch GroupNorm reference
    PyTorchreferencereference
    Read how input channels are split into num_groups groups and which axes each group normalizes; compare num_groups 1 against num_channels.
  • PyTorch RMSNorm reference
    PyTorchreferencereference
    Read the RMS formula and which axes normalized_shape covers; note the missing mean centering and bias.
  • PyTorch torch.optim.AdamW reference
    PyTorchreferencereference
    Read the AdamW algorithm box for the decoupled weight-decay step, the bias-corrected moments and the defaults, and follow the Decoupled Weight Decay Regularization link for the argument.
  • PyTorch CrossEntropyLoss reference
    PyTorchreferencereference
    Read Shape, Parameters (weight, ignore_index, reduction, label_smoothing) and both Examples; extend the class-index example with ignore_index and a class weight.
  • Deep Learning Tuning Playbook FAQ: debugging optimization failures
    Google Researchreferencereference
    Read How can optimization failures be debugged and mitigated (identifying unstable workloads, warmup, clipping) and Batch normalization implementation details.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels