Skip to content

[HIP] [FlyDSL] feat(flydsl): row-group FP4 paged MQA logits for 8- and 64-row pages - #6145

Merged
valarLip merged 7 commits into
mainfrom
ljin/fp4-mqa-rowgroup
Oct 6, 2026
Merged

valarLip merged 7 commits into
mainfrom
ljin/fp4-mqa-rowgroup

Conversation

@valarLip

@valarLip valarLip commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

Summary

FlyDSL row-group FP4 paged MQA logits (aiter/ops/flydsl/kernels/mqa_logits/pa_mqa_logits_fp4_rowgroup.py)

  • A wave scores a group of one sequence's query rows off each key load. The existing pa_mqa_logits_fp4 kernel reads every key once per row.
  • flydsl_pa_mqa_logits_fp4 now routes kv_block_size 8 and 64 to it. Its signature is unchanged.
    • On this path cta_info, block_k, num_warps and parallel_unit_num do not apply.
    • A given out is left as it was past each row's bound (no -inf fill).
  • Page 64 keeps the existing preshuffled cache layout and its writers.
  • Page 8 (DeepSeek-V4.1's indexer page size) has its own layout (pack_kv_cache). A candidate layer's kept 8-row blocks are its block table, so no gather is needed.
  • Work is shared out by each sequence's real length inside the kernel: a static split, or a length-balanced share when that walks shorter.
    • There is no separate schedule launch.
    • The grid depends on batch and max_seq_len only, so CUDA graphs keep it.
  • Keys and scales are read through 64-bit addresses, so pools can exceed 4 GiB.
  • Sequences are handled in runtime chunks of 64, so one build per shape serves any batch.
  • compute_varctx_schedule moves to pa_mqa_logits_fp4_common.py; it is still importable from pa_mqa_logits_fp4.

rope_rotate_activation (fp4): round_rope

  • When set, the RoPE-rotated values are rounded to the input dtype before the FP4 quant. This matches a model whose RoPE writes its output back in that dtype (DeepSeek-V4.1's index query).
  • Default off; existing behaviour is unchanged.
  • Covered by a bit-exact test: op_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):

shape speedup
page 64, B = 64, max 100K keys, H 64 next_n 1 1.07–2.6x
page 64, same, H 64 next_n 2 1.25–2.4x
page 64, same, H 32 next_n 6 1.37–3.0x
page 64, B = 1–16, ≤ 8192 row-steps (where dispatch used to keep the old kernel) 1.25–2.9x
page 8, V4.1 full layers, T = 6–768, 200K context 1.46–3.4x

vs aiter Triton paged_mxfp4_mqa_logits:

shape speedup
page 8 full layers 1.08–1.56x
page 8 reindex (ATOM's candidate table vs the Triton gather path) 1.03–1.44x
page 64 1.1–2.0x on most shapes, at or near parity on the rest

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)
  • Standalone correctness sweep against the official FP4 reference: ~40 cases.
    • Covers page 8/64, next_n 1/2/6, H 32/64, row_ends, bf16/f32 weights, random page permutations, uneven lengths up to 100K, and batch up to 200.
    • Result: all pass.
  • ruff / black clean on the changed Python files
  • python op_tests/test_dsv4_rotate_quant.py --round-rope
  • CI

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).
@valarLip
valarLip requested a review from a team October 5, 2026 14:12
@github-actions github-actions Bot changed the title feat(flydsl): row-group FP4 paged MQA logits for 8- and 64-row pages [HIP] [FlyDSL] feat(flydsl): row-group FP4 paged MQA logits for 8- and 64-row pages Oct 5, 2026
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
multigpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 6145 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants