Skip to content

Support full-iteration CUDA graphs with MFSDP v2 - #7075

Merged
wujingyue merged 13 commits into
NVIDIA:mainfrom
wujingyue:mfsdp-v2-cuda-graph-probe
Sep 10, 2026
Merged

wujingyue merged 13 commits into
NVIDIA:mainfrom
wujingyue:mfsdp-v2-cuda-graph-probe

Conversation

@wujingyue

@wujingyue wujingyue commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Closes #6152.

Adds full-iteration CUDA-graph support for Megatron-FSDP v2.

  • Preserve MFSDP v2 optimizer gradient bindings across CUDA-graph replay.
  • Add an 8-GPU HybridModel functional recipe with bit-exact determinism coverage. The dense Llama-3-shaped proxy intentionally excludes MoE, whose data-dependent expert-dispatch buffers are not capture-safe.
  • Add a two-rank unit test that compares eager MFSDP v2 training with full-iteration and optimizer CUDA graphs enabled, including BF16 optimizer-state coverage.

@copy-pr-bot

copy-pr-bot Bot commented Sep 4, 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.

@wujingyue
wujingyue force-pushed the mfsdp-v2-cuda-graph-probe branch from 4429266 to 3684f27 Compare September 4, 2026 03:32
@wujingyue wujingyue changed the title [DRAFT] Probe MFSDP v2 full-iteration CUDA graph support Support full-iteration CUDA graphs in Megatron-FSDP v2 Sep 4, 2026
@wujingyue
wujingyue marked this pull request as ready for review September 4, 2026 03:32
@wujingyue
wujingyue requested review from a team as code owners September 4, 2026 03:32
Comment thread megatron/core/optimizer/fully_sharded_optimizer.py
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
@wujingyue wujingyue changed the title Fix silently-zero gradients with MFSDP v2 and CUDA graphs Support full-iteration CUDA graphs with Megatron-FSDP v2 Sep 7, 2026
The test only exercised --cuda-graph-impl full_iteration, which captures
the forward and backward but not the optimizer step. --optimizer-cuda-graph
captures the step itself, so the two cover disjoint parts of the iteration
and the optimizer half went untested at scale.

Enabling it required dropping --log-num-zeros-in-grad. MegatronOptimizer.step
calls count_zeros() when that is set, and count_zeros() ends in a .item()
host sync that is illegal inside the captured step
(cudaErrorStreamCaptureUnsupported). That is not specific to MFSDP v2: the
shared count_zeros_fp32() syncs the same way for every optimizer, and nothing
in validate_args rejects the combination.

Golden values are regenerated because capturable=True routes FusedAdam
through multi_tensor_adam_capturable instead of multi_tensor_adam, which
shifts the loss slightly: lm loss at step 50 moves 7.00104 -> 7.00118, a
max relative deviation of 4.25e-4 over 50 steps with no compounding. Memory
metrics move by 2.5-3.5 KiB against a 4 GiB peak. A fresh run reproduces the
new values under the exact-match check, so the captured optimizer step is
still reproducible run-to-run.

Also drops the num-zeros series from the golden file. With
log_num_zeros_in_grad off the optimizer reports a literal 0, so the recorded
series was 50 zeros that measured nothing. It was never in this test's
METRICS.

Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>

assert fully_shard_context_calls == [True]

@pytest.mark.parametrize("optimizer_cuda_graph", [False, True], ids=["eager", "cuda_graph"])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

optimizer_cuda_graph is covered by the unit test added.

@Phlip79 Phlip79 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about GB200 functional test?

@copy-pr-bot

copy-pr-bot Bot commented Sep 8, 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 changed the title Support full-iteration CUDA graphs with Megatron-FSDP v2 Support full-iteration CUDA graphs with MFSDP v2 Sep 8, 2026
@wujingyue

Copy link
Copy Markdown
Contributor Author

What about GB200 functional test?

I don't think we need it before #5615

Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
@wujingyue
wujingyue force-pushed the mfsdp-v2-cuda-graph-probe branch from 0590b47 to 3183151 Compare September 9, 2026 19:36
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
@nemo-automation-bot

Copy link
Copy Markdown

🔄 Merge queue validation started!

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

This branch was previously deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Verify MFSDP v2 works with MCore's GraphableMegatronModule

10 participants