[Main][FSDP] TE Partial CUDA Graph + Megatron-FSDP compatibility#4232
Draft
buptzyb wants to merge 1 commit intoNVIDIA:mainfrom
Draft
[Main][FSDP] TE Partial CUDA Graph + Megatron-FSDP compatibility#4232buptzyb wants to merge 1 commit intoNVIDIA:mainfrom
buptzyb wants to merge 1 commit intoNVIDIA:mainfrom
Conversation
Signed-off-by: Robin Zhang <robinz@nvidia.com>
5 tasks
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.
What does this PR do ?
Dev PR #4231 . Depend on TE PR NVIDIA/TransformerEngine#2831 .
Summary
Enables TransformerEngine's partial (layerwise) CUDA Graph to work correctly with
Megatron-FSDP. Prior to this PR, FSDP's forward hooks that wrap backward handlers
were passed directly to TE's
make_graphed_callables, which would capture them atthe wrong phase, causing numerical divergence and illegal memory access during
graph replay.
Changes
Core (
megatron/core/transformer/cuda_graphs.py)_extract_module_hooks): copies all 4 PyTorch hook dicts uniformly andclears them from the module.
_apply_fsdp_hook_transforms): detects FSDP-specific hook wrappers viasentinel attributes (
_cuda_graph_backward_handler,_cuda_graph_backward_pre_handler) and reroutes their inner backward handlers intothe correct TE-facing key, withholding the wrappers from TE entirely.
*_restorekeys are populated independently in Phase 1 and never touched in Phase 2,ensuring clean teardown after graph deletion.
FSDP (
megatron/core/distributed/fsdp/src/megatron_fsdp/megatron_fsdp.py)by
cuda_graphs.py.Argument validation (
megatron/training/arguments.py)full_iterationscope), require:--fsdp-double-buffer(prevents dynamic buffer addresses across iterations)--fsdp-db-use-persist-buf-on-alloc-fail(prevents failed allocations falling backto dynamic buffers during graph replay, which would cause illegal memory access)
Tests
Unit test (
tests/unit_tests/distributed/megatron_fsdp/)test_cudagraph_alignment_with_fsdp: verifies Δloss == 0 (bit-for-bit) betweeneager FSDP and FSDP+CUDA graph across:
attn,attn+moe_router+moe_preprocess,moe_routerutils.py: consolidated training defaults intobase_argsso callers only need topass overrides.
Contribution process
Pre-checks
Code review
Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.
Step 1: Mark PR as "Ready for Review"
.github/CODEOWNERS.Final Review might get declined if these requirements are not fulfilled.
Step 2: Final Review
For PRs that change
megatron/core, once all expert reviewers have approved, theFinal Reviewlabel is applied automatically and final reviewers are assigned.For PRs outside
megatron/core, this step is skipped.Step 3: Approved
Once all required reviewers have approved, the
Approvedlabel is applied automatically.Merge
Any member of mcore-engineers will be able to merge your PR.
For MRs into `dev` branch
The proposed review process for `dev` branch is under active discussion.MRs are mergable after one approval by either
eharper@nvidia.comorzijiey@nvidia.com.