Repository navigation
[FlyDSL] MegaMoE EP: expert-parallel MoE as one persistent kernel - #7859
Yaowu-Xiong wants to merge 7 commits into
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
…x950, MXFP4)
Add aiter.ops.flydsl.mega_moe_ep.MegaMoeEP: an expert-parallel MoE layer that
runs a whole forward (all-to-all dispatch, in-kernel planner, the local experts'
GEMM1 + activation + GEMM2, all-to-all combine) as one persistent kernel launch
per rank. Same inputs as MegaMoEV2 (bf16 tokens, top-k ids / weights, MXFP4
weights; a8w4 or a4w4). Opt-in: nothing in aiter calls it.
- aiter/ops/flydsl/mega_moe_ep.py: public wrapper (MegaMoeEPConfig, MegaMoeEP,
mega_moe_ep_supported, the pure-torch oracle ep_reference).
- aiter/ops/flydsl/kernels/mega_moe_ep/: the kernel. One CTA per CU, 8 waves,
all of them compute; hipIpc symmetric arena with epoch flags.
- Small-batch kernel (mtpr <= 512): epoch-tagged sends, XCD-private
bucketing without a grid barrier, even-split planning for hot experts.
- Large-batch kernel: routing first so owners plan while the payload is in
flight, payload in 4 token buckets, hot experts split over the XCDs.
- Combine partials go back as per-8 FP8 + E8M0; the source does the top-k
weighted sum.
- Cross-rank waits are unbounded; AITER_MEGAMOE_EP_WATCHDOG=1 (debug)
bounds them by 2 s and traps.
- prepare() compiles every variant before graph capture.
- op_tests/multigpu_tests: test_mega_moe_ep.py (accuracy vs ep_reference,
CUDA graph, uneven ranks, back-to-back inputs) and bench_mega_moe_ep.py
(perf vs MegaMoEV2).
- mega_moe/gemm_util.py: ATileLoader threads beyond the chunk count repeat a
chunk instead of reading past the tile (V2 outputs unchanged).
Co-authored-by: Cursor <[email protected]>
d213038 to
a30b46c
Compare
aiter/aot/flydsl/mega_moe_ep.py builds the runtime's own MegaMoeEP inside dry_run (FakeTensor weights, a fake hipIpc SymmetricArena) and replays prepare() plus one forward(), so every launch variant and the activation quant land in the cache. Rank, world size, token count and peer addresses are launch arguments, so one job per profile serves every rank. Profiles: DeepSeek-V4-Pro epr 48/52/56 x a8w4/a4w4 and Kimi-K2.6 epr 48 a8w4, each at MTPR 512 and 8192 (14 jobs). run_aot runs it after the MegaMoEV2 replay; --check verifies a cache without compiling. Co-authored-by: Cursor <[email protected]>
Rename the kernel's terse constants, helpers and arena keys to descriptive names (e.g. TMAX -> TOKEN_SLOTS, NPES -> N_RANKS, cbar -> cta_barrier, recv_le -> recv_expert), keep comments to the EP op itself, and remove unused layout entries, re-exports and redundant branches. Public API and kernel names are unchanged; the compiled binaries are byte-identical. Co-authored-by: Cursor <[email protected]>
bench_mega_moe_ep.py --engine mori times the unfused EP MoE as ATOM runs it online: bf16 mori dispatch, fused_moe on the gate|up interleaved MXFP4 weights with MXFP8 activations, mori combine, with ATOM's launch geometry per phase (--mori-phase; auto picks decode at mtpr <= 512). a8w4 and SwiGLU networks only. Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
…ead_idx Co-authored-by: Cursor <[email protected]>
…, group-aware reference - set_weights binds preshuffled weights by pointer and refuses natural ones: a shuffled copy made there was freed while a captured graph still read it. The copies shuffled at construction live as long as the instance. - prepare() / forward() run on the engine's device and its current stream; an explicit stream on another device is refused. - ep_reference / _all_gather_variable take the EP group, so the reference matches the engine under DP x EP. - mega_moe_ep_supported() checks the live GPU (get_gfx_runtime), not the build target list. - test_mega_moe_ep.py: a rebind row per layer (graph replay after set_weights is bit-identical, eager matches the reference on the new weights), --ep-groups N, default spike routing sized to the EP world. Co-authored-by: Cursor <[email protected]>
coderfeli
left a comment
There was a problem hiding this comment.
Why another mega moe ep?
Summary
Adds
aiter.ops.flydsl.mega_moe_ep.MegaMoeEP, an expert-parallel MoE layer that runs a whole forward as one persistent kernel launch per rank. The launch does all of these:It takes the same inputs as
MegaMoEV2(bf16 tokens, top-k ids / weights, MXFP4 weights; a8w4 or a4w4).The baseline is main's
MegaMoEV2, which has three paths:AITER_MEGA_SMALL_M=1): quant, dispatch, GEMM1, GEMM2 and combine in one kernel, up to 512 tokens per rank.AITER_MEGA_LARGE_M=1): a quant kernel, then dispatch, GEMM1, GEMM2 and combine in one persistent kernel, at mtpr 8192.small_m and large_m only accept DeepSeek-V4-Pro a8w4 on 8 ranks; every other shape (including V4-Pro a4w4 and Kimi-K2.6) runs the 2-stage path. This PR runs every shape.
Results of this PR:
fused_moeEP MoE (what ATOM runs online; a8w4, V4-Pro and K2.6, mtpr 512 / 8192, uniform / zipf:1 / spike): 14–62% faster.Accuracy is also tested on the DeepSeek-V4.1-Flash, Kimi-K3 and Mixtral-8x7B shapes (see Testing).
The layer is opt-in: nothing in aiter calls it at run time, and frameworks choose it explicitly. The AOT build (
setup.py→run_aot) precompiles its kernels for the V4-Pro and K2.6 deployment profiles, so serving does not JIT them at startup.Motivation
This PR is one persistent kernel for every supported shape. Dispatch, planning, GEMMs and combine overlap inside the launch, and the planner places work by the routing it actually received.
Changes
New files:
aiter/ops/flydsl/mega_moe_ep.py: the public wrapper.MegaMoeEPConfigandMegaMoeEPprovideforward,prepare,set_weights,poll_errors,resetandreference_forward.mega_moe_ep_supported(gfx950) and the pure-torch oracleep_reference.aiter/ops/flydsl/kernels/mega_moe_ep/: the kernel (FlyDSL). One CTA per CU, 8 waves, all of them compute.mega_moe_ep_kernel.py: the entry and launcher.communication.py: hipIpc symmetric arena, epoch flags, dispatch sends, the combine finish and the cross-rank waits.schedule.py: the in-kernel planner and the XCD-local tile queue.gemm.py: inline-A GEMM1 + activation + GEMM2 with the combine fused into the GEMM2 epilogue.mega_moe_ep_config.py: compile-time layout.mega_moe_ep.py: the host engine, launch configs and oracle.max_tok_per_rank, so every rank runs the same one:AITER_MEGAMOE_EP_WATCHDOG=1(debug, default off) bounds each wait by 2 s. On expiry it sets the sticky error word and traps (s_trap 2)._wd, so the debug build has its own cache entry.prepare()compiles every variant the engine can launch and ends in a barrier. No rank then JIT-compiles inside a launch while its peers wait.aiter/aot/flydsl/mega_moe_ep.py(new), run byrun_aotafter theMegaMoEV2replay (aiter/aot/flydsl/common.py, 3 lines), documented inaiter/aot/flydsl/README.md.mega_moe.py, it builds the runtime's ownMegaMoeEPinsidedry_run, with FakeTensor weights and a fake hipIpcSymmetricArena, and replaysprepare()plus oneforward()(for the activation quant). No GPU or process group is needed.--check(FLYDSL_RUNTIME_RUN_ONLY=1) verifies a cache without compiling.op_tests/multigpu_tests/test_mega_moe_ep.py(accuracy vsep_reference) andop_tests/multigpu_tests/bench_mega_moe_ep.py(perf vsMegaMoEV2and the mori dispatch →fused_moe→ mori combine baseline,--engine mori).aiter/ops/flydsl/kernels/mega_moe/gemm_util.py, 3 lines.ATileLoader's surplus threads now repeat a chunk instead of reading past the tile.const_expr. In V2 it is taken only by the fp4 Stage1 with SBM32 and 8 waves (256 chunks, 512 threads): V4-Pro mtpr 8192 bucket 8, R1 buckets 8 / 16 / 32, V4.1-Flash EP2 bucket 512 (enumerated withselect_mega_moe_config). All of these use the async A copy, whose surplus waves are already masked bycopy_active. The modulo only changes addresses they never use, so V2 outputs are unchanged (see Testing).Performance (if applicable)
Columns. "V2 2-stage" and "V2 small_m / large_m" are main's
MegaMoEV2paths (see Summary); "mori + fused_moe" is the unfused EP MoE that ATOM runs online (--engine mori); "This PR" isMegaMoeEPat this PR's head. "–" means the path does not accept the shape, or (full network) was not measured. Change is this PR relative to each baseline column, in that order.Single-op setup:
op_tests/multigpu_tests/bench_mega_moe_ep.pyon 8x MI355X, EP8, FlyDSL 0.5.0. V2 on main9c119bb65: 2-stage withAITER_MEGA_SMALL_M=0 AITER_MEGA_LARGE_M=0, small_m / large_m with both set to 1.bench_mega_moe_ep.py --engine mori, a8w4 only): bf16 mori dispatch,fused_moe(per-1x32 MXFP4 weights with gate|up interleaved, MXFP8 activations,num_local_tokensfrom the dispatch), mori combine. ATOM's launch geometry: decode (mtpr ≤ 512) 64 blocks × 4 warps with the receive buffer cut to bs × 8 rows, prefill 128 × 16. Measured 11-12 in its own session, alternating with this PR's head, whose times there were within 4% of the This PR column.bench_mega_moe_ep.py -r):spike:0.8,0.5,0.35,0.26,0.2,0.17): the shape the full model shows after EPLB. Hot expert j lives on rank j and is picked by 80 / 50 / 35 / 26 / 20 / 17 % of the tokens; ranks stay fairly balanced (mean / max 0.83), but a hot rank's rows mostly belong to one expert, so the imbalance is within a rank.Full-network setup: random dataset, input 8192 / output 1024; c512 with 2048 prompts, c4096 with 8192 prompts.
a0c4b69c1, FlyDSL 0.4.2, on an ATOM tree from 11-01 with PR 6881's MQA-logits module overlaid (that tree needs the old interface). Measured 11-09.AITER_LM_DEDUP=1 AITER_LM_HELPER_CU=8. 3 rounds, 11-07.a0c4b69c1base, whose small_m / large_m accept the K2.6 shape) with FlyDSL 0.4.2.Notes:
Testing
Setup: accuracy runs on 8x MI355X with FlyDSL 0.5.0 on main
9c119bb65. Each case runs eager, then CUDA-graph replays on the current stream and two side streams, and the error word must stay 0. Then it runs 8 back-to-back launches (eager and replay) that alternate with a second input without a host sync, and each output must be bitwise equal to a standalone launch (b2b). All PASS:test_mega_moe_ep.py, 5 networks (v4_pro,kimi_k26,v41_flash,kimi_k3with the situv2 activation,mixtral_8x7bwith I > H). Commands, per networkN(Mixtral uses fewer token counts):--uneven(different token counts per rank): 80/80 per network.python -m aiter.aot.flydsl.mega_moe_ep, FlyDSL 0.5.0):--check: 14/14 ok.FLYDSL_RUNTIME_RUN_ONLY=1(any cache miss fails): the three test commands above, V4-Pro a8w4 / a4w4 180/180, 132/132, 80/80; K2.6 a8w4 90/90, 66/66, 40/40. The cache gains no artifact.bench_mega_moe_ep.py --engine ep, 2 alternating rounds): AOT / JIT geomean 0.998 (V4-Pro a8w4 mtpr 8192, 20 points), 1.001 (mtpr 512, 27 points), 1.002 (K2.6 mtpr 8192, 6 points); every point within ±2%.AITER_MEGAMOE_EP_WATCHDOG=1): V4-Pro 60/60, Kimi-K3 60/60. With one rank 5 s late, every rank traps as designed.forward(stream=s)with the inputs written late on the current stream (the forward waits for them);ep_reference, this PR's 0.053, and 0.027–0.031 from this PR's. With MXFP4 activations (a4w4) and interleaved gate|up,fused_moedoes not match the reference (relL2 1.41), so--engine morirejects a4w4.gemm_util.pychange:test_mega_moe_v2.pywith and without the change. Paths covered: the V4-Pro main path, a8w4 / a4w4, mtpr 512 (bs 1 … 512) and mtpr 8192 (bs 1 … 8192, including bs 8, which takes the branch); the R1 (r1_v3) a4w4 main path at mtpr 128 / 512, bs 8 / 16 / 32; small_m (AITER_MEGA_SMALL_M=1); large_m (AITER_MEGA_LARGE_M=1 --expect-large-m --compare-main, bs 1 … 8192). All 51 relL2 values are identical with and without the change (the large_m run exits withHIP error: invalid argumentafter its last case on main too). V2 perf of the branch-taking bucket (V4-Pro a4w4 mtpr 8192 bs 8, 2 alternating rounds): 0.336 / 0.336 ms uniform, 0.225 / 0.225 ms zipf:1; other sizes within one timer tick (0.002 ms).Documentation
The module docstrings describe the kernels, their assumptions and the debug watchdog.
aiter/aot/flydsl/README.mddescribes the new AOT module. Performance results are in this description (Performance section).Dependencies
Uses the FlyDSL pinned by main (0.5.0) and the existing
mega_moe/mega_moe_tphelpers. The symmetric memory uses hipIpc; in containers on the host network, ROCm 7.1 needsHSA_ENABLE_IPC_MODE_LEGACY=1(wrapper docstring).Breaking Changes
None. New module; the only shared-code change is the
ATileLoadersurplus-thread branch, which V2's tiles never take. The AOT build runs one more replay (14 jobs, gfx950 only; skipped whenGPU_ARCHSomits gfx950), and a failure there fails the build like the other replays.PR Self-Review
Reviewed revision
99c6e9948Behavior and intent
New opt-in module; no existing behavior changes.
MegaMoEV2's 2-stage path, the only one for most shapes, runs a forward as two launches with a host-visible boundary, which costs time at decode sizes (launches and handshakes) and at prefill sizes (Stage2 waits for all of Stage1); its fused small_m / large_m paths only accept DeepSeek-V4-Pro a8w4 on 8 ranks.MegaMoeEPdoes the whole forward in one persistent launch for every supported shape, and its planner places work by the received routing. The AOT build precompiles its kernels for the shipped profiles (newaiter/aot/flydsl/mega_moe_ep.py, run byrun_aot). The only shared-code change (gemm_util.py) only changes addresses that V2's surplus threads never use.Validation and unverified scope
torchrun --nproc_per_node=8 op_tests/multigpu_tests/test_mega_moe_ep.py --network v4_pro -q a8w4 a4w4 --mtpr 512 8192 -r uniform zipf:1 spike same invalid: 180/180 PASS on 8x MI355X;bench_mega_moe_ep.py --engine ep|v2|morifor the Performance table): accuracy on 5 networks × a8w4 / a4w4 × both kernels × 0 … 8192 tokens × 6 routings, uneven token counts, back-to-back inputs, epoch wrap-around, multi-stream mixes, a late rank, the watchdog build; V2 unchanged; single-op perf vs main's V2 2-stage / small_m / large_m and vs mori +fused_moe; full networks on ATOM (V4-Pro) and sglang (K2.6); the AOT module from an empty cache,--check, artifacts identical to the runtime JIT's, and accuracy / perf on the AOT cache withFLYDSL_RUNTIME_RUN_ONLY=1.python -m aiter.aot.flydsl.mega_moe_ep), not inside a fullsetup.pywheel build.PR-specific question
Which exact workload keys consume these configuration changes, and what evidence shows that unrelated shapes keep their previous selection?
This PR changes no tuning table, config file or kernel-selection rule of an existing op.
MegaMoeEPis a new class, used only by callers that construct it, and its launch configs live in the new module. The AOT change only adds a replay torun_aot; the existing modules' job lists are untouched. The one shared-code change,ATileLoaderinmega_moe/gemm_util.py, is a trace-time branch for tiles with fewer A chunks than threads. InMegaMoEV2it is taken only by the fp4 Stage1 with SBM32 and 8 waves (V4-Pro mtpr 8192 bucket 8, R1 buckets 8 / 16 / 32, V4.1-Flash EP2 bucket 512, enumerated withselect_mega_moe_config), whose surplus waves are already masked bycopy_active. Evidence:test_mega_moe_v2.pygives identical relL2 on all 51 cases with and without the change (main path, small_m, large_m; including bs 8), and the branch-taking bucket's V2 time is unchanged (0.336 / 0.336 ms uniform, 0.225 / 0.225 ms zipf:1); see Testing.Changed op tests
op_tests/multigpu_tests/test_mega_moe_ep.py(new): accuracy ofMegaMoeEPagainst the pure-torchep_referenceon 8 ranks, per network / quant / mtpr / token count / routing, including invalid routes, uneven token counts per rank and back-to-back alternating inputs (bitwise). No other test covers this module.op_tests/multigpu_tests/bench_mega_moe_ep.py(new): CUDA-graph perf ofMegaMoeEP,MegaMoEV2and the mori dispatch →fused_moe→ mori combine baseline on the same shapes and routings; not a correctness test.