Repository navigation
perf(paged-attn): faster long-context prefill and decode, same bits for Q8_0 K/V - #980
Merged
Merged
Conversation
…or Q8_0 K/V Prefill (WMMA kernel): V is staged transposed in shared memory, so each V fragment is one 16-byte load and the identity MMA that transposed it is gone; Q8_0 is dequantized in packed f16 with the same single rounding. Decode and tree verify: K.Q scores reduce two tokens at a time, and on wave32 RDNA the lane exchanges use DPP, permlanex16 and readlane instead of shuffles through shared memory. Same additions in the same order. The helpers move to paged-attn-common.cuh and test_paged_attn_lanes checks them bit for bit (reductions on random inputs with signed zeros and subnormals, the Q8_0 dequant over every f16 scale and quant, the transposed staging against the row-major one). Qwen3.8-27B on gfx1201, Q8_0 K/V, fixed verify width 16, main -> this: decode 41.2 -> 46.0 tok/s at 18K, 27.8 -> 31.8 at 53K, 18.5 -> 22.4 at 103K; time to first token 166.1 -> 142.1 s at 103K. Greedy text identical at 1K to 103K; kernel output hashes identical on gfx1201 and gfx1151. F16 K caches can change low bits at a few shapes: the kernel uses fewer registers, which moves the occupancy-derived partition count. Co-Authored-By: Claude <[email protected]>
davide221
added a commit
that referenced
this pull request
Nov 10, 2026
…path The fast path reads src1 with contiguous channels and any token and sequence stride that is a multiple of 4 bytes, so a token row may be a column slice of a stacked projection and sequences may be padded. Two comments still described the strict dense layout of the original specialization. With the fast path, the concat kernels at 102,766 prompt tokens on Qwen3.8-27B take 0.77 s of GPU time instead of 6.41 s (profiled on the branch of #980, whose concat is the same as main's). Co-Authored-By: Claude <[email protected]>
davide221
marked this pull request as ready for review
November 10, 2026 22:43
davide221
added a commit
that referenced
this pull request
Nov 10, 2026
* perf(ggml-cuda): strided source rows on the transposed concat fast path The Qwen 3.5/3.6 DeltaNet conv input is concat(conv_history, transpose(qkv)) along tokens. Where a layer's gate and qkv projections are stacked, qkv is a column slice of 16,384-float rows, so the dense-only fast path rejected it and the generic concat_non_cont kernel ran instead, reading one channel per lane `row` floats apart (about 64 GB/s on gfx1201). The 32x32 shared-memory transpose now takes the source's token-row and sequence strides; channels must still be contiguous. It is a copy, so the output bits are unchanged. Measured on an R9700 (Qwen3.8-27B IQ4_XS, paged prefill, 2,048-row chunks): CONCAT 6.41 s -> fast path at a 102,766-token prompt; time to first token 1K / 4K / 18K / 53K / 103K 1.2 / 3.8 / 17.3 / 59.4 / 142.3 s -> 1.2 / 3.6 / 16.5 / 57.1 / 138.0 s; greedy text without the drafter identical at all five sizes. test_concat_transpose: three strided cases (including 3 + 2048 tokens x 10,240 channels from 16,384-float rows), bit-exact against the host reference, all on the fast path. Co-Authored-By: Claude <[email protected]> * perf(qwen35): embed prompt chunks straight into pinned rows Each 2,048-row prefill step used to dequantize its token embeddings into a fresh, zero-filled 42 MB vector, copy them into a second host buffer, and upload that pageable buffer synchronously. Prompt chunks and decode rows are now embedded once into one pinned host buffer (the device's host buffer type), grown on demand and uploaded asynchronously. Lifetime: the buffer is only rewritten by the next step. Every step synchronizes after its compute, nothing returns between the upload and the compute, and the compute-failure path now synchronizes before it returns. Measured on an R9700 (Qwen3.8-27B IQ4_XS, paged prefill): host time per 2,048-row chunk staging 15.9 -> 7.9 ms, upload 14.1 -> 0.03 ms. With the checkpoint change that follows, time to first token at 1K / 4K / 18K / 53K / 103K tokens 1.2 / 3.6 / 16.5 / 57.1 / 138.0 s -> 1.1 / 3.5 / 16.3 / 56.7 / 137.0 s; greedy text without the drafter identical to the base at all five. Co-Authored-By: Claude <[email protected]> * perf(qwen35): huge pages for prefix checkpoint snapshots A checkpoint copies the paged KV and recurrent state into a fresh host buffer (3.7 GB at a 103K-token prompt), whose pages are first touched by that copy. On Linux, madvise(MADV_HUGEPAGE) on the buffer's 2 MiB-aligned interior lets the copy fault once per 2 MiB instead of once per 4 KiB. Advice only: contents and layout are unchanged, and it compiles out where MADV_HUGEPAGE does not exist. Measured on an R9700 box (THP in madvise mode): checkpoint time at 18K / 53K / 103K tokens 322 / 768 / 1,392 ms -> 282 / 690 / 1,271 ms. Co-Authored-By: Claude <[email protected]> * docs(ggml-cuda): describe the strided src1 layout of the concat fast path The fast path reads src1 with contiguous channels and any token and sequence stride that is a multiple of 4 bytes, so a token row may be a column slice of a stacked projection and sequences may be padded. Two comments still described the strict dense layout of the original specialization. With the fast path, the concat kernels at 102,766 prompt tokens on Qwen3.8-27B take 0.77 s of GPU time instead of 6.41 s (profiled on the branch of #980, whose concat is the same as main's). Co-Authored-By: Claude <[email protected]> * test(ggml-cuda): concat transpose with padded sequences Adds a case whose source sequences are not tokens x row width apart (5 unused token rows between them), so the sequence stride is checked on its own and not only through the token stride. Co-Authored-By: Claude <[email protected]> * revert(qwen35): drop huge pages for prefix checkpoint snapshots This reverts commit b2ca568. The test box runs transparent huge pages with enabled=madvise and defrag=madvise. Under defrag=madvise, a page fault in a MADV_HUGEPAGE region enters direct reclaim and compaction, so on a box whose free memory is fragmented the checkpoint copy can stall instead of getting faster. The measured gain was 120 to 135 ms per checkpoint at 53K to 103K tokens (0.1% of the time to first token), on a box with plenty of free 2 and 4 MiB blocks; that is too small to carry a platform setting dependent risk. Main (f5993e0) against this branch after the revert, Qwen3.8-27B, greedy without the drafter, time to first token: 1.18 to 1.16 s at 1,035 tokens, 3.79 to 3.62 s at 4,176, 18.06 to 17.25 s at 18,355, 66.02 to 63.47 s at 53,030 and 166.29 to 161.04 s at 102,766 tokens (-1.7 to -4.5%). The generated text is identical at all five sizes. Co-Authored-By: Claude <[email protected]> * refactor(qwen35): reserve the input embedding rows once, in a PinnedStage The pinned input-embedding rows grew during requests through a raw host buffer, freed by hand. They now use the shared PinnedStage helper and are reserved once when the engine is built, for the largest step step_plan_limits() allows: min(max prefills x 2048, the largest of the idle, mixed and long mixed token caps) prompt rows plus the decode bucket of every slot. step() already rejects plans above those limits, and checks the sum once more before staging. Without pinned memory the rows fall back to pageable memory, with one log line. Also drops a token offset that was initialized twice. Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
davide221
commented
Nov 10, 2026
davide221
left a comment
Contributor
Author
There was a problem hiding this comment.
Code review
No issues found. Checked for bugs and logic errors in the changed code.
Reviewed 297ab233 · 0 candidate issues checked, kept at ≥70% confidence · add the code-review label to review again
davide221
added a commit
that referenced
this pull request
Nov 10, 2026
The lane-isolation fix this branch was stacked on is on main as #981, so the branch now carries only the overlap step on top of main. Co-Authored-By: Claude <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Long-context speedups in
ggml-cuda/paged-attn.cu. All of them keep the outputs bit-identical for q8_0 K/V caches.Prefill (WMMA kernel,
paged_attn_wmma, RDNA4)1024 + (q + 128)with a byte permute and subtracts 1152. That givesqexactly, and one f16 multiply bydfollows.mmb-q8f16.cuhalready uses the same idea.static_assertfor every phase that aliases it (Q, K, V, combine).Decode and tree verify (
paged_attn_decode)paged_attn_reduce_group<2>. The first butterfly level trades half of the pair instead of a whole sum, so every exchange carries a value the partner still needs.row_xmaskandv_permlanex16replaceds_bpermutein every butterfly: scores, tile max and tile sum.readlanereplaces the broadcast shuffles of the token index and the softmax weights.__shfl_*_sync.Helpers and test
paged-attn-common.cuh: lane exchanges, group reduction, packed Q8_0 dequant and transposed store.test_paged_attn_lanes(HIP and CUDA) checks them bit for bit:reduce_group<2>and<1>, warp sum, warp max, xor exchange and lane broadcast againstwarp_reduce_sum,warp_reduce_maxand the plain shuffles. Inputs are random values with signed zeros, subnormals and cancelling pairs, in blocks of several warps.__float2half(__half2float(d) * q), exhaustively over all 65,536 f16 scales (NaN excluded) x 256 quants.Side effect. On
main,paged-attn.cudoes not compile for gfx90a or gfx942:A_identityis undeclared on the MFMA path. It now compiles for gfx90a, gfx942, gfx1100, gfx1151 and gfx1201. The non-RDNA, non-ldmatrixbranch of the V load isNO_DEVICE_CODE; the launcher already limits this kernel to RDNA4.Why it is exact
warp_reduce_sum, in the same order. DPP andpermlanex16move the same values a shuffle would. The unit test checks this bitwise.1024 + (q + 128)and the subtraction are exact in f16, so the only rounding is the f16 productd * q. The old path rounds the exact float productd * qto f16 once. The results are equal for every finite input.__float2half(-0.0f)returns +0.cudaOccupancyMaxActiveBlocksPerMultiprocessor), so a register change can shift bits.Verification
Hardware: Radeon AI PRO R9700 (gfx1201) and Radeon 8060S (gfx1151), ROCm 7.2,
GGML_HIP_GRAPHS=OFF. Base ismain, built the same way.test_paged_attn_lanes, both GPUstest_model_smoke PagedAttention, both GPUsmain, q8_0 K/V, gfx1201: tree, hot, seqs and rows modes (20 shapes each), chunk mode (5 prefixes)main, q8_0 K/V, gfx1151: tree mode (20 shapes)main, fixed verify width 16, 1K to 103K promptsMeasured
Model: Qwen3.8-27B, q8_0 K/V cache, on gfx1201. Prompts are 1K to 103K tokens, 300 output tokens, temperature 0.
Fixed verify width 16 (
LUCE_ADAPTIVE_SPEC_WIDTH=0). The text is run-stable here, and identical betweenmainand this branch.Kernel time per full-attention layer at 102,400 tokens of context:
On gfx1151, the 16-row tree verify goes from 12,812 to 9,897 us per layer (-23%).
Default adaptive verify width (development build with the same kernel diff). These numbers are noisy, and the greedy text is not run-stable even without this change: the width controller picks widths from wall-clock step time. Two base runs give a sense of the noise.
Known limits
mainas well. Pinning a per-arch occupancy constant would fix it, but that would change bits once, so it is left out.__shfl_*_sync,__byte_perm) and theldmatrixpath were not built here; there was no CUDA compiler. CI covers them.🤖 Generated with Claude Code