Fix optimizer momentum reset on checkpoint resume#115
Open
amazloumi wants to merge 1 commit into
Open
Conversation
Codecov Report❌ Patch coverage is
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CheckpointManagerround-tripped optimizer state through rawoptimizer.state_dict()/optimizer.load_state_dict(). On resume the optimizer is freshly constructed, so itsstate_dict()has noexp_avg/exp_avg_sqtensors yet (AdamW creates per-parameter state lazily on the first.step()).dcp.loadtherefore had no moment tensors to fill, the saved moments were silently dropped, and AdamW momentum reset to zero at every resume. Model weights, scheduler, dataloader position, and RNG all restored correctly — only the optimizer moments were lost, so resumed runs were not bit-exact.DCP's
get_model_state_dict/get_optimizer_state_dict/set_model_state_dict/set_optimizer_state_dict. The getters build a load template with the moment tensors allocated in the correct FSDP/DTensor layout, sodcp.loadrepopulates them; the setters write them back into the live optimizer. Theexclude_keysfine-tuning path (load model, skip optimizer) is preserved.tests/integration/test_checkpoint_roundtrip.py::TestCheckpointRoundtrip::test_manager_restores_optimizer_moments_single_gputests/distributed/test_checkpoint.py::TestCheckpointRoundTrip::test_resume_restores_optimizer_momentstests/e2e/test_training_e2e.py::test_resume_determinism_single_gputests/e2e/test_training_e2e.py::test_resume_determinism_2gpu_fsdp### Fixedentry toCHANGELOG.md; correcteddocs/checkpointing/dcp-model.md, which documented the old raw-state_dict()save/load and explained the empty-template behavior as if it were correct.Testing
uv run ruff check kempnerforge/ tests/passesuv run ruff format --check kempnerforge/ tests/ scripts/passesuv run pyright kempnerforge/passes (0 errors)uv run pytest tests/unit/ -v --timeout=60passes (unit suite unaffected)uv run torchrun --nproc_per_node=4 -m pytest tests/distributed/test_checkpoint.py -v— new moment test passes; existing 8 checkpoint tests still passuv run pytest tests/e2e/test_training_e2e.py -k resume_determinism --e2e -v— single-GPU and 2-GPU FSDP resume bit-exactmain(pre-fix), pass on this branchCloses #114