Repository navigation
[FlyDSL] Fused expert-parallel MoE in one launch per rank for DeepSeek-V4.1-Flash EP8 a8w4 (gfx950) - #8368
[FlyDSL] Fused expert-parallel MoE in one launch per rank for DeepSeek-V4.1-Flash EP8 a8w4 (gfx950)#8368Fangzhou-Ai wants to merge 3 commits into
Conversation
…gfx950) Add FusedEPMoE (aiter.ops.flydsl.fused_ep_moe): one persistent launch per rank runs the MXFP8 quant of bf16 rows, the dispatch to the expert owners, GEMM1 with clamp-SwiGLU and MXFP8 requant in the epilogue, GEMM2 with the route weights and the return of each weighted row to the token's rank, and a deterministic combine (slot order, no atomics into outputs). Layout: hidden 5120, intermediate 2304, top 6, up to 63 local experts per rank on 2 / 4 / 8 ranks of one node, MXFP4 weights in the layouts MegaMoEV2 takes, up to 1024 tokens per rank, gfx950 with 256 CUs. fused_ep_moe_supported() is the eligibility check for callers that fall back to MegaMoEV2. Tests: op_tests/multigpu_tests/test_fused_ep_moe.py (8 ranks, torch reference, 48 / 56 / 63 experts per rank, skewed and empty ranks, unrouted ids, bitwise repeatability and CUDA graph replay). Benchmark against MegaMoEV2: op_tests/multigpu_tests/bench_fused_ep_moe.py. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Fangzhou Ai <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
- Loads and stores go through FlyDSL buffer tensors and fx.copy instead of the legacy kernels/buffer_ops.py; the gfx950 ISA is unchanged. - Typed fx.rocdl.cvt_scalef32_pk_fp8_f32, fx.atomic_add / fx.atomic_xchg (agent scope), fx.generic_load / generic_store for LDS, and the typed scaled MFMA in the 32- and 64-row tiers. The XCC id and the DPP scan reuse communication_ops_utils.hw_xcc_id and dpp_utils.update_dpp_i32. - Raw asm / rocdl stays where the call gives a measured or ISA reason: the AGPR-accumulator MFMA of the 16- and 128-row tiers (the typed op spills the 128-row tier and is 5-10 % slower at 1-4 tokens per rank), the async LDS copy, the 16-byte sc0 sc1 store and the counter poll. - The arena rows and scales, which peers write into coarse-grained memory, are read with sc0 sc1 like the other peer data, so no peer data relies on L2 invalidation at launch. The module docstring states the coherence argument. - IpcHeap.alloc_coarse_grained replaces the private flag; the local state is sized to the words it uses. - forward() checks every tensor's device and rows_hint; the constructor checks rank / world_size against the group and resolves device="cuda". Routed ids within a token must be distinct (documented). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Fangzhou Ai <[email protected]>
… stress checks - test_fused_ep_moe.py starts torchrun itself when RANK is unset (as the multi-GPU job runs `python3 <file>`) and skips cleanly without 8 GPUs or on parts FusedEPMoE does not support. - The reference comparison covers each rank's first and last 24 tokens. - A stress case runs 64 back-to-back eager calls and 64 graph replays on fixed routes (the same arena rows) with changing payloads and no synchronization in between; each output must match its payload's checked eager output bitwise. - Rank 0 prints a summary table of the reference comparisons. - bench_fused_ep_moe.py: a megamoe_small_m impl (MegaMoEV2's small-input single kernel, when built), 256 and 1024 tokens per rank by default, and the same self-launch; the multi-GPU job skips it. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Fangzhou Ai <[email protected]>
What
aiter.ops.flydsl.fused_ep_moe.FusedEPMoEruns a whole expert-parallel MoE layer as one persistent FlyDSL launch per rank. The launch does five things:silu(min(g, 10)) * clamp(u, -10, 10)and MXFP8 requant in the epilogue.The output is bf16
[m, 5120].The symmetric-heap protocol follows
mega_moe/mega_moe_small_m.py. The protocol differences, the tile/queue design and the cache-coherence argument are in the kernel module docstring.Files:
aiter/ops/flydsl/kernels/fused_ep_moe_a8w4.py: the kernel (compile_fused).aiter/ops/flydsl/fused_ep_moe.py: the public op (FusedEPMoE,FusedEPMoEWeights,fused_ep_moe_supported).aiter/ops/flydsl/ipc_heap.py: addsIpcHeap.alloc_coarse_grained(the op used the private flag before).op_tests/multigpu_tests/test_fused_ep_moe.py: correctness test.op_tests/multigpu_tests/bench_fused_ep_moe.py: benchmark against MegaMoEV2..github/scripts/aiter_test.sh: skips the benchmark in the multi-GPU job, as forbench_mega_moe_v2.py.Why
On current main, DeepSeek-V4.1-Flash EP8 (hidden 5120, inter 2304, 384 experts, top 6) only runs MegaMoEV2's staged route.
small_m_eligible/large_m_eligibleadmit only the V4-Pro layout (hidden 7168). The staged route costs 84-2810 us per layer at 1-1024 tokens per rank. This kernel is 1.10-2.00x faster than it at 1-512 tokens per rank under every routing measured below. At 768 it is 1.41x / 1.17x faster under uniform / hot routing and at parity under Zipf routing (1.02x). At 1024 it is still faster under uniform / hot routing but 0.95x under Zipf routing.#8358 (open) is the other way to cover this layout: it opens main's small-M single kernel to V4.1-Flash. This kernel is 1.07-1.58x faster than #8358 at every size #8358 accepts (1-512 tokens per rank), see the tables below. #8358 also covers vLLM's fused shared expert (top 7, 392 experts). This PR does not.
API
Shapes and contract:
mtokens per rank,0 <= m <= 1024. Idle ranks call withm = 0.topk_idsare physical, rank-major ids (owner =id // experts_per_rank). An id outside[0, experts_per_rank * world_size), e.g. -1 padding, contributes zero. The routed ids of one token must be distinct, as a top-k produces them. This makes an EPLB placement only a renumbering, as long as experts per rank is at most 63.forwardonce per layer, in the same order. The kernel is persistent and spins on its peers, so it must not share the GPU with other work that waits on it, as with MegaMoEV2's small-input single kernel.rank/world_sizemust matchgroup. Every tensor must be on the instance's device (device="cuda"takes the current device).rows_hintmust be at most 1024.Eligibility (
fused_ep_moe_supported):Callers fall back to MegaMoEV2 otherwise. The choice must be the same on every EP rank, so decide it from the DP group's largest per-rank token count.
Dispatch rule measured here:
FusedEPMoEfor 1..768 tokens per rank (at 768 it is at parity under Zipf routing). Fall back to MegaMoEV2 above 768: at 1024 the staged route is 5 % faster under Zipf routing.FusedEPMoEis still faster at 1..512, so the rule above does not change.FusedEPMoEfor 1..512 tokens per rank and [FlyDSL] [CI] MegaMoEV2: one route plan for every path, shared arenas, and the DeepSeek-V4.1-Flash EP2 / EP4 / EP8 fused routes #7788's large-input route above.Correctness
python3 op_tests/multigpu_tests/test_fused_ep_moe.py. CI runs it this way. WithoutRANKset, the script startstorchrun --standalone --nproc-per-node 8itself. It skips cleanly when there are fewer than 8 GPUs or the GPUs are not 256-CU gfx950 parts.torchrun --nproc-per-node 8 <file>andpytest <file>also work.Result: PASS on 8x MI355X, FlyDSL 0.5.0, in 1 min 43 s. The benchmark (
bench_fused_ep_moe.py) is in the multi-GPU job's skip list.The test runs with 48, 56 and 63 experts per rank and covers:
[0, 5, 300, 64, 1, 0, 128, 33];The reference is a torch fp32 dequant of every rank's experts. Each rank's first and last 24 tokens are compared with it; at 1024 tokens per rank that includes the last dispatch workgroups and arena rows. The bounds are:
checkAllclose(rtol=atol=2e-2)flags at most 2 % of elements.Rank 0 prints a summary table. Measured, per case with the worst rank: cosine 0.9999988-0.9999995, worst row 0.999981-0.999995, and an allclose miss of 0.47-0.95 % of elements. The kernel and the reference sum in different orders, so a slot can differ by one bf16 ulp near a tie.
The test also checks that these are bitwise identical to the first eager run:
A stress case uses 48 experts per rank, 4 tokens per rank, uniform and all-to-rank-0 routing. The routes are fixed, so every call reuses the same arena rows. It runs 64 back-to-back eager calls and then 64 back-to-back graph replays, cycling 8 payloads with no synchronization in between. Each output must be bitwise equal to its payload's eager output, and those eager outputs are checked against the reference.
The test also checks that nothing compiles after
prepare().Performance
Setup:
op_tests/multigpu_tests/bench_fused_ep_moe.py.Columns:
DeepseekV4AiterMegaMoEExpertsbuilds it: a fixed-slot instance (128) plus an 8192 instance (compact), which gives the staged route.AITER_MEGA_SMALL_M=1,forward_small_m(max_tokens=T)(themegamoe_small_mimpl of the benchmark). It accepts at most 512 tokens per rank. main, [FlyDSL] MegaMoE small-M: DeepSeek-V4.1-Flash, any experts per rank / top-k, fused shared-expert slots #8358 and this PR ran A B B A in the same processes.routed=True, andforward(max_tokens=T)is passed on every call, including under capture. That selects the small-input route up to 512 and the large-input route from 513. It ran A B B A with this PR in separate processes.uniform (us per call)
zipf (us per call)
hot (us per call)
Summary:
This revision is within -2 % / +2.2 % of the previous revision of this PR at every point. Reading the arena with sc0 sc1 cost 0-1.2 % in an A/B run of the two versions.
Roofline / limit
Ceilings measured on this machine: HBM read stream 7.15 TB/s, xGMI all-to-all egress about 360 GB/s per rank, and FP8 scaled MFMA 4.97 PF. These figures come from a per-phase trace of the same kernel code. The timestamp instrumentation is not part of this PR.
Removing the return stores (a diagnostic only) shortens the call by 7 / 10 / 17 % at T 192 / 384 / 768. That is the most any return/compute overlap could recover.
Skewed routing at T >= 384 is bound by one rank's ingress and rows.
FlyDSL API use
fx.copywithBufferCopyatoms. A small helper takes a per-lane element index, a cache policy and a scalar offset. A masked lane gets an out-of-range offset, as before. The kernel does not use the legacykernels/buffer_ops.py. The gfx950 ISA is unchanged: the same buffer loads, stores and cache bits.fx.rocdl.cvt_scalef32_pk_fp8_f32;fx.atomic_add/fx.atomic_xchgwithSyncScope.Agent;fx.generic_load/fx.generic_storeon shared pointers;fx.rocdl.mfma_scale_f32_16x16x128_f8f6f4withopsel.communication_ops_utils.hw_xcc_id, and the wave prefix scan usesdpp_utils.update_dpp_i32.v_accvgprcopies and 540 instead of 100 bytes of scratch per lane. The 16-row tiers measured 5-10 % slower at 1-4 tokens per rank, e.g. 46.5 -> 50.2 us uniform and 107.6 -> 117.6 us hot at T 1.rocdl). TheBufferLoadAsyncLDS128batom has no cache-policy state. It also re-derives M0 with onev_readfirstlaneper copy: 207 instead of 78 in the 128-row tier, which then spills 36 more bytes.global_store_dwordx4 ... sc0 sc1(inline asm). A volatilefx.generic_storeemitss_waitcnt vmcnt(0)after every store on gfx950, and atomic stores are at most 8 bytes.BufferAtomicatom does not return the old value.Coherence. The pool is coarse-grained device memory. Everything a peer writes into it is now read with sc0 sc1, as
IpcHeap.allocrequires for coarse-grained memory: arena rows and scales, route records, and the combine inbox. Before this revision, the arena rows were read through L2. So peer data no longer depends on the launch-boundary L2 invalidation. The GEMM1 output rows are this GPU's own memory. They rely on the same kernel-boundary acquire as any kernel that reads another kernel's output across XCDs. The kernel module docstring has the full argument.Known limitations
fused_ep_moe_supportedgates all of these:fuse_shared_expert(top 7, 392 experts) is not supported and must fall back. [FlyDSL] MegaMoE small-M: DeepSeek-V4.1-Flash, any experts per rank / top-k, fused shared-expert slots #8358 covers it.close()releases the pool (collective).Related
AI assistance
AI assistance (Claude Code) was used to write this change; Fangzhou Ai is the human owner and will review/defend it end-to-end.
End-to-end status: end-to-end DSv4.1-Flash DEP8 AgentX results pending.
🤖 Generated with Claude Code