Skip to content

[SM100] Mega MoE: one-rank FP8xFP4 fast paths for die-local weights, with opt-in bitwise parity with trtllm-gen - #166

Open
aryatsc wants to merge 17 commits into
sgl-project:devfrom
aryatsc:arya/megamoe-one-rank-die-aware-dev
Open

aryatsc wants to merge 17 commits into
sgl-project:devfrom
aryatsc:arya/megamoe-one-rank-die-aware-dev

Conversation

@aryatsc

@aryatsc aryatsc commented Nov 25, 2026 •

Copy link
Copy Markdown

Motivation

Die locality (two-die GPUs). This is the kernel half of a die-locality change for Vera Rubin VR200, where each GPU has two dies and a read from the other die's memory crosses the die-to-die link. The numbers below are on weights homed per die with the existing deep_gemm.localize. That placement is worth 8.1 to 21.5 points of the call in time and 19.3 to 22.5 in net energy ("ordinary pages" columns), and without it this branch beats tuned trtllm-gen only at 235 rows. The PR does not change localize; it adds the one-rank path and numerics a serving engine needs to use it. The SGLang half, which places the weights, is sgl-project/sglang#52972.

With EP8, each rank of Kimi K3.1 owns 112 whole routed experts, so fp8_fp4_mega_moe can run on a one-rank group: no peer, no NVLink barrier, no global count (sgl-project/sglang#52972 does this for speculative-verify calls). This PR adds what dev lacks for that use: opt-in numerics that are bitwise equal to FlashInfer's trtllm-gen MoE, a low fixed cost for a group without peers, and a serving-shaped interface (packed top-k words, a small buffer, trtllm-gen's weight layout).

On one Vera Rubin VR200 GPU (CC 10.7) at the K3.1 EP8 shape, with deep_gemm.localized weights, this branch is 1.7 % (5 rows) to 23.6 % (235 rows) faster per call than trtllm-gen with a tuned tactic table and uses 12.5 to 19.9 % less net energy, with 0 differing output elements (3 processes). Everything new is off by default.

Changes

Nine code commits; the later commits only fold and trim tests and comments:

  1. numerics='trtllm': rounding placed as in trtllm-gen; split4_combine_max_tokens=N for its four-accumulator combine. Opt-in, FP8xFP4 SiTU, routed experts.
  2. Prefix-sum and ballot lookup of a pulled token's expert, instead of walking the experts. Any rank count.
  3. A one-rank group only needs its grid sync: no pull barrier, clean barrier, per-rank count load or combine-ready word.
  4. Batched expert count loads in the shared scheduler (every rank count); one-rank groups count from topk_idx instead of the global count protocol.
  5. On calls that take the L1-first order, the scheduler checks a task's inputs before publishing it, so load warps start without polling. One-rank groups take that order whenever the M blocks fit in the ring. FP8xFP4 only.
  6. 8-row activation tiles for blocks of at most 16 valid rows (mostly an energy change).
  7. topk_format='packed': int32 words (expert id << 16) | bf16(weight), which removes the caller's staging launch. One-rank groups.
  8. SymmBuffer(exact_num_max_tokens=True, max_block_m=N): a buffer sized for the calls it will see (256 rows instead of 1,920).
  9. weight_layout='row_shuffled' (alias 'trtllm') for routed FP8xFP4 SiTU experts on SM107, so one weight copy serves this kernel and trtllm-gen.

Relation to upstream: the branch is rebased over #160, #161 and #162 (commit 4 counts before #162's slot sort and keeps #161's union; commit 9 leaves #161's SM103 path alone), multi-rank groups keep #157's L1-first order and cap, which commit 5 builds on, and the open #156 overlaps commits 3 and 4, so if it lands first commit 3 shrinks and the one-rank half of commit 4 has to be re-derived.

Merged with dev on 2026-11-28 (two merge commits, up to 6840aa7: #170, #165, #169, #178, #171 and the 26/11/26 release sync #177). The files this PR touches are now csrc/apis/mega_moe_inference.hpp, csrc/jit_kernels/impls/sm100_fp8_fp4_mega_moe_inference.hpp, deep_gemm/include/deep_gemm/impls/sm100_fp8_fp4_mega_moe_inference.cuh and deep_gemm/include/deep_gemm/scheduler/mega_moe/inference.cuh; the Python names are unchanged. #165: commit 9's layout now asserts the packed K=64 mainloop (SM107PackedK64), which is chosen from the JIT arch; the K=128 FP4 mainloop does not apply to FP8xFP4. #170: its LPT ordering runs after commit 4's count loads. One commit follows the merges: the one-rank paths (scheduler-side input check, one-rank combine) and the split-4 combine skip waits after which the release added fence.proxy.async.global, so they now issue the same fence before their reads. Measured cost of that fence: within noise at 5 and 235 rows, 0.1 to 0.3 point of call time at 80 rows. After the merges: the three new test files pass at 1 and 2 processes, outputs stay bitwise equal to trtllm-gen (42 of 42 cells on each of two GPUs), and time against trtllm-gen is within 0.5 point of the pre-merge head at 5, 80 and 235 rows. The table below was measured before the merges, against dev 5121415; dev 12be8d3 measured the same within 0.3 point, and 6840aa7 was not re-measured as a baseline.

Existing callers: outputs are unchanged at the defaults, but commits 2, 4 (batched loads), 5 and 6 run on existing FP8xFP4 calls, and the BF16 kernel shares the headers touched by commits 3 and 4.

Performance

One VR200 GPU, dev 5121415 against this branch, FlashInfer 0.7.4. One rank of 112 experts, hidden 3,584, intermediate 3,072, top-16, real weights of one layer and captured routing. One CUDA graph of 8 MoE layers including routing, with a dense GEMM after each layer (subtracted). The reference is trtllm-gen with a tactic table from a live engine's autotune cache. Medians of 3 processes; ranges are within 0.2 points of the median in time and 0.6 in energy. Percent against tuned trtllm-gen, negative is better.

Rows trtllm-gen us Time: dev + localize Time: this branch Time: this branch, ordinary pages Energy: dev + localize Energy: this branch Energy: this branch, ordinary pages
5 27.3 +38.0 -1.7 +6.4 -9.8 -12.5 +6.8
10 34.4 +22.7 -7.5 +6.6 -10.0 -12.6 +7.9
20 47.8 +12.2 -12.0 +6.6 -7.2 -13.7 +8.8
40 63.4 +2.0 -16.4 +3.1 -9.2 -15.2 +6.8
80 76.1 -0.8 -15.1 +6.4 -9.8 -15.2 +6.1
160 100.9 -9.9 -19.9 +0.5 -11.3 -17.9 +3.2
235 113.3 -13.6 -23.6 -4.8 -13.7 -19.9 -0.5
  • Locality term. "Ordinary pages" is the same kernel with localize replaced by the identity. Localized weights are worth 8.1 to 21.5 points of the trtllm-gen call in time and 19.3 to 22.5 in net energy. Without localize this branch beats tuned trtllm-gen only at 235 rows.
  • Against dev + localize, measured directly: both builds in one process, alternating per round (3 processes, 12 rounds per row count). This branch is 29.1 / 24.2 / 22.0 / 17.9 / 15.2 / 11.3 / 12.7 % faster at 5 / 10 / 20 / 40 / 80 / 160 / 235 rows. Net energy is +0.8 / +1.3 / -3.1 / -3.7 / -4.4 / -5.8 / -6.2 %, so most of the energy saving against trtllm-gen is already in dev.
  • dev itself depends on localize as much: on ordinary pages it is 8.1 to 22.5 points slower and uses 18.3 to 21.1 points more net energy, and is slower than tuned trtllm-gen at every row count (+46.1 % at 5 rows to +6.5 % at 235).
  • Launch count. The dev arm runs SGLang's stock routing, a staging launch and MegaMoE; this branch runs one routing launch that writes packed words, then MegaMoE (commit 7). About 6 us per call of the lead over dev is that one fewer launch, not kernel time.
  • Row-shuffled weights with the 256-row buffer (one weight copy) cost 0.6 to 2.8 % in time and 6.6 to 13.5 % in energy against the MegaMoE layout. In that form localize is worth 7.6 to 19.7 points in time and 13.4 to 16.7 in net energy.
  • Earlier form. The same pieces were first measured on v0.2.8. That build is 3.4 points ahead of this branch at 5 rows and 1.6 to 2.2 points ahead at 10 to 235 rows. The difference is not explained.
  • In SGLang ([Perf] [NVIDIA] kimi-k3: Add an opt-in die-local routed MoE for DFlash target verify on two-die GPUs sglang#52972, Kimi K3.1, TP8, speculative verify, two boots each): 19.97 and 19.93 -> 19.14 and 19.14 ms, and 25.88 and 25.85 -> 24.63 and 24.64 ms per verify round at 80 / 160 rows.

Correctness

  • Bitwise equal to trtllm-gen with numerics='trtllm' and split4_combine_max_tokens=84: 0 differing output elements in every timed cell (42: 7 row counts, 2 harnesses, 3 processes), for localized weights, ordinary pages, and the row-shuffled layout with the 256-row buffer.
  • Default path identical to dev: with every option at its default, output sha256 matches dev at nine sizes (1, 2, 8, 32, 84, 85, 128, 256, 512 tokens).
  • split4_combine_max_tokens makes a row's bits depend on whether the call is above or below N. The 84-token threshold is where FlashInfer 0.7.4's finalize was observed to change its summation order, not a documented contract.

Tests

The kernel code of the posted head was re-run on 2026-11-26 (commit 1c34bbc; the commits after it only remove two test files, one duplicate test suite and three comments): the timing table above reproduces within 0.1 point in time, and the lists below pass.

Tests are in sgl_deep_gemm/tests, registered in run_tests.sh.

  • New test files, three: test_mega_moe_trtllm_numerics.py (commit 1), test_mega_moe_invariants.py (commits 2 to 6, one suite each) and test_mega_moe_packed_topk.py (commit 7). All pass on one VR200 GPU, and the first two at two processes. Commits 8 and 9 have no test file of their own; they are exercised by the bitwise timed cells above. Six existing MegaMoE test files (_situ, _nvfp4_trtllm, _bf16_trtllm and others) pass as on dev. Six existing MegaMoE test files (_situ, _nvfp4_trtllm, _bf16_trtllm and others) pass as on dev.
  • Two ranks (2 processes on 2 GPUs): _trtllm_numerics and _invariants pass; _global_tokens, _nvfp4_alphas and the BF16 one-rank _bf16_trtllm pass on this branch and on dev.
  • Not run: more than 2 ranks; test_mega_moe.py and test_mega_moe_sgl.py (they need DeepEP reference operators the image lacks; the latter fails the same way on dev); the non-MoE tests; the sanitizer.

Limits

  • 1.6 to 2.2 points behind the earlier v0.2.8-based form at 10 to 235 rows (3.4 at 5 rows), unexplained.
  • Multi-rank groups: correctness tests on 2 ranks only, no multi-rank timing; [SM100] Mega MoE: all L1 tasks first when few L1 waves are left, shared L2 at the tail for large tiles #157's benchmark was not repeated.
  • One device and one shape (VR200; 112 experts, intermediate 3,072, the K3.1 EP8 rank). EP16 and EP32 rank shapes were not run. Not run: SM100, SM103, single-die devices, shared experts, post-expert norm, FP8 combine. Commit 9 asserts the packed K=64 mainloop (SM107PackedK64).
  • The uncapped L1-first order for one-rank groups was only A/B-tested on a build before a later fix, not on the final build.
  • Shapes where the earlier form lost to tuned trtllm-gen (v0.2.8, not re-measured): 448 experts x intermediate 768 (+11.2 % at 5 rows), one token on a loaded rank (+8.4 %), prefill-sized calls (+38.5 % at 16,384 rows).
  • Still to add: unit tests of the row-shuffled layout and the compact buffer (today the evidence is the timed arm), a one-rank test past [SM100] Mega MoE: all L1 tasks first when few L1 waves are left, shared L2 at the tail for large tiles #157's cap, a full run_tests.sh run on 2 or more GPUs.

🤖 Generated with Claude Code

aryatsc and others added 10 commits November 24, 2026 20:41
…plit-4 combine threshold

numerics='trtllm': the top-k weight is applied at the combine, SiTU runs on
the FP32 accumulators, and the SiTU gate is literal.
split4_combine_max_tokens=N: for calls of at most N tokens a token's top-k
rows are summed in four interleaved accumulators. Both are opt-in per call
and reject blockwise-128 and broadcast_y.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…pert

The dispatch pull finds a token's expert with one prefix sum and a ballot
instead of walking the experts. Any rank count.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
For kNumRanks == 1 the NVLink barrier is the grid sync; there is no pull
barrier, no clean barrier, no per-rank count load and no combine-ready word.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…xpert tokens locally

The scheduler issues all expert count loads before the first check and
computes pool block offsets once per call (any rank count). One-rank groups
count expert tokens from topk_idx locally instead of through the global
count protocol.

The count loader keeps one pending bit per 32 experts, so it carries a
static assert for at most 1024 experts per rank. Counts are taken in
physical expert order before the LPT slot sort; the pull looks a slot's
source tokens up by its physical expert id. The one-rank flags sit behind
both members of the count / send-buffer union, where the pull cannot
overwrite them.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…t when all L1 tasks go first

When every L1 task of a call is issued before its first L2 task (the
L1-first order of sgl-project#157, which implies no ring reuse), the scheduler warp
waits for a task's inputs before publishing it: an L1 task for its block's
tokens, an L2 task for every L1 N block of its pool block. The load warps
then start their TMA copies without polling. A checked task carries
kDepsCheckedBit in m_block_idx. Opt-in per kernel (check_deps); only the
FP8xFP4 kernel opts in, the BF16 kernel is unchanged.

The task order is the scheduler's own (own-domain L1 tasks first, same steal
rule). A kernel can also opt in to the L1-first order whenever the M blocks
fit in the ring, past the cap on extra L1 waves (l1_first_whenever_fits);
the FP8xFP4 kernel does so for one-rank groups only, where the checked
order measures faster at 80 rows and above. Multi-rank groups keep the cap.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
A block of at most 16 valid rows loads an 8-row activation box per CTA
instead of the full box. Two new tensor-map arguments.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…FP8xFP4 groups

topk_format='packed': int32 words (expert id << 16) | bf16(weight) at the
start of topk_idx, ids counted from topk_first_expert, so a router can write
its output straight into the buffer.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
… symmetric buffer

SymmBuffer(exact_num_max_tokens=True) keeps num_max_tokens_per_rank as given
(a multiple of 128) instead of aligning it up to 1920 rows, and
max_block_m=N sizes the token and SF rings for M tiles of at most N rows
instead of every candidate tile. A call on such a buffer that would pick a
larger tile is rejected on the host. Both default to the current behaviour;
a decode-only group can allocate, for example, 256 rows with max_block_m=64.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
… SiTU experts on SM107

weight_layout='row_shuffled' ('trtllm') is accepted for FP8xFP4 with packed
FP4 weights and the SM107 K=64 MMA: the kernel reads the caller's weight and
scale tensors directly, one 128x128 weight tile and one scale slab per
stage. L2's 32-row shuffle is undone by the descriptors; L1's 16-channel
order is undone on the FP8 output of the L1 epilogue. Routed experts only,
hidden and intermediate sizes in multiples of 128; any expert and rank count.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…te tests

test_mega_moe_compact_buffer.py: SymmBuffer(exact_num_max_tokens=True,
max_block_m=64) keeps its row count, is smaller than the default buffer,
gives bitwise equal outputs and rejects a call that picks a larger tile.
test_mega_moe_fp8_fp4_row_shuffled_gates.py: weight_layout='row_shuffled'
for FP8xFP4 is rejected without SiTU and with shared experts.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@aryatsc
aryatsc marked this pull request as ready for review November 25, 2026 07:13
@aryatsc aryatsc changed the title [SM100] Mega MoE: one-rank FP8xFP4 fast paths and opt-in bitwise parity with trtllm-gen [SM100] Mega MoE: one-rank FP8xFP4 fast paths for die-local weights, with opt-in bitwise parity with trtllm-gen Nov 25, 2026
aryatsc and others added 4 commits November 25, 2026 13:46
… file

The seven files shared one driver and differed only in group shapes and
call sizes. test_mega_moe_invariants.py keeps every shape and size as a
named suite (--suite to run one); suites that ran with two ranks still do.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
zcnrex added a commit that referenced this pull request Nov 27, 2026
- Add SM10xArch, MegaMoEMainloop, is_mega_moe_mainloop_valid (including
  num_stages >= 3 for SM103UltraK96), and uses_packed_fp4_weights in
  types.cuh; keep get_compiled_sm10x_arch() device-only.
- Select the host SM10x arch from the JIT target (get_sm10x_arch()) and
  remove unused SM103ArchSpec.
- Consolidate per-arch MegaMoE mainloop, store-tile override, SIMT/TMA
  dispatch pull, and SMEM budget into get_mega_moe_arch_policy(), and
  fall back to Default if the resulting pipeline has too few stages.
- Add 5- or 6-int DG_MEGA_MOE_TILES support and include fp4_weights,
  gran_k, and mainloop in the DG_PRINT_CONFIGS dedup key.
- Add constexpr make_mega_moe_mainloop_plan() and remove unreachable
  SM103 BM=240 compact K=96 ring.
- Add SM107 FP4 (NVFP4/MXFP4) K=128 mainloop (2 MMAs per BLOCK_K=256
  stage, no Default tail), SM100_MMA_MXF4NVF4_BLOCK32_2x1SM_SS, and
  576-col Allocator2SmExclusive for BM=240.
- Drop redundant RowShuffled/K64 asserts already implied by RowShuffled
  requiring NVFP4 (avoiding unnecessary conflict with #166).
zcnrex added a commit that referenced this pull request Nov 27, 2026
- Add SM10xArch, MegaMoEMainloop, is_mega_moe_mainloop_valid (including
  num_stages >= 3 for SM103UltraK96), and uses_packed_fp4_weights in
  types.cuh; keep get_compiled_sm10x_arch() device-only.
- Select the host SM10x arch from the JIT target (get_sm10x_arch()) and
  remove unused SM103ArchSpec.
- Consolidate per-arch MegaMoE mainloop, store-tile override, SIMT/TMA
  dispatch pull, and SMEM budget into get_mega_moe_arch_policy(), and
  fall back to Default if the resulting pipeline has too few stages.
- Add 5- or 6-int DG_MEGA_MOE_TILES support and include fp4_weights,
  gran_k, and mainloop in the DG_PRINT_CONFIGS dedup key.
- Add constexpr make_mega_moe_mainloop_plan() and remove unreachable
  SM103 BM=240 compact K=96 ring.
- Add SM107 FP4 (NVFP4/MXFP4) K=128 mainloop (2 MMAs per BLOCK_K=256
  stage, no Default tail), SM100_MMA_MXF4NVF4_BLOCK32_2x1SM_SS, and
  576-col Allocator2SmExclusive for BM=240.
- Drop redundant RowShuffled/K64 asserts already implied by RowShuffled
  requiring NVFP4 (avoiding unnecessary conflict with #166).
Brings in sgl-project#170, sgl-project#165, sgl-project#169, sgl-project#178 and sgl-project#171.

Conflicts:
- csrc/jit_kernels/impls/sm100_fp8_fp4_mega_moe.hpp: the K=64 switch is now
  `uses_packed_fp4_weights(config.mainloop)` (sgl-project#165); the row-shuffled FP8 x FP4
  layout asserts that mainloop instead of `config.use_sm107_k64`. The kernel
  template takes the mainloop in place of the two K=64 / K=96 flags, followed by
  this branch's four parameters.
- deep_gemm/include/deep_gemm/impls/sm100_fp8_fp4_mega_moe.cuh: sgl-project#165's mainloop
  plan and validity check replace the K=64 static assert; the base MMA loop is
  taken from dev (now under `run_base_loop`) with this branch's SFB word order
  for row-shuffled packed FP4 weights re-applied in it.
Brings in sgl-project#177 (deepseek-ai public release 26/11/26, deepseek-ai#525) and deepseek-ai#478.

No textual conflicts: this branch's changes follow the renames
(csrc/apis/mega_moe.hpp -> mega_moe_inference.hpp,
impls/sm100_fp8_fp4_mega_moe.{hpp,cuh} -> ..._inference.{hpp,cuh},
scheduler/mega_moe.cuh -> scheduler/mega_moe/inference.cuh).
…d split-4 paths

The 26/11/26 release fences (`fence.proxy.async.global`) after the load warp's
and the combine warp's waits for data written by other CTAs or ranks. Three
paths of this branch skip those waits and so skipped the fence after the merge:

- a task whose inputs the scheduler already checked (`kDepsCheckedBit`): the
  load warp fences before its TMA loads;
- the combine of a one-rank group, which has no peer wait: one fence before the
  first row read;
- the split-4 combine: a fence after its own peer wait.
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.

1 participant