Skip to content

[FlyDSL] [MoE] MegaMoEV2: fused large-input route at capacity 32768, rank as a kernel argument (stacked on #8623) - #8806

Merged
GwilliamHu merged 7 commits into
ghu/mega_a4w4_20261124from
ghu/mega_32k
Nov 27, 2026
Merged

GwilliamHu merged 7 commits into
ghu/mega_a4w4_20261124from
ghu/mega_32k

Conversation

@GwilliamHu

@GwilliamHu GwilliamHu commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

PR Self-Review

Reviewed revision

ec210fa

Behavior and intent

Stacked on #8623 (this PR's base branch is #8623's; it will be rebased onto main once #8623 merges). Two parts: the MTPR 32768 large-input route (one commit) and the rank as a kernel argument (six commits).

A MegaMoEV2 instance of capacity 32768 (e.g. ATOM with max_num_batched_tokens 32768) builds no large-input route today, so its calls of 513..32768 rows a rank take the staged route. This PR adds 32768 to LARGE_M_MTPRS: every fused layout (V4.1-Flash EP8 / EP4 / EP2, V4-Pro EP8) and both quants build the large-input route at 32768, and every call that passes max_tokens takes a fused route.

At 32768 every large-input config sends a token row to a destination once (dedup_rows, indexed payload). The V4.1-Flash tunes and the V4-Pro A4W4 tune already do; V4-Pro A8W4's route-major payload would need a 15.29 GiB/GPU symmetric heap, so at 32768 only it takes the V4-Pro A4W4 large-input tune (renamed _tune_v4_pro_large; 8.04 GiB/GPU). Configs at 1024..16384 are unchanged. 65536 stays out of reach (16-bit tagged counts). The AOT default V4.1-Flash MTPRs gain 32768.

The rank was a compile-time constant in every MegaMoE kernel (const_expr(source < rank), rank*epr and rank*mtpr folded into address math), so the AOT build compiled each kernel once per rank with nothing shared. The rank is now a kernel argument: GEMM1 / GEMM2 index W1 / W2 with global expert id % experts_per_rank (the GEMMs no longer see the rank); the staged kernels, prepare, the small- and large-input kernels and combine take i32_rank at launch; the three const_expr(source < rank) prefix sums become selects. make_combine_kernel is shared with the generic EP combine, which passes the rank too. The AOT driver builds one job per (MTPR, experts per rank, quant) and replays it on rank 0; one cache serves every rank.

Validation and unverified scope

  • AOT, production job set: 276 jobs / 8898 entries / 13622 GPU kernels -> 42 / 1333 / 2041 (8x on EP8, 4x on EP4, 2x on EP2); replay CPU time about 7.5x lower. V4.1-Flash EP8 profile with 32768 (measured): 64 jobs / 1842 entries / 136 MB / 5773 CPU-s -> 8 / 232 / 17.3 MB / 653 CPU-s. Rank 0 and the last rank give identical AOT keys and sidecars on all 42 layouts. --suite routes from the AOT cache (V4.1-Flash EP8 and V4-Pro EP8, both quants, MTPR 32768, AITER_FLYDSL_AOT_CACHE=verify and normal): all pass, 0 misses, every rank hits the same entries.
  • Rank as a kernel argument: outputs bitwise identical to the base (asmval 142 points, all ranks, V4.1-Flash EP8 / EP4 / EP2 and V4-Pro EP8, both quants; and on the 32768 instance). No added VGPRs or VGPR spills in the GEMM kernels, K loops unchanged; the large-input kernel gains 5-11 SGPR spills outside the K loops. Timing within +-0.5% at MTPR 512 / 4096 / 16384 / 32768, except 1 row a rank on the small-input route, about 0.1-0.2 us (0.3%) slower.
  • Static: ruff 0.16.0 + black; tests/test_mega_moe_gfx950_route_plan.py (216 passed; the never-staged exhaustive check now covers pow2 capacities up to 32768, the 32-bit offset test also bounds the GEMM1 out-scale view and the GEMM1 output, every 32768 config uses dedup_rows), tests/test_aot_drivers.py.
  • Multi-GPU op tests at capacity 32768, V4.1-Flash EP8 / EP4 / EP2 and V4-Pro EP8, both quants (8x MI350X, MORI_SHMEM_HEAP_SIZE=16G): --suite size --expect-large-m --compare-main at 513..32768 rows with padded tails, uneven ranks, zipf, and all rows to rank 0 (GEMM1 output > 2^31 bytes); layer-slot stress (61 slots x 9 cycles, skewed route switches on EP8); --suite routes: all pass. relL2 vs fp32: A8W4 0.0525-0.0535, A4W4 0.181-0.184 (same as at 16384).
  • Bitwise: instances of capacity <= 16384 are bitwise identical to the base on every asmval point, timing unchanged; at 32768, calls up to 16384 rows are bitwise identical to the 16384 instance on every layout except V4-Pro A8W4 (its tune changes at 32768; relL2 vs staged 0.028).
  • Symmetric heap per GPU at 32768 with 61 layer slots: at most 8.20 GiB (V4-Pro A8W4).
  • Fused vs staged at capacity 32768 (speedup = staged time / fused time, 32768 rows a rank, uniform / zipf / hot, 8x MI350X): V4.1-Flash EP2 A8W4 1.72x / 1.45x / 1.88x, A4W4 1.54x / 1.36x / 1.54x; EP4 A8W4 1.20x / 0.89x / 1.62x, A4W4 1.36x / 0.99x / 1.68x; EP8 A8W4 1.01x / 0.98x / 1.46x, A4W4 1.07x / 1.06x / 1.54x; V4-Pro EP8 A8W4 0.91x / 0.95x / 1.12x, A4W4 1.00x / 0.91x / 1.23x.
  • Known: on the 32768 instance the large-input route at 24576 / 32768 rows gives one of two outputs across processes on one rank (relL2 between them up to 2.2e-3), with or without the rank change; repeated calls in one process are identical. About 1/7 of the fast prepare variants compile to .o files that differ by a few instructions between builds of the same IR (LLVM backend), so byte-exact AOT rebuild checks can differ there.
  • Not covered: MI355X timing. The large-input kernel is slower than staged on zipf for V4-Pro and Flash EP4 A8W4 and on uniform for V4-Pro A8W4 at 24-32K rows (down to 0.89x); the V4-Pro zipf gap already exists at 16384. Fused routes are kept there (no staged fallback); the large-input kernel is a follow-up.

PR-specific question

Does anything change below 32768? No: configs at 1024..16384 are unchanged and outputs bitwise identical; the kernels now take the rank at launch.

Changed op tests

None.

Motivation

Capacity 32768 is a common prefill setting; without this its prefill chunks ran staged. The AOT prebuild compiled every MegaMoE kernel once per rank.

Changes

  • LARGE_M_MTPRS up to 32768; V4-Pro A8W4 takes the shared V4-Pro large-input tune at 32768.
  • AOT: V4.1-Flash default MTPRs include 32768.
  • Rank as a kernel argument in the staged, prepare, small-input, large-input and combine kernels; GEMMs index experts without the rank.
  • AOT: one job per profile instead of one per rank.
  • Tests: never-staged check up to 32768, buffer bounds, dedup_rows at 32768, digests pinned; AOT driver job counts.

Test Plan

See "Validation and unverified scope".

🤖 Generated with Claude Code

@GwilliamHu
GwilliamHu force-pushed the ghu/mega_a4w4_20261124 branch from 7cadc30 to 19902fb Compare November 27, 2026 06:14
Add 32768 to LARGE_M_MTPRS, so a 32768-capacity instance (ATOM's prefill
max_num_batched_tokens) builds the large-input route and every call that
passes max_tokens takes a fused route (small-input up to 512 rows a rank,
large-input above) instead of staging 513..32768 rows.

At 32768 every layout's large-input config sends a token row to a
destination once (dedup_rows, indexed payload). The V4.1-Flash tunes and
the V4-Pro A4W4 tune already do; V4-Pro A8W4 (all fused knobs off,
route-major payload: a 15.29 GiB/GPU symmetric heap) takes the V4-Pro
A4W4 large-input tune at 32768 only, renamed _tune_v4_pro_large
(8.04 GiB/GPU). Its 1024..16384 configs are unchanged. 65536 stays out
of reach: the tagged count words hold 16-bit counts.

The AOT default V4.1-Flash MTPRs gain 32768 (+28 jobs per quant set);
V4-Pro's 32768 jobs build the large-input kernel through LARGE_M_MTPRS.
Tests: the never-staged exhaustive check covers pow2 capacities up to
32768, the 32-bit offset test also bounds the GEMM1 out-scale view and
the GEMM1 output (per-tile resources past 2^31), every 32768 config is
checked to use dedup_rows, and the A4W4 digests stay pinned to the
pre-existing capacities.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
GwilliamHu and others added 6 commits November 27, 2026 15:01
The GEMM1 tile scheduler and GEMM2 subtracted a compile-time rank * experts
offset from the global expert IDs the dispatch plan writes. A rank's IDs
modulo its expert count are the same indices, so the GEMMs no longer need
the rank: expert_offset becomes experts_per_rank. The aligned-pair Stage2
passes its pair's local experts directly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The staged Stage1 bundle, prepare, Stage2 and aligned-pair Stage2 kernels
took the rank as a compile-time constant, so the AOT cache built every
kernel once per rank. They now take it as an i32 kernel argument: the
compile functions and their AOT keys drop rank, and the run/preload
helpers pass it at launch. Rank-dependent index math becomes a few scalar
ops; the source < rank prefix sums become select chains over the running
sums. Outputs are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
make_combine_kernel baked the rank into its P2P slot offsets, its
cross-device barrier word and the invalid-slot peer, so MegaMoE's fused
combine (mega_moe_combine) was compiled once per rank. The kernel now
takes an i32 rank argument, which the op passes on every launch; the
generic dispatch/combine path shares the same kernel.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The small-input persistent kernel takes the rank as an i32 kernel
argument instead of a compile-time constant, so one AOT entry serves
every rank. The fused combine publishes its flag at a byte offset
(rank * 8) read from the runtime's dispatch table where it is used,
rather than keeping the rank live across the GEMMs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The large-input persistent kernel takes the rank as an i32 kernel
argument. large_m_kernel_specs drops ("rank", rank) from the fused Stage2
spec, so the kernel name hash (S2_KEY) and the AOT key no longer depend on
the rank; the skew boost of the payload geometry tests the destination
against the rank at run time, and the plan writes its counts through the
base of its own row in each destination's count matrix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every MegaMoE kernel now takes the rank at launch and no AOT key holds
it, so rank 0's replay builds the kernels of every rank. default_jobs
yields one job per (MTPR, experts per rank, quant): the default build
drops from 276 jobs and 8898 AOT entries to 42 jobs and 1333 entries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@GwilliamHu GwilliamHu changed the title [FlyDSL] [MoE] MegaMoEV2: fused large-input route at capacity 32768 (stacked on #8623) [FlyDSL] [MoE] MegaMoEV2: fused large-input route at capacity 32768, rank as a kernel argument (stacked on #8623) Nov 27, 2026
@GwilliamHu
GwilliamHu merged commit ec210fa into ghu/mega_a4w4_20261124 Nov 27, 2026
@GwilliamHu
GwilliamHu deleted the ghu/mega_32k branch November 27, 2026 18:08
@GwilliamHu

Copy link
Copy Markdown
Member Author

Folded into #8623: its branch now carries all commits of this PR (head ec210fa), so GitHub shows this PR as merged into that branch.

RolaoDenthu added a commit to RolaoDenthu/sglang that referenced this pull request Nov 30, 2026
aiter dropped the large_m_enabled constructor argument: routed=True now
builds the small- and large-input routes, and an unrouted instance holds
the staged route only. Pass routed=True and turn the large-input route
back on; with _AITER_LARGE_INPUT_ROUTE off, pin small_m_mtprs so the
small-input route is still built.

DEP4 DeepSeek-V4.1-Flash on gfx950 at MTPR 8192 and 16384 served prompts
of 61 to 44,030 tokens through the small- and large-input routes without
the stage1 illegal-address fault.
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