Skip to content

[Bug] Preserve Adam first-step state during optimizer initialization - #4382

Merged
tianyu-l merged 1 commit into
pytorch:mainfrom
taking-lying-flat:fix/init-optim-state-step
Aug 31, 2026
Merged

tianyu-l merged 1 commit into
pytorch:mainfrom
taking-lying-flat:fix/init-optim-state-step

Conversation

@taking-lying-flat

Copy link
Copy Markdown
Contributor

Summary

  • Reset Adam and AdamW step counters and moments after the zero-gradient, zero-LR state-materialization step.
  • Preserve the allocated state tensors and their dtype/device while keeping the first real optimizer update at step 1.
  • Keep this change production-only, without committing test-file changes.

Why

init_optim_state() materializes lazy optimizer state by calling optim.step() with zero gradients and a temporary learning rate of zero. This leaves parameters unchanged, but Adam and AdamW still advance state["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. Calling state_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)
  • Changed-file lint, formatting, docs, spelling, link checks: passed
  • Pyrefly on torchtitan/components/optimizer/utils.py: 0 errors

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 30, 2026
@tianyu-l
tianyu-l requested a review from ivy-zhou August 30, 2026 21:01

@fegin fegin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix

@tianyu-l
tianyu-l merged commit 14438dc into pytorch:main Aug 31, 2026
17 of 18 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants