Repository navigation
[Engine] qwen4: QSA prompt attention for eight query heads a KV head from registers, the same bits - #470
Closed
drakarah wants to merge 2 commits into
Closed
[Engine] qwen4: QSA prompt attention for eight query heads a KV head from registers, the same bits#470drakarah wants to merge 2 commits into
drakarah wants to merge 2 commits into
Conversation
…isters, the same bits q4_qsa_attend_batch_kernel (sparse attention for prompt batches over 16 rows) gets an instance for exactly eight query heads a KV head, Qwen4's geometry: each lane keeps its eight dimensions of the eight queries in registers instead of reading them from shared memory for every key; the eight dot products are summed across the warp in one transposed reduction (9 shuffles instead of 40) that adds the same pairs as warp_sum() (xor 16, 8, 4, 2, 1); and the V sum reads a key's eight weights as two float4 from a key-major copy. Every output has the same bits; other geometries take the old instance. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW
This was referenced Dec 5, 2026
…rnel's bits poly_qwen4_test's qsa_g8_bits runs q4_qsa_attend() with the eight-heads-a-KV-head instance and, through the test switch q4_qsa_attend_g8(false), with the general instance it stands in for, on the same input, and fails on any output whose bits differ: FP16 and Q8_0 caches, head dims 128 and 256, 40 rows (the batch path), selections of 1, 63, 64, 65 and 2,051 cells and random counts. On a HIP build it compares the two there, where the warp size differs. RTX 4060 Laptop, CUDA 13.4: 0 of 491,520 outputs differ; with the score of one key changed by 1e-7 in the eight-head kernel, 156,616 differ (the test fails). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW
Collaborator
|
Integrated on |
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.
Problem and result
In a long prompt Qwen4's sparse attention layers (QSA, 10 of 40) are a large share of the GPU's time: in a 6,000-token prompt on an RTX 4060 Laptop,
q4_qsa_attend_batch_kerneltook 1,187 ms of 4,024 ms of kernel time (29.5%, Nsight Systems), in 40 launches of about 30 ms. Nsight Compute on one launch (1,024 rows, Q8_0 KV): 34.3 ms, SM throughput 82%, L1/shared 66%, DRAM 0.4%. The kernel was bound by issuing instructions, not by memory: for every key each lane read its eight dimensions of all eight queries from shared memory (16 128-bit loads), the eight dot products took eightwarp_sum()s (40 shuffles), and the V sum read the eight weights one at a time.Now the same launch takes 12.9 ms (x2.66). Prompt speed, Qwen4-35B-A3B UD-IQ4_XS,
--kv q8_0, interleaved runs againstmainwith the same tokens out of every run:main--max-context 20000(2 runs)The gain grows with the prompt, as the attention's share does. Decode is not touched: verify windows of 16 rows or fewer take the split kernel.
Related discussion
none (no issue).
Approach
q4_qsa_attend_batch_kernelgets a third template parameterG8andq4_qsa_attend()launches the<IS_Q8, 8, true>instance when there are exactly eight query heads a KV head (Qwen4: 16 heads, 2 KV heads). In it:warp_sum()adds, over the same lane sets in the same order, and float addition is commutative, so each score has the bits it had. The lane with(lane & 3) == 0writes the score its bits 2-4 name;spt[c][h], 2 KiB more shared memory), and the V sum reads a key's eight weights as twofloat4, with thefmafchain unchanged.__launch_bounds__(256, 3)(tried: 85 registers, not faster).Validation
Revision
a20ebca92. RTX 4060 Laptop GPU 8 GB (sm_89), i9-12900H, 30 GB RAM, Linux, CUDA 13.4; Qwen4-35B-A3B UD-IQ4_XS (Unsloth, pinned742bc191),--kv q8_0 --max-context 8192unless said.Passed:
mainand from this branch side by side (96 rows, 5,000 cells, selections of 1, 63, 65, 2,051 and random counts,sel_cap2,051): 0 of 5,013,504 outputs differ in any bit, FP16 and Q8_0 KV, head dims 128 and 256, 8, 12, 16 and 32 query heads over 2 KV heads. Times there: eight heads a KV head 2.02 → 0.56 ms (FP16) and 2.14 → 0.72 ms (Q8_0); the other geometries within 1%.poly_qwen4_test(built with-DSTRATA_BUILD_TESTS=ON): all checks, includingqsa_kernels(split + batch sparse GQA against the host).mainand this branch for the 2,048, 6,000 and 16,000 token prompts in every run above, default and with the experts held still (STRATA_FREEZE_EXPERTS=1 --expert-swaps 0, 32 tokens on 6,000 too).mainnumbers in the table are from a build ofmainplus myfeat/quant-iq4xs-q4k-rowsCPU kernels, which give the same bits (that PR's own validation); the comparisons are against that build.Failed:
<IS_Q8, 8>instance (beforeG8): 0 bits differ, but G = 4 and 6 were 40-55% slower; hence the separate instance.Not run:
__shfl_xor_syncat width 32 like the rest ofqwen4.cu, but I have no ROCm build), Metal (not taken there).--batchserving.Limits
🤖 Generated with Claude Code