Repository navigation
[Bug] Preserve Adam first-step state during optimizer initialization - #4382
Merged
tianyu-l merged 1 commit intoAug 31, 2026
Merged
Conversation
taking-lying-flat
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
August 30, 2026 08:00
saforem2
added a commit
to saforem2/torchtitan
that referenced
this pull request
Sep 6, 2026
Surveyed, not merged -- two 80B captures are mid-flight and folding in 64 commits would make any new failure ambiguous. This records the analysis so the merge is cheap later. Diverged at 6ee6d5d (2026-08-30), upstream tip 966befc. ZERO upstream commits touch experiments/ezpz, so there are no conflicts in our own code. The risk is entirely in core files we import from. Of those, components/loss.py matters most: we import BaseLoss, CrossEntropyLoss, ChunkedLossWrapper and IGNORE_INDEX, all four still present, with one method renamed (typecheck_forward -> spmd_typecheck). zloss.py overrides only __init__ and __call__, so it does not inherit that rename -- checked rather than assumed, since a preserved name with a changed contract is the dangerous case, not a deleted one. Corrects my earlier risk ranking. I called pytorch#4328 (refactor max_context_length) the highest risk; that was wrong. max_context_length is UNCHANGED upstream -- same field, same default, plus a validation guard. The PR changes seq_len in model specs and registries, not the --training.max-context-length flag we pass. The single seam is agpt/config_registry.py:291, where our own agpt(seq_len=...) assigns cfg.training.max_context_length. The real risks, ordered: pytorch#4398 (valid-token counts move into collation -- that is the gradient-accumulation path), then the checkpoint cluster (pytorch#4187/pytorch#4188/pytorch#4197/pytorch#4270/pytorch#4474, which we subclass and where pinned clones have already broken on optim-statedict changes), then pytorch#4382 (Adam first-step state). Includes a merge order: smoke at 2N, then a resume from a real checkpoint, a seeded loss/grad_norm comparison, and config_registry.py:291 by inspection.
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
Why
init_optim_state()materializes lazy optimizer state by callingoptim.step()with zero gradients and a temporary learning rate of zero. This leaves parameters unchanged, but Adam and AdamW still advancestate["step"]from 0 to 1. Adam with coupled weight decay can also populate non-zero moments during that dummy step.TorchFT calls
init_optim_state()for every optimizer during construction, before any real training update. As a result, the first real Adam/AdamW update uses step-2 bias correction and diverges from an unwrapped optimizer from the first update. Callingstate_dict()before training has the same counter side effect.Resetting the newly materialized Adam state fixes the optimizer math without changing the generic materialization flow or state tensor layout.
Testing
python -m pytest tests/unit_tests/cpu/test_state_dict_keys.py tests/unit_tests/cpu/test_optimizer_param_groups.py -q(28 passed)torchtitan/components/optimizer/utils.py: 0 errors