Unit 5 of 7 · about 4 hours · implement, measure

Checkpoints that really resume

A weights-only checkpoint restarts training but not the run: learning rate, moments and data order all jump. Equivalence tests turn resume from a hope into a measured fact.

You'll be able to

  • Save and load complete training state: model, optimizer, schedule step, scaler, RNG and data order.
  • Bind tokenizer, data and configuration identity to each checkpoint so mismatches fail loudly.
  • Prove interrupted training matches uninterrupted training to a stated equivalence level.

Covers

Model, optimizer, scheduler, scaler and RNG checkpoint stateTokenizer, data and configuration identity bindingInterrupted versus uninterrupted resume equivalence
1

Read

In this order, only the parts listed

0 of 5 done

  1. CS336 Assignment 1 handout: checkpointing and the training loopStanford CS336 Assignment 1 - training and decoding contract

    Section 5.2, Checkpointing (p. 35): model and optimizer state_dict plus iteration through torch.save and torch.load; then Section 5.3, Training loop (p. 36): configurable hyperparameters, memmap datasets, checkpoint paths and step plus wall-clock logging

  2. LLMs from Scratch ch05: saving and loading weightsLLMs from Scratch companion - training and generation

    Section 5.4 (torch.save on state_dict, load_state_dict into a fresh model, and why adaptive optimizers need their state saved too)

  3. PyTorch saving and loading: general checkpoint plus RNG controlPyTorch saving, loading and reproducibility references

    Saving and Loading a General Checkpoint for Inference and/or Resuming Training (Save and Load: model plus optimizer state dicts with the epoch or step)

  4. PyTorch reproducibility notes: RNG and DataLoader statePyTorch saving, loading and reproducibility references

    Controlling sources of randomness (torch, Python and other libraries) and DataLoader (generator seeds for worker shuffling), plus Avoiding nondeterministic algorithms

    Read for the checklist of what counts as state; skip CUDA kernel backend options until GPU work needs them.

  5. Modern LLM Notebook 09-model-config: repository and config identityModern LLM Notebook - configuration, loss and generation

    Repository File Map (config, tokenizer_config and generation_config roles) and the tokenizer_config section (special tokens, max length, truncation and padding sides)

2

Focus on

  • The iteration number restores the schedule exactly, the optimizer state restores the moments, and the scaler state restores the loss scale.
  • RNG seeds and data-loader order are training state. A fresh shuffle on resume silently starts a different run.
  • Equivalence needs a stated level: exact loss match for a few steps on fixed hardware, then tolerance bands beyond that.
  • Tokenizer and config versions pin the meaning of every saved tensor. A changed vocabulary or width redefines all IDs and shapes.
Common mistakes · 3
  • Saving weights only. The resumed run restarts the schedule and moments, so loss jumps even though the model is identical.
  • Recreating the DataLoader without its generator state. Batches arrive in a new order and the run diverges from the uninterrupted reference.
  • Loading weights into an edited config. Shapes may still match while token IDs or widths mean something new, and nothing raises an error.
3

Try it yourself

Closed-book, before checking anything

Write save and load for model, optimizer, iteration, scaler and RNG seeds, then an interrupted-versus-uninterrupted equivalence test over fixed steps.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.The checkpoint holds model, optimizer and iteration. Why is the iteration number needed?Answer

    It restores the schedule position. Without it the learning rate restarts at the wrong phase, for example back at the warmup peak, and the run diverges from the original.

  2. 2.Your resume test matches the uninterrupted run exactly for five steps on CPU, but on a GPU the losses drift apart by about 1e-6 per step. Is resume broken?Answer

    Not necessarily. Nondeterministic kernels and reduction order give tiny float differences that compound, so state the equivalence level: exact on CPU with deterministic algorithms, a tolerance band on GPU. A sudden jump at the resume step, by contrast, means missing state.

  3. 3.Resumed loss diverges from the uninterrupted run within a few steps. Name two suspects.Answer

    Unrestored RNG or DataLoader order, which feeds different batches, and missing scaler or optimizer moment state, which changes the update itself.

  4. 4.Why pin tokenizer_config and config versions alongside the checkpoint?Answer

    Token IDs and tensor shapes are config-relative. A changed vocabulary or width silently redefines what every saved number means, so the binding must fail loudly on mismatch.

5

Practice

  • CS336 Assignment 1: checkpointing

    Problem (checkpointing) in Section 5.2; connect save and load to run_save_checkpoint and run_load_checkpoint in tests/adapters.py and pass test_checkpointing in tests/test_serialization.py

    The test trains 10 AdamW steps, round-trips through a file and compares model and optimizer states exactly. Then extend it in your own repository with scaler, RNG and DataLoader order plus a resume-equivalence check.

You're done when

Your checkpoint round-trips through test_checkpointing, and your own resume test matches the uninterrupted loss to your stated tolerance.

Only when you can do this without notes.

Leads to

Notes

Resources · 4 for this unit, 2 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.

  • 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.

Also in this section

  • 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.