Skip to content

[SM100] Mega MoE: dynamic LPT expert ordering, N-outer L2 weight reuse, and skewed prefill BLOCK_M heuristic - #162

Merged
zcnrex merged 1 commit into
sgl-project:devfrom
Denji-kk:perf/sm100-mega-moe-lpt-l2-heuristics
Nov 23, 2026
Merged

zcnrex merged 1 commit into
sgl-project:devfrom
Denji-kk:perf/sm100-mega-moe-lpt-l2-heuristics

Conversation

@Denji-kk

Copy link
Copy Markdown

Under realistic skewed prefill routing, hot local experts receive multiple M-blocks (2..5+ blocks per expert) while cold experts receive 0..1. In the current MegaMoEScheduler and tile heuristic:

  1. Experts are scheduled strictly in static index order (0 .. kNumExperts - 1), so a hot multi-block expert at the end of the array can create a tail wave across SMs, and within a multi-block expert the task order sweeps all N-clusters for m_block = 0 before moving to m_block = 1 (M-outer), thrashing L2 weight tiles when total expert weight bytes exceed L2 capacity.
  2. Weight/SFB TMA loads always use EVICT_NORMAL (EVICT_FIRST for activations when BLOCK_M > 64), even when an expert has num_m_blocks >= 2 and reuses the exact same weight tiles across consecutive m_blocks.
  3. get_block_config_for_mega_moe adds a 1.0 * sqrt(expected) dispersion margin at all token counts. On multi-block prefill (num_expected_tokens >= 240), heavy-tailed expert popularity regularly exceeds +1 sigma, spilling hot experts into an extra M-block and reloading all of their L1 + L2 weight tiles from HBM.

This PR addresses all three bottlenecks while keeping Decode (num_expected_tokens <= 24, max_m_blocks <= 1) completely untouched:

  • Dynamic register-only LPT expert ordering (scheduler/mega_moe.cuh): when kEnableLPTSwizzle is enabled (sm100_fp8_fp4_mega_moe.cuh with kNumSharedExperts == 0) and runtime max_m_blocks >= 3, bucket experts in registers by descending m_blocks (>=4, 3, 2, 1) in O(E) warp-parallel operations with zero extra SMEM. Uniform and light loads (max_m_blocks <= 2) bypass the sort at zero cost.
  • Locality-domain-preserving intra-expert N-outer swizzle & EVICT_LAST hints (scheduler/mega_moe.cuh, sm100_fp8_fp4_mega_moe.cuh):
    • Group up to kSwizzleGroupM = 4 M-blocks of the same expert per N-cluster within each kNumLocalityDomains partition so all M-blocks in the group reuse the resident L1/L2 weight tile in L2 while preserving 2-die HBM locality affinity and deadlock-free ring/warmup bounds (get_num_l1_warmup_waves, get_num_max_live_pool_blocks).
    • Pack uint16_t m_block_idx, num_m_blocks into TaskInfo (keeping sizeof(TaskInfo) == 32), promote W1 / W2 / SFB TMA cache policy to EVICT_LAST whenever task_info.num_m_blocks >= 2, and use EVICT_LAST for activation ring TMA loads.
  • Multi-block prefill dispersion margin (heuristics/mega_moe.hpp): use 3.0f * std::sqrt(num_expected_tokens) when num_expected_tokens >= 240.0f (1.0f otherwise), reducing hot-expert M-block spills on skewed multi-block prefill without changing Decode or medium-batch tile choices.

Outputs across all tested workloads are 100% bitwise identical (max_abs_diff = 0.0, 100.00% exact element match) to dev.

Tests

Passed the Blackwell MegaMoE test suite on 8x B300 (test_mega_moe_pre_dispatch, test_mega_moe_nvfp4_trtllm, 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 across 8-rank NVFP4, 2-rank MXFP4 ring wrap, and 2-rank x_scales) and verified the setup.py pybind build.

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; M = tokens per rank):

Shape (H, I, E, top_k) Workload (M/rank, routing) BLOCK_M (dev -> PR) dev (us) This PR (us) Delta Bitwise Exact vs dev
H=7168, I=5120, E=384, K=4 M=16, uniform 16 -> 16 470.19 405.66 -13.7% 100.00%
H=7168, I=5120, E=384, K=4 M=2048, uniform 192 -> 192 681.23 610.94 -10.3% 100.00%
H=7168, I=5120, E=384, K=4 M=2048, skewed 192 -> 192 881.12 853.28 -3.2% 100.00%
H=7168, I=5120, E=384, K=4 M=4096, skewed 192 -> 240 1,406.85 1,336.11 -5.0% 100.00%
H=7168, I=5120, E=384, K=4 M=8192, skewed 240 -> 192 2,764.56 2,572.24 -7.0% 100.00%
H=7168, I=2048, E=256, K=8 M=8, uniform 16 -> 16 192.91 189.38 -1.8% 100.00%
H=7168, I=2048, E=256, K=8 M=2048, uniform 192 -> 240 615.66 611.25 -0.7% 100.00%
H=7168, I=2048, E=256, K=8 M=4096, skewed 240 -> 240 1,640.24 1,615.66 -1.5% 100.00%

Nsight Compute (2026.3.0) 1-GPU (EP = 1, H=7168, I=5120, E_local=48, top_k=4) hardware counters — both fixed-tile (M=2048 skewed, BM=192 -> 192 isolating LPT + N-outer + EVICT_LAST) and multi-block skewed (M=4096 skewed, BM=192 -> 240):

Workload Arm BLOCK_M dram__bytes_read.sum lts__t_sector_hit_rate.pct sm__pipe_tensor_cycles_active Kernel Time
M=2048, skewed dev 192 3,704.87 MB 46.42% 31.12% 666.82 us
M=2048, skewed This PR 192 3,202.28 MB (-13.6%) 51.17% (+4.75 pp) 32.93% 639.78 us (-4.1%)
M=4096, skewed dev 192 4,393.49 MB 54.65% 45.83% 934.69 us
M=4096, skewed This PR 240 3,456.12 MB (-21.3%) 60.92% (+6.27 pp) 50.94% (+5.11 pp) 880.42 us (-5.8%)
M=4096, uniform dev 192 4,219.77 MB 51.14% 53.92% 845.44 us
M=4096, uniform This PR 240 3,410.56 MB (-19.2%) 61.07% (+9.93 pp) 55.08% 840.77 us (-0.6%)

@zcnrex

zcnrex commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

The LPT ordering, N-outer kSwizzleGroupM = 4 swizzle, physical-vs-slot ID translation, and updated warmup/ring-buffer bounds all look clean and sound (and great to see the combined #161 + #162 BLOCK_M = 240 K=96 verification already covered in #161).

One tiny doc nit: the PR description mentions a 4-bucket O(E) sort (>=4, 3, 2, 1), while the implementation does a full exact register-only rank sort (which is even better — just worth aligning the description).

@zcnrex
zcnrex merged commit ca17027 into sgl-project:dev Nov 23, 2026
trevor-m added a commit to trevor-m/DeepGEMM that referenced this pull request Nov 23, 2026
…ebased dev

Re-measured after rebasing onto dev a96d065 (sgl-project#160, sgl-project#162), same Vera Rubin setup:
- L1-first with decode tiles (BLOCK_M <= 64) now allows at most 4 extra L1 waves after the warmup (was 8).
  4 extra waves still gain 0.7-1.4%; 5 were 0-0.6% slower than dev and 7 up to 1.2% slower, worst without a
  shared expert (sgl-project#162's swizzled order).
- Shared L2 keeps its early slot for every BLOCK_M < 192, on every part (was: decode tiles, or 128-row tiles below
  100 CTA pairs). With 128-row tiles the tail no longer gains on Vera Rubin either: alone it was 0-0.8% slower,
  and with L1-first the early slot is 1.3% faster. This drops the CTA-pair term from the gate.

Outputs stay bitwise identical to dev.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zcnrex pushed a commit that referenced this pull request Nov 23, 2026
…ed L2 at the tail for large tiles (#157)

* [SM100] Mega MoE: all L1 tasks first for decode-sized loads, shared L2 at the tail above BLOCK_M 64

- Routed tasks: issue every L1 task before the first L2 task when the L1
  work is decode-sized (`BLOCK_M <= 64`, or at most one L1 wave left after
  the warmup waves) and every pool block has its own ring slot. The last
  M blocks start their L1 earlier, and their L1 -> L2 chain sets the tail.
  Larger loads keep the interleaved order.
- Fused shared expert: the shared L2 tasks keep the early slot (before the
  routed tasks) only with decode tiles (`BLOCK_M <= 64`); with larger tiles
  they go after the routed tasks.

Only the task order changes, so outputs are bitwise identical. Measured on
Vera Rubin (CC 10.7), fp8xfp4 with a fused FP8 shared expert, DeepSeek-V4.1-
Flash shapes, EP4: -2.3% to -7.2% per call at 32-rank decode loads and
-2.1% to -6.9% at 16-rank loads.

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

* [SM100] Mega MoE: bound L1-first by extra L1 waves, keep shared L2 early for 128-row tiles below 100 CTA pairs

Review follow-up:
- L1-first now needs at most 8 extra L1 waves after the warmup with decode tiles (BLOCK_M <= 64), and at
  most 1 with larger tiles. Unbounded, it was slightly slower with many local experts and few tokens each
  (104 experts per rank at EP4: up to +0.9%), while 13 experts per rank still gain through 7 extra waves.
- With 128-row tiles the shared L2 keeps its early slot below 100 CTA pairs (B300 / GB300), where the tail
  was ~1% slower. BLOCK_M >= 192, and 128 on Vera Rubin's 106 pairs, keep the tail.

Outputs stay bitwise identical to dev.

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

* [SM100] Mega MoE: retune the L1-first cap and shared L2 gate on the rebased dev

Re-measured after rebasing onto dev a96d065 (#160, #162), same Vera Rubin setup:
- L1-first with decode tiles (BLOCK_M <= 64) now allows at most 4 extra L1 waves after the warmup (was 8).
  4 extra waves still gain 0.7-1.4%; 5 were 0-0.6% slower than dev and 7 up to 1.2% slower, worst without a
  shared expert (#162's swizzled order).
- Shared L2 keeps its early slot for every BLOCK_M < 192, on every part (was: decode tiles, or 128-row tiles below
  100 CTA pairs). With 128-row tiles the tail no longer gains on Vera Rubin either: alone it was 0-0.8% slower,
  and with L1-first the early slot is 1.3% faster. This drops the CTA-pair term from the gate.

Outputs stay bitwise identical to dev.

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

---------

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