Repository navigation
[FlyDSL] [MoE] [Perf] DeepSeek-V4.1-Flash TP4 A8W4: stage 1 -> stage 2 in one launch for verify steps up to 512 tokens, opt-in exact stage-1 FP8 rule (gfx950) - #8658
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]>
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]>
The v2 layout GEMM2 ran every K tile up to the padded inter_dim, and fused_moe never told it about intermediate_pad. With the padding passed down (D_INTER_VALID), K tiles past the unpadded width are not run; in the last partial tile, lanes whose 32-wide K block is padding load A as zero and use unit scales, so their products are exactly zero whatever the padding holds. Since stage 2 no longer reads the padding, stage 1 can skip the padding tile when it feeds a v2 layout stage 2 (stage2_skips_inter_pad, set only by fused_moe next to the stage-2 padding). The a8w4 test data helper takes inter_pad and activation so the e2e test can choose its padding. DSv4.1 TP4 a8w4 fused_moe (inter 576 padded to 640, real routing, MI355X): -7.5% at 256-1024 tokens, -5% at 3000-4096, -4.5% at 16384; flat at 1-16. Reduce rows are bit-exact against intermediate_pad=0. 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]>
# Conflicts: # op_tests/flydsl_tests/test_flydsl_moe.py
…erify steps, opt-in exact stage-1 FP8 rule A new FlyDSL module traces AITER's stage-1 body and the mixed or v2-layout stage-2 tile into one kernel with per-sort-block ready counters, behind an opt-in env switch with row-limit and occupancy settings. The stage-1 builder gets trace-time hooks and an opt-in FP8 epilogue that quantizes like the two-kernel path (switch and key list). The next commit gives the module, the env vars and the log tag their final names. 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: Reviews: |
…ars AITER names Rename the module, its env vars, log tag and trace-time hook names; no behaviour change (the AST equals the previous commit's after the reverse rename). - Module: moe_fused_s12.py - AITER_MOE_FUSED_S12=1 turns on the fused launch - AITER_MOE_FUSED_S12_MAX_ROWS, AITER_MOE_FUSED_S12_OCC: row limit and occupancy - AITER_MOE_S1_FP8_EXACT=1, AITER_MOE_S1_FP8_EXACT_KEYS: exact stage-1 FP8 rule - Hooks in mixed_moe_gemm_2stage_common.py: _FUSED_S12_HOOKS, _FUSED_S12_BUILT Co-Authored-By: Claude Opus 5.5 <[email protected]>
a32ecfd to
da06fd8
Compare
|
Heads-up: #8255 (moev2 phases 0-2) just merged as The good news for this PR specifically: the function(s) it changes — What actually moved: a set of helpers now live under Nothing changes at runtime by default: |
# Conflicts: # aiter/fused_moe.py
|
Thanks @zufayu for the heads-up. I merged current main into the branch twice (normal merge commits, no force push); the head is now
GPU: on MI355X (TP4, DeepSeek-V4.1-Flash, DSpark, random dataset) the same kernels, switched on against off on our FlyDSL 0.4.0 build, are neutral overall (throughput geomean -0.1 to +0.1 % over three on/off pairs, GSM8K 97.1 vs 96.9). One point, 4k input / 1k output at 64 requests, is 2 to 5 % lower with the fused launch in all three pairs; I will look at it before asking for review. This branch on main with FlyDSL 0.5.3 has not run on GPU yet. |
|
A follow-up on the point I flagged in my last comment (4096/1024 at 64 concurrent requests, 2 to 5 % lower with the fused launch in three pairs): it did not repeat in two rechecks, so the fused launch is neutral at these points. Same setup as before: MI355X (gfx950), ROCm 7.2.4, FlyDSL 0.4.0, DeepSeek-V4.1-Flash, TP4, DSpark block size 5, in a container build that carries this code under earlier names; the switch selects the arm. Random dataset ( Recheck 1: order on, off, on, off; the 4096/1024 point with three seeds. Output tokens/s, mean of the two runs per arm:
Geometric mean change in throughput in the three on/off pairs: +0.4 % / +0.3 % / +0.4 % (over the three 4096/1024 seeds: +1.3 % / +0.3 % / -0.3 %). The two runs of one arm differ by up to 2 % per row. Recheck 2: 4096/1024 at 64 requests with seed 108 (the seed of the first run), two runs per arm in both orders (on, off, off, on): on vs off -0.8 % (on first) and +3.5 % (off first); mean of the two runs per arm 8694 vs 8583 tok/s (+1.3 %), while the two runs of one arm differ by 1.8 % (on) and 2.5 % (off). 8192/1024 at 64 requests: +1.3 % / +0.5 %. (1024/1024 had one slow off run in this recheck and is left out.) The first reading sat on two high off runs. No point shows a loss or a gain beyond the run-to-run spread. This branch on main (FlyDSL 0.5.3) has still not run on a GPU; the GPU checks listed in the description stay open. I also updated the description to name the current head, |
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
Summary
For DeepSeek-V4.1-Flash TP4 on gfx950 (A8W4: MXFP8 activations x MXFP4 weights;
model_dim5120,inter_dim640, 385 experts, top-7),fused_moecan run MoE stage 1 and stage 2 as one kernel launch for calls of up to 512 tokens (DSpark verify steps), and the stage-1 FP8 epilogue can quantize with the exact rule of the two-kernel path. Both are opt-in through environment variables; with them unset nothing changes.Stacked on #7917 and #7918 (merge commits
8806e9a55and004ced2ddon top of mainecb8d3f81). Review only two commits:b056787b1f(the change) andda06fd8cdb(AITER names for the module, env vars and log tag; no behaviour change). The last two commits merge main into the branch.ac2c4fa154merged main3fd42598c(which has moev2 phases 0-2); its only conflict was the_flydsl_v2_stage2_wrappersignature inaiter/fused_moe.py(bothinter_dim_padfrom #7917 andscatter_intermediatefrom main are kept). The headbd85bd0320merges main9c9fb96e0without conflicts and does not change this PR's two kernel files.This is the upstream form of code we tested in a DeepSeek-V4.1-Flash build (fork draft amd-pedghazi#8; MI355X, ROCm 7.2.4, FlyDSL 0.4.0). The upstream form is not GPU-tested yet: main needs FlyDSL 0.5.3 and our test build has FlyDSL 0.4.0. It is compile-checked on CPU only (see "Validation and unverified scope"). The GPU runs it needs are listed below.
The only similar upstream PR, #8381 (closed, draft), is written for another shape (H5120 / I2304, top-6 / top-3, EP8) with its own kernels. This PR instead traces AITER's own stage-1 body and AITER's stage-2 tile (mixed stage 2, or the v2-layout tile through
compile_gemm2_a4w4_port(_composition=...)) into one kernel, so it keeps the tuned tiles and the fused output can be compared bit for bit with the two-launch path.Motivation
At 257-512 tokens, the stage-1 and stage-2 launches of a verify-step MoE call each leave the GPU partly idle at their start and end. One launch removes one launch gap and lets stage-2 tiles start while later stage-1 tiles still run.
Changes
aiter/ops/flydsl/kernels/moe_fused_s12.py:sc1), waits for its stores, and adds 1 to its sort block's ready counter;AITER_MOE_FUSED_S12=1installs it for the process: it holds the stage-1 launch of afused_moecall and launches it with the stage-2 launch of the same call.AITER_MOE_FUSED_S12_MAX_ROWS(default 512) caps the token count,AITER_MOE_FUSED_S12_OCC(default 3) sets the LDS size for 3 workgroups per CU. The reduction kernel after stage 2 is unchanged.mixed_moe_gemm_2stage_common.py:SharedAllocatorper kernel, a call after each stage-1 tile and around each stage-2 tile. The stage-1 grid formula moves into_grid1so the fused kernel can reuse it. With the switches off, the standalone kernels build the same ISA as before (checked, see below);fp8_exactfor the stage-1 FP8 epilogue (AITER_MOE_S1_FP8_EXACT=1): values rounded to bf16, amax floored at 1e-10, e8m0 = ceil_pow2(amax / 448), asfused_dynamic_mxfp8_quant_moe_sortdoes. Without it the FP8 epilogue keeps AITER's own rule, and the MoE output error goes from 0.0268 to 0.0305 (relative L2, measured on the image form);xcd_swizzle0 and to the<tile_m>/<waves_per_eu>/<xcd_swizzle>keys listed inAITER_MOE_S1_FP8_EXACT_KEYS. We serve32/2/1,32/3/4,64/3/1: the stage 1 of the 512, 1024 and 2048 verify rows of [FlyDSL] [Perf] Add DeepSeek-V4.1-Flash TP4 A8W4 MoE rows for 512-8192 tokens with the XCD-remap stage 1 on gfx950 (E385, top-7) #8650. Exact builds get_fp8qxin the kernel name instead of_fp8q.What differs from the tested code (amd-pedghazi#8,
23d4f8169b)fxoperators (bitcast,select,fx.generic_store) instead ofarith/llvm.StoreOp. The write-through store isfx.generic_store(..., memory_order=Monotonic, syncscope="agent"), the same LLVM store as before.gpu.grid_dim); inside the fused 1-D grid that is wrong, so the stage-1 body reads it through a_grid_dimhook, and the fused kernel passes the stage-1 grid.get_g2cache key hasg2_scatter_lds_pairs; the fused file's key list follows it. Main also adds fp4 route-out rows to the reduce stage 2; those are not fused (they did not exist in the tested code).AITER_MOE_FUSED_S12_MAX_ROWSdefault 512 instead of 1024: 512 is the value our test build sets and the only one measured.blackformatting (the tested form failedblack --check), comments without local file paths, and dict literals instead ofdict(key=...)calls (ruff C408). The module, env var and log tag names are AITER names (moe_fused_s12.py,AITER_MOE_FUSED_S12,AITER_MOE_FUSED_S12_MAX_ROWS,AITER_MOE_FUSED_S12_OCC,AITER_MOE_S1_FP8_EXACT,AITER_MOE_S1_FP8_EXACT_KEYS,[moe_fused_s12]); the tested code uses earlier names.Dependencies on open PRs
stage2_skips_inter_pad; the moved stage-1 grid code uses it) and [FlyDSL] MoE stage 1: XCD remap over the valid rows only #7918 (stage-1 XCD remap over the valid rows) are merged into this branch.tile_n128, which the fused rows do not use.Performance (if applicable)
Measured on the image form (amd-pedghazi#8, FlyDSL 0.4.0), not on this branch.
fused_moecall, 512-token row (stage 1t32x128x256_w2_gui_xcd1, v2-layout reduce stage 2), real verify routing, CUDA graph, median of 10 rounds, 288 / 320 / 384 / 480 tokensThe serving row measures this change together with the rows of #8650. The fused launch alone, switched on against off in the same image form (8 random-dataset points in the order on, off, on, off, then two rechecks of 4096/1024 at 64 requests), is neutral within noise: throughput geometric mean -0.1 % to +0.05 % over three on/off pairs, GSM8K 97.11 % on, 96.88 % off; details in the comments. The 8-point A/B on this branch is still pending (see below).
Testing
gfx950 only (MI355X), so MI250X and MI300X do not apply. No op test is added yet; the GPU checks of the image form ran with our own scripts. Unticked until the GPU runs on this branch are done:
AITER_MOE_FUSED_S12=0vs=1(all other settings equal): random 1k/1k, 8k/1k and 60k/600 with DSpark, GSM8K per arm, order on, off, off, on, proof line[moe_fused_s12] installedonly in the on arm.Documentation
N/A (small opt-in change): the switches are off by default and documented in the module docstring and the builder docstring.
Dependencies
Uses FlyDSL 0.5.3 as pinned on main. Open PRs it needs: see "Dependencies on open PRs" above.
Breaking Changes
None. With
AITER_MOE_FUSED_S12andAITER_MOE_S1_FP8_EXACTunset, nothing changes.PR Self-Review
Reviewed revision
bd85bd03200e44cc6a08c17ee20fc22c6e4c6628Behavior and intent
With
AITER_MOE_FUSED_S12=1, afused_moecall of up toAITER_MOE_FUSED_S12_MAX_ROWStokens whose row has an FP8-epilogue stage 1 and a reduce v2-layout stage 2 (or an_fp8stage 1 and a mixed stage 2) runs both stages in one launch. Other calls, and calls during a CUDA-graph capture before the workspace exists, run as before. WithAITER_MOE_S1_FP8_EXACT=1, the selected stage-1 builds quantize exactly like the two-kernel path.Validation and unverified scope
On this branch (CPU only, FlyDSL 0.5.3 wheel,
COMPILE_ONLY=1,FLYDSL_GPU_ARCH=gfx950):3fd42598c(byte-equal.sfiles)._fp8qx, same VGPR count as the default rule, 24-48 more instructions).fused,fused_v2) compile for the three buckets with occupancy 3; the ISA hassc1stores, 5 atomics and the poll loop. LDS and VGPRs match the image form: 40976 B for the 32-row tiles, 49408 B and 194 VGPRs for the 64-row tile.black --checkandruff check(0.16.0) pass on both files.These checks ran on
ac2c4fa154. The mergebd85bd0320does not changemoe_fused_s12.pyormixed_moe_gemm_2stage_common.py, so they were not rerun.Tested only on the tested form (
23d4f8169b, MI355X, ROCm 7.2.4, FlyDSL 0.4.0): bitwise equal to the two-launch path at 288-2048 tokens, 0 differing reruns with the LDS-DMA wait, GSM8K 0.9723 vs 0.9730 in serving (paired difference within noise).Not verified: anything on GPU for this branch, other shapes, other GPUs.
PR-specific question
Are the
AITER_MOE_FUSED_S12*andAITER_MOE_S1_FP8_EXACT*env var names acceptable? The tested build used other names for the same switches.Changed op tests
None.