Skip to content

[SM103] Mega MoE: add native K=96 Ultra MMA mainloop for NVFP4 on Blackwell Ultra - #161

Merged
zcnrex merged 2 commits into
sgl-project:devfrom
Denji-kk:feat/sm103-mega-moe-k96-ultra
Nov 23, 2026
Merged

zcnrex merged 2 commits into
sgl-project:devfrom
Denji-kk:feat/sm103-mega-moe-k96-ultra

Conversation

@Denji-kk

@Denji-kk Denji-kk commented Nov 20, 2026 •

Copy link
Copy Markdown

Blackwell Ultra (CC 10.3) introduces native K = 96 block-scaled FP4 Tensor Core instructions (tcgen05.mma.cta_group::2.kind::mxf4nvf4.block_scale.block16, SM103_MMA_F4_2x1SM_SS_ULTRA in CUTLASS) that process 96 FP4 elements per MMA atom (1.5x the K = 64 width of SM100). Currently on CC 10.3, csrc/runtime/jit.hpp compiles with -arch=sm_100f and sm100_fp8_fp4_mega_moe.cuh runs the 4-chunk K = 64 mainloop.

Following the SM107ArchSpec / use_sm107_k64 pattern from #113 and #133, this PR adds native SM103 K = 96 Ultra MMA support across all NVFP4 MegaMoE BLOCK_M candidates ({16, 64, 128, 192, 240}):

  • Compiler & arch spec (csrc/runtime/jit.hpp, heuristics/sm100.hpp): select -arch=sm_103a on CC 10.3 with NVCC >= 12.9 and add SM103ArchSpec::is_current().
  • 6-stage (2 x 3) TMA pipeline on BLOCK_M = 192 & compact 32-col TMEM SF ring on BLOCK_M = 240 (heuristics/mega_moe.hpp, sm100_fp8_fp4_mega_moe.cuh):
    • Each K = 96 Ultra super-step spans 3 256-element BLOCK_K stages (3 x 256 = 768 = 8 x 96 FP4 elements).
    • Union expert_token_count (used only in Dispatch/Cleanup) with dispatch_send_buffer in SharedStorage, and use asymmetric store_block_m_l1 = 32, store_block_m_l2 = 8 on BLOCK_M = 192 when use_sm103_k96 is active so 6 TMA stages (230,788 B) fit inside the 227 KiB (232,448 B) shared-memory budget, giving double-buffered 3-stage K = 96 super-steps.
    • On BLOCK_M <= 192 (kFreeTmemCols >= 128), allocate multi-slot 3-chunk TMEM SF rings (kNumK96SFSlots up to 4; 3 slots = 108 cols at BLOCK_M = 192).
    • On BLOCK_M = 240 (UMMA_N = 240, kFreeTmemCols = 512 - 480 = 32 < 36), 3-chunk SF slots do not fit; when umma_n <= 128 (valid_m <= 128), each 2-chunk K = 96 SF window needs 2 x 4 (SFA) + 2 x 4 (SFB) = 16 columns, fitting two disjoint double-buffered K = 96 SF slots (480..495 and 496..511) inside the 32 free TMEM columns while tiles with umma_n > 128 use double-buffered 2-slot K = 64 (480..491 and 492..503).
  • Hybrid K = 96 + K = 64-tail mainloop (sm100_fp8_fp4_mega_moe.cuh):
    • Guarded by #if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ == 1030) and kUseSM103K96 (controllable via DG_MEGA_MOE_SM103_K96, default 1 on CC 10.3 for NVFP4, BLOCK_K == 256, GRAN_K == 16, BLOCK_M <= 240).
    • Processes num_k_blocks / 3 3-stage groups via 8 K = 96 lbo_mode = 1 Ultra MMAs across 256B-strided SMEM sub-blocks and automatically drains any num_k_blocks % 3 remainder (0, 1, 2 blocks, e.g. K = 7168 -> 28 = 9 x 3 + 1 or K = 5120 -> 20 = 6 x 3 + 2) with the K = 64 path across all weight layouts (GateUpBlock8, RowShuffled, GateUpInterleaved, UpGateBlock16) and shared experts.
    • SM100 (CC 10.0), SM107 (CC 10.7), and non-NVFP4 MMA kinds are completely unchanged.

Tests

Verified on 8x B300 (CC 10.3):

  • Full Blackwell MegaMoE suite (test_mega_moe_pre_dispatch, test_mega_moe_nvfp4_trtllm across both BLOCK_M = 192 and BLOCK_M = 240, test_mega_moe_nvfp4_alphas, test_mega_moe_l1_fp4_accuracy, test_mega_moe_l1_sentinel, test_mega_moe_situ, test_mega_moe_global_tokens 8-rank NVFP4 / 2-rank MXFP4 / 2-rank x_scales) — all pass.
  • Non-MegaMoE regression coverage under -arch=sm_103a: test_bf16.py (GEMM + m-grouped + k-grouped), test_mega_gate.py, test_mega_mhc.py, and test_fp8_fp4.py test_gemm — all pass.
  • setup.py / build_sgl_deep_gemm.sh builds compile cleanly with zero additional register spills on sm_103a.

Microbenchmark & NCU

8x B300 (EP = 8) standalone nvfp4_mega_moe microbenchmark (fast_math=True, use_x_scales=True, median of 60 iterations across slowest rank; comparing dev, this PR with DG_MEGA_MOE_SM103_K96=0, and this PR with default DG_MEGA_MOE_SM103_K96=1):

Shape (H, I, E, top_k) Workload (M/rank, routing) BLOCK_M dev (us) K96=0 (us) K96=1 (us / TFLOPS/GPU) K96=1 vs dev Bitwise Exact vs dev
H=7168, I=5120, E=384, K=4 M=16, uniform 16 470.19 463.98 409.30 (34.4 TF) -13.0% 100.00%
H=7168, I=5120, E=384, K=4 M=2048, uniform 192 681.23 622.82 622.64 (2,897.2 TF) -8.6% 100.00%
H=7168, I=5120, E=384, K=4 M=2048, skewed 192 881.12 871.95 864.53 (2,086.6 TF) -1.9% 100.00%
H=7168, I=5120, E=384, K=4 M=4096, uniform 192 941.20 940.53 920.50 (3,919.4 TF) -2.2% 100.00%
H=7168, I=5120, E=384, K=4 M=4096, skewed 192 1,406.85 1,367.82 1,372.58 (2,628.5 TF) -2.4% 100.00%
H=7168, I=5120, E=384, K=4 M=5120, uniform 240 1,121.41 1,098.98 1,110.34 (4,061.6 TF) -1.0% 100.00%
H=7168, I=5120, E=384, K=4 M=8192, uniform 240 1,722.98 1,688.74 1,668.93 (4,323.5 TF) -3.1% 100.00%
H=7168, I=5120, E=384, K=4 M=8192, skewed 240 3,425.09 3,391.55 3,408.86 (2,116.7 TF) -0.5% 100.00%
H=7168, I=2048, E=256, K=8 M=8, uniform 16 192.91 191.79 190.83 (29.5 TF) -1.1% 100.00%
H=7168, I=2048, E=256, K=8 M=2048, uniform 192 615.66 620.96 611.20 (2,361.1 TF) -0.7% 100.00%
H=7168, I=2048, E=256, K=8 M=2048, skewed 192 841.34 845.06 836.38 (1,725.4 TF) -0.6% 100.00%

(In combination with #162 where skewed multi-block prefill selects BLOCK_M = 240 at M = 4096 with ~50% umma_n <= 128 tail tiles, the compact BLOCK_M = 240 K = 96 ring reduces skewed M = 4096 kernel time by up to 10.4% over BLOCK_M = 240 K = 64 while remaining 100% bitwise exact.)

Nsight Compute (2026.3.0) 1-GPU (EP = 1, H=7168, I=5120, E_local=48, top_k=4) hardware counters confirming the 2/3 Tensor Core pipe duty-cycle reduction from 1.5x K = 96 MMA throughput and 6-stage SMEM fit:

Workload Arm Tile / Stages SMEM / CTA sm__pipe_tensor_cycles_active Kernel Time
M=4096, uniform dev BM=192, S=48/48, 5-stg 217,972 B 53.92% 845.44 us
M=4096, uniform PR (K96=0) BM=192, S=48/48, 5-stg 216,948 B 54.31% 844.19 us
M=4096, uniform PR (K96=1) BM=192, S=32/8, 6-stg 230,788 B 36.80% (0.68x) 825.54 us (-2.4%)
M=4096, skewed dev BM=192, S=48/48, 5-stg 217,972 B 45.83% 934.69 us
M=4096, skewed PR (K96=1) BM=192, S=32/8, 6-stg 230,788 B 31.27% (0.68x) 918.82 us (-1.7%)

@zcnrex
zcnrex merged commit 6eddf92 into sgl-project:dev Nov 23, 2026
zcnrex added a commit that referenced this pull request Nov 28, 2026
…178)

PR #161 made the host size the dispatch region as
max(expert_token_count, dispatch_send_buffer), matching the union it added
to the FP8xFP4 kernel. The BF16 kernel shares that host function but kept
the two as separate 1 KiB-aligned fields, so its SharedStorage is 1 KiB
larger than the launched dynamic smem and its barriers sit past the end.

On B300 the 2-rank bf16xbf16 test (hidden 1024, 8 experts) faults on
warp 1's dispatch-barrier init (Warp Out-of-range Address, STS at smem
0x35c40 with smem_size=219524), and DeepEP reports it later as
symmetric.hpp:136 Cuda failure 700.

The two lifetimes are disjoint in the BF16 kernel as well: the counts are
last used before the dispatch NVLink barrier and the pull buffer only after.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants