Skip to content

[Triton/Gluon] [Perf] Support page size 8 in the preshuffle MQA logits kernel - #5768

Merged
zhiding512 merged 2 commits into
zhiding512/mqa-logits-page8-main-20260922from
zhiding512/mqa-logits-page8-preshuffle-20260922
Sep 23, 2026
Merged

zhiding512 merged 2 commits into
zhiding512/mqa-logits-page8-main-20260922from
zhiding512/mqa-logits-page8-preshuffle-20260922

Conversation

@zhiding512

@zhiding512 zhiding512 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Problem

DeepSeek V4.1 decode pages the indexer KV cache at 8 tokens per page. #5749 makes that page size work, but only on the plain (non-preshuffle) path, which is the slow one. Preshuffle is refused outright:

assert KVBlockSize % 16 == 0, "Preshuffle mode only supports KVBlockSize aligned to 16."

The shuffled KV feeds the 16-token MFMA B operand directly, and a page of 8 is half a tile, with the other half in a physically unrelated page.

This is not hypothetical. ATOM's V4.1 index scorer already calls this op with Preshuffle=True (atom/model_ops/deepseek_v41/paged_scoring.py), and its candidate_block_size is 8, so the plain-path fix alone does not reach it.

What this changes

No new load structure is needed. The per-lane page index already varies at KVBlockSize granularity, and a page shorter than ChunkK // 2 already takes the "page index per lane" branch, so the two pages behind one tile are already addressable. What was hardcoded is the shuffle group: the B-operand offset assumed 16 rows per group.

  • ShuffleRows = min(KVBlockSize, 16) in both _gluon_deepgemm_fp8_paged_mqa_logits_preshuffle and its _varctx twin, replacing the literal 16 in the B-operand offset. The tile is then assembled from 16 // KVBlockSize pages.
  • The wrapper accepts a page that divides the tile, not only one that is a multiple of it. gfx1250 preshuffle reshapes by a literal KVBlockSize // 16 for its TDM block load, so pages below 16 stay rejected there rather than mis-addressed.
  • The row-group stride goes from a literal 16 * 128 to ShuffleRows * HiddenDim to stay consistent with the above. This is not what makes page 8 work — a page that short holds one group, so the term is zero either way — and at HiddenDim 128 the two expressions are equal. No configuration a caller can reach today changes; every caller runs HiddenDim 128.
  • The benchmark can now express a page-8 run. --blocksize applied only to the preshuffle path and had to be a multiple of 16, Preshuffle was inferred from it rather than taken from the flag, and the host shuffle used the default (16, 16) layout. Those are the same assumptions the kernel drops. It also records the scored token count, which a bandwidth figure needs.

Caller-visible: a page below 16 must be shuffled in groups of its own length, shuffle_weight(kv, layout=(KVBlockSize, 16)), rather than the default (16, 16).

Performance

gfx950, heads 32 / head-dim 128, via op_tests/op_benchmarks/triton/bench_deepgemm_attention.py. Three rounds per point with the arms rotated inside a round, medians below; round-to-round spread is under 1.9 us everywhere. Every point passes the benchmark's own FP32 reference check, and the four arms of a row share a seed, so their context lengths and page tables are identical.

The rows below carry a 286 MB KV working set, past this card's 256 MB MALL, so their TB/s is DRAM-bound.

batch 16, next_n 1, 1M context

page layout latency effective BW
8 plain 431.384 us 5.125 TB/s
16 plain 410.064 us 5.382 TB/s
8 preshuffle 379.863 us 5.820 TB/s
16 preshuffle 359.784 us 6.134 TB/s

Halving the page is not free: it costs 5.58% here, and 3.01% to 5.20% across the five other shape and layout pairs measured (batch 32 / next_n 2 / 128K and batch 128 / next_n 1 / 128K, each on both layouts). Plain and preshuffle move together, so what is being paid for is the page and not the shuffle — one tile's contiguous run drops from 256 B to 128 B, and the page table doubles in width. The page table is 0.18% of the byte model, which leaves the contiguity.

That cost does not change what a page-8 caller should do, because page 16 is not one of its options. Against the only alternative it has, page-8 plain, preshuffle is 11.9% faster, and it also beats page-16 plain by 7.4%. The preshuffle speedup itself barely changes with the page: 1.140x / 1.692x / 1.096x at page 16 against 1.136x / 1.664x / 1.088x at page 8, over the three shapes.

An earlier revision of this PR claimed page-8 and page-16 preshuffle were level. That came from a single point measured with a throwaway probe whose KV allocation differed from the benchmark's; the numbers above replace it.

Testing

op_tests/test_pa_mqa_logits_offset.py, 105 cases, all passing on gfx950 (MI35x).

The new test_paged_mqa_logits_preshuffle gives the preshuffle path the sweep the plain one already had — previously only the large-KV-offset cases reached it, all at the default ChunkK. It covers pages 8 / 16 / 32 / 64 / 128 / 256 x ChunkK 128 / 256 x compact / padded page tables x the decode and MTP shapes, 48 cases, against an FP32 reference built from the same FP8 inputs, with the output rows sentinel-guarded on both sides.

The page sizes pick out the ways the B tile gets assembled: 8 is half a tile and draws from two pages, 16 is exactly one, 32 through 128 are several row groups inside one page, and 256 exceeds ChunkK // 2 and so takes the one-page-per-stage branch. ChunkK 128 and 256 are the two the callers ask for — the bench runs preshuffle at 128, the indexer at 256.

Against the parent commit the 8 page-8 rows fail, rejected by the wrapper before they reach the device, and the other 40 pass unchanged. test_paged_mqa_logits_large_kv_offsets also picks up a preshuffle-B8 row at the 2 / 4 / 8 GiB boundaries.

Relationship to #5749

Stacked on #5749, which is the plain-path half of the same page-8 requirement. Only the top two commits belong to this PR; please review it after #5749 lands, or with base set to that branch.

@zufayu
zufayu requested review from a team and vgokhale September 23, 2026 01:12
zhiding512 and others added 2 commits September 23, 2026 01:43
The shuffled KV layout feeds the 16-token MFMA B tile directly, so the page
size had to be a multiple of 16. A page of 8 -- what DeepSeek V4.1 decode
allocates -- is half a tile, and the other half lives in a physically
unrelated page, so page-8 decode was stuck on the plain path.

No new load structure is needed for this. The per-lane page index already
varies at KVBlockSize granularity, and a page shorter than ChunkK // 2
already takes the "page index per lane" branch. What was hardcoded is the
shuffle group: the B-operand offset assumed 16 rows per group. Derive it as
ShuffleRows = min(KVBlockSize, 16) instead, matching the layout the host
passes to shuffle_weight, and the tile is read from 16 // KVBlockSize pages.

The row-group stride goes from a literal 16 * 128 to ShuffleRows * HiddenDim
to stay consistent with that. It is not what makes page 8 work -- a page that
short holds one group, so the term is zero either way -- and at HiddenDim 128
the two expressions are equal, so no configuration a caller can reach today
changes. Every caller runs HiddenDim 128.

gfx1250 preshuffle still reshapes by a literal KVBlockSize // 16 for its TDM
block load, so pages below 16 are rejected there rather than mis-addressed.

The benchmark could not express any of this: --blocksize applied only to the
preshuffle path and had to be a multiple of 16, Preshuffle was inferred from
it rather than taken from the flag, and the host shuffle used the default
(16, 16) layout. Those are the same assumptions the kernel drops, so drop them
there too, and record the scored token count that a bandwidth figure needs.

Measured with it on gfx950, heads 32 / head-dim 128, three rounds per point
with the arms rotated inside a round, medians below. Round-to-round spread is
under 1.9 us everywhere. These rows carry a 286 MB working set, past the
256 MB MALL, so their TB/s is DRAM-bound:

  batch 16, next_n 1, 1M context
    page 8  plain        431.384 us   5.125 TB/s
    page 16 plain        410.064 us   5.382 TB/s
    page 8  preshuffle   379.863 us   5.820 TB/s
    page 16 preshuffle   359.784 us   6.134 TB/s

Halving the page costs 5.58% here, and 3.01% to 5.20% over the five other
shape and layout pairs measured. Both layouts move together, so what is being
paid for is the page and not the shuffle: one tile's contiguous run drops from
256 B to 128 B, and the page table doubles in width. The page table is 0.18%
of the byte model, which leaves the contiguity.

That cost does not change what a page-8 caller should do, because page 16 is
not one of its options. Against the only alternative it has, page-8 plain,
preshuffle is 11.9% faster; it also beats page-16 plain by 7.4%.

Signed-off-by: zhimding <[email protected]>
Co-authored-by: Cursor <[email protected]>
Preshuffle had no page-size sweep of its own; only the large-KV-offset cases
reached it, and every one of those ran the default ChunkK. Mirror the plain
sweep instead: pages 8 through 256 across ChunkK 128 and 256, compact and
padded page tables, and the decode and MTP shapes.

The page sizes pick out the ways the MFMA B tile gets assembled. Page 8 is
half a tile and draws from two pages, 16 is exactly one, 32 through 128 are
several row groups inside one page, and 256 exceeds ChunkK // 2 and so takes
the one-page-per-stage branch. ChunkK 128 and 256 are the two the callers
ask for: the bench runs preshuffle at 128, the indexer at 256.

Against the parent commit the 8 page-8 rows fail, rejected by the wrapper
before they reach the device, and the other 40 pass unchanged.

Signed-off-by: zhimding <[email protected]>
Co-authored-by: Cursor <[email protected]>
@zhiding512
zhiding512 force-pushed the zhiding512/mqa-logits-page8-preshuffle-20260922 branch from d0031f8 to 4c780dc Compare September 23, 2026 01:45
@zhiding512
zhiding512 merged commit a74f806 into zhiding512/mqa-logits-page8-main-20260922 Sep 23, 2026
2 checks passed
@zhiding512
zhiding512 deleted the zhiding512/mqa-logits-page8-preshuffle-20260922 branch September 23, 2026 03:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants