Skip to content

[FlyDSL] MoE stage 1: XCD remap over the valid rows only - #7918

Draft
cpersson-amd wants to merge 18 commits into
ROCm:mainfrom
cpersson-amd:moe-xcd-valid-rows
Draft

cpersson-amd wants to merge 18 commits into
ROCm:mainfrom
cpersson-amd:moe-xcd-valid-rows

Conversation

@cpersson-amd

@cpersson-amd cpersson-amd commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #7567. Two changes to the stage-1 xcd_swizzle remap:

  • It takes the launch grid instead of recounting it. With an intermediate pad (DSv4.1 TP4: 576 of 640 columns) the recount is one N block short, so an _xcd stage 1 leaves the padded columns unwritten. On main, the tuned DSv4.1 TP4 32768-token row gives NaN output from this.
  • It spreads only the sorted rows that num_valid_ids covers over the 8 XCDs, instead of the whole worst-case grid.

The existing DSv4.1 TP4 xcd4 rows get 22–29% faster fused_moe, with no config change. Across the 105 runnable xcd stage-1 rows in model_configs (19 configs), fused_moe is 2.1% faster (geomean) at pad 0, and the output is unchanged. The retune that also moves the 2048–16384 rows to xcd1 stage 1 comes in a separate tuning PR.

Motivation

The remap recounted the grid from the unpadded width, but stage 1 launches tile2_pad more N columns, so the remap permuted a smaller grid than the one launched.

The stage-1 grid is also sized for the worst-case padded row count. Under real routing, 11–25% of its tiles have no rows. The remap distributed the whole grid, so that empty tail landed on the last XCDs and the live work piled onto the others.

Changes

  • mixed_moe_gemm_2stage_common.py:
    • The remap takes gpu.grid_dim for the grid size.
    • It runs over valid_y = ceil(num_valid_ids / (sort_block_m * persist_m)), clamped to [1, grid_y]. Blocks past it keep their ids and exit as before. The num_valid_ids load moves up and the duplicate load goes away.
  • moe_kernels.py: register xcd_swizzle=1 for every stage-1 dtype, not only a16w4.

Performance

MI355X, fused_moe on the DSv4.1 TP4 a8w4 shape (E=384, top-6, model_dim 5120, inter 640, intermediate_pad 64), real routing captured from vLLM, L2/MALL flushed. The tuned rows on main, which use an xcd4 stage 1 at these sizes:

Tokens main (µs) Launch grid only (µs) This PR (µs) Change
8192 1197.1 984.1 938.9 -21.6%
16384 2209.9 1655.6 1590.7 -28.0%
32768 4267.8 (NaN) 3080.9 3034.6 -28.9%

main + #7567 is within 1% of main.

With this PR, xcd1 stage 1 also beats the current rows from 1100 to 16384 tokens (fused_moe -0.9% to -9.9%); those rows come in the tuning PR.

All tuned xcd stage-1 rows

Every row in aiter/configs/model_configs/*tuned_fmoe*.csv whose kernelName1 is an _xcd FlyDSL stage 1: 106 rows over 19 configs. Inputs as in the tuner's --run_config (random weights, synthetic fused_topk routing), called the way vLLM calls fused_moe (the row's activation dtype, INTERLEAVE gate mode for a8w4). The dispatched stage-1 kernel was checked against the row. L2/MALL flushed, host overhead excluded, main + #7567 vs this PR on the same GPU, 4 pairs per row.

Rows whose model gets an intermediate pad from vLLM ran a second time with that pad (and zeroed pad weights): DSv4.1 TP4 (576 → 640) and MiniMax-M2.5 TP8 (192 → 256), both pad 64. vLLM passes pad 0 for DSv4.1 until the stage-1 pad fix is in the pinned AITER.

Rows Geomean Range
Pad 0, all configs 105 -2.1% -7.5% to +1.9%
vLLM pad, DSv4.1 a8w4 (fp8 act) 5 -25.3% -28.2% to -19.7%
vLLM pad, DSv4.1 a4w4 3 -2.8% -4.4% to -1.7%
vLLM pad, MiniMax-M2.5 4 -1.9% -3.1% to -0.5%
Pad 0, per config
Config Rows Geomean Range
dsv3_fp4 1 +0.5%
dsv41_fp4 17 -2.2% -6.2% to +0.4%
dsv41_fp8 6 -3.3% -6.9% to -1.5%
dsv4_fp8fp4 1 -0.7%
glm53flash_fp4 1 +1.8%
glm5_fp4 7 -1.4% -2.5% to +0.2%
glm5_mxfp8 7 -2.0% -4.2% to +0.8%
kimik2_fp4 7 -1.9% -3.1% to +0.6%
kimik2_fp8fp4 7 -0.3% -1.4% to +1.9%
kimik3_a8w4 7 -4.7% -7.5% to -1.4%
mimo_v2_5_pro 6 -2.0% -4.0% to -0.7%
minimax_m25_fp4 8 -2.0% -3.2% to -0.5%
minimax_m3_fp4 4 -4.3% -7.4% to -0.9%
minimax_m3_mxfp4 7 -1.6% -5.6% to +0.6%
minimax_m3_mxfp8 5 -3.5% -6.1% to -1.5%
muse-spark-open-mxfp4_fp4 2 -2.7% -3.6% to -1.7%
qwen3_5_397b_fp4 9 -1.0% -3.4% to +1.7%
qwen3_8_2400b_fp4 2 -2.5% -3.0% to -1.9%
qwen3_vl_fp4 1 -3.1%

Six rows are slower on all four pairs, all at 256 tokens or fewer: glm53flash 256 (+1.8%), qwen3_5_397b 16 (+1.7%), kimik2_fp8fp4 16 (+1.9%), and three at +0.5–0.7%.

No NaN in either tree. Every row with deterministic output in both trees (87 of 117) is bit-identical between them. The other 30 change from run to run within each tree as well. With the vLLM pad, each padded run matches its pad-0 run bit for bit on this PR. On main + #7567, the DSv4.1 a8w4 32768-token row does not: it differs by up to 0.52 in 2 of 4 runs, because stage 1 leaves the padded columns unwritten.

Not covered: the qwen3_8_2400b_fp4 32768 row fails in both trees (stage-2 output over the 32-bit addressing limit), and the mimo_v2_5_pro 65536 row dispatches the w2 variant of its xcd4 kernel because another config has the same key.

Testing

  • Unit tests added or updated

  • Performance benchmarks run

  • Tested on MI250X

  • Tested on MI300X

  • tests/test_flydsl_moe_xcd_swizzle.py:

    • test_flydsl_moe_xcd_swizzle_stage1_padded_tail (xcd_swizzle 1 and 4): routing where the grid is longer than the valid blocks, NaN-filled output. A valid tile the remap skipped would leave NaN.
    • test_flydsl_moe_xcd_swizzle_stage1_inter_pad (xcd_swizzle 1 and 4): the DSv4.1 TP4 padding; the output must match xcd_swizzle 0, padded columns included. Fails on main + [FlyDSL] [Bugfix] Fix MoE XCD swizzle on grids not divisible by 8 #7567.
  • fused_moe with the rows above: no NaN at 8192, 16384 or 32768 tokens.

  • fused_moe on all xcd stage-1 rows in model_configs, with and without the vLLM pad: no NaN, and the output is bit-identical to main + [FlyDSL] [Bugfix] Fix MoE XCD swizzle on grids not divisible by 8 #7567 wherever it is deterministic (details above).

  • op_tests/flydsl_tests/test_flydsl_moe.py and the XCD tests pass.

Documentation

  • No documentation change is needed: kernel scheduling only.

Dependencies

  • No new third-party dependencies added

Requires #7567 (xcd_remap_grid, the XCD swizzle test file) to land first; until then its commits show in this diff. #6598 and #7822 fix the stage-1 padding columns in other ways and do not conflict: the remap reads the launched grid whatever its size.

Breaking Changes

None.

AI assistance

Developed with AI assistance (Claude). I reviewed every change and ran the tests and benchmarks on MI355X.

eppaneamd and others added 14 commits November 6, 2026 09:08
Both FlyDSL MoE GEMM stages remap their workgroups for XCD locality with

    wgid = (lin % 8) * (n // 8) + lin // 8

which is a permutation of the n workgroups only when n is a multiple of
8. Otherwise n % 8 tiles run twice and n % 8 tiles never run: stage 1
leaves rows of its output unwritten (and split-K stage 1 adds some
partial sums twice), the atomic stage 2 adds some expert contributions
twice and drops others, and the reduce stage 2 reads slots nobody
wrote. n depends on the token count, so an `_xcd` kernel can be right
at one token count and wrong at the next.

xcd_remap_bx_by already has the remainder term. Move its remap into
xcd_remap_grid, which takes the grid size from the caller, and call it
from both MoE stages. Also correct the stage-2 comment that called
xcd_swizzle an ignored argument.

Add test_flydsl_moe_xcd_swizzle_uneven_grid: both stages with
xcd_swizzle=4 on a 2 x 18 grid, every tile holding a routed row.

The same fix was proposed in ROCm#4315, closed for inactivity.

Signed-off-by: Rony Leppänen <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The MoE stages put M blocks on grid y and N blocks on grid x, so they
pass grid_y as grid_m; naming the arguments makes that visible. The
docstring now states the helper's xcd_swizzle > 0 precondition, which
xcd_remap_bx_by checks before calling it.

No compiled kernel changes: the gfx950 ISA of both MoE stages, the
persistent and heterogeneous stage 2, and both GEMM callers is
byte-identical to the previous commit's.

Signed-off-by: Rony Leppänen <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI runs op_tests/*.py as sharded scripts, and op_tests/flydsl_tests is not in
the plan, so test_flydsl_moe_xcd_swizzle_uneven_grid never ran there. The
Focused regression tests step runs `python3 -m pytest -q tests` on pull
requests (MI35X shard 0), so the test moves, unchanged, to
tests/test_flydsl_moe_xcd_swizzle.py. It imports its data generator, tolerance
check, tile_k helper and gfx950 skip from the op test file, which keeps the
distinct_experts argument of _generate_a8w4_gui_data.

On MI355X (torch 2.13.0+rocm10.0.0, FlyDSL 0.4.2, an empty FlyDSL cache per
run): with the three kernel files of the merge-base the test fails
(xcd4_uneven_grid_stage1: output has NaN), with this commit's it passes, 0
skipped; the whole tests/ directory in CI's layout runs in 13 s.

Signed-off-by: Rony Leppänen <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The gfx950 skip imported from the op test uses get_gfx(), which follows
GPU_ARCHS and takes its last entry, so on an MI355X with
GPU_ARCHS='gfx950;gfx942' the regression was skipped. It now checks
get_gfx_runtime(), the running GPU, in the test body, as
op_tests/test_flydsl_pa_decode.py does.

On MI355X with GPU_ARCHS='gfx950;gfx942', get_gfx() returns gfx942 and
get_gfx_runtime() gfx950. Under the other settings tried (unset, 'gfx950',
'gfx942;gfx950') both return gfx950.

Signed-off-by: Rony Leppänen <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	aiter/ops/flydsl/kernels/mixed_moe_gemm_2stage_common.py
#	op_tests/flydsl_tests/test_flydsl_moe.py
The stage-1 grid is sized for the worst-case padded row count, and the
xcd_swizzle remap spread that whole grid over the 8 XCDs, so the empty
padding tail (11-25% of the tiles under real routing) landed on the last
XCDs. Remap only the rows num_valid_ids covers; rows past them keep their
ids and exit as before.

Register xcd_swizzle=1 for every stage-1 dtype, not only a16w4.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[email protected]>
With the stage-1 remap over valid rows, the xcd_swizzle=1 stage 1 beats the
rows ROCm#7656 picked. fused_moe on MI355X, real routing, cache flushed, against
the ROCm#7656 rows on the same build: 1536 tokens -6.3%, 3000 -6.5%, 4096
-10.0%, 8192 -2.9%, 12000 -4.2%, 16384 -0.6%.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[email protected]>
…_ids

Routing that leaves the worst-case stage-1 grid longer than the valid
sorted blocks, with xcd_swizzle 1 and 4; a valid tile the remap skips
leaves NaN in the output.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[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 7918 --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.

FP8-A stage 2 sizes its grid from occupancy and ignores the name's persist
flag, so t32x128x128_atomic_bnt2_persist and t32x128x128_atomic_bnt2 compile
and launch the same kernel (identical compile arguments, persist_m=1). Name
the row by the kernel it runs.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[email protected]>
cpersson-amd and others added 3 commits November 18, 2026 20:37
…h-grid remap

ROCm#7796 was closed without merging; ROCm#7822 handles the stage-1 padding in
the dispatch instead. Drop its padding changes and tests, but keep the
remap over the launch grid: stage 1 launches tile2_pad more N columns
than the count the remap recomputed, so with intermediate_pad (DSv4.1
TP4: 576 + 64) an xcd_swizzle stage 1 left the padded columns unwritten.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[email protected]>
DSv4.1 TP4 padding (576 of 640 columns, tile_k 256): stage 1 launches a
tile2_pad N block past the unpadded width. xcd_swizzle 1 and 4 must
write the same output as xcd_swizzle 0, padded columns included.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[email protected]>
Restore dsv41_fp8_tuned_fmoe.csv; the xcd1 stage-1 rows come with the
retune of the DSv4.1 TP4 rows.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[email protected]>
@peymanr

peymanr commented Nov 23, 2026

Copy link
Copy Markdown

We hit the failure this PR fixes in the 32768-token row through #8382: DSv4.1 TP4 a8w4 with intermediate_pad=64 and the xcd4 stage 1 returns non-finite rows. We tested this PR against it. It fixes that case, and the grid-count change alone is what does it. That part is now also posted as a one-term change in #8512.

What we ran. One MI355X, using the vLLM ROCm nightly image of 2026-11-22 (aiter 0.1.29.post1, FlyDSL 0.5.0, torch 2.13 on ROCm 10). The shape was 5120 / 640 (576 real + 64 pad) / 384 experts / top-6, at 8192 and 16384 tokens, with random MXFP4 weights and random routing. Stage 1 was forced to t64x128x256_w4_bnt0_gui_xcd4_fp8 with intermediate_pad=64. Every buffer allocated inside fused_moe was filled with 0xFF before the call, so any stage-1 storage that is never written shows up as NaN. A call counts as failing if any output row is non-finite.

Tree Failing calls Stage-1 columns 576-639 left unwritten
unpatched 32 of 32 32 of 32
#7567 only 32 of 32
this PR (with #7567) 0 of 32
#8512 (remap counts tile2_pad) 0 of 32 0 of 32

The non-swizzled stage 1 failed 0 of 32 in every tree.

What this means for this PR. The part that fixes #8382 is the remap covering the launched grid. Spreading only the valid rows is a separate change and is not needed for the fix. If this PR stays blocked on #7567, #8512 could land first so the bug is fixed in the meantime. It changes one term in compile_mixed_moe_gemm1_common, and with pad 0 the kernel is unchanged. Rebasing this PR over it should then mostly mean dropping that term in favour of gpu.grid_dim.

Not tested:

  • Anything except MI355X with the ROCm 10 / torch 2.13 images. The reporter's ROCm 7.2.4 / torch 2.11 stack was not covered.
  • intermediate_pad=0: we saw no failure there.
  • How often NaN appears under the normal allocator without the 0xFF fill.
  • End-to-end vLLM or SGLang, real DeepSeek-V4.1 weights, or multi-GPU tensor parallelism.
  • The gate-only and separate gate/up paths, tile_k other than 256, atomic stage 2 and split-K.
  • Kernel timing of either change.

@Arist12

Arist12 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Some numbers for the valid-rows remap under EP routing, which the tuner's all-local routing doesn't show.

MI355X, aiter main 21ef589 + FlyDSL 0.5.5, stage 1 cold (512 MB flush before each call), 3 seeds x 20 calls, A/B interleaved, outputs bitwise equal. Routing as EP dispatch produces it (expert_mask, each token has at least one local route):

row speedup
kimik3_a8w4 E112 tok4 (topk 16/17) 3.2-3.5x
kimik3_a8w4 E112 tok8 3.7-3.9x
dsv4 E24 tok8 2.8-2.9x

With all-local routing the same rows are 0.96-1.09x, in line with your tuner numbers. The same-tile non-xcd variants give the same gain under EP routing, so xcd rows tuned on all-local routing can hide this.

This branch has not been deployed

No deployments
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.

5 participants