7 routes42 sections324 units

Unit 2.8 · about 5 h

184 / 324

Checkpoints, configuration and reproducibility

An experiment you cannot resume is an anecdote. Checkpoints plus config plus seeds turn a run into evidence you can re-enter.

  • implement
  • measure

You can now

  • Save and restore model, optimizer, scheduler, epoch and RNG state, and state the resume tolerance you actually tested.
  • Record hyperparameters, data position and generator state so a resumed run continues the same stream, not a similar one.
  • Draw the reproducibility boundary: what matches bitwise, what matches statistically, and what never matches across CPU, GPU and workers.

Covers

  • Checkpointing model, optimizer, scheduler and scaler state
  • Configuration, data position and random generator state
  • Reproducibility claims across CPU, GPU and data workers

Study, in this order

3 steps

  1. Saving and Loading Models: `state_dict`, checkpoints, devices
    PyTorch - Autograd mechanics, serialization and reproducibility

    What is a state_dict (model and optimizer dicts); Saving and Loading Model for Inference (Save and Load state_dict, model.eval); Saving and Loading a General Checkpoint for Inference and/or Resuming Training (epoch, model and optimizer state, loss); Saving and Loading Model Across Devices (map_location, DataParallel module prefix)

    Depth · Skip Saving an Exported Program, Saving Multiple Models in One File and Warmstarting. The deprecated general-checkpoint recipe redirects to this page.

  2. Automatic Mixed Precision recipe: saving and resuming the GradScaler
    PyTorch Automatic Mixed Precision recipe - Saving/Resuming

    Saving/Resuming: the scaler state_dict saved beside model and optimizer, when to save it, and resuming across runs with and without AMP

  3. Reproducibility: randomness control and nondeterminism limits
    PyTorch - Autograd mechanics, serialization and reproducibility

    Reproducibility intro (no cross-release, cross-platform or CPU-versus-GPU guarantee); Controlling sources of randomness (manual_seed, Python and NumPy seeds, cudnn benchmark); Avoiding nondeterministic algorithms (use_deterministic_algorithms, uninitialized memory); DataLoader (worker_init_fn, generator)

    Depth · Read the section intros and the DataLoader part fully. Skim the per-op catalog (convolution, SDPA backends, RNN); the boundary table is the assignment, not every kernel.

Focus on

  • A resume checkpoint holds model, optimizer and scheduler state, the GradScaler state when you train with AMP, the epoch and step, and the RNG states. Model weights alone restart the model but not the training.
  • Config means every number that changes the stream: hyperparameters, data order and position, transform versions, worker count and seeds. Log them with the checkpoint.
  • Identical seeds do not promise identical results across releases, platforms, or CPU versus GPU. Promise bitwise match only inside one tested configuration.
  • Deterministic flags trade speed for repeatability and can throw on ops without a deterministic path. cudnn benchmark selection is itself a randomness source.

Reproduce closed-book

Interrupt a small training run mid-schedule, restart the process, reload the full checkpoint, and show the next-epoch loss matches an uninterrupted run within your stated tolerance. Then list what your checkpoint deliberately excludes.

Traps

  • Saving model.state_dict() and calling it a training checkpoint. Without optimizer, scheduler, epoch and RNG state, resume restarts the optimization, not the run.
  • Pickling the whole model and loading it with the default weights_only=True. Since PyTorch 2.6 that default refuses arbitrary pickled objects, and weights_only=False runs pickle, which can execute code. Save state_dicts, and load untrusted files only with weights_only=True.
  • Claiming GPU determinism from a CPU test. Nondeterministic kernels, benchmark selection and worker seeding each break the claim independently.

Practice

Learn PyTorch 01 task 5: save, reload and verify predictions

Exercises, task 5: save state_dict, reload into a new instance, verify predictions match

Extend it: add optimizer state, epoch and torch, Python and NumPy RNG states, then run the interrupted-versus-uninterrupted resume comparison from Reproduce.

Learn PyTorch 05: scripted training with argparse config (tasks 1 to 3)

Exercises, tasks 1 to 3: get_data.py with existing-data check, train.py with argparse for data dirs, lr, batch size, epochs and hidden units, predict.py that loads the saved model

Treat the argparse flags plus seeds as the config record. A run whose flags are not logged does not count as reproduced.

Self-test

  1. The resumed weights give the same validation loss as the uninterrupted run at the checkpoint, yet the training curves split within a few updates. Name three state items that could be missing.

  2. Same seed, same code, CPU run matches but GPU run differs in the last decimals. Is anything wrong?

  3. use_deterministic_algorithms(True) throws on your model. What does that tell you, and what is the cost of obeying it?

  4. A DataParallel checkpoint loads with missing-key errors on a single-GPU model. What happened?

Completion criterion

Stop when

An interrupted and an uninterrupted run agree within your written tolerance, and your config log plus boundary statement say exactly what would break that agreement.

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

Leads to

Comes after

Resources for this unit

Also in this section

  • PyTorch - Learn the Basics
    PyTorchprimarycourse
    Complete Tensors, Datasets and DataLoaders, Transforms, Build Model, Autograd, Optimization, and Save and Load Model. Rebuild each essential example in a small src package instead of retaining a tutorial notebook.
  • Learn PyTorch - selected framework exercises
    Daniel Bourkerequiredpractice
    Complete exercise 00, tasks 1-5 in 01, tasks 1-7 in 02, tasks 5-13 in 03, tasks 1-3 in 05, and exercises 1-2 in 07. Recreate solutions independently and verify behavior against current official docs.
  • Deep Learning with PyTorch, 2nd edition - framework foundations
    Howard Huang, Luca Antiga, Eli Stevens and Thomas Viehmann (Manning)supportimplementation
    Read Sections 3.8-3.9 and 5.5 when tensor storage, strides or autograd need a second explanation, with the Chapter 3 and 5 companion notebooks for runnable cells. Inspect the Chapter 4 and 7 code only for a diagnosed data-representation or loading gap.
  • NeetCode ML - PyTorch closing drills
    NeetCodeoptionalpractice
    After the module gate, solve Pytorch Basics, Layer Normalization, Batch Normalization and RMS Normalization cold. Explain shapes, state and train-evaluation behavior aloud. These do not replace the package and resume test.
  • Hands-On Machine Learning with Scikit-Learn and PyTorch
    Aurelien Geron (O'Reilly, 2025)supporttheory
    Read Chapter 10, Implementing Mini-Batch Gradient Descent Using DataLoaders and Model Evaluation, for the train and evaluate loops and how batch metrics must be aggregated.

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