Skip to content

[SM100] Mega MoE: port upstream #525 features (barrier-free dispatch, partial-row loads, batched pull, RMSNorm/FP32 out) - #179

Merged
zcnrex merged 19 commits into
devfrom
chunan/megamoe-upstream-features
Dec 2, 2026
Merged

zcnrex merged 19 commits into
devfrom
chunan/megamoe-upstream-features

Conversation

@zcnrex

@zcnrex zcnrex commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Based on dev (merged with dev at c618b326, including #177, #182/#184 PowLU, and #168 in-combine post-expert norm). Ports the Mega MoE features and fixes from upstream deepseek-ai/DeepGEMM#525 that #177 did not take onto dev's kernels (NVFP4, SIMT pull, full-pool path, post-expert norm, PowLU, SiTU, broadcast y, LPT scheduler). Each feature was measured on its own before the next one was added; features that regressed perf were fixed or dropped.

Commits

  1. Barrier-free dispatch (FP8/FP4 and BF16 kernels, which share the workspace).
    • The last SM to arrive publishes the expert counts. This replaces the dispatch grid sync and the pre-pull NVLink barrier.
    • Source slots hold token_topk_idx + 1. The pull polls them, and cleanup zeroes them.
    • Combine and output readiness are -1/+1 counters, so graph replays need no reset. They replace the grid-index fields at the same workspace offset.
  2. Partial-row activation loads. Routed L1/L2 activation TMA loads use tensor maps sized to the block's UMMA-aligned valid rows (8-row steps). Shared-expert blocks keep full-row loads.
  3. L1 tail advance. Upstream's scheduler tweak, applied only where dev's L1-first does not.
  4. Batched TMA pull. Several whole tokens per warp in one transaction, one token per lane. Never used for the SIMT pull (the SM107 NVFP4 default), the full-pool path, or BF16. Commit 9 makes it opt-in (DG_MEGA_MOE_PULL_TOKENS=<n>).
  5. Post-combine RMSNorm and FP32 output on fp8_fp4_mega_moe (rms_weights, rms_eps, FP32 y, also forwarded in nvfp4_mega_moe).
    • The norm is computed in FP32, over the BF16-rounded output when y is BF16.
    • Two-chunk tokens use adjacent warps that exchange partial sums in smem.
    • FP32 y with broadcast y is rejected.
  6. Per-rank arrival word on the count path. Per-rank counts carry an arrival word (1ull << 32), the arrival counter uses atom.acq_rel.gpu, and the scheduler polls sums relaxed.
  7. Send counts loaded before the publish loop. Hoists per-expert send-count loads into registers before remote stores, eliminating 2 × E/32 serialized round trips on the critical path (−1.5–2% on EP4).
  8. Balanced expert blocks dropped. dev already sizes UMMA N by valid rows, so balancing saved no MMA work and cost 1.6% at NVFP4 T=4096 and 0.3–0.7% elsewhere. Partial-row loads alone are a 1.2–1.6% win and stay.
  9. Batched pull made opt-in (DG_MEGA_MOE_PULL_TOKENS=<n>). Measured on B300 below: it cost 2–3% at EP4 decode. Default compiles the single-token pull path.
  10. Review fixes & RMSNorm/FP32 test coverage. Requires ring_covers_full_pool when num_pull_tokens > 1 (prevents a batch from deadlocking on its own unpublished ring slot), adds rms_weights device & alignment checks, tightens the rms_sum_sq smem assert to kNumChunks == 2, and ports --rms-norm / --output-dtype fp32 checks to tests/test_mega_moe_inference.py.
  11. Coalesced expert_recv_count, parallelized source-slot cleanup, and scheduler fixes (from upstream #525).
    • Transposes expert_recv_count to expert_idx * num_ranks + rank_idx so per-expert count loads are coalesced across ranks.
    • Flattens source-slot and count cleanup across all (kNumSMs - 1) * kNumDispatchWarps warps.
    • Ports cute::elect_one_leader_sync() in get_next_local_task_idx and upstream's sentinel tail-drain ordering.
  12. Faster SwiGLU PTX primitives (from upstream #525, FP8/FP4 and BF16 kernels). Uses ptx::clamp_bf16x2_no_nan, ptx::cvt_bf16x2_to_fp32x2, and math::fast_exp_ftz. Bitwise identical; −4.6% on 1-rank NVFP4 T=4096 and −1.1% on EP4 NVFP4.
  13. Streaming direct global combine stores & SMEM-cached RMSNorm weights (from upstream #525). Drops the combine TMA store buffer (kNumChunkSlots 3 → 2), writes BF16 y directly via ptx::st_evict_first (and direct register broadcast for kBroadcastY), caches FP32 rms_weights in the freed smem, and uses 4-accumulator RMSNorm ILP.
  14. 0-token cleanup hang fix (from upstream #525). Hoists dispatch warps' scheduler.fetch_expert_recv_count() before kDispatchWithEpilogueBarrierIdx in both FP8/FP4 and BF16 kernels so fast SMs cannot clear expert_recv_count_sum before slower SMs' dispatch warps read it.
  15. SMEM/mainloop-aware pull-token cap, shared-buffer slot check, and release-acquire on count poll.
    • Caps DG_MEGA_MOE_PULL_TOKENS to preserve at least min_stages pipeline stages (3 for SM103UltraK96, 2 otherwise).
    • Adds SymmBuffer(base=...) extent check (num_experts * num_max_tokens_per_rank) on SM100+ (verified compatible with all sglang base= callers).
    • Adds a single ptx::fence_acq_rel_sys() on the last SM before count publish paired with ptx::ld_acq_sys + __syncwarp() on the coalesced once-per-expert count poll.
  16. Merged with dev (#168, #172, #182/#184). Integrates #168's in-combine post-expert norm cleanly with kNumChunkSlots = 2 streaming combine (laying out smem_norm_w → smem_rms_weights → rms_sum_sq with no overlap and removing the dead wait_result_store TMA wait).

Not ported:

  • Upstream's L1 SF smem staging for FP8/MXFP4 output. Measured +3.5% at FP8 hidden 7168, 4096 tokens/rank, and no gain elsewhere. dev's NVFP4 path already stores its SFs coalesced.
  • TMEM 16dp256b2x loads and removal of dev's BM240 epilogue prefetch.
  • Fixed 128 MiB source_token_topk_idx reservation inside MegaMoESignals (guarded instead by the SymmBuffer(base=...) extent check).
  • Upstream's deep_gemm/mega_moe Python package and heuristics (dev keeps its own).

Validation (SM107 / VR, 4 GPUs)

Method. Every comparison runs both arms on the same node in ABBA order: 6 reps of 5-run medians for EP4, 3 reps for 1-rank. A row is flagged when arm/base > 1.02 and more than twice the A/A spread. Geo is the arm/base geomean; lower is faster.

Correctness.

  • Cross-arm output dumps (fp8, nvfp4, post-expert norm, broadcast_y, masked, graph replay, graph replay + broadcast_y) are bitwise identical to dev: 56/56.
  • RMSNorm (rms_fp8, rms_nvfp4): FP32 y rounded to BF16 matches BF16 y bitwise on every iteration (0 mismatches); relative error vs FP32 reference is 3.88e-3 for BF16 y and 3.3e-7–3.8e-7 for FP32 y.
  • Post-expert norm (test_mega_moe.py --post-expert-norm at 9af6741a, job 7912): passed for no-gain, weight_first=True, and weight_first=False at H=4096 (T=64 and T=512, kPostNormInCombine), H=6144, and H=7168 (kFusedPostNormPartials).
  • All 91 cached JIT instantiations compile, plus pull-token, RMS, and FP32 variants.

Perf vs dev (#177 at 7f533845, jobs 7820 / 7827):

Row Geo (branch/base, job 7820 / 7827) Worst row (7827) Flagged
EP4 fp8xfp4 h7168 i2048, T 1–4096/rank 0.966 / 0.971 0.987 0
EP4 NVFP4 h4096 i4096 E256 top8, T 1–4096/rank 0.970 / 0.976 0.994 0
1-rank fp8xfp4 SiTU top16 masked decode T40 / T160 0.966 / 0.975 (7827: 0.978 / 0.972) 0.978 / 0.972 0
1-rank NVFP4 decode T32 / T128 1.003 / 0.996 (7827: 1.002 / 0.996) 1.008 / 1.000 0
1-rank NVFP4 prefill T4096 0.967 / 0.971 0.976 0

Per-step isolation (SM107 / VR):

Step Effect
1 (barrier-free dispatch) EP4 +1.7% / +2.1%, 1-rank +1.2%
6 (arrival word on per-rank counts) neutral
2 (with balancing) EP4 −0.7% / −0.1%
3 (L1 tail advance) neutral
7 (hoist send-count loads) EP4 −1.5% / −2.0%
8 (drop balanced blocks, keep partial-row loads) NVFP4 T=4096 −1.6%, rest −0.3% to −0.7%
10–11 (fixes + coalesced counts + parallel cleanup + scheduler, job 7817) neutral (0.998–1.002)
12 (SwiGLU PTX primitives, job 7818) 1-rank NVFP4 T=4096 −4.6%, EP4 NVFP4 −1.1%, EP4 FP8 −0.4%
13 (streaming combine + SMEM RMSNorm, job 7819) neutral (1.000–1.001)
15 (fence_acq_rel_sys + ld_acq_sys, job 7863 vs st_rel_sys job 7864) +1.1% FP8 / +1.3% NVFP4 (vs st_rel_sys: +16.0% / +15.1%, +19–20 µs at T=1)

Batched pull (commit 4) on B300

Setup.

  • NVFP4, hidden 4096, intermediate 4096, E256, top-8. Each token's activations are 2 KiB, so the batch is 2 tokens.
  • Arms: DG_MEGA_MOE_PULL_TOKENS=1 (the pre-batching path) vs upstream's batch size, in one build.
  • 3 off/on/on/off blocks per config. Each cell is the geomean of the 3 block ratios (on/off).
  • Controls: FP8 hidden 7168 and the 1-rank NVFP4 runs never batch, so they act as A/A. They stayed flat at T=4096 and within 2.1% (1-rank). Decode run-to-run spread on this box is 8–30%, so only the sign across blocks is used.
T/rank EP4 on/off EP8 on/off
1 1.073 0.914 (blocks 0.79–1.04)
8 1.030 0.952 (0.89–1.04)
32 1.019 1.003
128 1.022 0.938 (0.89–0.99)
512 1.034 0.997
4096 0.994 0.982 (3/3 blocks < 1)

Reading.

  • At EP4, T 1–512, all 15 blocks are ≥ 1.000.
  • The larger send buffer costs one pipeline stage at T ≤ 8 and at EP4 T=512. At EP4 T=32 and 128 the stage count is unchanged and it is still about 2% slower.
  • The only clear gain is EP8 prefill (−1.8%).
  • So commit 9 turns it off by default. B300 tests pass with the default and with DG_MEGA_MOE_PULL_TOKENS=2 (test_mega_moe.py at 8 ranks, hidden 2048, where it engages).

zcnrex and others added 8 commits November 28, 2026 04:44
…#525)

Drop the dispatch grid sync and the NVLink barrier before the pull. The last
SM to arrive publishes the expert counts (per-rank counts, a system fence,
then the sums), source slots hold token_topk_idx + 1 and are polled per slot
and cleared in the cleanup, and combine / broadcast readiness become counters
that each launch decrements and the peer increments, so graph replays need no
reset. The scheduler acquires the expert-count sums.

Unlike upstream, SMs still skip the send-count atomic for experts they send no
tokens to; one arrival counter replaces upstream's per-expert arrival bits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oads (port of upstream deepseek-ai#525)

Spread an expert's tokens evenly over its M blocks in 16-row steps (BLOCK_M >= 32),
so its last block is no longer a short tail, and count each block's padding at
its own last token. Routed L1/L2 activation loads use tensor maps sized to the
block's UMMA-aligned valid rows, so short blocks skip the padding rows. Shared
blocks keep full-row loads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… of upstream deepseek-ai#525)

Upstream advances up to kNumScheduleStages + 1 L1 waves when the remaining L1
work is within kNumScheduleStages waves of the warmup. Dev's L1-first rule
(#157) already covers most of that range; keep upstream's rule only where
L1-first does not apply.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eek-ai#525)

When a token fits one pull chunk, the TMA pull keeps one token per lane and
pulls a batch of them with one transaction: every token's load, then the
weight, x_scale and SF loads, then one wait and every token's store. Batch
sizes follow upstream's table (2 tokens at 2 KiB, 4 at 1 KiB, ...), not for the
SIMT pull, the one-shot full-pool pull or BF16. DG_MEGA_MOE_PULL_TOKENS
overrides the batch size.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… upstream deepseek-ai#525)

`fp8_fp4_mega_moe` takes `rms_weights` (in `y`'s dtype) and `rms_eps` to apply
an RMSNorm to the combined output, and `y` may be FP32. The norm is computed
in FP32, over the BF16-rounded output when `y` is BF16; a two-chunk token keeps
both chunks in adjacent warps of one CTA and exchanges the partial sums through
shared memory. FP32 `y` is stored directly from registers; BF16 keeps the TMA
store and the broadcast path (FP32 does not support `broadcast_y`).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each per-rank count now carries an arrival word, which the pull and the
cleanup wait for, so the last SM pushes counts and sums without a system
fence, the arrival is one acq_rel atomic instead of a fence plus an atomic,
and the scheduler polls the sums relaxed again. Only the fused post-expert
norm keeps an acq_rel system fence on both sides, for its zeroed sums.

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

Each expert's send-count load sat behind the previous expert's remote store in the publish loop.

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

Measured on SM107 EP4: balanced blocks cost 1.6% at 4096 NVFP4 tokens per rank and 0.3-0.7% elsewhere; the partial-row loads alone are a 1.2-1.6% win.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zcnrex
zcnrex changed the base branch from chunan/sync-upstream-261126 to dev November 29, 2026 01:18
zcnrex and others added 5 commits November 28, 2026 17:45
On B300 NVFP4 hidden 4096 the batched pull was 2-3% slower at EP4 decode
(T 1-512, every ABBA block), flat at EP4 prefill, and 1.8% faster only at
EP8 T=4096. DG_MEGA_MOE_PULL_TOKENS still enables it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zcnrex
zcnrex marked this pull request as ready for review December 2, 2026 23:57
@zcnrex
zcnrex merged commit 6697ec1 into dev Dec 2, 2026
@zcnrex
zcnrex deleted the chunan/megamoe-upstream-features branch December 2, 2026 23:58
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