Skip to content

[SM100] Mega MoE: batch the dispatch counters and let every SM wait on the NVLink barrier - #147

Merged
zcnrex merged 4 commits into
sgl-project:devfrom
zcnrex:chunan/megamoe-dep32-decode
Nov 11, 2026
Merged

zcnrex merged 4 commits into
sgl-project:devfrom
zcnrex:chunan/megamoe-dep32-decode

Conversation

@zcnrex

@zcnrex zcnrex commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

At decode sizes the fp8xfp4 Mega MoE kernel is a chain of memory round trips, and three of them sit in the dispatch prologue before any token can be pulled:

  • Every SM does one atom.global.add.u64 per expert on the send counters, including experts it sends nothing to. A dispatch thread issues them one at a time, each waiting for the previous result. With 448 experts that is 3.5 serial round trips per thread on counters all 210 SMs contend for.
  • SM 0's count push reads the totals back the same way: load, two remote stores, next load.
  • After the cross-rank barrier, only SM 0 has seen the signal. The other SMs learn of it through a trailing grid sync, which adds an atomic round trip and a poll after SM 0's own poll.

Change

  • Batched counters. A dispatch thread issues all of its send-count atomics, then stores the results. SM 0 issues all of its count-push loads, then the remote stores.

  • Counters only for SMs with tokens. An SM with no tokens for an expert skips that expert's atomic. The upper half of the counter (SM arrivals) is no longer accumulated; SM 0 writes kNumSMs into the status it pushes, after the grid sync that already orders it behind every SM. The receiver's check (kNumSMs * kNumRanks) is unchanged.

  • Every SM waits on the barrier signal. nvlink_barrier takes an optional known_counter: thread 0's copy of the barrier counter, read at the start of the dispatch role and incremented locally per barrier. With it, the non-zero SMs poll the signal word themselves and the trailing grid sync becomes a plain CTA barrier. The copy is read before the CTA's first grid sync, and SM 0 only advances the counter after that grid sync, so the phase and sign cannot be stale. Callers that do not pass it keep the old behaviour (sm100_bf16_mega_moe, sm90_fp8_mega_moe).

  • Combine waits per slot. The combine waited for the ranks of all of a token's experts before issuing its first load. Now each slot waits for its own rank right before its load, so the shared slot and the slots of ranks that are already done load while the rest are awaited. Slot order is unchanged. The post-expert-norm path keeps the single wait, since it reads every slot's sum first.

Outputs are bitwise identical to dev.

Benchmark

Setup:

  • fp8_fp4_mega_moe on Vera Rubin (CC 10.7), 448 experts, top-6, 1 shared expert, hidden 5120, intermediate 2304, K=64 MMA and SIMT pull as SGLang runs it.
  • 16 calls per round under kineto, 3 reps, ABBA over 4 rounds (DEP8: 2 rounds), 8 routings, 4 weight sets.
  • Metric: per-call kernel time of the last-starting rank (min over ranks), this PR / dev.
ranks x experts per rank 32 tok/rank 256 tok/rank 1024 tok/rank
32 x 14 (8 nodes) 0.940
16 x 28 (4 nodes) 0.952 0.949 0.978
8 x 56 (2 nodes) 0.967 (64 tok) 0.961 (1000 tok)
4 x 112 (1 node) 0.973 0.980 0.983
2 x 224 0.981 0.988 0.995

Each change measured on its own at 32 ranks, 256 tokens per rank: batched counters 0.971, plus the barrier 0.949, plus the token-owning counters 0.940.

End to end: DeepSeek-V4.1-Flash on SGLang, DEP32 decode, 64 requests per rank at 128K context, ABBA with two runs per arm: decode step 0.989 of dev.

The per-slot combine wait on top of the rest (this PR / previous commit), 32 ranks: 0.997 at 64 and 256 tokens per rank, 0.982 at 896; 16 ranks: 0.996 at 256, 0.987 at 1024. End to end at DEP32 with 224 requests per rank, ABBA with two runs per arm: decode step 0.995; at 64 requests per rank 1.000.

Tried and left out: a grid sync that arrives with red.release instead of atom.release (1.003 of the batched version), and red instead of skipping for the zero-count experts (1.009). Also measured at 32 ranks, 256 tokens per rank, relative to this PR without the combine change:

  • Cleanup barrier without its grid sync (SM 0 clears everything peers write) plus the count push overlapped with the source-index grid sync: 0.992 in the rig, 1.000 end to end, 1.006 at 16 ranks with 32 tokens per rank.
  • The SM that arrives last at the epilogue grid sync notifies the peers instead of SM 0: 1.002.
  • Per-rank counts of all local experts loaded beside the count fetch in the pull: 1.038.
  • SM 0 takes the barrier phase from the local counter copy: 1.003.
  • One L1 warm-up wave in the scheduler: 1.194; three: 0.997.
  • Doses, not candidates: pull fence on one lane 1.001; pull loading a tenth of the token bytes 0.980.

Tests

  • Bitwise against dev in the multi-rank rig at 1, 7, 64, 256 and 1000 tokens per rank with 112 and 448 experts on 8 ranks, and at 32, 256 and 1024 tokens per rank on 2, 4, 16 and 32 ranks.
  • The combine change: bitwise against the previous commit at 64, 256 and 896 tokens per rank on 32 ranks and at 32, 256 and 1024 on 16 ranks. Its post-expert-norm branch is unchanged code and was not run.
  • tests/test_mega_moe.py needs DeepEP for its correctness path and was not run.
  • Only measured on CC 10.7. Not run on B200 / GB300.

🤖 Generated with Claude Code

zcnrex and others added 4 commits November 10, 2026 17:07
…n the NVLink barrier

Dispatch prologue of the fp8xfp4 kernel:
- a dispatch thread issues all of its per-expert send-count atomics before
  it stores any result, and SM 0 does the same with its count-push loads
- an SM without tokens for an expert no longer touches that expert's
  counter; SM 0 writes the arrival count into the status it pushes
- nvlink_barrier takes thread 0's copy of the barrier counter; with it every
  SM polls the signal word and the trailing grid sync is skipped

Outputs are bitwise identical.

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

- nvlink_barrier decodes the status once and uses the same wait loop for
  SM 0 and the SMs that wait on the signal word; the counter copy is const
  and only passed to the barrier that reads it
- the dispatch prologue's per-thread expert loops go through one helper and
  keep the send status in 32 bits

Outputs are bitwise identical.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
… check the counter copy

- nvlink_barrier asserts on SM 0 that the caller's counter copy decodes to
  the same signal word as the live counter, and its timeout print names the SM
- comments state what the all-SM wait relies on: the next barrier's prologue
  grid sync, and the grid sync before the count push
- the combine's two waits share one timeout print

Outputs are bitwise identical.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@zcnrex
zcnrex marked this pull request as ready for review November 11, 2026 20:51
@zcnrex
zcnrex merged commit 75cfbcd into sgl-project:dev Nov 11, 2026
@zcnrex
zcnrex deleted the chunan/megamoe-dep32-decode branch November 11, 2026 21:13
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