Repository navigation
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 #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 GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
|
@aiter-bot review |
There was a problem hiding this comment.
🟡 Changes recommended
The new regression is excluded from CI’s test list and requires pytest dispatch, so CI does not execute its assertions.
1 open issue
What changed in this PR
Fixes FlyDSL MoE workgroup mapping on grids not divisible by eight, preventing duplicate and skipped tiles in both GEMM stages.
Changes:
- Extracts the remainder-aware XCD remap into a shared helper and uses it in both MoE stages.
- Adds a regression test covering both stages on a 36-workgroup grid, with NaN-filled stage-1 output to detect unwritten rows.
- Corrects a comment describing stage-2
xcd_swizzlebehavior.
| File | Description |
|---|---|
op_tests/flydsl_tests/test_flydsl_moe.py |
Adds distinct-expert routing and an uneven-grid regression test. |
aiter/ops/flydsl/moe_kernels.py |
Clarifies that xcd_swizzle selects the workgroup remap; comment-only change. |
aiter/ops/flydsl/kernels/mixed_moe_gemm_2stage_common.py |
Replaces both incomplete MoE remaps with the shared helper. |
aiter/ops/flydsl/kernels/mfma_preshuffle_pipeline.py |
Extracts and exports xcd_remap_grid, preserving the existing GEMM remap behavior. |
Review effort: Balanced
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]>
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
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]>
There was a problem hiding this comment.
🔵 Needs a closer look
GPU execution was unavailable, leaving correctness and latency unverified across shared-remap paths—including split-K, reduce, persistent, and heterogeneous MoE variants—beyond the focused regression’s coverage.
1 open issue
1 resolved since last review
Review effort: Balanced
Brings in #7774, which ports op_tests/test_flydsl_moe_a16wfp4.py's stage-2 cache-key test to FlyDSL 0.5.0. The version this branch last merged imports _jit_function_cache_key, which FlyDSL 0.5.0 does not have, so that test failed in CI. No conflicts. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Rony Leppänen <[email protected]>
|
|
||
|
|
||
| def test_flydsl_moe_xcd_swizzle_uneven_grid(): | ||
| """xcd_swizzle on a 2 x 18 grid in both stages: 36 workgroups, not a multiple |
There was a problem hiding this comment.
36 workgroups is not a multiple of 8 but is still a multiple of 4. So on a 4-die part the test still passes with the previous code. Changing to topk=3 and token=7 gives a workgroup count of 42, which is divisible by neither.
There was a problem hiding this comment.
🔵 Needs a closer look
Shared GPU scheduling changes affect MoE and GEMM, but hardware correctness/performance validation is unavailable, leaving the author’s gfx950 evidence and unexecuted split-K, reduce, and persistent variants for human review.
No open issues 🎉
Review effort: Balanced
|
No activity for 15 days, so this is now labelled |

Summary
Fixes the XCD swizzle of the FlyDSL MoE GEMMs (
_xcd{N}kernels, stage 1 and stage 2) for grids whose workgroup count is not a multiple of 8. On such grids some tiles ran more than once and as many never ran, which gave wrong MoE output at some token counts.Motivation
Both MoE stages in
mixed_moe_gemm_2stage_common.pyremapped workgroups withThat is a permutation of the
nworkgroups only whenn % 8 == 0. Withn ≥ 8andr = n % 8 != 0, wgidsq, 2q, …, r·q(q = n // 8) are produced twice and the lastrnever; with fewer than 8 workgroups every workgroup computes tile 0:_kb{N}) also adds the partial sums of the duplicated tiles twice, since it accumulates atomically.n = gx * gydepends on the token count, so an_xcdkernel can be correct at one token count and wrong at the next. Tuned rows are tuned and checked at their bucket's token count, where the grid is often a multiple of 8, so this survives tuning. For example, a stage-2t32x128x256_atomic_xcd4kernel that tuning picked for model 2560, inter 512, 513 experts, top-11 (a row not inaiter/configs) is correct at even token counts and wrong at every odd one (9–15).xcd_remap_bx_byinmfma_preshuffle_pipeline.pyalready has the remainder term (_clip = min(xcd, n % 8)). Among the FlyDSL XCD remaps, only these two MoE copies lacked it.Tuned tables on main (
f705d6b7a0) that name these kernels (computed from the launch-grid code, not run): 113 rows inaiter/configsname a v1_xcdMoE kernel with fp8/fp4 activations. For 83 of them, some token count in the row's bucket launches a grid that is not a multiple of 8. 81 of those are stage-1 rows, whose output is wrong only when the skipped last M blocks hold routed rows (a duplicated stage-1 tile stores the same values twice). The other two, intuned_fhmoe.csv(model 7168, inter 384, 385 experts, top-7; buckets 128 and 256), use_xcd4in an atomic stage 2, where a duplicated tile adds twice. At 100, 200 and 256 tokens their stage-2 grid is 28 × 407, 28 × 429 and 28 × 441.Changes
aiter/ops/flydsl/kernels/mfma_preshuffle_pipeline.py: newxcd_remap_grid(bx, by, grid_m, grid_n, *, xcd_swizzle, num_xcds=8), the remapxcd_remap_bx_byalready did, with the grid size from the caller.xcd_remap_bx_bycalls it; a preshuffle and an mxscale GEMM withxcd_swizzle=4compile to byte-identical ISA before and after.aiter/ops/flydsl/kernels/mixed_moe_gemm_2stage_common.py: stage 1 and stage 2 callxcd_remap_gridinstead of their own copy of the remap (net −19 lines). Thenum_xcds = 8and grid-size lines are unchanged.aiter/ops/flydsl/moe_kernels.py: the stage-2 comment no longer callsxcd_swizzlean ignored argument; it selects the XCD remap.tests/test_flydsl_moe_xcd_swizzle.py(new):test_flydsl_moe_xcd_swizzle_uneven_grid, both stages withxcd_swizzle=4on a 2 × 18 grid (36 workgroups), 9 tokens routed to 18 distinct experts so every tile holds a routed row. Stage 1 writes into a NaN-filled buffer. It runs only when the running GPU is gfx950 (get_gfx_runtime()), so a multi-archGPU_ARCHSdoes not skip it. It is intests/because CI runspython3 -m pytest -q testson pull requests, andop_tests/flydsl_tests/is not in the sharded op-test plan; it imports its data generator and check fromop_tests/flydsl_tests/test_flydsl_moe.py.op_tests/flydsl_tests/test_flydsl_moe.py:_generate_a8w4_gui_datagainsdistinct_experts(default off) for the new test.Compiled for gfx950, the stage-1, stage-2 and heterogeneous stage-2 MoE kernels gain 9 instructions and the persistent stage 2 gains 2, with the same VGPR, SGPR, LDS and spill counts.
Performance (if applicable)
MI355X, model 2560, inter 512, 513 experts, top-11, fp4 activations and weights. Each row alternates fresh main and branch processes (main, branch, branch, main, main, branch, branch, main) and gives the median of the four per-process medians, with their range. At 16 tokens both grids are multiples of 8, so main and the branch run the same tiles.
t64x128x256_w2_xcd4alone, 16 tokens, 5 × 100 launchest32x128x256_atomic_xcd4alone, 16 tokens, 5 × 100 launchesxcd_swizzle=0(code unchanged), stage 1 / stage 2fused_moethrough the one-rowAITER_CONFIG_FMOEdescribed under Validation,run_perftest(20 iterations, 3 per process), 16 tokens_xcd4kernel in a profile of those calls (100 calls, inputs rotated over 20 copies), 16 tokensAn earlier
fused_moetiming run inside an accuracy script, which computed torch references between timings, had the branch 1.4–2.3 % slower at 9 and 16 tokens, while main's own four processes there differed by up to 3.7 %. Neither the kernel profile nor the runs above reproduce the gap.Testing
Tested on MI355X (gfx950) only; see "Validation and unverified scope".
Documentation
xcd_remap_gridhas a docstring; no user-facing behavior changes beyond the fix.Dependencies
Breaking Changes
None.
PR Self-Review
Reviewed revision
d7ec3d633Behavior and intent
The remap in
_xcd{N}MoE kernels is now a permutation of the grid the kernel computes, for any workgroup count. Before, grids with a workgroup count not divisible by 8 ran some tiles twice and skipped as many, so the output was wrong at some token counts (stage 1 rows unwritten; stage 2 contributions doubled or dropped). The remap is unchanged when the count is divisible by 8. Stage 1 still computes its grid size in the kernel, without thetile2_padthe host adds to its launch; where the two differ, #6598'sgpu.grid_dimreads are the fix.Validation and unverified scope
One MI355X (gfx950), ROCm 7.2, FlyDSL 0.4.2; main at
f705d6b7a0against this branch's first commit, except where stated. The second commit only names arguments and edits a docstring; every kernel below compiles to byte-identical gfx950 ISA under both commits. The third commit only moves the test. The fourth merges main (9c119bb65), which pins FlyDSL 0.5.0 (#7600); this branch merged without conflicts. The fifth only changes the test's skip condition fromget_gfx(), which takes the lastGPU_ARCHSentry, toget_gfx_runtime(), the running GPU; the test runs below predate it.vllm/vllm-openai-rocmnightly083fbac021(torch 2.13.0+rocm10.0.0), an empty FlyDSL cache for each run:python3 -m pytest -rA tests/test_flydsl_moe_xcd_swizzle.pyfails with main's three kernel files (AssertionError: xcd4_uneven_grid_stage1: output has NaN) and passes with this branch's, 0 skipped, both in CI's layout (the merged source tree with the image's prebuilt.sofiles copied in) and with the kernel files overlaid on the installed aiter. The wholetests/directory in CI's layout: main's kernels 4 failed and 355 passed (15 s); this branch's 3 failed and 356 passed (14 s). The 3 common failures are in files this change does not touch:test_all_reduce_config.py::AllReduceConfigTest::test_communicator_reads_explicit_bytes_before_import,test_import_without_gpu.py::test_import_aiter_without_visible_gpu(RuntimeError: No gfx arch found in rocminfo output.in that container) andtest_mega_moe_gfx1250_recipe.py::test_layout(an arena-size assertion).python3 -m pytest -rA tests/test_flydsl_moe_xcd_swizzle.pyfails with the merge-base's kernel files (AssertionError: xcd4_uneven_grid_stage1: output has NaN) and passes with this branch's, 0 skipped.tests/directory in CI's layout (the source tree, with the image's prebuilt.sofiles copied in): 13 s with this branch's kernels, 236 passed and 3 failed; with the merge-base's, 235 passed and 4 failed (the moved test). The 3 common failures are in files this change does not touch:test_all_reduce_config.py::AllReduceConfigTest::test_communicator_reads_explicit_bytes_before_importandtest_import_without_gpu.py::test_import_aiter_without_visible_gpu(RuntimeError: No gfx arch found in rocminfo output.in that container) andtest_mega_moe_gfx1250_recipe.py::test_layout(an arena-size assertion).op_tests/flydsl_tests/test_flydsl_moe.pycollected 34 tests after the move (before the merge of main) and they were not run:xcd_swizzleis non-zero only in the moved test. With the test still in that file, main 34 passed and 1 failed (the new test), and the branch 35 passed.xcd4_uneven_grid_stage2: checkAllclose failed (err=0.2665, max=0.05)), and with the remainder term removed from the helper (stage-1 NaN).op_tests/test_fhmoe.py -k production_decode_and_prefill: 17 passed on both. Its 4096-, 8192- and 32768-token rows run_xcd4stage 1 on grids that are not multiples of 8 (4 × 833, 4 × 833 and 4 × 2561, computed from the launch code), so the new remap runs there. The test routes every token to the same 9 experts, which leaves the skipped last M block empty, so main passes too.xcd_swizzle=4kernel againstxcd_swizzle=0in the same process (a standalone script, not in this PR):t32x128x256_atomic, fp4 activations and weights (model 2560, inter 512, 513 experts, top-11), 9–16 tokens: on main, odd counts give rel L2 0.026–0.040 against the torch reference vs 0.005 forxcd_swizzle=0; on the branch every count gives 0.005, with a max abs difference fromxcd_swizzle=0of 0.25–0.44, the scale of twoxcd_swizzle=0runs (0.25–0.38, atomic order).t32x256x128_atomic_bnt2, fp8 activations, fp4 weights (model 7168, inter 384, 385 experts, top-7), 100/102/200/256 tokens: on main the (token, N-tile) pairs that differ by more than 4× thexcd_swizzle=0run-to-run spread are exactly the ones the old remap runs twice or skips (9/0/22/13 pairs); on the branch none differ. This ran the generic stage 2 at that shape, not the heterogeneous kernel thetuned_fhmoe.csvrows name.fused_moewith a one-rowAITER_CONFIG_FMOE(token 16,flydsl_moe1_afp4_wfp4_bf16_t32x64x256_w4_kw2_fp4+flydsl_moe2_afp4_wfp4_bf16_t32x128x256_atomic_xcd4, model 2560, inter 512, 513 experts, top-11), the dispatch log naming both kernels, 9–16 tokens × 3 seeds, against the torch reference with the stage-1 output re-quantized: odd counts rel L2 0.046–0.093 on main vs 0.031–0.044 on the branch; even counts equal to within 0.0001. Main's worstlogits_diffis 0.0043, undertest_moe_2stage.py's 0.01 gate.GPU_ARCHSunset,gfx950orgfx942;gfx950,get_gfx()andget_gfx_runtime()both return gfx950; withgfx950;gfx942,get_gfx()returns gfx942, so the earlier condition skipped the test and the new one runs it.aiter.aot.flydsl.dry_run): the twoxcd_remap_bx_bycallers, a preshuffle and an mxscale GEMM withxcd_swizzle=4, give byte-identical ISA on main and the branch (theirxcd_swizzle=0builds differ, so the comparison sees code changes).Not verified:
_xcd; their failure modes above come from reading the code.Focused regression testsstep (python3 -m pytest -q tests, MI35X shard 0), not in the op-test shards (op_tests/flydsl_tests/test_flydsl_moe.pyis not insplit_tests.sh's plan). The new runs used torch 2.13.0+rocm10.0.0 and a FlyDSL cache emptied for each run; CI's torch differs, and CI starts from the wheel's prebuilt FlyDSL cache.op_tests/flydsl_tests/test_flydsl_moe.pydo not run in CI; none of them passesxcd_swizzle.PR-specific question
What the changed op test fails on before this PR, and why existing tests do not cover it:
tests/test_flydsl_moe_xcd_swizzle.py::test_flydsl_moe_xcd_swizzle_uneven_gridfails on main at its stage-1 assertion (xcd4_uneven_grid_stage1: output has NaN: the rows of the skipped tiles keep the NaN fill). With only the stage-2 call reverted it fails at stage 2 (xcd4_uneven_grid_stage2: checkAllclose failed (err=0.2665, max=0.05): a duplicated tile adds its contribution twice). It passes on the branch._xcdkernels only at a tuned row's bucket token count, where the grid is often a multiple of 8 (test_moe_2stage.py's CSV cases,test_fhmoe.py's production rows). Where it is not,test_fhmoe.pyroutes every token to the same 9 experts, so the skipped last M block holds no routed row and main passes (17 passed on both);test_moe_2stage.pyfails a case only whenlogits_diffexceeds 0.01, and main's worst is 0.0043.Changed op tests
op_tests/flydsl_tests/test_flydsl_moe.py: only_generate_a8w4_gui_datachanges, with adistinct_expertsargument (default off) that the new test uses; the new test itself istests/test_flydsl_moe_xcd_swizzle.py::test_flydsl_moe_xcd_swizzle_uneven_grid. It runs both MoE stages withxcd_swizzle=4on a grid that is not a multiple of 8, with every tile holding a routed row, fails on main (stage-1 rows left NaN) and passes with the fix. Existing tests run_xcdkernels only as tuned rows at the row's bucket token count (test_moe_2stage.py's CSV cases,test_fhmoe.py's production rows). There the skipped tiles often hold no routed row:test_fhmoe.pyroutes every token to the same 9 experts, and random routing often leaves the last M blocks empty.test_moe_2stage.pyalso fails a case only whenlogits_diffexceeds 0.01, which a few doubled tiles do not reach.Related
xcd_remap_bx_by's remap, and adds a GPU test of both stages.num_xcdsa parameter in the same blocks; it merges with this change without conflicts.gpu.grid_dim, which fixes a separate stage-1 mismatch (the launch addstile2_padto the grid the kernel computes), and edits the remap lines this change replaces; it already conflicts with main in that block. Whichever lands second keeps [FlyDSL] [MoE] Fix stage-1 grid leaving padded intermediate columns unwritten #6598'sgrid_dimreads and this change'sxcd_remap_gridcall.🤖 Generated with Claude Code