Repository navigation
[Perf] [NVIDIA] moe: Rewrite moe_finalize_fuse_shared as a JIT kernel with all top-k gathers in flight - #47784
Conversation
… with all top-k gathers in flight Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
BBuf
left a comment
There was a problem hiding this comment.
[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)) { |
There was a problem hiding this comment.
[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?
There was a problem hiding this comment.
(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.
There was a problem hiding this comment.
[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
left a comment
There was a problem hiding this comment.
LGTM. The earlier PDL validation follow-up remains.
…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]>
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.blockIdx.y, so even one token spreads overH / 1024CTAs.fma.rn.f32.bf16(math::fma_f32_bf16), fp32 weights the packedfma.rn.f32x2(math::fma_f32x2, added tomath.cuh); both give the fp32 FMA's bits.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_outand triggers PDL right after its wait (like the trtllm-gen gemm2); old = current main.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, paddedgemm2_outrows, PDL with and withoutrouting_ready, andout=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-testto run the tests your change touches, or copy this prompt for an agent: