Repository navigation
[FlyDSL] [gfx1250] grouped MoE GEMM1 opts into mega - #7921
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
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 |
31853f2 to
31371b1
Compare
DeepSeek-V4-Pro (hidden 7168) dies in warmup on this branch: packed A is enabled for every tile_m 256 stage-1, but its store budget depends on hiddenI picked this branch up for 455-CI, where DeepSeek-V4-Pro currently fails on the All 4 ranks, in Why V4-Pro hits it and DSV4.1-Flash does not.
On an fp4 wire the cutoff is
Suggestion. The decision to pack A is made per tile_m, but whether the storer can wait on those stores is a function of hidden (and of the wire's payload and scale bytes). Computing Repro. Image For context, on the same image with only #7973 (already in main) V4-Pro comes up and scores GSM8K 3-shot 0.9492 flexible / 0.9500 strict, so this PR is not what unblocks V4-Pro; it is the next thing in its way. |
…age-1 (#7919) * Optimize FlyDSL grouped MoE kernels * improve test * support a preshuffle * add csv support * update quant and shuffle * update * update mega-moe * update * update csv * update * simplify code * fix accuracy drop * update csv * fix format * fix(flydsl): align A-preshuffle guards and AOT coverage * improve cs test * fix ruff * Tune conc16384 benchmark with WPT2 and cluster sync disabled * Add standalone conc16384 GEMM1 performance reproducer Rename the benchmark to reproduce.sh and pin the validated zero-input configuration. Include standalone activation/quantization dependencies and tests so the script does not rely on stashed implementation changes. Preserve unrelated work in stash. GPU timing was not rerun for this commit. * Tune grouped MoE LDS scheduling and prologue initialization Use normal WMMA order with operand B reuse and precomputed payload LDS bases. Initialize accumulators before the prologue TDM wait and select WPT1 for the conc16384 tuned configuration. Include the local benchmark scripts and the reproducer iteration update. * Pin FlyDSL WMMA scale registers to disjoint low VGPR slots * update * update * update * remove useless tests * add script * update script * update script and wheel * update * update1 * update * Fix LDS helper shadowing after GEMM task refactor * Simplify gfx1250 prefill scheduling and add reproducible MoE benchmarks * Keep local GPU benchmark scripts and microtests out of the branch * Restore LDS layout controls and decouple interleaving from scheduling * Fix strict VGPR pinning with FlyDSL 0.5.0 Resolve sched_vgpr through rocdl.experimental on FlyDSL 0.5.0 while retaining compatibility with the older public API. Keep strict placement enabled for prefill kernels. Validated DSV4 Pro conc16384 with three const/random runs at 32 iterations. Both GEMMs have zero scratch instructions and accumulator placement at physical v512-v1023; median const GEMM1/GEMM2 is 465.77/322.86 us. * update for ds flash v4.1 * Tune DSV4.1 Flash 8192 prefill configuration * update for release * add csv * update csv * update csv for dsr1 fused expert * Move DSR1 grouped MoE configs into a dedicated CSV * remove useless csv * update csv * [FlyDSL] Simplify prefill WMMA emission and barriers * Tune DSR1 65-expert (64 routed + 1 shared, topk 9) grouped MoE rows for 1-1024 tokens The single 16x256x256 four-wave as-prologue row left gemm1 at ~16 TB/s for decode-sized batches (three workgroup rounds, extra round at 4 tokens, and a 64-row tile that doubled weight reads at 1024 tokens). Re-swept tiles and knobs for this shape on gfx1250: narrower gemm1 tiles (tile_n 64/128) with deeper buffering for 1-32 tokens, as-prologue kept only at 64 and 256 tokens where it measures faster, and 128x256 tiles at 1024 tokens. gemm1+gemm2 vs the previous rows (test_flydsl_grouped_gemm, constant + uniform init): 1 tok 1.81x, 2 1.11x, 4 1.25x, 8-256 1.12-1.16x, 512 1.08x, 1024 1.38x; >= 2048 unchanged. * Simplify FlyDSL KSL0 fence configuration and kernel symbols * Merge PR #5899 (zxe/mega_stage1_compact_overlap) for local benchmarking Co-authored-by: Cursor <[email protected]> * mega: adapt S1F/GEMM2 to the MoE prefill path, tune DSV4.1 Flash EP4 buckets - Pin VGPRs only for the 2x2 256^3 tile; S1F's 1x4 256 variant overflowed the map. - S1F GEMM1 warp grid per tile_m (2x2 at 256, AITER_MEGA_S1F_WARPS override) and pass the GEMM kernel_attrs to the S1F launch. - DSV4.1 Flash E96 rows: GEMM2 next_stage_prefetch + lds_soa_load_interleave at 8192+, add the missing 131072 bucket (tpr 32768: 3783 -> 3258 us/layer). Co-authored-by: Cursor <[email protected]> * [FlyDSL] [gfx1250] Pack GEMM1 A inside mega stage-1 for large prefill buckets The stage-1 producer writes the M16 packed A layout, and the 256-row GEMM1 turns on a_preshuffle, lds_soa and next_stage_prefetch together with the grouped-MoE prefill schedule. This runs only when the recv bound is at least 65536, which for EP4 top-6 is tpr 16384 and 32768. Payload and scale use separate buffer resources so the masked store sentinel stays out of range. DSV4.1 Flash EP4, 8-layer graph: 1654 vs 1704 us/layer at tpr 16384 (-2.9%) and 3111 vs 3258 at tpr 32768 (-4.5%). Smaller buckets stay on the 128-tile path. Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: yadaish <[email protected]> Co-authored-by: JiaoliangYu <[email protected]> Co-authored-by: lalala-sh <[email protected]> Co-authored-by: hyi <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: XingerZhu <[email protected]>
…EP4 buckets A persistent bf16 EP scatter now stages its rows in the last pipeline stage and skips the tensor_wait after the remote gather-store (defer_c_wait): the next tile's first fence retires it and the persistent loop drains at exit. It turns on by itself when the rows fit there (tile_m > 32, > 2 buffers, no planar LDS / A-scale prologue / k_waves); AITER_FLYDSL_EP_DEFER_C_WAIT=0 turns it off. DSV4.1-Flash EP4 a4w4 fp4-wire, mega S1F+S2F, uniform routing, 3x A/B: - tpr 512 (token 2048): persistent_workers2=512 -> GEMM2 57.6 -> 55.2 us, layer 217.0 -> 214.7 us (-1.1%). - tpr 1024 (token 4096): num_buffer_stage2=3 -> GEMM2 81.2 -> 78.9 us. Accuracy (layers=2) PASS at both with an unchanged logits_diff. Co-authored-by: Cursor <[email protected]>
…8 dwords FP4 256x256 at 1x4 needs 160 dwords and asserts in make_rmem_slot. Gate the explicit map on the same slot width, so that shape stays on the ordinary allocator while 2x2 (128 dwords) keeps the pinned schedule. Co-authored-by: Cursor <[email protected]>
A launch preloads only the first ~32 KiB of a kernel into the instruction cache. The S1F tile-256 kernel (~46 KiB) and the stage-1 GEMM (up to ~62 KiB) ran their tails cold whenever another large kernel had evicted them, so UT gains did not always show up end to end. Add prefetch_kernel_code(num_waves) to gemm_common_gfx1250, following #7827: set MODE.SCALAR_PREFETCH_EN through llvm.amdgcn.s.setreg, find the kernel span with s_getpc_b64 and .Lfunc_end0, and have each wave issue every num_waves-th 4 KiB s_prefetch_inst chunk up to 56 KiB, without branches. S1F replaces its old inline-asm prefetch, which was a no-op without MODE[24]; the a8w4 TDM GEMM calls it at kernel entry and tags the kernel name with _ipf. AITER_FLYDSL_INST_PREFETCH=0 turns it off. Co-authored-by: Cursor <[email protected]>
a85897e to
e880ec7
Compare
# Conflicts: # aiter/ops/flydsl/kernels/mxfp4_preshuffle_gfx1250_tdm.py
…orecnt (#8889) #7921 widens S1F prefill to tile 256 with packed A (a_preshuffled) keyed by (world_size, topk) only. For DeepSeek-V4-Pro (hidden 7168, fp4 wire) one packed-A storer op issues 61 VMEM stores, 61 + 3 > 63 overflows the 6-bit s_wait_storecnt field, make_s1f_stage1 raises in _precompile and the server fails to start. Gate packed A on one helper (s1f_packed_a_fits) shared by _launcher, launch_args (scale layout), _s1f_warps, s1f_prefill_tile and the compact plan tile alignment, so they always agree. When it does not fit, s1f_prefill_tile returns 0 and the 256 variant runs without packed A on 1x4 warps. The prefill threshold table is now keyed by hidden size as well. Verified on the V4-Pro MoE shape (EP2, fp4 wire, S1F): forced prefill widening and AITER_TDM_TILE_M=256 both pass accuracy; V4.1-Flash still builds the packed-A 2x2 tile-256 variant and passes.
Summary
Port the grouped-MoE GEMM1 prefill schedule into mega stage-1 (S1F), and land the gfx1250 grouped-MoE prefill rows that schedule depends on.
For EP4 top-6, a step whose receive bound is at least 65536 (tpr 16384 and 32768) has the stage-1 producer write M16 packed A. GEMM1 then runs the 256³ 2×2 prefill kernel:
a_preshuffle,lds_soa_load_interleave,next_stage_prefetch, pinned VGPRs, and WMMA 2×4. Smaller steps keep the existing 128-tile row-major stage-1 kernel.On DSV4.1 Flash, 4×gfx1250, 8-layer graph, the packed path is 1654 vs 1704 µs/layer at tpr 16384 (−2.9%) and 3111 vs 3258 µs/layer at tpr 32768 (−4.5%).
A follow-up commit lets mega GEMM2's persistent bf16 EP scatter defer its final TDM wait (
defer_c_wait), and tunes the tpr 512 / 1024 buckets: tpr 512 goes from 217.0 to 214.7 µs/layer (−1.1%, GEMM2 57.6 → 55.2 µs), and tpr 1024 GEMM2 goes from 81.2 to 78.9 µs.A third commit prefetches kernel code past the launch window (following #7827). A launch preloads only the first ~32 KiB of a kernel. The S1F tile-256 kernel (~46 KiB) and the stage-1 GEMM (up to ~62 KiB) start their tails cold whenever another large kernel has evicted them, which is one reason UT gains do not always show up end to end. With the prefetch, S1F tile 256 at tpr 8192 goes from 930.2 to 918.6 µs/layer (−1.2%), and a stage-1 GEMM alternating with another shape drops from 7.0K to 0.5K instruction-cache misses per dispatch. End-to-end impact is still to be measured.
Motivation
Grouped MoE prefill GEMM1 is faster once A is in the packed M16 layout, because that is what makes the TDM loads, LDS SoA interleave, and next-stage prefetch pay off together. Mega stage-1 was still producing row-major wire rows, so turning those flags on did not speed the fused kernel up. The wide DSV4.1 Flash bucket was also missing a 131072-token grouped-GEMM row, so tpr 32768 fell back to a 64-row tile.
Changes
(M/16, K/128, 4, 16, 16B)plus row-major e8m0 scales, with masked VMEM stores. Payload and scale are separate buffer resources so the masked-lane offset0x7FFFFFFFstays out of range. One resource for the whole region is about 2.21 GB at tpr 32768 and turned every masked lane into a real system-scope store (S1F ~50 ms).mxfp4_preshuffle_gfx1250_tdm.py. Stage-1 quant output stays row-major, which mega GEMM2 already reads.AITER_MEGA_S1F_PREFILL_RECV=0disables the 256-tile path.AITER_MEGA_S1F_WARPSoverrides the warp grid.num_buffers=4(planar LDS is about 328 KB, over the 320 KB per-CU budget; S1F keeps 3) and cluster 2×2 / 4×2 (explicit_tileplus persistent workers requires both cluster dimensions to be 1).topk=-1rows at tokens 8192, 16384, and 32768 turn on GEMM2next_stage_prefetchandlds_soa_load_interleave. A 131072-token row with the same 128-tile is added.topk=6grouped-MoE rows for E96 (tokens 1 through 8192, plus 16384) and the E97 shared-expert rows. DSR1 65-expert rows move intodsr1_tuned_grouped_fmoe.csv.defer_c_wait: a persistent bf16 scatter stages its rows in the last pipeline stage and skips thetensor_wait(0)after the remote TDM gather-store. The next tile's first fence retires it, because TDM ops complete in issue order, and the persistent loop drains withtensor_wait(0)before exit. It turns on only when the rows fit that stage and the prologue does not reach it (tile_m > 32, more than 2 buffers, no planar LDS, no A-scale prologue,k_waves == 1). The kernel name gets a_dcwsuffix.AITER_FLYDSL_EP_DEFER_C_WAIT=0turns it off.prefetch_kernel_code(num_waves)ingemm_common_gfx1250.py, after [FlyDSL] [gfx1250] DSv4.1 Flash 32x32 GEMMs: tune C1024 decode, prefetch a8w8/a4w4 compute kernel code #7827. It sets MODE.SCALAR_PREFETCH_EN (bit 24) through thellvm.amdgcn.s.setregintrinsic, not inline asm, because immediate-form inline setreg clears the VGPR MSB bits MODE[19:12]. The kernel span comes froms_getpc_b64and.Lfunc_end0 - label. Each wave issues everynum_waves-th 4 KiBs_prefetch_instchunk up to 56 KiB, with no branches; chunks past the end repeat the last line. S1F calls it at entry and drops its old inline-asm_prefetch_cold_code, which was a no-op without MODE[24]. The a8w4 TDM GEMM (stage-1 and GEMM2) calls it at kernel entry, fromgemm_taskwhen not persistent and from the persistent wrapper otherwise, and adds an_ipfsuffix to the kernel name.AITER_FLYDSL_INST_PREFETCH=0turns it off.topk=-1ep_fused=1fp4 rows (mega only): token 2048 setspersistent_workers2=512, so the tpr 512 GEMM2 is persistent and takes the deferred path. Token 4096 setsnum_buffer_stage2=3. The rest of each row copies the existing blank-ep_fusedrow.Performance (if applicable)
Hardware is 4× gfx1250. Workload is DSV4.1 Flash, EP4, 384 experts, top-6, hidden 5120, intermediate 2304, A4W4 MXFP4, fp4 dispatch, stage-1 and stage-2 fused, uniform routing, 8-layer CUDA graph, median per layer. Before is the same mega stage-1 without packed A (GEMM2 prefetch/SoA and the 131072 row already present).
S1F / GEMM2 device time on those runs: tpr 16384 is 632 / 575 µs, tpr 32768 is 1160 / 1126 µs. tpr 32768 is 17894 TFLOPS across the four GPUs (90% of the 19845 TFLOPS peak used for this shape). Before the buffer split, that S1F kernel was about 50 ms and still numerically correct.
The 131072-row fix, measured earlier on the same bench before packed A, took tpr 32768 from 3783 to 3258 µs/layer. The 3111 µs number above is on top of that.
Full 16-point sweep (
--layers 8 --warmup 5 --iters 5), per-layer µs, before → after: 65.07→64.67, 72.32→72.03, 83.47→82.83, 106.55→106.46, 146.59→145.34, 169.54→167.19, 182.55→181.48, 191.58→191.78, 196.28→196.05, 218.21→216.01, 283.61→283.03, 363.20→363.37, 544.48→543.87, 887.93→889.16, 1704.20→1654.11, 3257.91→3110.75. Points at tpr 8192 and below stay within 1.4%.GEMM2 deferred scatter follow-up. Same bench, interleaved A/B, 3 runs each. A is the previous rows with
AITER_FLYDSL_EP_DEFER_C_WAIT=0. B is the new rows.Instruction prefetch follow-up. Same bench,
--layers 4 --iters 10,AITER_FLYDSL_INST_PREFETCH=0vs1. Counters are rocprofv3SQC_ICACHE_MISSES, median over 8 dispatches, with the stage-1 GEMM (5120/2304) alternating with a 7168/2048 GEMM.AITER_MEGA_S1F_PREFILL_RECV=1), layerThe #7827 switch penalty (alternating vs back-to-back sum, threshold 1.10) is 1.00–1.065 at tokens 512 and 2048 with or without prefetch. At tokens 8192 it is 1.15 both ways, which is data-side, as #7827 also notes. The tpr 4096 row and the switch ratios were measured on the earlier base, and the other rows on this branch.
A full 16-point sweep on the new revision stays within ±1.2% of the sweep above at every tpr. Low-tpr GEMM2 tile, buffer, and persistent sweeps (tpr 4, 16, 64), and S1F tile_n / tile_k / producer / BTH sweeps at tpr 64, found nothing better than the current rows.
Testing
Accuracy, same shape,
op_tests/multigpu_tests/bench_mega_moe.py --acc_verify 1 --layers 2on 4× gfx1250: PASS at tpr 8, 512, 1024, 8192, 16384, and 32768. Average logits diff is 0.0423 against tolerance 0.0636. tpr 512 and 1024 were re-run on the deferred-scatter revision, and logits_diff is unchanged (0.042289 / 0.042290). The instruction-prefetch revision passesMEGA-CHECKat tpr 8192 with tile 256 forced, both with and without prefetch. The ISA showss_setreg_imm32_b32 hwreg(HW_REG_MODE, 24, 1),s_getpc_b64, and thes_prefetch_instchunks at entry, and the stage-1 and GEMM2 kernel names carry_ipf. Kernel names confirm the gate: tpr 8192 ist128with noapre; tpr 16384 and 32768 areapret256.No MI250X or MI300X run. This schedule is gfx1250. Non-uniform routing, other EP sizes, and top-k other than 6 were not swept.
Documentation
The packed path is an internal schedule of the existing mega stage-1 launch.
s1f_prefill_tiledocumentsAITER_MEGA_S1F_PREFILL_RECV.Dependencies
Breaking Changes
None. Receive bounds below 65536 keep the previous stage-1 tile.
AITER_MEGA_S1F_PREFILL_RECV=0disables the packed 256-tile path. Instruction prefetch is on by default for every gfx1250 a8w4 TDM GEMM and S1F launch, and changes their kernel names (_ipf), so cached JIT artifacts are rebuilt once.AITER_FLYDSL_INST_PREFETCH=0restores the previous code. Grouped-MoE rows for other(gfx, token, model_dim, inter_dim, expert, topk)keys are unchanged, except the DSR1 65-expert rows, which move todsr1_tuned_grouped_fmoe.csvwith the same lookup key.PR Self-Review
Reviewed revision
a85897e
Behavior and intent
Mega stage-1 on a large EP4 top-6 prefill step now writes and consumes packed A and runs the grouped-MoE 256³ prefill schedule. Row-major compact A could not use those loads, so the same GEMM flags did not make S1F faster. Steps under the recv threshold are unchanged. The missing 131072 grouped-GEMM row is what was sending tpr 32768 to a 64-row tile.
Validation and unverified scope
Command:
torchrun --nproc_per_node=4 op_tests/multigpu_tests/bench_mega_moe.py -tpr <tpr> --layers 8 -e 384 -k 6 -hd 5120 -id 2304 --fused_shared_experts 0 --routing uniform -q a4w4_mxfp4 --dispatch_wire fp4 --stage1_fused true --stage2_fused true --warmup 5 --iters 5on 4× gfx1250. Accuracy used--layers 2 --acc_verify 1at tpr 8, 8192, 16384, and 32768 and passed. Results are in the performance table.Instruction prefetch:
AITER_MEGA_S1F_PREFILL_RECV=1 AITER_FLYDSL_INST_PREFETCH={0,1} torchrun --nproc_per_node=4 op_tests/multigpu_tests/bench_mega_moe.py -q a4w4_mxfp4 -e 384 -k 6 -hd 5120 -id 2304 --layers 4 -tpr 8192 --iters 10 --stage1_fused true --stage2_fused true, plusrocprofv3 --pmc SQC_ICACHE_MISSESon standalone stage-1 GEMMs launched alternately. Not yet measured for prefetch: the end-to-end model, tpr 16384 / 32768 (where tile 256 is the default), and GEMM2 I$ counters, which read 0 under rocprofv3, so for GEMM2 only the ISA and accuracy were checked.Not run: MI250X, MI300X, zipf routing, EP other than 4, top-k other than 6, and the standalone grouped-GEMM preshuffle rows at tokens other than the mega sweep.
num_buffers=4and GEMM1 clustering stay off.PR-specific question
Which exact workload keys consume these configuration changes, and what evidence shows that unrelated shapes keep their previous selection?
Grouped-GEMM CSV keys that change are gfx1250, cu 256, model_dim 5120, inter_dim 2304:
next_stage_prefetch=1andlds_soa_load_interleave=1only. Tile stays 128. Mega GEMM2 selects these ep-fused rows._get_padded_muses for tpr 32768. It did not exist, so that step previously missed and kept the caller's 64-tile default.a_preshuffle=1is only on tokens 4096, 8192, and 16384.ep_fused=1, fp4 A, tokens 2048 and 4096: new rows. Explicitep_fusedrows win only on the EP-fused (mega) lookup, so grouped MoE still selects the blank rows.The deferred scatter needs EP scatter, bf16 combine, and persistent GEMM2 workers. In the current CSVs only these two DSV4.1 rows and two minimax rows set
persistent_workers2, and the minimax rows aretile_m2=16, which is not eligible. So the new path runs only at DSV4.1 EP4 tpr 512.Mega stage-1 does not read the topk-6
a_preshufflebit. Its own gate is(world_size, topk) = (4, 6)and recv bound ≥ 65536, implemented ins1f_prefill_tile. The 8-layer sweep is the evidence for everything else: tpr 8192 still launchess1f_stage1_..._t128x256x256_w1x4_b3with noapre, and tpr 1 through 4096 stay within 1.4% of the previous mega scan. Rows for other gfx, model dimensions, expert counts, or top-k values are not in the diff, so their lookup result is unchanged. The DSR1 65-expert rows moved file but keep the same key.Instruction prefetch reaches every launch of
s1f_stage1and of the a8w4 TDM GEMM (mxfp4_preshuffle_gfx1250_tdm.py) on gfx1250, independent of CSV keys. Kernels under ~32 KiB already fit the launch window, so the extra chunks re-touch lines that are already resident. Running the prefetch costs a handful of scalar instructions per wave at entry, and the tpr 4096 mega run (default tiles) shows no change.Changed op tests
op_tests/flydsl_tests/test_flydsl_grouped_gemm.py:test_shared_expert_byte_accountingandtest_shared_expert_routingcover fused shared experts, which the existing routed-only cases do not. One shared expert is appended with unit weight and must not be counted as another routed weight read, and it must not change the renorm of the routed weights. The byte test checks a 2-token, 8-of-64 routed plus 1 shared case (16 routed weights plus one shared weight, not 18). The routing test checks tokens 1 and 16: the shared id is last, its weight is 1, routed weights still sum to 1, and routed ids stay inside the routed expert range. No mega stage-1 op test was added; that path is the multi-GPUbench_mega_moe.py --acc_verify 1run above.