Repository navigation
[HIP] [FlyDSL] feat(flydsl): row-group FP4 paged MQA logits for 8- and 64-row pages - #6145
Conversation
Add pa_mqa_logits_fp4_rowgroup, an FP4 paged MQA logits kernel in which a wave scores a group of a sequence's query rows off each key load (the existing kernel reads every key once per row), and route flydsl_pa_mqa_logits_fp4 to it for kv_block_size 8 and 64. - Page 64 keeps the existing preshuffled cache layout and its writers; page 8 (DeepSeek-V4.1's indexer pages) has its own layout (pack_kv_cache), so a candidate layer's kept blocks are its block table with no gather. - Work is shared out by each sequence's real length inside the kernel (static split, or a length-balanced share when that walks shorter); the grid depends on the batch and max_seq_len only, so a captured graph keeps it. No schedule launch; cta_info / block_k / num_warps / parallel_unit_num do not apply on this path. - Keys and scales are read through 64-bit addresses (pools past 4 GiB); sequences are handled in runtime chunks of 64 (one build per shape for any batch). - A given `out` is left as it was past each row's bound (no -inf fill). - compute_varctx_schedule moves to pa_mqa_logits_fp4_common. Cold, MI355X, against the original FlyDSL kernel (its schedule prebuilt): page 64 1.07-3.0x (B = 64, H 32/64, next_n 1/2/6, up to 100K keys) and 1.25-2.9x at B = 1-16; page 8 V4.1 full layers 1.5-3.4x (T = 6-768, 200K context). Against aiter's Triton paged_mxfp4_mqa_logits: page 8 1.03-1.5x on every T; page 64 ahead or level on most shapes. Also: rope_rotate_activation (fp4) gains round_rope, rounding the rotated values to the input dtype before quantizing them, as a model whose RoPE writes its output back in that dtype (DeepSeek-V4.1's index query) does; with a bit-exact test (op_tests/test_dsv4_rotate_quant.py --round-rope).
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
flydsl_pa_mqa_logits_fp4_rowgroup takes query_start_loc [B + 1] and max_query_len: sequence b's rows are the flattened rows query_start_loc[b] .. query_start_loc[b + 1] - 1, any count up to max_query_len (0 included, as cudagraph padding has). q_fp4 is [rows, 1, H, D / 2]; q_scale, weights, row_ends and out go by flattened row. Without row_ends, row n of a count-row sequence sees context - count + n + 1 keys. A sequence's rows still share each key load, so a batch whose requests carry different numbers of query rows no longer has to be scored one row a sequence. Rows past a sequence's count read row 0 and get bound 0: no keys walked, no stores. One build flag (ragged); counts and batch stay runtime values. The rectangular [B, NEXT_N] call is unchanged. V4.1 full layers (page 8, 200K context, T = 6..768, cold, MI355X): the ragged call runs 1.0-2.35x faster than scoring each row as its own sequence (T = 48: 24.8 vs 43.4 us, T = 768: 246 vs 580 us), and within -4.5..+1.6% of the rectangular call; uniform counts give the rectangular call's logits bit for bit.
…_ends Ragged rows (query_start_loc) now require row_ends and take no context_lens: a sequence's length, which the kernel shares its workgroups out by, is its last row's bound, read in the kernel. A caller with per-row bounds no longer builds a per-sequence length. pages_per_block: a block-table entry may name that many consecutive pages (its first page's id / pages_per_block), expanded in the kernel where the page is used, so a caller whose pages are a multiple of the kernel's page size (DeepSeek-V4.1: 16 pages of 8 rows a 128-row PAGE) passes its own table as is. flydsl_pa_mqa_logits_fp4 forwards row_ends, query_start_loc, max_query_len and pages_per_block to the row-group kernel (page 8 / 64) and refuses them on its other pages. Uniform per-row bounds and pages_per_block = 1 build the same kernel as before; op_tests/test_flydsl_pa_mqa_logits_fp4.py passes at page 64 and 8.
N=7168, K=2048 (128x128 blocks), M 1..65536: the attention output projection at TP8, which had no row and took the heuristic kernel. Rows tuned on gfx950 as the table's others were; errRatio <= 0.0009 against the dequantized reference. Against the heuristic pick, same operands, cold: 1.01-1.03x at most M, 1.22x at M = 256. Only rows added.
…h plan The row-group kernel scores MQA as a GEMM: M a sequence's query rows times the heads, cut into tiles of whole rows (tile_m, a row's head sum stays in its tile), N its keys. Prefill and decode are the same ragged rows (query_start_loc, row_ends); next_n is gone from the kernel. Work is shared out over M tiles by real length inside the kernel, so a long sequence's tiles get more workgroups. - make_fp4_mqa_plan / Fp4MqaPlan: the launch (tile from TILE_ROWS, rows a wave, warps, workgroups, depth, nontemporal loads) from the batch's shape alone, so a captured graph keeps it; lru_cached. - flydsl_pa_mqa_logits_fp4_rowgroup(plan, ...) takes ragged rows only. - Page 8 compiles a single depth-2 walk: the two walks in one kernel spilled 40 VGPRs at 6 rows of 32 heads; one walk is as fast or faster on every shape measured (B=64 6 rows at 131K keys 185 -> 157 us, per-row tables T=3072 954 -> 838 us). Page 64 keeps both walks. - Launch through a cached CompiledFunction (tensor_shim._run_compiled): host cost per call 87 -> 16 us (FlyDSL rebuilt the cache key from every argument on each call). - flydsl_pa_mqa_logits_fp4: ragged rows (query_start_loc + row_ends, an optional plan) run the row-group kernel; a rectangular call runs the varctx kernel as before rowgroup existed, no silent conversion. The page-8 op test passes its rows as ragged rows. Prefill (V4.1 geometry) 1.9-13x faster than scoring each row as its own sequence; decode on par with the previous row-group kernel; outputs bit-exact against it.
Summary
FlyDSL row-group FP4 paged MQA logits (
aiter/ops/flydsl/kernels/mqa_logits/pa_mqa_logits_fp4_rowgroup.py)pa_mqa_logits_fp4kernel reads every key once per row.flydsl_pa_mqa_logits_fp4now routeskv_block_size8 and 64 to it. Its signature is unchanged.cta_info,block_k,num_warpsandparallel_unit_numdo not apply.outis left as it was past each row's bound (no -inf fill).pack_kv_cache). A candidate layer's kept 8-row blocks are its block table, so no gather is needed.max_seq_lenonly, so CUDA graphs keep it.compute_varctx_schedulemoves topa_mqa_logits_fp4_common.py; it is still importable frompa_mqa_logits_fp4.rope_rotate_activation(fp4):round_ropeop_tests/test_dsv4_rotate_quant.py --round-rope.Performance (MI355X, cold: 1 GiB read before each timed call, kernel only)
All numbers are the speedup of the new kernel (old time / new time).
vs the original FlyDSL
pa_mqa_logits_fp4(its schedule prebuilt, i.e. its best case):vs aiter Triton
paged_mxfp4_mqa_logits:On page 64 the comparison is to the faster of Triton's static and dynamic schedules, and the dynamic schedule is built outside the timing.
Test plan
python op_tests/test_flydsl_pa_mqa_logits_fp4.py(page 64) and--kv_block_size 8: PASS (cos 1.0, past-bound columns untouched)row_ends, bf16/f32 weights, random page permutations, uneven lengths up to 100K, and batch up to 200.ruff/blackclean on the changed Python filespython op_tests/test_dsv4_rotate_quant.py --round-rope