Repository navigation
[FlyDSL] [Perf] gfx1250 NVFP4 sparse prefill v3 kernel: tile rows over waves, 1.69x on M3.1 prefill - #8819
[FlyDSL] [Perf] gfx1250 NVFP4 sparse prefill v3 kernel: tile rows over waves, 1.69x on M3.1 prefill#8819fsx950223 wants to merge 6 commits into
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
c4e0b7f to
5a04258
Compare
NVFP4 K/V (packed e2m1, one E4M3 scale per 16 values, token-major per (page, KV head): [128, 64] payload and [128, 8] scale bytes) for the gfx1250 FlyDSL pa_decode, selected by key/value scales of dtype float8_e4m3fn. Gfx1250Nvfp4WavePipeline serves tiles of one query tile (<= 16 rows, M3.1 TP4 decode, MTP and prefill rows): each wave owns 32 tokens of every page and DMAs them itself, QK is v_wmma_scale16_f32_16x16x128_f8f6f4 straight from the raw K rows and their E4M3 scales (the query split into FP8 terms exact to 2^-21 of a 16-dim block's maximum), V is dequantized and transposed by the same instruction with a constant selector, and softmax and P stay in registers, so the page loop has no CTA barrier. Direct, last-arriver, cluster and reducer outputs and the FP8 output epilogue are supported. v_wmma_scale16 misreads 64-bit scale operands that come from an inline constant or an SGPR pair on gfx1250; every selector scale is ORed with a lane-varying zero so it stays in a VGPR pair. Co-Authored-By: Claude Opus 5.5 <[email protected]> (Carried in #8819 without op_tests/test_flydsl_pa_decode_nvfp4.py; that test is in #8365.)
…t_scale) MiniMax-M3.1's MXFP4 checkpoint runs o_proj as a native MXFP8 GEMM (gemm_a8w8_mx32: E4M3 activations, one E8M0 scale per 32 along K). Each of the 57 sparse layers stored the PA's BF16 output and quantized it in a separate kernel (ATOM quantize_fp8: 2.4 us decode, 7.2 us prefill a layer). The per-token quantized_output (one FP32 descale a token) does not fit it. An E8M0 output_scale [rows, H * D / 32] (dtypes.fp8_e8m0 or uint8) now selects that output in pa_decode: FP8 bytes in the output's layout and row-major scales, bit for bit the BF16/FP16 output of the same call quantized as the split path did: code = ceil_pow2(max(amax, 1e-10) / 448) (emit_mx_e8m0_scale RoundUp), values e4m3(clamp(x * 2^(127 - code), +-448)) after rounding to the output dtype. Padded bucket rows are FP8 zeros with E8M0 1.0. A float32 [rows, 1] scale keeps the per-token output unchanged. A 32-element group lies in one head row, so its maximum takes lane shuffles only (kernels/pa_decode/op_mxfp8.py), no LDS, CTA or cluster exchange, and the plan's split is kept in every output mode: - NVFP4 wave tile: under the MXFP8 output each wave merges output tiles 2 w, 2 w + 1 instead of w, w + 4 (dims 32 w + [0, 32)), so a group is a lane pair; direct rows, single-task rows of merging plans, empty and padded rows (Gfx1250OutputMxQuant); - cluster merges of any size, clusters shared by several sequences, and the last-arriver merge: 8 lanes a group, no extra cluster barrier; - the reducer (pa_decode_reduce_kernel_*_mxfp8[only]): 16 lanes a group with pairs; its FP8 / scale pointers take the unused sink slot so every argument still preloads, and it runs the XCD-contiguous grid: a token's 64-byte scale row written by 16 CTAs spread over the XCDs cost 2.7 us at 512 rows, nothing with the token's CTAs on one XCD. Other tiles (FP8, BF16) take the reducer path and its epilogue. Padded rows of plans whose kernel has no padding CTAs (last-arriver merge, shared clusters, reducer) are filled after the launch. No sinks. gfx1250 compile, no spills, no extra LDS: wave direct 212 VGPR, cluster 2/4/8 and merges 210, reducer np3/8/32 9/14/53 (as BF16). The existing tile and reducer kernels (NVFP4 wave direct, cluster2 shared, cluster8, merge, reducer-fed; per-token quant direct/cluster8; FP8 page16 direct and cluster8 quant; reducer np3/8/32) compile to byte-identical ISA. op_tests/test_flydsl_pa_decode_nvfp4.py: test_pa_decode_nvfp4_mxfp8_output (direct prefill/decode, cluster8 c4/c24, cluster4, shared clusters 2 x4, 3 x4 and 8 x2, last-arriver merge, reducer np8; bf16/fp16; kept or skipped BF16 store; padded and empty rows) against a torch reference and a copy of the split quant kernel, and a __main__ benchmark (fused vs split vs BF16). Co-Authored-By: Claude Opus 5.5 <[email protected]> (Carried in #8819 without op_tests/test_flydsl_pa_decode_nvfp4.py; that test is in #8365.)
Adds pa_sparse_prefill_nvfp4_v3, a gfx1250 NVFP4 sparse-prefill attention kernel for MiniMax-M3.1's block-sparse prefill (16 query heads a KV head, head_dim 128, 128-token NVFP4 pages, top-16 pages a token), next to #8459's v1/v2. v2 splits a page's 128 tokens over a CTA's four waves, so every wave holds every row's softmax state and output, and a tile stops at two rows. v3 takes 8 consecutive rows of one KV head per CTA, and each wave owns two of them, walking every token of its rows' pages. The tile's union of pages is built once; per row that is 3.0-3.5 union pages instead of v2's 8.8-9.4. Pages go to one of two phases: - Phase A: pages all waves select. They go through a CTA ring, with one tensor DMA a page and wave and one barrier a page. V is dequantized once a CTA into a shared buffer. - Phase B: the rest. They go through wave-private quarter rings: no barrier, DMA descriptors built in SALU, and a K/V plane pair in one 2-D tensor DMA. The rest of the design: - Rows are paired over the waves by their phase-B pages. - Walk metadata is read a step ahead. - PV takes FP16 operands, with a lazy softmax rebase. - The union is built from hashed first-entry tables, representative masks and branch-free list stores. Dead chunks are skipped, and its barriers fence LDS only. - Query rows load at the kernel start, and the epilogue is spill-free. - An optional MXFP8 output (o_proj's input, bit for bit the separate group-32 quant) sits behind a constexpr. The M3.1 build (tq8, nvfp4_buffers 3): 336 VGPRs, 0 spills, LDS 99,888 B, 3 CTAs a WGP. The module stands alone. It imports only pa_decode's NVFP4 modules and utils, and carries its own copies of the v1/v2 geometry constants and page-step helpers. Callers use compile_pa_sparse_prefill_nvfp4_v3 directly; its launch takes #8459's compile_pa_sparse_prefill_nvfp4 arguments (listed in its docstring). It needs #8365's pa_decode NVFP4 modules and nothing from #8459. On MI455X it wins from about 1536 v3 CTAs up, and smaller grids should stay on v2 at tile_rows 2. MI455X, FlyDSL 0.5.3, vs #8459's v2 at tile_rows 2 (the current M3.1 call): the 8 M3.1 prefill chunks run at a geomean of 1.446x (982 -> 1419 TFLOPS), and the guard shapes v3 serves (pick swaps, TP4 one KV head, 4K-token small T) run 1.28-1.39x. Error vs FP32 is no worse than v2's: relative L2 0.94x on the large cases, max abs error at most 1.10x, MXFP8 bit-exact. Co-Authored-By: Claude Opus 5.5 <[email protected]>
5a04258 to
1d0fe13
Compare
The NVFP4 PA decode carried from #8365 has its tests in op_tests/test_flydsl_pa_decode_nvfp4.py, which this PR leaves to #8365. Some of those cases move here so the NVFP4 path still runs in this PR's tests: - test_pa_decode_nvfp4: M3.1 TP4 rows over compacted top-16 tables with NaN-poisoned tail bytes, against an FP32 reference and through the recorded-launch path. Cases: decode c4, MTP8 c64 cluster8, one-partition direct, last-arriver merge, reducer, FP16 query, two KV heads (GQA 8), and the fp4x2 payload view. - test_pa_decode_nvfp4_mxfp8_output: the fused MXFP8 output equals the group-32 quant of the same call's BF16 output. Modes: direct, cluster8, merge and the reducer, each with the BF16 store and quant-only. Padded rows are FP8 zeros with E8M0 1.0 scales. The helpers are copied from #8365's test with an _nvfp4_ prefix. The cases run on gfx1250 only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
NOT 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 |
…s) and v2 TDM ring, from #8819 Port of #8819 at a3ddace (the commits from "test: M3.1 sparse PA UT" on) onto the MiniMax-M3.1 image branch, keeping its P2 (MXFP8 output with more than one KV head, 8ccf673) and P3 (v2 opt-in pipe / prefetch / lean / rsplit walks, a889381 + 155f94f): * kernels/pa_sparse_prefill_nvfp4_v3.py (new, as in #8819): a CTA takes 8 consecutive rows of one KV head, each wave owns 2 of them; pages most waves select go through a CTA ring (V dequantized once a CTA), the rest through wave-private quarter rings; FP16 PV operands with a lazy softmax rebase. BF16 output only. * v2 (bit for bit the same output): the page DMA descriptors built in SALU from operands prepared once a CTA, an always-full tensor DMA ring (one unconditional wait a page step), and the opt-in tdm_fold (a K/V pair of planes with one 2-D tensor DMA). The v2 helpers v3 shares moved to module level. * pa_sparse_prefill.py: impl="v3" with its v3= options, M31_PREFILL_CALL = {"impl": "v3", "tile_rows": 8}; "v3" runs v2 (tile_rows 2, tdm_fold) on one KV head, for the MXFP8 output and on grids under V3_MIN_CTAS (4096) CTAs. * op test: #8819's simplified test with the M3.1 chunks (bench_m31_prefill) and the v3 cases. The image branch held an older #8459 revision (9c3f0ef); after black it differs from #8819's base (ff9ddf6) only in lint comments and the null MXFP8 pointers, so the image's changes since were merged onto #8819's head: * P3 on the always-full ring: pipe orders a page's DMAs K, K scales, V, V scales and waits (nb - 2) * 4 + 2 before QK and (nb - 1) * 4 before the V rows (the ring is full, so no bound checks), prefetch hints from both prologues and every page step; pipe and tdm_fold exclude each other (a folded DMA moves K and V together). lean's selector / tail mask live in the module-level helpers (_selector_operand(zero), _bound_mask(lean)). rsplit implies cta_dma, so it keeps its own staging. * P2: the MXFP8 output with several KV heads (padding in whole tokens, the bucket passed in tokens, pad row token * kv_heads + kv_head) in v1 and v2; the null pointers without the MXFP8 output from #8819. The op test runs the MXFP8 check with 1 and 4 KV heads (v2, v2 tdm_fold, v1) and P3's walks (lean / pipe / prefetch bit for bit vs the default v2, rsplit vs FP32). Host compile-only, gfx1250, FlyDSL 0.5.0 (the image's pin; same with 0.5.3), VGPRs / insts: v2 tq2 334 / 2808 (was 336 / 2935), MXFP8 334 / 3069, tdm_fold 334 / 2771, lean 244 / 2646, pipe 324, pipe + prefetch 2 330, lean + pipe 244, rsplit tq4 190, tq8 ring 2 250, v1 unchanged (580 / 3559): no spills. v3 tq8: 336 VGPRs, 2 spilled (one store in the prologue, reloaded in the epilogue only, not in the walks); #8819's head compiles the same. v3 tq4 201. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…REUSE_IMPL=v3 aiter's pa_sparse_prefill (with ROCm/aiter#8819) has impl="v3": a CTA takes 8 consecutive tokens of one KV head, each wave owns 2. M3.1's P (PP4 x TP1, 4 KV heads a GPU) runs it with ATOM_M3_SPARSE_PREFILL_REUSE_IMPL=v3 ATOM_M3_SPARSE_PREFILL_REUSE_TQ=8 (MI455X, 32K-token chunks: 1.31x v2 tq2). aiter itself runs v2 on one KV head, small grids and, by default, the MXFP8 output (ATOM_MINIMAX_M3_PA_MXFP8_OUT=1). ATOM_M3_SPARSE_PREFILL_REUSE_OPTS now goes to the chosen implementation's option dict (aiter's v2= or v3=), so OPTS=mxfp8=1 with IMPL=v3 lets v3 write o_proj's MXFP8 input itself (aiter's opt-in). v2's OPTS behave as before. Values stay integers: tdm_fold (a bool in aiter) is not settable from here. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replace the older v3 port (f487a42, MXFP8 store 54dbda6) with #8819's standalone pa_sparse_prefill_nvfp4_v3.py (1d0fe13): CTA-wide phase-A ring, wave-private phase-B quarter rings, hashed union index, lazy rebase, built-in MXFP8 output behind quant_mx. v2's TDM ring changes stay. Fix on top of the PR file: a padded-bucket row of the MXFP8 output is flattened row token * kv_heads + kv_head (the PR stored token, which with 4 KV heads zeroes live rows and leaves heads 1-3 of the bucket unwritten). Wrapper: impl="v3" with v3={"mxfp8": True} maps to quant_mx (ATOM's interface unchanged); v3 serves grids of 1536 v3 CTAs or more (PR), one KV head included; smaller grids run v2 tile_rows 2. Op test: parametrize test_pa_sparse_prefill_nvfp4 (benchmark() applied in main) so pytest runs it: 8 passed on MI455X. MI455X, 8 M3.1 chunks, 4 KV heads, tq8 (geomean us, same GPU and session): v2 tq2 1842.3; old v3 1486.9 / +MXFP8 1530.5; new v3 1420.8 / +MXFP8 1437.1 (1.297x / 1.282x v2). MXFP8 bit for bit the group-32 quant of the BF16 rows. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Seven bit-exact changes from round 4 of the kdev sp-prefill campaign (checkpoint 4): - the per-head maximum is a pairwise maxnum tree (nnan): 23 fewer instructions a walk step; - the lazy rebase and tail-mask bodies, and the uncommon row-pair bodies, are laid out of line with llvm.intr.expect.with.probability (raw LLVM dialect: FlyDSL has no branch-likelihood API), so the common walk step falls through (A walk 9,060 -> 3,220 B, B walk 9,516 -> 4,184 B of final ISA); - shorter branch-condition chains in the A walk step; - the K/V code planes are padded in LDS (16 B every 4 token rows), so every 16-byte operand read spans all 64 banks (LDS 99,888 -> 102,960 B, still 3 CTAs a WGP); - a paired step's late V blocks are read in one group; - a page head step's K tiles are read before its barriers. Measured on MI455X (FlyDSL 0.5.2, sp_ab paired A/B, 60 pairs): - vs the previous v3: the 8 M3.1 prefill chunks run 1.101x warm, 1.099x with the arms swapped and 1.091x cold. Every scored, guard and small-T output is bit-identical. - vs #8459's v2 at tile_rows 2: 1.577x (988 -> 1558 TFLOPS, 3 rounds). The final ISA is 336 VGPRs with 0 spills. This self-contained copy compiles to the same final ISA as the campaign's kernel on all 23 production builds (checked compile-only). Co-Authored-By: Claude Opus 5.5 <[email protected]>
| offset = ( | ||
| fx.Int64(token + row // g) * stride_row | ||
| + (row % g) * stride_head | ||
| + index % t.head_dim | ||
| ) | ||
| target = fx.recast_iter( | ||
| fx.PointerType.get(fx.Int32.ir_type, fx.AddressSpace.Global, 4), | ||
| fx.add_offset(self.groups.quantized, offset), | ||
| ) | ||
| if const_expr(size % (t.BLOCK_THREADS * 16)): | ||
| _store_word(target, zeros, index < size) | ||
| else: | ||
| fx.ptr_store(zeros, target) | ||
| code = (fx.Float32(scale) == fx.Float32(0.0)).select( | ||
| group_code(fx.Float32(0.0)), fx.Int32(E8M0_ONE) | ||
| ) | ||
| byte = fx.Vector.from_elements([code.to(fx.Int8)]) | ||
| groups = size // GROUP | ||
| first = fx.Int64(token) * stride_row // GROUP | ||
| for i in range_constexpr(-(-groups // t.BLOCK_THREADS)): | ||
| index = i * t.BLOCK_THREADS + tid | ||
| target = fx.add_offset(self.groups.scales, first + fx.Int64(index)) |
| return tq, nb_a, nb_b, share_min, waves_per_eu, dbg, tdm_fold | ||
|
|
||
|
|
||
| def compile_pa_sparse_prefill_nvfp4_v3( |
Five changes from round 5 of the kdev sp-prefill campaign (checkpoint 5): - the lazy-rebase test reads the lane's own maximum, in the B walk as in the A walk; - each lane keeps its part of the softmax row sum, and the epilogue adds the lane ^ 16 half once, so the per-quarter v_permlanex16 + add goes away; - a paired A step computes P before its rebase branches; the rare rebase body re-reads K from the ring slot and redoes QK and P; - the A walk's next item word is uniform in the step body; - an A page's DMA is staged without branches, with its descriptor built at barrier 1. Numerics: every change is bit-exact except the row-sum one. That one adds the two lane halves of each row sum in a different order, so v3 outputs move in their last BF16 bits (max relative difference 1.4e-3 to 5.3e-3 against the previous v3). On every case of the 124-check correctness gate, relative L2 and worst-row error vs FP32 equal the previous v3's to 5 digits, and max abs error is equal except on one case (25% pick swaps, x1.0013). Rows that take v2 are unchanged. Measured on MI455X (FlyDSL 0.5.2, sp_ab paired A/B, 60 pairs): - vs the previous v3: the 8 M3.1 prefill chunks run 1.077x warm, 1.077x with the arms swapped and 1.065x cold. - vs #8459's v2 at tile_rows 2: 1.691x (979 -> 1653 TFLOPS, 3 rounds). The final ISA is 336 VGPRs with 0 spills on all 23 builds. This self-contained copy compiles to the same final ISA as the campaign's kernel on all 23 production builds (checked compile-only). Co-Authored-By: Claude Opus 5.5 <[email protected]>
| err = (output.float() - expected).abs() | ||
| tol = 2e-2 + 2e-2 * expected.abs() | ||
| assert (err <= tol).all(), ( | ||
| f"max abs err {err.max().item():.4g} at " | ||
| f"{torch.nonzero(err > tol)[:4].tolist()}" | ||
| ) |
zhiding512
left a comment
There was a problem hiding this comment.
FlyDSL PA review (automated harness), round 1
Verdict: changes needed. The v3 kernel reproduces its speed and passes your v3 test on hardware, and the carried pa_decode changes leave every existing kernel ISA- and bit-identical on gfx942, gfx950 and gfx1250. But the carried #8365 copy predates #8365's fix for the MXFP8 output with more than one KV head, which I reproduced on gfx1250, and no test in this PR runs v3. · head 2173680c7c · base = merge-base f70ac7f90a · FlyDSL 0.5.3 on both sides
What this PR touches, by arch
- v3 sparse prefill (
pa_sparse_prefill_nvfp4_v3.py): gfx1250 only, and nothing routes to it. This is outside the harness'spa_decodescope, so I checked it with your own test and an extra A/B (below). - The two carried #8365 commits: they change the shared
pa_decodehost code, the main and reduce kernels,op_merge.pyandtraits.pyfor every arch. So I ran thepa_decodebase/head A/B on all three archs.
pa_decode: base vs head
| arch (GPU) | prod | unit | grid | outputs | ISA | pytest (base → head) |
|---|---|---|---|---|---|---|
| gfx942 (MI308X, 80 CUs) | 0.999 | 1.001 | 1.000 | 2056 / 2056 bit-identical | 236 / 236 unit kernels identical | 414 → 414 passed; the 16 new NVFP4 tests skip |
| gfx950 (MI355X, 256 CUs) | 0.999 | 0.998 | 1.001 | 2212 / 2212 bit-identical | 263 / 263 unit kernels identical | 510 → 510 passed; the 16 new tests skip |
| gfx1250 (256 CUs) | 0.998 | 0.995 | 0.998 | 2501 / 2501 bit-identical | 495 / 495 unit + prod kernels identical | 480 → 496 passed; the 16 new NVFP4 tests pass |
- Each column is the geomean of head/base time; below 1 = head faster. Method: paired ABBA CUDA-graph replays, 3 memory layouts per case, ATOM-style launches. The A/A noise band is 2.5-2.6%.
- Flagged cases. The first pass put cases above the band on every arch: 24 on gfx942, 27 on gfx950 and 62 on gfx1250, most of them under 15 µs. Every one of them is bit-identical, with the same plan. Re-measured at 30 pairs, plus a second run with base and head swapped, 0 of 24, 0 of 27 and 0 of 63 stay slower in both orders (the 63 are the 62 plus the crashed prod case below). Geomeans: 1.000 / 0.999 on gfx942, 1.009 / 1.016 on gfx950, 0.999 / 1.005 on gfx1250. So there is no regression on any arch.
- New arch-neutral code is inert on existing paths.
- The reduce kernel's launch gains two MXFP8 pointers, passed as null.
- The
op_merge.pyMXFP8 branches areconst_expr. output_scaleaccepts E8M0 only on gfx1250.- The ISA and bit identity above confirm this.
- Side note:
prod|qwen3-235b-tp4|q4|b128|c16384|atomcrashed its worker on gfx1250 on both sides. It is not caused by this PR. It ran cleanly on both sides in the re-measure, so it was a one-off worker failure.
v3 sparse prefill on gfx1250 (your claims)
How I tested it. This branch has no v3 wrapper or test, so I used the earlier revision 67a6173a0 of this PR (#8459 + the v3 wiring + test_flydsl_pa_sparse_prefill_nvfp4.py) and swapped in this head's v3 file.
Correctness:
test_flydsl_pa_sparse_prefill_nvfp4.pypasses with this head's v3 (main, no M3.1 bench). That covers:- every v3 candidate: tq8, tq4, nb2, bb3, reversed K/V planes, no fold;
- the MXFP8 bitwise checks, including padded rows;
- the spread-scores test.
- On the 8 M3.1 chunks: 0 mismatches at 3e-2 against FP32 and no NaN. Relative L2 is 0.00167 vs v2's 0.00177 (0.94x, as you report).
- The outputs differ from the pre-trims v3 (
67a6173a0) only in last BF16 bits, as the description says.
Speed. Your 8 M3.1 chunks (4 KV heads, 10% pick swaps):
- Paired, interleaved rounds: 40 rounds × 5 calls, the four kernels in rotating order on one idle GPU.
- Before =
impl="v2", tile_rows=2of67a6173a0; after = this head's v3 at tq8.
| chunk | v2 TFLOPS (yours → here) | v3 TFLOPS (yours → here) | speedup (yours → here) |
|---|---|---|---|
| 1req-32k-on-82k | 987 → 1046 | 1666 → 1663 | 1.695x → 1.583x |
| 2req-on-53k | 977 → 1039 | 1664 → 1668 | 1.706x → 1.606x |
| 2req-short | 981 → 1040 | 1665 → 1671 | 1.704x → 1.605x |
| 5req-on-21k-206k | 976 → 1034 | 1649 → 1649 | 1.694x → 1.597x |
| hi95-5req-6.5k-on-125k | 972 → 1032 | 1647 → 1640 | 1.696x → 1.591x |
| hi98-12req-2.7k-on-128k | 962 → 1021 | 1623 → 1619 | 1.683x → 1.583x |
| hi95-4req-8k-on-350k | 964 → 1021 | 1620 → 1617 | 1.678x → 1.585x |
| hi99-24req-1.3k-on-130k | 955 → 1015 | 1596 → 1591 | 1.662x → 1.572x |
| geomean | 972 → 1031 | 1641 → 1640 | 1.690x → 1.590x |
- v3 reproduces: its absolute TFLOPS are within 0.5% of yours on every chunk.
- The speedup is 6% lower here because
67a6173a0's v2 is about 6% faster than your baseline (#8459 atf4763f221). That is within the ±10% rule, so I count the claim as reproduced. - With v2's two-plane fold (
v2={"tdm_fold": True}), the geomean is 1.545x. - The two trims commits: this head's v3 vs
67a6173a0's v3 is 1.178x geomean (1.170-1.183 per chunk). You claim 1.10 × 1.08 = 1.19x.
Findings
-
Major (reproduced on gfx1250): the MXFP8 output leaves empty and padded rows partly unwritten with more than one KV head.
- Where:
Gfx1250OutputMxQuant.store_empty(op_merge.py:749-770) has no KV-head offset. The FP8 store covers heads 0..g-1, and the scale run starts at the token's first byte.pa_decodedoes not limit the MXFP8 output to one KV head. - Reproduction: NVFP4 K/V, 2 KV heads × GQA 8, poisoned buffers (0x7F / 0xFF), empty rows from
_nvfp4_rows, 5 padded rows.- direct, cluster8 and merge: heads 8-15 and scale bytes 32-63 of every empty row keep the poison (3072 / 1024 / 1024 FP8 bytes).
- direct and cluster8 also get half of the padded rows wrong (5120 FP8 bytes and 160 scale bytes).
- The reducer path is clean, and every mode is clean with 1 KV head. Live rows are exact in every case.
- Already fixed in #8365: its current head
b5f687d7has the fix (head0 = block_id.y // query_splits * g, plus per-token scale offsets). The carried copy is older. It also lacks #8365's later NVFP4 sliding-window support. This copy's host code still refuses windows for NVFP4, so that part is consistent. - Ask: re-carry #8365 at its current head, or rebase once it lands. A 2-KV-head case in
test_pa_decode_nvfp4_mxfp8_outputwould have caught this: every MXFP8 case there uses one KV head.
- Where:
-
Major: no test in this PR runs v3. I agree with the open Copilot thread. The check above needed
67a6173a0's wrapper and test.- If v3 lands before its wiring, please carry a gfx1250-gated test that compiles and launches
compile_pa_sparse_prefill_nvfp4_v3against the FP32 reference. - Your old test's v3 candidates and MXFP8 checks are a good base. They passed here with this head's file.
- If v3 lands before its wiring, please carry a gfx1250-gated test that compiles and launches
-
Minor: two NVFP4 test cases run a different mode than their names say. On a 256-CU gfx1250:
"reducer"compilescluster8x2;"mtp8-c64-cluster8"compiles the last-arrivermerge.
So
test_pa_decode_nvfp4never compares the NVFP4 reducer path with FP32. The MXFP8 test'sreducer-np8does run the reducer, but it only compares against the same call's BF16. Asserting the mode for every case, as you already do for 3 of the 8, would pin this down. -
Minor: the NVFP4 test uses a local tolerance instead of
checkAllclose. Agreed with the Copilot thread. -
Minor: v3's launch contract, for whoever wires it. The launch takes raw pointers. These rules are only in the docstring:
- MXFP8 needs one KV head.
pad_row(row)(line 2234) indexes padded rows by token, while real rows userow * kv_heads + kvh.67a6173a0's wrapper enforces this;check_options/ the compile could too. softmax_scalemust be positive. With 0 or a negative scale, every row with a partial last page comes out NaN. I checked this on hardware. v2 behaves the same, so it is not new in v3, but the eventual wrapper should reject non-positive scales.tdm_foldhas to match the planes' address order.
- MXFP8 needs one KV head.
-
Nit:
AITER_FLYDSL_NVFP4_PA_BUFFERSis validated whenpa_decode_kernel.pyis imported (pa_decode_kernel.py:179). A bad value breakspa_decodeon every arch, not only the NVFP4 path.
Notes
- Merge order: the PR must merge after #8365, which is still a draft. Its carried commits should match #8365's final version.
- Not covered by the harness: the v3 kernel's design. I read its union, ring and barrier logic and found no correctness issue on the M3.1 path. The hardware checks above are what back that up.
- Environments:
- gfx942: torch 2.9.1, ROCm 7.2.0.
- gfx950: torch 2.10.0, ROCm 7.2.4.
- gfx1250: torch 2.11, ROCm 10.1.
| row = index // t.head_dim | ||
| offset = ( | ||
| fx.Int64(token + row // g) * stride_row | ||
| + (row % g) * stride_head |
There was a problem hiding this comment.
Major, reproduced on gfx1250: store_empty has no KV-head offset here (and the scale run at line 767 starts at the token's first byte), so with 2 KV heads (GQA 8) and an E8M0 output_scale both CTAs write heads 0-7 of an empty token and heads 8-15 / scale bytes 32-63 keep whatever was in the buffer. With 0x7F/0xFF-poisoned buffers: direct, cluster8 and last-arriver merge leave heads 8-15 of every empty row poisoned (3072 / 1024 / 1024 FP8 bytes), and direct and cluster8 also get half of the padded rows wrong (5120 FP8 bytes, 160 scale bytes); the reducer and all 1-KV-head cases are clean. #8365's current head b5f687d7 already fixes this (head0 = block_id.y // query_splits * g plus per-token scale offsets); this carried copy predates it. Re-carrying #8365 at its head and adding a 2-KV-head case to test_pa_decode_nvfp4_mxfp8_output would close it.
| "merge", | ||
| torch.uint8, | ||
| ), | ||
| "reducer": ( |
There was a problem hiding this comment.
Minor: on a 256-CU gfx1250 this case compiles cluster8x2, not the reducer, and mtp8-c64-cluster8 (line 3605) compiles the last-arriver merge (checked with the same spy). So test_pa_decode_nvfp4 never compares the NVFP4 reducer with FP32. Could every case assert its mode, as the three named ones do, with plan kwargs that reach the reducer and cluster8?
| if row < rows: | ||
| mx_row(row * kv_heads + kvh, inv, final[r * 10 + 2 : r * 10 + 10]) | ||
| elif row < quant_rows: | ||
| pad_row(row) |
There was a problem hiding this comment.
Minor (launch contract): padded MXFP8 rows are indexed by token here, while real rows use row * kv_heads + kvh (line 2232). With quant_mx and kv_heads > 1 the pad stores would land on real rows of other heads. The docstring says one KV head and 67a6173a0's wrapper enforces it; since compile_pa_sparse_prefill_nvfp4_v3 is the public entry here, rejecting it there (or in check_options) would keep a future caller safe.
| # Page slots in each wave's DMA ring of the NVFP4 wave pipeline (2-4; 3 measured | ||
| # within noise of 4 at less LDS and fewer VGPRs). | ||
| NVFP4_BUFFERS = int(os.environ.get("AITER_FLYDSL_NVFP4_PA_BUFFERS", "3")) | ||
| if NVFP4_BUFFERS not in (2, 3, 4): |
There was a problem hiding this comment.
Nit: this check runs when pa_decode_kernel.py is imported, so a bad AITER_FLYDSL_NVFP4_PA_BUFFERS breaks pa_decode on every arch, not only NVFP4 calls. Validating it where the NVFP4 tile is compiled would keep it local.



Summary
Adds
aiter/ops/flydsl/kernels/pa_sparse_prefill_nvfp4_v3.py, v3 of the gfx1250 NVFP4 sparse prefill attention kernel for MiniMax-M3.1's prefill. It also adds NVFP4pa_decodecases toop_tests/test_flydsl_pa_decode.py.On the eight M3.1 prefill chunks, v3 at 8 rows a CTA runs 1.69x #8459's v2 at
tile_rows2, the current M3.1 call: geomean 972 -> 1641 TFLOPS on MI455X (1.691x over 3 rounds). On the guard shapes v3 serves (grids of 1536 v3 CTAs or more), it runs 1.40-1.63x.Commits.
[FlyDSL] [Perf] gfx1250 NVFP4 sparse prefill v3: tile rows over waves: adds the v3 file.test pa_decode: NVFP4 K/V cases (gfx1250) in test_flydsl_pa_decode.py: moves some of [FlyDSL] gfx1250 PA decode: MXFP8 (E8M0 group-32) output for o_proj #8365's NVFP4 decode cases into the existing test file.pa_sparse_prefill v3: critical-path and LDS trims, 1.10x (bit-exact): 1.10x on the M3.1 chunks, every output bit-identical.pa_sparse_prefill v3: walk-step trims and branch-free A staging, 1.08x: 1.08x on top of that. One change in it moves the last bits of v3's outputs; see Performance.The v3 module stands alone.
pa_decode's NVFP4 modules andutils, and carries its own copies of [FlyDSL] [Perf] gfx1250 NVFP4 sparse prefill attention with K/V reuse across query tokens #8459's v1/v2 geometry constants and page-step helpers. So it needs nothing from [FlyDSL] [Perf] gfx1250 NVFP4 sparse prefill attention with K/V reuse across query tokens #8459, and works next to any [FlyDSL] [Perf] gfx1250 NVFP4 sparse prefill attention with K/V reuse across query tokens #8459 revision, including9556c589f, which folds v2 into one kernel.compile_pa_sparse_prefill_nvfp4_v3; its launch takes [FlyDSL] [Perf] gfx1250 NVFP4 sparse prefill attention with K/V reuse across query tokens #8459'scompile_pa_sparse_prefill_nvfp4arguments, which the docstring lists.pa_sparse_prefill_nvfp4is left to [FlyDSL] [Perf] gfx1250 NVFP4 sparse prefill attention with K/V reuse across query tokens #8459 or a follow-up: animpl="v3", the M3.1 call, and v2 for grids under about 1536 v3 CTAs.Motivation
ATOM's M3.1 prefill runs
pa_sparse_prefill_nvfp4atimpl="v2", tile_rows=2. That kernel splits a page's 128 tokens over the CTA's four waves, so every wave carries every row's softmax state and output, and a tile stops at two rows: 9.4 union pages a row on the M3.1 chunks. v3 takes 8 rows a CTA. Each wave owns two rows and walks every token of their pages: 3.0-3.5 union pages a row, one owner a row, no cross-wave merge.Changes
aiter/ops/flydsl/kernels/pa_sparse_prefill_nvfp4_v3.py(new):tile_rows8; 4 also served). Wave w owns two rows, paired over the waves by their private pages (LPT).tdm_fold, which the caller reads off the planes' addresses).quant_mx) behind a constexpr: o_proj's FP8 values + E8M0 scales, bit for bit the separate group-32 quant, padded rows zero. The BF16 build does not change.check_optionsvalidates the compile options;OPTIONSandTILE_ROWSname them.nvfp4_buffers3): 336 VGPRs, 0 spills, LDS 102,960 B, 3 CTAs a WGP. The wave-uniform reads, ballots and lane counts stay raw ROCDL, with the reason at each call: the stable forms add 33-77 instructions a tile, and FlyDSL has no stable ballot.op_tests/test_flydsl_pa_decode.py: NVFP4 K/V cases for gfx1250 (see "Changed op tests"). It also passes two null MXFP8 output pointers to the reduce kernel's launch, which the carried [FlyDSL] gfx1250 PA decode: MXFP8 (E8M0 group-32) output for o_proj #8365 needs.Performance (if applicable)
Paired A/B (
sp_ab ab): 60 interleaved pairs of CUDA-graph replays after a 300 ms warm-up, on one otherwise idle GPU, FlyDSL 0.5.2.pa_sparse_prefill_nvfp4(impl="v2", tile_rows=2)atf4763f221.tile_rows8 with the same arguments: the kernel this PR adds, at the head commit (identical final ISA, see Validation).The tables are round 1 of three. At an earlier revision, the same A/B under FlyDSL 0.5.2 and main's 0.5.3 agreed within 0.1% (1.4461x vs 1.4463x). With FlyDSL 0.5.3 (main's pin), this exact v3 file was timed in a tree with #8459 and the v3 wiring, with no other process on the GPU. It ran 1.684x on the 8 chunks (968 -> 1632 TFLOPS) and 1.295x on the guard (worst 1.084x), matching the 0.5.2 tables within 0.5%.
1req-32k-on-82k: 1 x 32768 new on 81920 cached2req-on-53k: 21720 on 52736, 11008 on 128 (new on cached)2req-short: 11456 on 11136, 21248 on 128 (new on cached)5req-on-21k-206k: 1793 on 21376, 24165 on 165760, 1592 on 66560, 3073 on 23808, 2048 on 205568 (new on cached)hi95-5req-6.5k-on-125k: 5 x 6528 new on 124928 cachedhi98-12req-2.7k-on-128k: 12 x 2688 new on 128000 cachedhi95-4req-8k-on-350k: 4 x 8192 new on 349952 cachedhi99-24req-1.3k-on-130k: 24 x 1344 new on 129920 cached1req-32k-on-82k-s255req-on-21k-206k-s252req-on-53k-s50hi98-12req-2.7k-on-128k-rndtp4-14336-on-99968tp4-32768-on-65280smallT-4k-on-100kGrids under about 1536 v3 CTAs should stay on v2
tile_rows2. Measured before the two trims commits: 0.33x at 128 CTAs, 0.91-1.0x at 800-1088 CTAs, and a v3 win at every one of 110 grids from 1536 up (1.025-1.26x).v3 rows are not bit-exact with v2, because a row's pages accumulate in a different order. The error against FP32 is no worse than v2's. These ratios were measured before the trims commits; the first trims commit leaves every output bit-identical:
The last commit's per-lane row sum adds each row sum's two halves in a different order, so v3 outputs move in their last BF16 bits (max relative difference 1.4e-3 to 5.3e-3 against the previous v3). On every case of the 124-check correctness gate, relative L2 and worst-row error vs FP32 are unchanged to 5 digits, and max abs error is unchanged except on one case (25% pick swaps, x1.0013). Rows that take v2 are unchanged.
Testing
The new kernel and the new test cases are gfx1250-only; see "Validation and unverified scope".
Documentation
Dependencies
Stacked on #8365 (ruili / lirui927: NVFP4 PA decode + MXFP8 output, which carries #8289).
pa_decode/pipeline_nvfp4.py,pa_decode/op_mxfp8.py), whichmaindoes not have yet.main. The rebase conflicted withmain's gfx1250 MXFP4 PA decode inpa_decode/traits.py,pa_decode_kernel.pyandpa_decode.py; the resolution keeps both features.op_tests/test_flydsl_pa_decode_nvfp4.py, which lands with [FlyDSL] gfx1250 PA decode: MXFP8 (E8M0 group-32) output for o_proj #8365.Breaking Changes
None. The v3 module is new and nothing imports it yet; the test file only gains cases.
PR Self-Review
Reviewed revision
2173680
Behavior and intent
pa_decode; they are [FlyDSL] gfx1250 PA decode: MXFP8 (E8M0 group-32) output for o_proj #8365's, gated to gfx1250 NVFP4.Validation and unverified scope
MI455X (gfx1250).
This revision (
2173680c7ca4):python3 op_tests/test_flydsl_pa_sparse_prefill_nvfp4.py: 75checkAllclosepassed, 0 failed;5fec542f2of the development tree, which imports its helpers from [FlyDSL] [Perf] gfx1250 NVFP4 sparse prefill attention with K/V reuse across query tokens #8459's v2 instead of copying them.main+ [FlyDSL] gfx1250 PA decode: MXFP8 (E8M0 group-32) output for o_proj #8365 + this PR, no [FlyDSL] [Perf] gfx1250 NVFP4 sparse prefill attention with K/V reuse across query tokens #8459), the module imports and compiles: tq8 and tq4, each fold order, BF16 and MXFP8. LDS is 102,960 B.ruff check(0.16.0) andblack --checkare clean on the changed files.Earlier revisions, with FlyDSL 0.5.3 (the v3 file before the trims commits):
python3 op_tests/test_flydsl_pa_sparse_prefill_nvfp4.py, in this PR's earlier revision67a6173a0([FlyDSL] [Perf] gfx1250 NVFP4 sparse prefill attention with K/V reuse across query tokens #8459 plus the v3 wiring, with this v3 file swapped in): 75checkAllclosepassed, 0 failed. Its v3 candidates (tq8, tq4, reversed K/V plane order, no fold, ring variants, MXFP8) are compared with FP32 andpa_decode.pytest op_tests/test_flydsl_pa_decode.pyate2ba80658a9e: 496 passed, 109 skipped, including the 16 new NVFP4 tests (480 passed, 109 skipped before them). The trims commits do not touch the files this test reaches.Unverified:
PR-specific question
What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
op_tests/test_flydsl_pa_decode.py's NVFP4 cases fail on any error in the NVFP4 K/V decode path the carried [FlyDSL] gfx1250 PA decode: MXFP8 (E8M0 group-32) output for o_proj #8365 adds:No other test in this PR reaches that path, because [FlyDSL] gfx1250 PA decode: MXFP8 (E8M0 group-32) output for o_proj #8365's own NVFP4 test is not carried.
The two added reduce-kernel arguments keep the existing split-group test launching after [FlyDSL] gfx1250 PA decode: MXFP8 (E8M0 group-32) output for o_proj #8365 adds the MXFP8 pointers. Without them that test fails at launch.
Changed op tests
op_tests/test_flydsl_pa_decode.py:test_flydsl_pa_decode_nvfp4.pywith_nvfp4_-prefixed helpers:test_pa_decode_nvfp4: M3.1 TP4 rows over compacted top-16 tables with NaN-poisoned tail bytes, against an FP32 reference and through the recorded-launch path. Cases: decode c4, MTP8 c64 cluster8, one-partition direct, last-arriver merge, reducer, FP16 query, two KV heads (GQA 8), and the fp4x2 payload view.test_pa_decode_nvfp4_mxfp8_output: direct, cluster8, merge and reducer modes, with the BF16 store and quant-only.op_tests/test_flydsl_pa_decode_nvfp4.py([FlyDSL] gfx1250 PA decode: MXFP8 (E8M0 group-32) output for o_proj #8365's full NVFP4 decode test) is not part of this PR.🤖 Generated with Claude Code