Skip to content

[Perf] [NVIDIA] moe: Rewrite moe_finalize_fuse_shared as a JIT kernel with all top-k gathers in flight - #47784

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

BBuf merged 3 commits into
sgl-project:mainfrom
DarkSharpness:moe-finalize-rewrite

Conversation

@DarkSharpness

@DarkSharpness DarkSharpness commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

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

What

Rewrites moe_finalize_fuse_shared (the deferred-finalize combine of the trtllm-gen MoE path: out[t] = sum_k w[t, k] * gemm2_out[permuted_idx[t * K + k]] (+ shared[t])) as a header-only JIT kernel and removes the old cutlass-based .cu.

  • Each thread owns 8 bf16 of one token and issues all K row gathers at once, so a token costs the index load plus one gather round trip; tokens map to blockIdx.y, so even one token spreads over H / 1024 CTAs.
  • The token's route (K row indices and weights) is spread over the warp (lane k holds slot k, read by shuffle), which keeps top-k 16 within one wave.
  • Skipped rows (index -1) use a predicated load into zeroed registers with weight -0.0, so the sum stays bitwise unchanged without a branch per slot.
  • bf16 weights use the mixed-precision fma.rn.f32.bf16 (math::fma_f32_bf16), fp32 weights the packed fma.rn.f32x2 (math::fma_f32x2, added to math.cuh); both give the fp32 FMA's bits.
  • One token per CTA row while the grid fits one resident wave; past it, a persistent variant walks tokens and prefetches the next token's route.
  • New optional arguments: out= writes into a caller buffer with a row stride (e.g. a symmetric-memory buffer); routing_ready (default on, with PDL) reads the route and the shared row before the PDL wait, which is valid because the trtllm-gen gemm2 triggers its dependents only after its own wait and the routing runs in an earlier kernel.

Results are bitwise equal to the old kernel (fp32 accumulation in k order, shared row added last).

Performance

B300, us per call, warm (inputs in L2 as right after the gemm2), measured inside a CUDA graph behind a producer kernel that writes gemm2_out and triggers PDL right after its wait (like the trtllm-gen gemm2); old = current main.

hidden / top-k (shared) weights T=1 T=64 T=192 T=384
7168 / 8 (yes) fp32 1.30 -> 0.60 4.88 -> 1.34 5.38 -> 2.42
7168 / 6 (yes) fp32 1.39 -> 0.57 5.63 -> 1.14 5.92 -> 2.13 8.82 -> 3.46
7168 / 6 (yes) bf16 1.55 -> 0.70 5.84 -> 1.22 6.13 -> 2.11 8.96 -> 3.38
4096 / 6 (yes) fp32 1.41 -> 0.54 3.18 -> 0.90 3.26 -> 1.95 5.01 -> 2.40
4096 / 6 (yes) bf16 1.41 -> 0.53 3.36 -> 1.01 3.44 -> 1.82 5.14 -> 2.31
3584 / 16 (no) fp32 2.58 -> 0.96
3584 / 16 (no) bf16 2.50 -> 0.75

3584 / 16 at T = 32 / 96 / 186 / 256: fp32 2.94 -> 1.06, 4.05 -> 1.60, 4.27 -> 2.26, 4.35 -> 2.97; bf16 2.99 -> 0.98, 3.95 -> 1.49, 4.27 -> 2.26, 4.32 -> 2.69.

Tests

  • test_moe_finalize_fuse_shared (new): against a torch reference over hidden 1028-7168, top-k 6/8/10/16, T 1-1000, fp32/bf16 weights, with and without the shared row, skipped (-1) rows, padded gemm2_out rows, PDL with and without routing_ready, and out= into a row-strided caller buffer; 131 passed, 6 skipped (by design) on B300.
  • bench_moe_finalize_fuse_shared (new): registered benchmark over the trtllm deferred-finalize model shapes.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ✅ Run #41358184358
Latest PR Test (Extra): ❌ Run #41358184251
Latest PR Test (AMD ROCm 10): ⏳ Run #41358184196

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

… with all top-k gathers in flight

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

@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 1, 2026

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

};
const auto dev = device.unwrap();
const uint32_t grid_x = div_ceil(params.num_chunks, kBlockSize);
if (uint64_t{grid_x} * num_tokens <= resident_ctas<false>(dev.device_id)) {

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] With CUDA 0 current and inputs on CUDA 1's non-default stream, this launches with a CUDA 1 stream on CUDA 0 and fails with an invalid resource handle. Could we retain a device guard around the occupancy queries and launch, as the old launcher's cudaSetDevice did?

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.

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

Good catch, fixed in 76b3fa3: the launcher now holds a tvm::ffi::CUDADeviceGuard on the inputs' device around the occupancy queries and the launch.

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] Confirmed at 76b3fa3, the guard uses the inputs' device and stays alive through both occupancy queries and launch branches. This addresses the device mismatch.

@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. The earlier PDL validation follow-up remains.

@BBuf
BBuf merged commit ca819c1 into sgl-project:main Nov 2, 2026
171 of 177 checks passed
@DarkSharpness
DarkSharpness deleted the moe-finalize-rewrite branch November 2, 2026 10:58
DarkSharpness added a commit to DarkSharpness/sglang that referenced this pull request Nov 6, 2026
…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]>
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