Skip to content

[Perf] [NVIDIA] moe: Re-land the JIT moe_finalize_fuse_shared with the shared row read after the PDL wait - #48174

Merged
BBuf merged 2 commits into
sgl-project:mainfrom
DarkSharpness:moe-finalize-reland
Nov 6, 2026
Merged

BBuf merged 2 commits into
sgl-project:mainfrom
DarkSharpness:moe-finalize-reland

Conversation

@DarkSharpness

@DarkSharpness DarkSharpness commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

(auto generated by Claude)
(auto generated by MyGO (Make your GPU-kernel Optimal) agent)

What

Re-lands #47784 (the JIT moe_finalize_fuse_shared, reverted by #47994) with one fix: the shared-expert row is now read after PDLWaitPrimary. The routing tensors still load before the wait when routing_ready=True. It also drops the device guard #47784 added around the launch.

cc @hnyls2002 @BBuf

Why the revert happened

#47994 suspected the early shared_output load, and that is the cause. shared_output is written by the shared expert on alt_stream and joined with wait_stream before the finalize. In a CUDA graph, that cross-stream join into a PDL launch is a programmatic edge as well, so a shared expert that triggers its dependents early lets the finalize start before the row is written. Only the PDL wait orders it.

Reproduced on B300 with a shared-expert stand-in on a side stream that triggers at entry and spins 300 us before writing, joined with wait_stream, followed by the PDL finalize:

kernel eager CUDA graph eager, PDL writer CUDA graph, PDL writer
#47784 0/20 wrong 0/20 0/20 20/20 wrong
this PR 0/20 0/20 0/20 0/20

The reproduction needs a purpose-built early-triggering writer kernel, so it is not added as a registered test.

Performance

B300, hidden 7168, top-k 8, bf16 with fp32 weights, CUDA graph behind a real PDL producer that triggers before its final stores. Finalize cost = pair time minus producer-only time, medians of 2 x 15 replays.

this PR vs #47784 this PR vs the pre-#47784 kernel on main
warm (the shared row was just written, in L2) within +-0.04 us at 1-8192 tokens 1 token -0.65 us, 32-128 tokens -1.5 to -2.9 us, 4096-8192 tokens equal
cold (rotated buffers) +0.12 / +0.23 us at 64 / 128 tokens, about 0 elsewhere 32-128 tokens -2.6 to -6.5 us
without PDL about 0

Issuing the shared load right after the wait is as fast as or faster than issuing it in use order (after the gathers): for one-wave grids ptxas places it after the gathers either way, and the persistent kernel is up to 0.6 us faster with the early issue.

Tests

test_moe_finalize_fuse_shared.py on B300: 132 passed, 6 skipped (unchanged from #47784). SASS: the shared LDG.E.128 is after the grid-dependency wait; no stack or spills in any instance.

Needs before merge: /rerun-test of the nightly test_dsa_glm52_hisparse.py on 8-gpu-b200, which caught the regression and is not part of the per-PR runs.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ✅ Run #42153621680
Latest PR Test (Extra): ❌ Run #42153621818
Latest PR Test (AMD ROCm 10): ⏳ Run #42153621640

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@DarkSharpness DarkSharpness added the run-ci CI: run the baseline test suite on this PR label Nov 3, 2026
@DarkSharpness

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/e2e/models/test_dsa_glm52_hisparse.py

@DarkSharpness

Copy link
Copy Markdown
Collaborator Author

@BBuf for review

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

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

cd test/ && python3 registered/e2e/models/test_dsa_glm52_hisparse.py

@BBuf BBuf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[BBuf Review Agent] One thing to fix before this can go in.

@BBuf

BBuf commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/moe/test_flashinfer_fp8_routed.py::TestFlashinferFp8Routed::test_block_fp8_live_routes_and_deferred_finalize test/registered/e2e/models/test_dsa_glm52_hisparse.py

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (1 test): ❌ View workflow run

cd test/ && python3 registered/moe/test_flashinfer_fp8_routed.py TestFlashinferFp8Routed::test_block_fp8_live_routes_and_deferred_finalize

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

cd test/ && python3 registered/e2e/models/test_dsa_glm52_hisparse.py

@DarkSharpness

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/moe/test_flashinfer_fp8_routed.py::TestFlashinferFp8Routed.test_block_fp8_live_routes_and_deferred_finalize

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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

cd test/ && python3 registered/moe/test_flashinfer_fp8_routed.py TestFlashinferFp8Routed.test_block_fp8_live_routes_and_deferred_finalize

@BBuf BBuf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM.

@BBuf

BBuf commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@BBuf

BBuf commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@BBuf

BBuf commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/moe/test_flashinfer_fp8_routed.py::TestFlashinferFp8Routed::test_block_fp8_live_routes_and_deferred_finalize test/registered/e2e/models/test_dsa_glm52_hisparse.py

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (1 test): ❌ View workflow run

cd test/ && python3 registered/moe/test_flashinfer_fp8_routed.py TestFlashinferFp8Routed::test_block_fp8_live_routes_and_deferred_finalize

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

cd test/ && python3 registered/e2e/models/test_dsa_glm52_hisparse.py

@DarkSharpness

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/moe/test_flashinfer_fp8_routed.py::TestFlashinferFp8Routed.test_block_fp8_live_routes_and_deferred_finalize

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

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

cd test/ && python3 registered/moe/test_flashinfer_fp8_routed.py TestFlashinferFp8Routed.test_block_fp8_live_routes_and_deferred_finalize

@BBuf

BBuf commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@BBuf

BBuf commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/moe/test_flashinfer_fp8_routed.py::TestFlashinferFp8Routed::test_block_fp8_live_routes_and_deferred_finalize test/registered/e2e/models/test_dsa_glm52_hisparse.py

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (1 test): ❌ View workflow run

cd test/ && python3 registered/moe/test_flashinfer_fp8_routed.py TestFlashinferFp8Routed::test_block_fp8_live_routes_and_deferred_finalize  # extra-b

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

cd test/ && python3 registered/e2e/models/test_dsa_glm52_hisparse.py  # nightly

@DarkSharpness

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/moe/test_flashinfer_fp8_routed.py::TestFlashinferFp8Routed.test_block_fp8_live_routes_and_deferred_finalize

@DarkSharpness

Copy link
Copy Markdown
Collaborator Author

(auto generated by Claude)

@BBuf test_flashinfer_fp8_routed.py runs under unittest, so the selector after :: needs Class.method, not Class::method; the :: form exits with AttributeError: module '__main__' has no attribute ... before any test runs. Re-dispatched above as TestFlashinferFp8Routed.test_block_fp8_live_routes_and_deferred_finalize.

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

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

cd test/ && python3 registered/moe/test_flashinfer_fp8_routed.py TestFlashinferFp8Routed.test_block_fp8_live_routes_and_deferred_finalize  # extra-b

…e shared row read after the PDL wait

Re-land sgl-project#47784 (reverted by sgl-project#47994) with one change: the shared-expert row is read after PDLWaitPrimary; the routing still loads before it. In a CUDA graph, the shared expert's cross-stream join into the finalize is a programmatic edge too, so an early-triggering shared expert let the old kernel read the row before it was written.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@BBuf

BBuf commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/moe/test_flashinfer_fp8_routed.py::TestFlashinferFp8Routed::test_block_fp8_live_routes_and_deferred_finalize test/registered/e2e/models/test_dsa_glm52_hisparse.py

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (1 test): ❌ View workflow run

cd test/ && python3 registered/moe/test_flashinfer_fp8_routed.py TestFlashinferFp8Routed::test_block_fp8_live_routes_and_deferred_finalize  # extra-b

🚀 8-gpu-b200 (1 test): ❌ View workflow run

cd test/ && python3 registered/e2e/models/test_dsa_glm52_hisparse.py  # nightly

@DarkSharpness

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/moe/test_flashinfer_fp8_routed.py::TestFlashinferFp8Routed.test_block_fp8_live_routes_and_deferred_finalize

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (1 test): ❌ View workflow run

cd test/ && python3 registered/moe/test_flashinfer_fp8_routed.py TestFlashinferFp8Routed.test_block_fp8_live_routes_and_deferred_finalize  # extra-b

…nalize kernel

> (auto generated by Claude)

sgl-project#48366 removed the packed fp32x2 FMA / multiply helpers from sgl_kernel/math.cuh as unused, but moe_finalize_fuse_shared.cuh uses math::fma_f32x2. Restore both helpers verbatim, as they were before sgl-project#48366.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@DarkSharpness

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/moe/test_flashinfer_fp8_routed.py::TestFlashinferFp8Routed.test_block_fp8_live_routes_and_deferred_finalize test/registered/kernels/ops/moe/test_moe_finalize_fuse_shared.py

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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

cd test/ && python3 registered/moe/test_flashinfer_fp8_routed.py TestFlashinferFp8Routed.test_block_fp8_live_routes_and_deferred_finalize  # extra-b

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

cd test/ && python3 registered/kernels/ops/moe/test_moe_finalize_fuse_shared.py  # base-b-kernel-unit

@BBuf BBuf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM.

@BBuf
BBuf merged commit c795995 into sgl-project:main Nov 6, 2026
158 of 167 checks passed
@DarkSharpness
DarkSharpness deleted the moe-finalize-reland branch November 6, 2026 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants