Skip to content

[SM100] Mega MoE: dispatch without the NVLink barrier before the pull - #156

Closed
zcnrex wants to merge 4 commits into
devfrom
chunan/mega-moe-sentinel-dispatch
Closed

zcnrex wants to merge 4 commits into
devfrom
chunan/mega-moe-sentinel-dispatch

Conversation

@zcnrex

@zcnrex zcnrex commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Before pulling tokens, every FP8/FP4 Mega MoE call runs a cross-rank NVLink barrier (kBeforeDispatchPullBarrierTag). The barrier orders two things before the pull:

  • the plain remote stores of per-rank expert counts and source token-topk indices;
  • the combine-readiness protocol: each launch pushes its grid index to every peer and clears its own readiness words, and peers tag readiness with that index.

At decode sizes the barrier is a visible part of the call. The scheduler already polls the per-expert arrival sums, so the barrier only has to cover the counts and indices.

Change

sm100_fp8_fp4_mega_moe.cuh, plus the index-slot clear in sm100_bf16_mega_moe.cuh and a base check in deep_gemm/mega/__init__.py.

  • Sentinel values. Per-rank expert counts and source token-topk indices are stored as value + 1. The workspace starts zeroed, so 0 means "not arrived".
    • The pull warps poll each count and index word with ld.volatile and subtract 1.
    • An index slot is read once per call, so the reader clears it after reading.
    • Counts are cleared by the existing workspace cleanup. In sentinel mode the cleanup first waits for each count to arrive: the pull reads counts only for experts with tokens, so an empty expert's count store could otherwise land after the clear and be read as the next call's count.
  • Index slots are zero at every call's start. Every config clears a slot after reading it, including configs that keep the barrier and the BF16 kernel (one store per pulled token). A buffer shared between a barrier config and a sentinel config therefore never hands a stale index to the pull.
  • No barrier before the pull (kSentinelDispatch).
  • SM 0 clears the per-expert token sums after the end barrier's grid sync, not each SM in its cleanup. The pull barrier held every SM until SM 0 had pushed its counts. Without it, on a call where a rank receives no tokens, the other SMs can fetch the sums, find no work and clear them while SM 0 is still pushing counts and has not fetched them yet. SM 0 then spins forever (found on CC 10.7, below).
  • Per-call epoch for readiness. Combine readiness and broadcast-output readiness (broadcast_y) are tagged with a per-call epoch kept in grid-sync counter 3. SM 0 bumps it after the end barrier. Every rank makes the same calls, so all ranks agree on the tag. This replaces the grid-index push and the local readiness clears, which relied on the removed barrier. The epoch also keeps increasing across CUDA graph replays, where grid indices repeat.
  • Readiness tags set bit 63. The BF16 kernel still tags the same readiness words with grid index + 1 (and clears them to 0); bit 63 keeps the two namespaces apart when a buffer is shared through base.
  • SymmBuffer(base=...) requires the same index-slot extent on SM100. The slots span num_experts * num_max_tokens_per_rank words after the fixed MegaMoESignals and are followed by per-call metadata, so a config with a different extent would put that metadata in another config's slots. The check compares the aligned token counts (get_token_alignment_for_mega_moe() is 1920 on this B300), so configs that differ only in hidden size, intermediate size or MMA type still share.
  • Fused post-expert-norm partials keep the barrier (kSentinelDispatch = not kFusedPostNormPartials), because their reset is ordered by it. They also use the epoch tag.
  • The end-of-kernel barrier is unchanged.

Outputs are bitwise identical.

Tests

B300 SXM6 (148 SMs), 8 ranks on one node, CUDA 13.0, built with build_sgl_deep_gemm.sh. No localization (CUDA < 13.4). DeepSeek-V3 MoE shape: hidden 7168, intermediate 2048, 256 experts (32 per rank), top-8.

Race test. 160 back-to-back calls per arm with no host sync, random per-rank GPU sleeps, and 0..32 tokens per rank (every 7th call has 0 tokens on all ranks). Output compared bitwise against v0.2.9 (same kernel and host code as dev) on all 8 ranks:

MMA output mismatched calls
FP8xFP8 per-rank y 0 / 160 on 8/8 ranks
FP8xFP8 broadcast_y 0 / 160 on 8/8 ranks
FP8xFP4 per-rank y 0 / 160 on 8/8 ranks
FP8xFP8, FP8xFP4 per-rank y, only 16 of 256 experts routed (most experts empty) 0 / 160 on 8/8 ranks
FP8xFP8 every third call with post_expert_norm=True on the same buffer plain calls 0 / 160 on 8/8 ranks
FP8xFP8, FP8xFP4 every third call is BF16 Mega MoE on a BF16 SymmBuffer sharing the memory through base 0 / 162 on 8/8 ranks (BF16 and FP8 calls)
FP8xFP8 same, plus every third call with post_expert_norm=True plain FP8 and BF16 calls 0 / 162 on 8/8 ranks

Post-norm calls are not bitwise reproducible on dev itself (the partial sums of squares are float atomics): dev against dev differs on those calls by up to 3e-3 relative, and this PR shows the same.

Workspace invariant. After every call (host sync between calls, mixed post-norm sequence), the index-slot region and the per-rank count words are read back:

calls leaving nonzero index slots calls leaving nonzero counts
first commit of this PR 39 / 40 (stale raw indices from post-norm calls) 0 / 40
this PR 0 / 40 0 / 40

base check (single rank, BF16 base buffer with 256 experts, 256 tokens):

reused config dev this PR
128 experts (different slot extent) accepted rejected by the extent check
hidden 4096, same extent accepted accepted
same config accepted accepted

Kernel time (this PR / dev, CUPTI). Both arms run in one process, switched with _C.init, in alternating rounds. Uniform routing, FP8xFP8. Min over ranks is the mean over calls of the fastest rank's time; max over ranks is the slowest's.

shape tokens/rank min over ranks max over ranks
hidden 7168 16 0.981 (4 runs: 0.981, 0.981, 0.980, 0.981) 0.981
hidden 7168 2048 0.998 1.005 (2 rounds)

Max over ranks is from rounds without host-jitter spikes; spikes appeared in both arms on this shared box.

tests/test_mega_moe.py defaults (FP8xFP4, hidden 7168, intermediate 3072, 384 experts top-6, 1 shared expert), 8 ranks. Perf only, since deep_ep is not installed:

tokens/rank this PR / dev
32 0.997
2048 1.003

BF16 (--mma-type bf16xbf16, 32 tokens), with the slot clear: 1.002.

That test's calls are not rank-aligned, and the shared expert plus intermediate 3072 make the call longer, so the barrier is a smaller share.

The B300 results above were measured on the commit before the sum-clear fix.

Multi-node, CC 10.7 (sm_107a)

4 GPUs per node, 1 to 6 nodes in one NVLink domain, CUDA 13.4, localized weights, 210 SMs. FP8xFP8, hidden 7168, intermediate 2048, top-8, 16 experts per rank (16 ranks = DeepSeek-V3's 256 experts). Kernel time, this PR / dev, 8 alternating rounds in one process, uniform routing. All outputs bitwise equal to dev.

ranks (nodes) 16 tokens/rank, min over ranks 16 tokens/rank, max over ranks 64 tokens/rank, min over ranks 64 tokens/rank, max over ranks
4 (1) 0.935 0.999 0.938 0.992
8 (2) 0.934 0.942 0.937 0.949
16 (4) 0.930 0.944 0.930 0.943 (one host-jitter round in the PR arm excluded; 0.968 with it)
24 (6) 0.929 0.930 0.929 0.930

On one node, one rank was slower than the rest in both arms by a similar margin, so max over ranks there shows no gain.

Race test on CC 10.7, same rig as above:

MMA output ranks mismatched calls
FP8xFP8 per-rank y 4, 24 0 / 160
FP8xFP8 broadcast_y 4 0 / 160
FP8xFP8 broadcast_y 8, 16 0 / 14
FP8xFP8 broadcast_y 24 0 / 80
FP8xFP4 per-rank y, 16 experts routed 4, 24 0 / 160

Before the sum-clear fix, broadcast_y hung on the first call (0 tokens on every rank) at 4, 8 and 24 ranks; dev passed. The device timeout prints showed one rank's SM 0 missing from both grid syncs while its other SMs had already cleaned. The per-rank-y runs at 24 ranks were measured before the fix and passed.

Not covered

  • The race in the count drain was not observed before the fix either (count words were zero after every call in all arms); the drain closes it by construction.
  • A BF16 grid-index tag colliding with an FP8 epoch was not reproduced (grid indices are large); the shared-buffer runs above only show that alternating BF16 and FP8 calls stays exact with the disjoint tags.
  • SymmBuffer(base=...) callers whose configs differ in slot extent now get an assertion instead of sharing; nothing in this repo or in sglang passes base.
  • The BF16 Mega MoE kernel keeps its barrier; only the slot clear was added there.
  • SM100 (B200).
  • B300 was not rerun after the sum-clear fix.
  • Multi-node runs used one shape (16 experts per rank) and no post_expert_norm or BF16 mixing.

🤖 Generated with Claude Code

zcnrex and others added 4 commits November 18, 2026 10:34
Counts and source token-topk indices pushed to peers are stored as value + 1;
the workspace starts zeroed, so 0 means "not arrived". The pull warps poll each
count and index with ld.volatile and clear an index slot after reading it (each
slot is read once per call; counts are cleared by the existing workspace
cleanup). The NVLink barrier before the pull is skipped: the scheduler already
polls the per-expert arrival sums.

Combine and broadcast-output readiness are tagged with a per-call epoch (grid
sync counter 3, bumped by SM 0 after the end barrier) instead of each launch's
grid index. The grid-index push and the local readiness clears relied on the
removed barrier.

Configs with the fused post-expert-norm partials keep the barrier, since their
reset is ordered by it. Outputs are bitwise identical.

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

Sentinel dispatch: the pull polls per-rank counts only for experts with
tokens. A count store for an empty expert can land after the cleanup cleared
it, and the next call would read the leftover as its count. Cleanup now waits
for every count to arrive before clearing it.

Index slots must be zero when a sentinel call starts. Configs that keep the
barrier (fused post-expert-norm partials) and the BF16 kernel left raw
indices behind, so a buffer shared with a sentinel config could hand a stale
index to the pull. Every config now clears a slot after reading it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… different slot extent

The BF16 kernel still tags combine readiness with grid index + 1 in the same
words the FP8/FP4 kernel now tags with a per-call epoch. In a buffer shared
through `base`, a BF16 tag equal to the next FP8 epoch would be accepted as
readiness before the peer's L2 writes land. FP8/FP4 epoch tags now set bit 63.

The index slots span `num_experts * num_max_tokens_per_rank` words after the
fixed signals and are followed by per-call metadata. `SymmBuffer(base=...)`
on SM100 now requires the same extent as the base buffer, so no config's
metadata can sit in another config's slots.

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

Without the pull barrier, a call with no tokens for a rank let the other SMs
fetch the per-expert sums, find no work and clear them while SM 0 was still
pushing counts to peers; SM 0 then spun forever in fetch_expert_recv_count.
Seen as a hang on the first (0-token) call of a broadcast_y stress on CC 10.7.
SM 0 now clears the sums after the end barrier's grid sync.

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

zcnrex commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

Superseded by #179 (ports upstream deepseek-ai#525's barrier-free dispatch across both FP8/FP4 and BF16 kernels, removing both the dispatch grid sync and pre-pull NVLink barrier).

@zcnrex zcnrex closed this Dec 1, 2026
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