Skip to content

[Bugfix] Fix top-1 MoE routing with non-unit scaling - #40187

Merged
BBuf merged 1 commit into
sgl-project:mainfrom
BBuf:bbuf/fix-single-expert-routing-20260918
Sep 18, 2026
Merged

BBuf merged 1 commit into
sgl-project:mainfrom
BBuf:bbuf/fix-single-expert-routing-20260918

Conversation

@BBuf

@BBuf BBuf commented Sep 18, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

TestFusedMOE.test_single_expert_routing intermittently fails on H100 for
topk=1 with routed_scaling_factor=1.5 (for example,
this scheduled CI job).

For top-1 routing, _use_intermediate was false regardless of the routed
scaling factor, so the down-projection kernel wrote directly to
out_hidden_states. The non-unit scaling path then reduced
intermediate_cache3, which had never been populated, overwriting the valid
output with uninitialized memory. This made the failure allocator-dependent.

Regression history

Bottom line: the regression that remained in main was reintroduced by #6970, authored by @BBuf. #6220, also authored by @BBuf, had first introduced the same mismatch, but it was fully reverted by #6968. #22660 only added the test coverage that eventually exposed the bug; it did not introduce the production issue.

  • #3988 introduced the
    top-1 direct-output fast path. This was valid at the time because the path
    did not perform post-GEMM routed scaling.
  • #6220 first added
    routed_scaling_factor to the reduction path without updating the
    down-projection output selection for top-1 routing. It introduced this exact
    mismatch, but was reverted by
    #6968.
  • #6970 reintroduced the
    routed-scaling implementation. This is where the bug that remained in the
    current codebase came from.
  • #9269 and
    #23019 subsequently
    refactored and moved the code while preserving the faulty condition.
  • #22660 later added
    test_single_expert_routing, including coverage for scaling factors 1.0
    and 1.5; it did not introduce the production bug. Because the invalid path
    reads uninitialized memory, the test failure was intermittent.

Modification

Keep the intermediate path enabled for non-unit routed scaling factors. The
down-projection kernel now populates intermediate_cache3 before the existing
scaled reduction. The unit-scaling top-1 fast path is unchanged.

The existing test_single_expert_routing coverage already exercises both
1.0 and 1.5 scaling factors.

Tests

  • Targeted H100 rerun: test_fused_moe.py passed, including test_single_expert_routing
  • python3 -m compileall -q python/sglang/srt/layers/moe/moe_runner/triton_utils/fused_moe.py
  • pre-commit run --files python/sglang/srt/layers/moe/moe_runner/triton_utils/fused_moe.py

Checklist

  • Format the code with pre-commit.
  • Reuse the existing regression test.
  • No documentation update is needed.

CI States

Latest PR Test (Base): ⏳ Run #35350658659
Latest PR Test (Extra): ❌ Run #35350780246
Latest PR Test (AMD ROCm 10): ❌ Run #35350658582

@BBuf

BBuf commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator Author

/tag-run-ci-label

@BBuf BBuf added the run-ci CI: run the baseline test suite on this PR label Sep 18, 2026
@BBuf

BBuf commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@BBuf

BBuf commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/moe/test_fused_moe.py

@github-actions

github-actions Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/moe/test_fused_moe.py:

🚀 1-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/moe/test_fused_moe.py

@BBuf
BBuf merged commit 7714b18 into sgl-project:main Sep 18, 2026
244 of 281 checks passed
@BBuf
BBuf deleted the bbuf/fix-single-expert-routing-20260918 branch September 18, 2026 14:55
guapisolo added a commit that referenced this pull request Sep 21, 2026
Keep the down-projection output in intermediate_cache3 when routed scaling
is non-unit. The scaled reduction otherwise reads an unwritten buffer
after the kernel has already written directly to the final output.

Backport the one-file upstream fix from #40187. Preserve
the existing test_single_expert_routing assertions and unit-scaling fast path.

(cherry picked from commit 7714b18)
Leoyzen pushed a commit to Leoyzen/sglang that referenced this pull request Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant