Skip to content

Fix TE grouped MLP fused main-grad setup#5209

Merged
yaox12 merged 1 commit into
NVIDIA:mainfrom
Wong4j:codex/fsdp-te-fuser-main-grad
Jul 23, 2026
Merged

Fix TE grouped MLP fused main-grad setup#5209
yaox12 merged 1 commit into
NVIDIA:mainfrom
Wong4j:codex/fsdp-te-fuser-main-grad

Conversation

@Wong4j

@Wong4j Wong4j commented Jun 8, 2026

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do ?

This PR exposes Megatron-FSDP main-grad buffers to the TE grouped MLP op-fuser path before fused execution.

Issue tracking

Linked issue: N/A. This is a small compatibility bug fix for Megatron-FSDP with the TE grouped MLP op-fuser path.

Changes

  • Initialize param.main_grad from param.get_main_grad() for grouped MLP fused parameters when needed.
  • Keep overwrite_main_grad enabled for the fused path.
  • Add focused unit coverage for the hook behavior and real TE construction path.

Validation

  • python3 -m compileall -q megatron/core/transformer/moe/experts.py tests/unit_tests/transformer/moe/test_grouped_mlp.py
  • On a GB200 node:
    • targeted main-grad / TE GroupedMLP unit tests passed
    • 1-node / 4-GPU mock-data MoE + TE op-fuser + Megatron-FSDP e2e training reached iter 3/3
    • skipped iterations: 0; NaN iterations: 0; Slurm exit: 0:0
  • CHECK_ONLY=true tools/autoformat.sh passed for this PR branch

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

@copy-pr-bot

copy-pr-bot Bot commented Jun 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.

@Wong4j
Wong4j force-pushed the codex/fsdp-te-fuser-main-grad branch from 242c0d2 to 20fc008 Compare June 9, 2026 08:22
@Wong4j
Wong4j marked this pull request as ready for review June 9, 2026 08:23
@Wong4j
Wong4j requested review from a team as code owners June 9, 2026 08:23
@Wong4j
Wong4j force-pushed the codex/fsdp-te-fuser-main-grad branch from 20fc008 to d53396b Compare June 13, 2026 05:06
@Wong4j
Wong4j force-pushed the codex/fsdp-te-fuser-main-grad branch 2 times, most recently from 5b5ec96 to 05236b6 Compare July 1, 2026 09:44
@Phlip79

Phlip79 commented Jul 1, 2026

Copy link
Copy Markdown
Member

/ok to test 05236b6

@yaox12

yaox12 commented Jul 2, 2026

Copy link
Copy Markdown
Member

/claude strict-review

@claude claude Bot 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.

Strict review passed — no significant issues found. LGTM

Traced the full data/dtype flow for this fix:

Correctness (verified): The change fills a genuine gap. Megatron-FSDP's _bucket_forward sets overwrite_main_grad=True but does not pre-populate param.main_grad; the non-fused wgrad paths (tensor_parallel/layers.py:586, core/utils.py:1195) set it lazily inside wgrad_compute, but the TE op-fuser path has no equivalent hook, so param.main_grad would be None when TE's fused wgrad accumulation writes into it. Exposing it in the pre-forward hook mirrors the reference FSDP pattern (megatron_fsdp.py:652,670).

Backward compatibility (verified safe): get_main_grad/overwrite_main_grad exist only on FSDP-managed params, so the getattr(..., None) is not None and hasattr(...) guards leave the DDP + fuse_wgrad_accumulation path completely untouched — no default or behavior change for existing users.

Details that are correct:

  • is None identity check (not truthiness) avoids the ambiguous-tensor-bool error.
  • Under gradient accumulation, main_grad is reset to None only at grad-reduce time (reset_param_main_grad), so the guard is a no-op on later microbatches and the same bucket is reused — consistent.
  • parameters(recurse=False) is correct: TEGroupedLinear registers its weights/biases directly on the module.
  • No new direct parallel_state.get_*_group() reads; all new identifiers have live use paths.

The unit test cleanly validates the guard logic (uses pytest.fail to assert get_main_grad is skipped when main_grad is already set, and that non-fuse_wgrad params are left alone).

Risk level: low.

@kvareddy

kvareddy commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

@fanshiqing can you please take a look at these changes?

@kvareddy
kvareddy requested a review from fanshiqing July 6, 2026 09:54
@Wong4j
Wong4j force-pushed the codex/fsdp-te-fuser-main-grad branch from 05236b6 to d000dad Compare July 13, 2026 07:08
@Wong4j

Wong4j commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

@aklife97 @yashaswikarnati Could you please help review these changes?

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Final Review PR is in the "final review" stage label Jul 17, 2026
@ericharper

Copy link
Copy Markdown
Contributor

/ok to test d000dad

@Wong4j

Wong4j commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

@kvareddy @lmcafee-nvidia Could you please help review these changes?

@svcnvidia-nemo-ci svcnvidia-nemo-ci added Approved All necessary approvals have been made and removed Final Review PR is in the "final review" stage labels Jul 21, 2026
auto-merge was automatically disabled July 22, 2026 02:57

Head branch was pushed to by a user without write access

@Wong4j
Wong4j force-pushed the codex/fsdp-te-fuser-main-grad branch from d000dad to 0ac5c6f Compare July 22, 2026 02:57
Signed-off-by: Shijie Wang <jaywan@nvidia.com>
@Wong4j
Wong4j force-pushed the codex/fsdp-te-fuser-main-grad branch from 0ac5c6f to fba3816 Compare July 23, 2026 02:30
@yaox12
yaox12 enabled auto-merge July 23, 2026 02:41
@yaox12

yaox12 commented Jul 23, 2026

Copy link
Copy Markdown
Member

/ok to test fba3816

@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/29988911330

@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/30002442211

Merged via the queue into NVIDIA:main with commit c5398de Jul 23, 2026
89 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants