Skip to content

[Fix] Avoid duplicate residual in LongCat MoE shortcut - #40799

Merged
ch-wan merged 1 commit into
mainfrom
cheng/hot-fix/longcat-shortcut-residual
Sep 23, 2026
Merged

ch-wan merged 1 commit into
mainfrom
cheng/hot-fix/longcat-shortcut-residual

Conversation

@ch-wan

@ch-wan ch-wan commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

This PR is part of a stack (oldest at bottom):

Motivation

With scattered MoE tokens, LongCat's final-layer postprocessing gathers moe_output + residual. The dense branch already owns that residual, so merging the shortcut adds it twice. A regression example that should produce 16 instead produces 21.

Modifications

Pass a zero residual to MoE shortcut postprocessing, leaving the residual with the dense branch. Add a regression through the actual decoder forward and communicator postprocessing, covering TP1/TP2, full/scattered tokens, terminal/nonterminal layers, and empty inputs.

Accuracy Tests

Stacking and comment cleanup preserve the executable AST of the tested revisions.

  • The regression fails on main and passes with this fix: 1 test, 16 subtests.
  • Two B200 GPUs: real NCCL all-gather through the final-layer path, BF16, decode and extend. Eager results match the mathematical reference; 4 rank-local CUDA graph captures and 12 replays with changing inputs match eager exactly.
  • Pre-commit checks pass, including CI registration validation.

The GPU probe uses synthetic Attention/MoE/dense outputs to isolate residual ownership. Full-checkpoint inference has not been run.

Speed Tests and Profiling

No performance benchmark. The fix replaces one residual clone with a same-sized zero allocation and adds no collective.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): 🚫 Run #35923572261
Latest PR Test (Extra): 🚫 Run #35923571941
Latest PR Test (AMD ROCm 10): ❌ Run #35923571921

@ch-wan ch-wan left a comment •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewed by agent.

Superseded by the review on 6057b34. The inline comments from this snapshot were removed after the head moved.

@ch-wan ch-wan left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewed by agent.

Summary

Passing a zero residual to MoE shortcut postprocessing is correct: the dense branch already owns that residual, and the shortcut folds the argument into its output only when gathering scattered tokens on the final layer. The new test locks that case. Fifteen of the sixteen subtests still pass if residual.clone() is restored, so they do not guard the double-add.

Issue counts by severity

  • bugs: 0
  • suggestions: 2
  • nits: 1


class TestLongcatShortcut(unittest.TestCase):
def test_shortcut_contributes_only_moe_output(self):
for tp in (1, 2):

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[suggestion] Only tp=2, scattered=True, terminal=True, rows=4 fails on the unfixed forward. CommunicateSummableTensorPairFn.get_fn adds the residual solely in _gather, which requires SCATTERED and TP_ATTN_FULL to have different group sizes. FULL is given the same size as TP_ATTN_FULL, and tp=1 collapses SCATTERED to that size too, so those cells hit _trivial and ignore the residual. target treats every non-scattered layer as terminal (terminal or not scattered), so that axis is duplicated. rows=0 is empty, and assert_close accepts any values of the same shape.

Suggestion: Keep the one cell that goes from 16 to 21 under residual.clone(). Drop the duplicated non-scattered pairs, and do not count the empty or _trivial cells as coverage of this bug.



class TestLongcatShortcut(unittest.TestCase):
def test_shortcut_contributes_only_moe_output(self):

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[suggestion] This is a bug regression, and the test no longer states the behavior that must not come back. The method name is the only hint.

Suggestion: Add a one-line docstring that the MoE shortcut must not carry the dense branch's residual.

register_cpu_ci(est_time=3, suite="base-a-test-cpu")


class TestLongcatShortcut(unittest.TestCase):

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[nit] New registered tests should subclass CustomTestCase, not unittest.TestCase. This case starts no server, so the gap is the CI retry wrapper, not a leaked process.

Suggestion: Use sglang.test.test_utils.CustomTestCase.

@ch-wan ch-wan changed the title [Fix] Avoid duplicate residual in LongCat MoE shortcut [Fix] LongCat MoE shortcut residual; Nemotron-H DP attention via the standard layer communicator Sep 23, 2026
@ch-wan
ch-wan force-pushed the cheng/hot-fix/longcat-shortcut-residual branch from 1a66b01 to 6057b34 Compare September 23, 2026 04:45
@ch-wan ch-wan changed the title [Fix] LongCat MoE shortcut residual; Nemotron-H DP attention via the standard layer communicator [Fix] Avoid duplicate residual in LongCat MoE shortcut Sep 23, 2026
@ch-wan
ch-wan marked this pull request as ready for review September 23, 2026 07:32
@ch-wan
ch-wan force-pushed the cheng/hot-fix/longcat-shortcut-residual branch from 6057b34 to f63f0a1 Compare September 23, 2026 09:32
@ch-wan
ch-wan force-pushed the cheng/hot-fix/longcat-shortcut-residual branch from f63f0a1 to d9a28a0 Compare September 23, 2026 21:37
@ch-wan
ch-wan merged commit 0e80c73 into main Sep 23, 2026
26 of 36 checks passed
@ch-wan
ch-wan deleted the cheng/hot-fix/longcat-shortcut-residual branch September 23, 2026 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant