Skip to content

Add fully_shard_optimizer for mixed-precision FSDP - #5411

Merged
wujingyue merged 12 commits into
NVIDIA:mainfrom
wujingyue:fsdp/optimizer
Jul 21, 2026
Merged

Add fully_shard_optimizer for mixed-precision FSDP#5411
wujingyue merged 12 commits into
NVIDIA:mainfrom
wujingyue:fsdp/optimizer

Conversation

@wujingyue

@wujingyue wujingyue commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add fully_shard_optimizer(optimizer) and export it for the experimental fully_shard path.
  • Adapt optimizer steps in place with pre/post hooks that temporarily cast FSDP-managed grads while the optimizer consumes them, then restore the original grad tensors.
  • Reject optimizer step closures because the hook-only adapter cannot intercept grads materialized inside the closure.
  • Add focused MFSDP v2 optimizer coverage for mixed param/grad precision:
    • raw torch.optim.Adam fails on the mismatch without adaptation
    • TE FusedAdam accepts mismatched grads and updates parameters
    • fully_shard_optimizer covers Adam mixed-precision stepping through the adapter

Notes

  • DistributedOptimizer is intentionally not used for the MFSDP v2 test. With use_megatron_fsdp=True, it delegates to the wrapped optimizer for this path; with False, it expects the older _ParamAndGradBuffer setup rather than v2 FsdpParameterGroup/DBuffer state.
  • A follow-up can extend the adapter to sync main_weight to model_weight from a post-step hook. This PR keeps the current per-microbatch sync behavior.

Testing

  • BASE_REF=main CHECK_ONLY=true SKIP_DOCS=false bash tools/autoformat.sh
  • uv run --no-sync python -m torch.distributed.run --nproc-per-node 1 -m pytest -q tests/unit_tests/distributed/megatron_fsdp/test_optimizer.py --capture=fd --tb=short --disable-warnings -rN (2 passed)
  • uv run --no-sync python -m torch.distributed.run --nproc-per-node 2 -m pytest -q tests/unit_tests/distributed/megatron_fsdp/test_optimizer.py --capture=fd --tb=short --disable-warnings -rN (2 passed on each rank)
  • uv run --no-sync python -m torch.distributed.run --nproc-per-node 2 -m pytest -q tests/unit_tests/distributed/megatron_fsdp/test_experimental_fully_shard.py -k "adam or optimizer" --capture=fd --tb=short --disable-warnings -rN (2 passed, 8 deselected on each rank)

@copy-pr-bot

copy-pr-bot Bot commented Jun 21, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@copy-pr-bot

copy-pr-bot Bot commented Jun 21, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@wujingyue
wujingyue force-pushed the fsdp/optimizer branch 3 times, most recently from 7ff1865 to de0e0cf Compare June 21, 2026 22:56
@wujingyue
wujingyue marked this pull request as ready for review June 21, 2026 23:15
@wujingyue
wujingyue requested review from a team as code owners June 21, 2026 23:15
@wujingyue
wujingyue requested review from a team as code owners June 21, 2026 23:19
@wujingyue wujingyue changed the title Add experimental fully_shard_optimizer hook adapter Add fully_shard_optimizer for mixed-precision FSDP Jun 21, 2026
@wujingyue
wujingyue removed request for a team June 21, 2026 23:25
wujingyue added 11 commits July 9, 2026 19:41
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/29785947399

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/29787467984

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants