Unit 3 of 7 · about 4 hours · derive, implement
AdamW, schedules and the global token batch
Learning rate, batch size and schedule interact, and runs compare by tokens, not steps. Until you can state any run's token batch, tuning is guessing.
You'll be able to
- Derive one
AdamWstep from the two moments, the bias correction and the decoupled decay. - Implement
AdamWand the warmup-cosine schedule so both pass the CS336 public tests. - Derive the global token batch and step count from sequences, length, accumulation and parallelism.
Covers
Read
In this order, only the parts listed
0 of 4 done
- CS336 Assignment 1 handout:
AdamWand its accountingStanford CS336 Assignment 1 - training and decoding contractSection 4.3,
AdamW(pp. 31 to 32): Algorithm 1 with moments, bias-corrected step and decoupled decay, typical betas(0.9, 0.999)versus(0.9, 0.95), then theadamw_accountingdeliverables for memory, FLOPs and MFU time - CS336 Assignment 1 handout: cosine schedule with warmupStanford CS336 Assignment 1 - training and decoding contract
Section 4.4, Learning rate scheduling (p. 33): linear warmup, cosine annealing and post-annealing phases as a pure function of step
- LLMs from Scratch Appendix D: warmup, cosine and the modified loopLLMs from Scratch companion - training and generation
Sections D.1 (warmup over 0.1 percent to 20 percent of steps), D.2 (cosine decay) and D.4 (the modified training function combining both with gradient clipping)
- Modern LLM Notebook 10-training-loss: mini
Trainerand batch arithmeticModern LLM Notebook - configuration, loss and generationSection 6 (mini
Trainerskeleton with optimizer and scheduler step order) and Homework 2 (effective batch size from per-device batch, GPU count and accumulation steps)
Focus on
- Weight decay sits outside the gradient in
AdamW: parameters shrink by times directly, not through the moments. - The schedule is a pure function of , so resuming needs itself rather than a schedule object.
- Global tokens per optimizer step equal sequences times length times accumulation times data-parallel width.
- Appendix D sizes warmup as a fraction of total steps, typically 0.1 to 20 percent; many runs fix a step count instead. Either way, the warmup length belongs in the config.
Common mistakes · 3
- Adding weight decay to the gradients and calling it
AdamW. That isAdamwith L2, and the decay gets divided by the adaptive denominator. - Stepping the scheduler per microbatch under accumulation. The schedule then decays faster than intended because it sees more steps.
- Comparing two runs by step count at different batch sizes. Fix tokens first: batch times steps times length is the common currency.
Try it yourself
Closed-book, before checking anything
AdamW update from Algorithm 1, the learning rate at a step inside each of the three schedule phases, and the step count for a 327.68M-token budget.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.How does
AdamWapply weight decay differently fromAdamwith L2 regularization?AnswerAdamWsubtracts times times from the parameters directly. With L2 inAdam, the decay enters the gradient and is rescaled by the adaptive denominator, so large-gradient parameters get less decay.2.Per-device batch 2, 4 GPUs, 8 accumulation steps. What is the effective batch size?Answer
64 samples. accumulation groups form one optimizer step, matching the notebook Homework 2 assert.
3.Batch size 1024, context 256, budget 327.68M tokens. How many optimizer steps?Answer
1250. Each step consumes tokens, and .
4.Training resumes at step . What schedule state must the checkpoint hold?Answer
Only . The schedule is a closed-form function of , so restoring the exact step restores the exact rate. The optimizer moments are separate state and must be restored alongside.
Practice
- CS336 Assignment 1:
adamwProblem (
adamw) in Section 4.3; expose your optimizer throughget_adamw_clsintests/adapters.pyand passtest_adamwintests/test_optimizer.pyThe test trains a small linear model for 1000 steps and matches PyTorch
AdamWor the reference snapshot. Implement it in your own repository. - CS336 Assignment 1:
learning_rate_scheduleProblem (
learning_rate_schedule) in Section 4.4; expose your function throughget_lr_cosine_scheduleintests/adapters.pyand passtest_get_lr_cosine_scheduleintests/test_optimizer.py - Modern LLM Notebook 10-training-loss Homework 2: effective batch size
Homework 2: compute
effective_batch_sizefrom per-device batch, GPU count and accumulation steps; the cell asserts 64Derive the token batch for your own planned run the same way before trusting any step-based comparison.
You're done when
Your AdamW and schedule pass both optimizer tests including phase boundaries, and you can state any run's token batch and step count from its config.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 3 more in the section
- LLMs from Scratch companion - training and generationSebastian Raschka · primary
Use the ch05 training and generation code for the small pretraining loop. Add full checkpoint state, explicit loss masks and resume-equivalence tests that the base example does not supply.
- Stanford CS336 Assignment 1 - training and decoding contractStanford University · required
Use cross_entropy, adamw, adamw_accounting, learning_rate_schedule, gradient_clipping, data_loading, checkpointing, training_together and decoding specifications plus public tests. Debug on CPU and implement independently in your own repository.
- Modern LLM Notebook - configuration, loss and generationWalking Labs · support
Use English notebooks 09-model-config, 10-training-loss and 20-generation to visualize configuration ownership, shifted loss and decoding policies. Change edge cases and cross-check probability calculations against your own implementation.
Also in this section
- PyTorch saving, loading and reproducibility referencesPyTorch · support
Use the state-dict and reproducibility notes to enumerate all state needed for the supported resume guarantee. Test model, optimizer, schedule, scaler, data order and RNG state across an interruption.
- Hugging Face LLM Course - library translationHugging Face · optional
Only after the blank-file loop passes, map tokenization, model configuration, data collation, generation configuration and stopping behavior into Transformers APIs. Keep the from-scratch tests as the authority.
- PyTorch Automatic Mixed Precision recipePyTorch · support
Use the
autocast,GradScaler, unscale-before-clip, scaler checkpointing and inf-NaN troubleshooting sections to add mixed precision to the blank-file loop. Keep the CS336 clipping contract as the authority for the clipping rule.