Save Enough State to Repeat or Resume the Run

Separate inference reload, a comparable new run, and exact training continuation. Save model, optimizer, step, configuration, vocabulary, data identity, environment, and random-generator state accordingly.

Saving trained weights is enough to reproduce inference under the same model definition. Continuing training exactly requires more state because AdamW and the batch sampler remember the past.

Separate Three Reproducibility Goals

GoalRequired state
reproduce current logitsmodel definition, configuration, vocabulary, model weights, evaluation mode
start a comparable new runcode and environment, configuration, corpus identity, split and model seeds
continue the interrupted runall above plus optimizer state, step, scheduler if any, and random-generator states

The frozen checkpoint record contains:

Every entry has a reason. AdamW stores moving averages and a step count. The batch generator determines the next examples. Vocabulary order determines what embedding row and logit column each ID means.

Test Loading, Not Merely Saving

A successful file write proves little. Construct a new model and optimizer, load both state dictionaries, restore the generator, and verify:

  1. logits before the next update match exactly in the same environment;
  2. the next batch indices match;
  3. the next loss and gradients match;
  4. the next updated parameters match.

The verified CPU experiment passes this one-step continuation test.

Seeds Limit Variation; They Do Not Guarantee Portability

PyTorch warns that complete reproducibility is not guaranteed across releases, platforms, or CPU and GPU execution. Record at least Python version, PyTorch version, device, operating system, and relevant package versions with the run.

For this CPU toy model, separate generators make model initialization, corpus sampling, and batch order independently repeatable. Larger data pipelines may also require worker and library seeds.

Q1. Choose state for exact continuation

You saved only model.state_dict() after step 500. Can you guarantee the same step 501 that the uninterrupted AdamW run would have taken?

Choose one

Select one choice, then check.

Hint
AdamW stores moments, and sampling has its own state.
Solution
No. Restore optimizer state, step and any scheduler state, and the random-generator states that determine the next batch, in addition to model weights and configuration.
Not attempted
Review

Not marked done.

References

These are current framework instructions and should be rechecked when the project changes its supported PyTorch release.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.

LLM PrimerSave Enough State to Repeat or Resume the Runhttps://llmprimer.com/transformers/training-a-tiny-transformer/save-enough-state-to-repeat-or-resume-the-run© 2026 LLM Primer