Skip to content

[dev] fix(moe): fix the bug where gate was not sliced when kv_head < tp_size. - #3529

Merged
yanring merged 4 commits into
NVIDIA:devfrom
LiuXTao:itao/fix_output_gate
Feb 27, 2026
Merged

[dev] fix(moe): fix the bug where gate was not sliced when kv_head < tp_size.#3529
yanring merged 4 commits into
NVIDIA:devfrom
LiuXTao:itao/fix_output_gate

Conversation

@LiuXTao

@LiuXTao LiuXTao commented Feb 21, 2026

Copy link
Copy Markdown

What does this PR do ?

This PR mainly fixes the bug where the gate lacked indexing handling when kv_head < tp_size. The original code only indexed the query, causing an error when output_gate is True.

[rank6]: from user code:
[rank6]:    File "/home/Megatron-LM/megatron/core/transformer/attention.py", line 1226, in _apply_output_gate
[rank6]:     gate = gate.view(*x.shape)
[rank6]: torch._dynamo.exc.TorchRuntimeError: Dynamo failed to run FX node with fake tensors: call_method view(*(FakeTensor(..., device='cuda:6', size=(1128, 1, 16, 256), dtype=torch.bfloat16), 1128, 1, 2048), **{}): got RuntimeError("shape '[1128, 1, 2048]' is invalid for input of size 4620288")

This error refers to the shape mismatch between gate and core_attn_out. Therefore, I added some code to index the gate, similar to query.

PR for main: #3575

Contribution process

flowchart LR
    A[Pre-checks] --> B[PR Tests]
    subgraph Code Review/Approval
        C1[Expert Review] --> C2[Final Review]
    end
    B --> C1
    C2 --> D[Merge]
Loading

Pre-checks

  • I want this PR in a versioned release and have added the appropriate Milestone (e.g., Core 0.8)
  • 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

Code review

The following process is enforced via the CODEOWNERS file for changes into megatron/core. For changes outside of megatron/core, it is up to the PR author whether or not to tag the Final Reviewer team.

For MRs into `main` branch

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!

(Step 1): Add PR label Expert Review

(Step 2): Collect the expert reviewers reviews

  1. Attach the Expert Review label when your PR is ready for review.
  2. GitHub auto-assigns expert reviewers based on your changes. They will get notified and pick up your PR soon.

⚠️ Only proceed to the next step once all reviewers have approved, merge-conflict are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

(Step 3): Final Review

  1. Add Final Review label
  2. GitHub auto-assigns final reviewers based on your changes. They will get notified and pick up your PR soon.

(Optional Step 4): Cherry-pick into release branch

If this PR also needs to be merged into core_r* release branches, after this PR has been merged, select Cherry-pick to open a new PR into the release branch.

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.com or zijiey@nvidia.com.

Merging your PR

Any member of core-adlr and core-nemo will be able to merge your PR.

@LiuXTao
LiuXTao requested review from a team as code owners February 21, 2026 04:20
@copy-pr-bot

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

@yuzhongw-nvidia yuzhongw-nvidia 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.

LGTM. Thanks.

@yuzhongw-nvidia

Copy link
Copy Markdown
Contributor

/ok to test 6c02944

@janEbert

Copy link
Copy Markdown
Contributor

Hey, would you be so kind to add a test for this? Would be super useful to catch potential future regressions early!

@yuzhongw-nvidia

Copy link
Copy Markdown
Contributor

Hey, would you be so kind to add a test for this? Would be super useful to catch potential future regressions early!

Thanks for your advice! I have added UTs to capture the fixed error.

@yuzhongw-nvidia

Copy link
Copy Markdown
Contributor

/ok to test cf94d9f

@yuzhongw-nvidia yuzhongw-nvidia changed the title fixed the bug where gate was not sliced when kv_head < tp_size. [dev] fix(moe): fix the bug where gate was not sliced when kv_head < tp_size. Feb 24, 2026
@yanring

yanring commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

/ok to test b848275

@yanring yanring 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.

LGTM

@yanring
yanring enabled auto-merge February 24, 2026 10:06
@janEbert janEbert added Final Review PR is in the "final review" stage complexity: low labels Feb 24, 2026
@yanring

yanring commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

@yuzhongw-nvidia could you help replicate a main PR?

@yuzhongw-nvidia

Copy link
Copy Markdown
Contributor

@yuzhongw-nvidia could you help replicate a main PR?

Yes. I create one (#3575), and it is ready for review as well.

@yuzhongw-nvidia

Copy link
Copy Markdown
Contributor

/ok to test ca7d3b5

@yuzhongw-nvidia
yuzhongw-nvidia requested review from a team and removed request for a team February 26, 2026 05:45
@yanring
yanring added this pull request to the merge queue Feb 27, 2026
@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/22476908512

Merged via the queue into NVIDIA:dev with commit 2e4a5d4 Feb 27, 2026
108 of 113 checks passed
zpltys pushed a commit to zpltys/Megatron-LM that referenced this pull request Apr 8, 2026
…tp_size. (NVIDIA#3529)

Co-authored-by: xiaotaoliu <xiaotaoliu@tencent.com>
Co-authored-by: Yuzhong Wang <yuzhongw@nvidia.com>
Co-authored-by: Zijie Yan <zijiey@nvidia.com>
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.

6 participants