Skip to content

[FusedMoE] DSV4.1-Flash MoE kernel improvements - #27

Draft
sshlyapn wants to merge 11 commits into
Inferact:mainfrom
sshlyapn:sshliapn/features/fused-ep-clean
Draft

sshlyapn wants to merge 11 commits into
Inferact:mainfrom
sshlyapn:sshliapn/features/fused-ep-clean

Conversation

@sshlyapn

@sshlyapn sshlyapn commented Dec 1, 2026 •

Copy link
Copy Markdown

This PR speeds up the fused expert-parallel MoE kernel for DeepSeek-V4.1-Flash decode on 8x MI355X, mostly when expert routing is skewed. Outputs are bit-identical to the current kernel on every tested case. The largest change gives each XCD its own work queue over a run of consecutive row blocks, so that a hot expert's blocks share one XCD's L2. The other changes are smaller scheduling, dispatch, load-addressing and epilogue changes.

Changes (one commit each, in order).

  • One work queue per XCD over a run of consecutive row blocks, with stealing from the fullest queue when the own queue is empty. Used in the 64-row tiers and up, and only when some expert has more than one block (otherwise the existing single queue runs). A hot expert's blocks then land on one or two XCDs, whose L2 serves its weights to the later blocks.
  • Weight prefetch during the arrival wait (8-wave tiers). While wave 0 polls the arrival flags, waves 1-7 touch the first GEMM1 tile's weights (8 K steps), so HBM is not idle before the rows arrive.
  • Late queue claim in the 64-row tier. For the last 128 items of an XCD queue, the next item is claimed when the current one ends rather than when it starts, so an item is not reserved by a busy workgroup at the end of the call.
  • Late record-slot claim from 160 tokens per rank. A route's record slot on the owner is claimed after that route's row stores are acknowledged, rather than all at once at the start of the dispatch.
  • Combine spread over every thread of the launch (8-wave tiers). Each thread reduces one 8-column chunk with all inbox loads in flight. The sum order per output element is unchanged.
  • Wait states after the inline 16-byte inbox store. s_nop 1 after the store gives the 2 wait states the ISA requires before its data registers change. LLVM does not insert them after inline asm. The current builds complied only by scheduling; a reordered build produced wrong, nondeterministic outputs (standalone repro: 6.1% / 0.1% / 0 wrong dwords at 0 / 1 / 2 wait states).
  • K-loop weight and scale loads fold the K offset into the per-lane index instead of rewriting the scalar offset register on every K step (SGPR spills 115 to 76).
  • Item-to-expert lookup by one LDS read plus a ballot instead of a 6-step dependent search, and the GEMM1 epilogue clamps and absmax as single v_min_f32 / v_max_f32. Results differ only for a -inf gate accumulator.
  • GEMM1 activation ring DMA in full 128-byte lines (8 rows x 128 B per instruction instead of 16 rows x 64 B), with the LDS chunks XOR-swizzled by row so that the MFMA operand reads stay free of bank conflicts.
  • 128-row tiles: spill-free K loops, and empty 16-row tiles skip their MFMAs.
  • Non-temporal weight-scale loads for single-block experts, matching their weight loads.

Results: kernel. Measured on 8 x MI355X with graph replay, 8 rotated weight sets (cold), max over ranks. Synthetic routing at M = tokens per rank (C128 / C160 / C256 with DSpark k=5 = M96 / M120 / M192). Ratio = new / current kernel, lower is better. Measured on an intermediate head, same hour as the current kernel:

routing M80 M96 M120 M160 M192 M256 M512
skewL30 (synthetic skew set) 0.936 0.909 0.913 0.899 0.888 0.856 0.834
skewL10 (synthetic skew set) 0.933 0.913 0.893 0.864 0.848 0.851 0.844
uniform 0.976-0.983 0.976-0.983 0.976-0.983 0.976-0.983 0.976-0.983 0.984 0.924
  • The last three commits (128-byte DMA, epilogue / ballot defaults, scale cache policy) add another 0.5-1.5% on top of this table.
  • M16 / M32 are unchanged (0.998-1.006); the 4-wave tier does not use the queue or prefetch changes.
  • Real captured C128 routing at M96: 0.888-0.946 with linear expert placement, 0.960-0.974 with recipe G's S49 replica plan.

Results: end to end. Recipe G's serving configuration (DeepSeek-V4.1-Flash-1017, DEP8, DSpark k=5, S49 plan, input 8192 / output 6144, real acceptance). Median ITL over 2 runs, with this branch's package mounted over the image's amd_kernels:

concurrency current kernel this branch ratio
C128 14.50 / 14.52 ms 13.94 / 14.10 ms 0.966
C160 15.33 / 15.38 ms 14.69 / 14.75 ms 0.959
C256 18.89 / 18.90 ms 17.85 / 17.86 ms 0.945

Correctness.

  • Bit-identical outputs against the current kernel: 24 standard cells, 18 cells at M80-192, and 6 serving-like sets from captured routing (48 and 49 slots). Every cell covers 8 ranks x 8 samples, and two runs gave identical output.
  • pytest tests/moe/fused_ep_mxfp4_h5120_i2304_e48_top6_ranks8: 4 passed.

…(64-row tiers up, when an expert has several blocks)

Signed-off-by: Sergei Shliapnikov <sergei.shliapnikov@amd.com>
… in the 8-wave tiers

Signed-off-by: Sergei Shliapnikov <sergei.shliapnikov@amd.com>
…when the current item ends

Signed-off-by: Sergei Shliapnikov <sergei.shliapnikov@amd.com>
…fter its rows are acknowledged

Signed-off-by: Sergei Shliapnikov <sergei.shliapnikov@amd.com>
…-wave tiers, all inbox loads in flight

Signed-off-by: Sergei Shliapnikov <sergei.shliapnikov@amd.com>
Signed-off-by: Sergei Shliapnikov <sergei.shliapnikov@amd.com>
…rites

Signed-off-by: Sergei Shliapnikov <sergei.shliapnikov@amd.com>
… as min / max

Signed-off-by: Sergei Shliapnikov <sergei.shliapnikov@amd.com>
…OR-swizzled by row

Signed-off-by: Sergei Shliapnikov <sergei.shliapnikov@amd.com>
…kip their MFMAs

Signed-off-by: Sergei Shliapnikov <sergei.shliapnikov@amd.com>
Signed-off-by: Sergei Shliapnikov <sergei.shliapnikov@amd.com>
@Fangzhou-Ai

Copy link
Copy Markdown
Collaborator

Hi @sshlyapn thanks for the PR, can you test your kernel on some real workloads? e.g. an hour long agentx test

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.

2 participants