Skip to content

[FlyDSL] Fused expert-parallel MoE in one launch per rank for DeepSeek-V4.1-Flash EP8 a8w4 (gfx950) - #8368

Closed
Fangzhou-Ai wants to merge 3 commits into
ROCm:mainfrom
Fangzhou-Ai:fai/dsv41f-fused-ep8-moe
Closed

Fangzhou-Ai wants to merge 3 commits into
ROCm:mainfrom
Fangzhou-Ai:fai/dsv41f-fused-ep8-moe

Conversation

@Fangzhou-Ai

@Fangzhou-Ai Fangzhou-Ai commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What

aiter.ops.flydsl.fused_ep_moe.FusedEPMoE runs a whole expert-parallel MoE layer as one persistent FlyDSL launch per rank. The launch does five things:

  1. MXFP8 quant of the bf16 rows. MXFP8 rows with e8m0 scales are also accepted as input.
  2. Dispatch to the expert owners. Each row goes once to each distinct destination rank.
  3. GEMM1 with silu(min(g, 10)) * clamp(u, -10, 10) and MXFP8 requant in the epilogue.
  4. GEMM2 with the route weights. Each GEMM2 tile returns its weighted rows to the token's rank as soon as it finishes.
  5. A deterministic combine: the six slots are summed in slot order and rounded to bf16 once. There are no atomics into outputs.

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: adds IpcHeap.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 for bench_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_eligible admit 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

from aiter.ops.flydsl.fused_ep_moe import FusedEPMoE, FusedEPMoEWeights, fused_ep_moe_supported

ok = fused_ep_moe_supported(world_size=8, model_dim=5120, inter_dim=2304, experts=384, topk=6,
                            quant="a8w4", swiglu_limit=10.0, act="silu", max_tokens_per_rank=T)
moe = FusedEPMoE(rank=r, world_size=8, group=ep_cpu_group, device=dev, experts_per_rank=48)  # collective
w = FusedEPMoEWeights(w13, w13_scale, w2, w2_scale)  # the shuffled tensors MegaMoEV2 takes
# or FusedEPMoEWeights.from_row_major(...) from checkpoint layouts
moe.prepare(w)                                      # collective, before graph capture: compiles all tiers
out = moe.forward(x_bf16, None, w, topk_weights_f32, topk_ids_i32)   # bf16 [m, 5120]
out = moe.forward(x_fp8, x_scales_e8m0, w, topk_weights, topk_ids)   # MXFP8 input

Shapes and contract:

  • Weights: one set per layer. One instance serves every layer.
  • Tokens: m tokens per rank, 0 <= m <= 1024. Idle ranks call with m = 0.
  • Expert ids: topk_ids are 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.
  • Graphs: the call is CUDA-graph capturable, and the kernel resets its own counters.
  • Concurrency: every rank calls forward once 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.
  • Checks: rank / world_size must match group. Every tensor must be on the instance's device (device="cuda" takes the current device). rows_hint must be at most 1024.

Eligibility (fused_ep_moe_supported):

  • gfx950 with all 8 XCDs (256 CUs).
  • Hidden 5120, inter 2304, top 6, a8w4, SiLU, swiglu_limit 10.
  • 2, 4 or 8 ranks, 1-63 experts per rank, at most 1024 tokens per rank.

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:

Correctness

python3 op_tests/multigpu_tests/test_fused_ep_moe.py. CI runs it this way. Without RANK set, the script starts torchrun --standalone --nproc-per-node 8 itself. 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> and pytest <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:

  • uniform, skewed and all-to-rank-0 routing;
  • bf16 and MXFP8 rows;
  • unequal and empty ranks, e.g. [0, 5, 300, 64, 1, 0, 128, 33];
  • unrouted ids (-1, and ids past the last expert);
  • 16 to 1024 tokens per rank.

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:

  • cosine >= 0.99999 over the output;
  • cosine >= 0.9999 per row;
  • 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 second run;
  • a run after other tokens on the same routes;
  • CUDA-graph replays with new inputs.

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:

  • 8x MI355X, one process per GPU, DeepSeek-V4.1-Flash EP8 layer (48 experts per rank), bf16 tokens in.
  • 100 distinct calls captured as one CUDA graph each, replayed back to back.
  • us per call is the max over the ranks. Each number is the best of 4 A B B A samples from 2 runs (8 for this PR, which ran in both). Spread is under 2 % except at hot T 1 ([FlyDSL] [CI] MegaMoEV2: one route plan for every path, shared arenas, and the DeepSeek-V4.1-Flash EP2 / EP4 / EP8 fused routes #7788 4.1 %, this PR 2.3 %) and one 281 us outlier sample of this PR at hot T 16 (the other seven are 154.0-154.8 us).
  • Command: op_tests/multigpu_tests/bench_fused_ep_moe.py.
  • Routing: uniform (random top 6 of 384); zipf (Zipf 1.2 popularity over a fixed random expert order); hot (every route to rank 0's 48 experts).

Columns:

uniform (us per call)

tokens/rank main #8358 #7788 this PR main / this PR #8358 / this PR #7788 / this PR
1 87.8 69.8 47.3 47.1 1.86x 1.48x 1.01x
16 198.1 167.6 162.7 144.5 1.37x 1.16x 1.13x
32 207.3 179.0 167.5 156.4 1.33x 1.14x 1.07x
64 228.4 180.1 170.3 167.8 1.36x 1.07x 1.02x
128 231.4 192.6 181.5 178.5 1.30x 1.08x 1.02x
192 356.7 228.7 221.1 188.3 1.89x 1.21x 1.17x
256 362.6 242.7 233.7 199.7 1.82x 1.22x 1.17x
384 394.9 280.7 268.8 227.6 1.73x 1.23x 1.18x
512 437.0 365.1 350.9 299.6 1.46x 1.22x 1.17x
768 514.9 - 354.3 363.9 1.41x - 0.97x
1024 581.3 - 383.2 489.9 1.19x - 0.78x

zipf (us per call)

tokens/rank main #8358 #7788 this PR main / this PR #8358 / this PR #7788 / this PR
1 83.6 53.3 44.2 41.7 2.00x 1.28x 1.06x
16 154.8 119.9 114.3 104.2 1.49x 1.15x 1.10x
32 192.7 164.9 149.7 136.4 1.41x 1.21x 1.10x
64 261.1 221.7 204.2 168.6 1.55x 1.32x 1.21x
128 365.6 319.4 284.2 234.3 1.56x 1.36x 1.21x
192 465.2 358.8 338.3 293.1 1.59x 1.22x 1.15x
256 534.3 419.5 394.1 355.1 1.50x 1.18x 1.11x
384 571.5 534.3 503.3 492.2 1.16x 1.09x 1.02x
512 682.8 647.7 629.5 602.4 1.13x 1.08x 1.05x
768 887.6 - 686.5 874.1 1.02x - 0.79x
1024 1095.5 - 823.2 1154.3 0.95x - 0.71x

hot (us per call)

tokens/rank main #8358 #7788 this PR main / this PR #8358 / this PR #7788 / this PR
1 165.7 148.9 111.4 109.9 1.51x 1.36x 1.01x
16 213.0 179.4 167.6 154.0 1.38x 1.17x 1.09x
32 291.5 266.6 232.8 207.2 1.41x 1.29x 1.12x
64 474.0 377.3 315.6 238.1 1.99x 1.58x 1.33x
128 757.6 641.5 516.6 405.1 1.87x 1.58x 1.28x
192 764.4 706.0 626.8 574.6 1.33x 1.23x 1.09x
256 935.4 887.1 786.1 736.1 1.27x 1.21x 1.07x
384 1182.3 1276.6 1131.7 1071.4 1.10x 1.19x 1.06x
512 1510.7 1651.7 1477.0 1241.2 1.22x 1.33x 1.19x
768 2155.2 - 1464.4 1843.5 1.17x - 0.79x
1024 2810.4 - 1888.5 2442.3 1.15x - 0.77x

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.

  • Up to 192 tokens per rank: HBM-bound on the expert weights. The GEMM loops stream the weights at 87-97 % of the HBM ceiling. The whole call reaches 71-80 %, because arrival, the first tile and the combine tail leave HBM idle.
  • From 384 tokens per rank: bound by the return stores of GEMM2 over xGMI. At T 768 that is 47 MB in about 122 us, about 385 GB/s, at the ceiling.

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

  • Typed ops. Loads and stores go through FlyDSL buffer tensors and fx.copy with BufferCopy atoms. 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 legacy kernels/buffer_ops.py. The gfx950 ISA is unchanged: the same buffer loads, stores and cache bits.
  • Typed ops, continued. Also typed:
    • the FP8 converts: fx.rocdl.cvt_scalef32_pk_fp8_f32;
    • the device-scope atomics: fx.atomic_add / fx.atomic_xchg with SyncScope.Agent;
    • LDS: fx.generic_load / fx.generic_store on shared pointers;
    • the 32- and 64-row tiers' MFMA: fx.rocdl.mfma_scale_f32_16x16x128_f8f6f4 with opsel.
  • Shared helpers. The XCC id comes from communication_ops_utils.hw_xcc_id, and the wave prefix scan uses dpp_utils.update_dpp_i32.
  • Raw ops that remain, each with its reason at the call:
    • AGPR-accumulator MFMA (inline asm) in the 16- and 128-row tiers. With the typed op, the 128-row tier compiles to about 6500 v_accvgpr copies 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.
    • Async buffer-to-LDS copy (raw rocdl). The BufferLoadAsyncLDS128b atom has no cache-policy state. It also re-derives M0 with one v_readfirstlane per copy: 207 instead of 78 in the 128-row tier, which then spills 36 more bytes.
    • 16-byte global_store_dwordx4 ... sc0 sc1 (inline asm). A volatile fx.generic_store emits s_waitcnt vmcnt(0) after every store on gfx950, and atomic stores are at most 8 bytes.
    • Counter poll (raw buffer atomic). Lanes are dropped by an out-of-range offset with no branch, and the BufferAtomic atom 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.alloc requires 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

  • Shape-specialized. fused_ep_moe_supported gates all of these:
    • hidden 5120, inter 2304, top 6, a8w4, clamp-SwiGLU 10;
    • at most 63 experts per rank and at most 1024 tokens per rank;
    • gfx950 with 256 CUs.
  • No fused shared expert. vLLM's 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.
  • 769-1024 tokens per rank. The gate admits up to 1024, but under Zipf routing the kernel is not faster than main's staged route there: 1.02x at 768 and 0.95x at 1024. Callers should fall back above 768. Prefill beyond 1024 tokens per rank is out of scope.
  • Memory. Each instance holds, per rank, an IPC-mapped pool of about 0.11 GB (8 ranks, 48 experts per rank) plus about 0.13 GB of scratch. close() releases the pool (collective).
  • Not wired into MegaMoEV2 or any framework here. The vLLM call site is a separate PR.

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

…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]>
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter standard tests on MI35X
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 8368 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

Fangzhou-Ai and others added 2 commits November 20, 2026 14:52
- 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]>
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