Repository navigation
[FlyDSL] MoE stage 1: XCD remap over the valid rows only - #7918
cpersson-amd wants to merge 18 commits into
Conversation
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]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
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]>
…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]>
|
We hit the failure this PR fixes in the 32768-token row through #8382: DSv4.1 TP4 a8w4 with 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
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 Not tested:
|
|
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 (
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. |
Summary
Follow-up to #7567. Two changes to the stage-1
xcd_swizzleremap:_xcdstage 1 leaves the padded columns unwritten. On main, the tuned DSv4.1 TP4 32768-token row gives NaN output from this.num_valid_idscovers over the 8 XCDs, instead of the whole worst-case grid.The existing DSv4.1 TP4
xcd4rows get 22–29% fasterfused_moe, with no config change. Across the 105 runnablexcdstage-1 rows inmodel_configs(19 configs),fused_moeis 2.1% faster (geomean) at pad 0, and the output is unchanged. The retune that also moves the 2048–16384 rows toxcd1stage 1 comes in a separate tuning PR.Motivation
The remap recounted the grid from the unpadded width, but stage 1 launches
tile2_padmore 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:gpu.grid_dimfor the grid size.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. Thenum_valid_idsload moves up and the duplicate load goes away.moe_kernels.py: registerxcd_swizzle=1for every stage-1 dtype, not only a16w4.Performance
MI355X,
fused_moeon the DSv4.1 TP4 a8w4 shape (E=384, top-6, model_dim 5120, inter 640,intermediate_pad64), real routing captured from vLLM, L2/MALL flushed. The tuned rows on main, which use anxcd4stage 1 at these sizes:main + #7567 is within 1% of main.
With this PR,
xcd1stage 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
xcdstage-1 rowsEvery row in
aiter/configs/model_configs/*tuned_fmoe*.csvwhosekernelName1is an_xcdFlyDSL stage 1: 106 rows over 19 configs. Inputs as in the tuner's--run_config(random weights, syntheticfused_topkrouting), called the way vLLM callsfused_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.
Pad 0, per config
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_fp432768 row fails in both trees (stage-2 output over the 32-bit addressing limit), and themimo_v2_5_pro65536 row dispatches thew2variant of itsxcd4kernel 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_moewith the rows above: no NaN at 8192, 16384 or 32768 tokens.fused_moeon allxcdstage-1 rows inmodel_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.pyand the XCD tests pass.Documentation
Dependencies
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.