Skip to content

[SM100] Mega MoE: fix FP4 full-pool TMA pull, cache ready peers in combine, and vectorize L1 FP4 epilogue - #160

Merged
zcnrex merged 1 commit into
sgl-project:devfrom
Denji-kk:perf/sm100-mega-moe-fp4-dispatch-combine-epilogue
Nov 23, 2026
Merged

zcnrex merged 1 commit into
sgl-project:devfrom
Denji-kk:perf/sm100-mega-moe-fp4-dispatch-combine-epilogue

Conversation

@Denji-kk

Copy link
Copy Markdown

This PR bundles four targeted, layout-preserving fixes and optimizations for the SM100 FP8/FP4 MegaMoE kernel (sm100_fp8_fp4_mega_moe.cuh and common/math.cuh):

  1. Fix kUseFullPoolFP8FP4Path for packed FP4 activations (NVFP4 / MXFP4):
    • In sm100_fp8_fp4_mega_moe.cuh, kUseFullPoolFP8FP4Path required BLOCK_K == BLOCK_N and kNumHiddenTokenBytes == kNumBytesPerPull. Because BLOCK_K = 256 and BLOCK_N = 128 for all 4-bit MMA kinds (MmaKind::NVFP4 and MmaKind::MXFP4), BLOCK_K == BLOCK_N was unconditionally false for every FP4-activation kernel, forcing multi-descriptor TMA pulls even when an entire token fits in one pull (hidden <= 16384).
    • Drop the spurious BLOCK_K == BLOCK_N check so kUseFullPoolFP8FP4Path = (kNumHiddenTokenBytes == kNumBytesPerPull) activates for single-pull FP4 tokens, and use __stcg on strided local_sf_ptr Dispatch writes to avoid L1 write-allocate cache line thrashing.
  2. Cache observed-ready peers across tokens in Combine (known_ready_ranks):
    • Each rank's symmetric arrival flag is monotonic within a MegaMoE launch. Hoisting a per-warp uint64_t known_ready_ranks bitmask across the Combine token loop (__reduce_or_sync(0xffffffff, lane_bit)) avoids re-issuing ld_acq_sys polling over NVLink once a peer rank has already been observed ready.
  3. Eliminate the 1 KiB nvfp4_sf_inv_lut SMEM table and vectorize the L1 FP4 epilogue:
    • Replace the 256-entry nvfp4_sf_inv_lut shared-memory lookup + __frcp_rn fallback with a branchless 1-step Newton-Raphson reciprocal (rcp.approx.ftz.f32 + 2 fma.rn.f32 refinements), which is 0-ULP bit-identical to 1.0f / decode(code) across all 127 finite non-negative UE4M3 codes, freeing 1,024 B of shared memory per CTA and removing LUT bank conflicts.
    • Replace scalar byte shared-memory stores in the L1_OUT_BLOCK_N_BYTES == 32 FP4 epilogue path with warp-shuffled bank-conflict-free uint32_t (STS.32) writes, and coalesce the 4 warps' NVFP4 L1 UE4M3 scale bytes via amax_reduction SMEM into a single uint32_t global store per row instead of 4 strided byte stores.
  4. Skip upper-half SFA UTCCP when valid_m <= 128:
    • Guard the second 128-row SFA UTCCP transpose (i == 1 when SF_BLOCK_M == 256) on i * kNumUTCCPAlignedElems < task_info.valid_m so partially filled BLOCK_M = 192 / 240 tiles with <= 128 valid tokens skip redundant TMEM scale copies.

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

Shape (H, I, E, top_k) Workload (M/rank, routing) BLOCK_M dev (us) This PR (us) Delta Bitwise Exact vs dev
H=7168, I=5120, E=384, K=4 M=16, uniform 16 470.19 411.46 -12.5% 100.00%
H=7168, I=5120, E=384, K=4 M=2048, uniform 192 681.23 624.78 -8.3% 100.00%
H=7168, I=5120, E=384, K=4 M=2048, skewed 192 881.12 876.88 -0.5% 100.00%
H=7168, I=5120, E=384, K=4 M=8192, skewed 240 2,764.56 2,755.06 -0.3% 100.00%
H=7168, I=2048, E=256, K=8 M=8, uniform 16 192.91 191.07 -1.0% 100.00%
H=7168, I=2048, E=256, K=8 M=2048, uniform 192 615.66 614.80 -0.1% 100.00%
H=7168, I=2048, E=256, K=8 M=4096, skewed 240 1,640.24 1,622.26 -1.1% 100.00%

Nsight Compute (2026.3.0) 1-GPU (EP = 1, H=7168, I=5120, E_local=48, top_k=4) hardware counters confirming the SMEM and L1/L2 sector reductions:

Workload Metric dev This PR Change
M=4096, uniform launch__shared_mem_per_block 217,972 B 216,948 B -1,024 B
M=4096, uniform l1tex__t_sectors_pipe_lsu_mem_global_op_st.sum 14,861,353 9,969,153 -32.9%
M=4096, uniform l1tex__data_bank_conflicts_pipe_lsu_mem_shared_op_st.sum 5,216,323 4,208,396 -19.3%
M=2048, skewed l1tex__t_sectors_pipe_lsu_mem_global_op_st.sum 7,614,281 5,006,911 -34.2%
M=2048, skewed l1tex__data_bank_conflicts_pipe_lsu_mem_shared_op_st.sum 2,134,215 1,990,137 -6.8%

@zcnrex

zcnrex commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Looks very solid overall — the kUseFullPoolFP8FP4Path fix, known_ready_ranks caching, and STS.32 / coalesced SF epilogue are all clean and well-verified. A few small items before merge (now that #159 and #161 are merged into dev):

  1. Rebase on dev: This PR currently has merge conflicts with [SM100] Mega MoE: optimize pre-dispatch with 1-group/thread NVFP4 mapping and PTX e2m1x2 #159 (common/math.cuh) and [SM103] Mega MoE: add native K=96 Ultra MMA mainloop for NVFP4 on Blackwell Ultra #161 (heuristics/mega_moe.hpp, sm100_fp8_fp4_mega_moe.cuh). Note that [SM103] Mega MoE: add native K=96 Ultra MMA mainloop for NVFP4 on Blackwell Ultra #161 already added the i == 0 or i * kNumUTCCPAlignedElems < task_info.valid_m guard inside utccp_sf_chunk for the SM103 K=96 path, so please keep the corresponding guard in the K=64 loop using sfa_tmem_col when resolving the conflict.
  2. Edge-case predicate in get_nvfp4_sf_and_sf_inv (common/math.cuh): Previously sf.x > 0.0f ? __frcp_rn(sf.x) : 0.0f returned 0.0f for NaN (0x7f) or -0.0 (0x80). Switching the guard to qx.__x ? rx : 0.0f treats 0x7f/0x80 as nonzero (e.g. fast_rcp(-0.0f) = -inf -> NaN in the NR step). Even though amax is normally finite non-negative, guarding on sf.x > 0.0f (or (qx.__x & 0x7fu) != 0 && qx.__x != 0x7fu) preserves exact fallback semantics.
  3. Minor comment nit (sf_smem reuse of amax_reduction): Reusing shared_storage.amax_reduction[epilogue_wg_idx * 4] for NVFP4 SF coalescing is race-free because amax_reduction is not written when kIsNVFP4 before the ptx::sync_aligned(128, ...) at the start of the quantize stage (so warps 1..3 wait for warp 0 before overwriting sf_smem on the next s iteration). Adding a one-line comment noting that barrier invariant would help future readers avoid breaking it if the barrier placement ever changes.

@Denji-kk
Denji-kk force-pushed the perf/sm100-mega-moe-fp4-dispatch-combine-epilogue branch from b5ba377 to 357f34b Compare November 23, 2026 08:44
@Denji-kk

Copy link
Copy Markdown
Author

Thanks for the review! Rebased onto latest dev and addressed all three points (sfa_tmem_col in the K=64 loop, sf.x > 0.0f / sf.y > 0.0f guard in get_nvfp4_sf_and_sf_inv, and the amax_reduction barrier invariant comment).

@zcnrex
zcnrex merged commit a96d065 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