Skip to content

[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
simplexai-labs:mainfrom
drakarah:feat/qwen4-qsa-prefill
Closed

drakarah wants to merge 2 commits into
simplexai-labs:mainfrom
drakarah:feat/qwen4-qsa-prefill

Conversation

@drakarah

@drakarah drakarah commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

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_kernel took 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 eight warp_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 against main with the same tokens out of every run:

Prompt main This branch Change
6,000 tokens (3 runs each) 1,154.9 ± 11.5 tokens/s 1,351.1 ± 18.7 +17.0%
6,000 tokens, experts held still (3 runs) 1,156.9 ± 30.3 1,338.5 ± 46.7 +15.7%
16,000 tokens, --max-context 20000 (2 runs) 1,203.9 ± 1.9 1,487.7 ± 4.9 +23.6%
2,048 tokens (3 runs, experts held still) 1,005.9 ± 39.4 994.5 ± 60.1 the same (QSA selects every key below 2,048)

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_kernel gets a third template parameter G8 and q4_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:
    • each lane loads its eight dimensions of the eight queries into registers once (64 floats; head dims up to 256, so a lane has at most one group of eight);
    • the eight dot products are summed across the warp by one transposed reduction: at xor 16 a lane keeps four heads and sends four, at xor 8 two, at xor 4 one, then xor 2 and xor 1 (9 shuffles instead of 40). The partial sums added are the ones 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) == 0 writes the score its bits 2-4 name;
    • the online softmax also writes the chunk's weights key-major (spt[c][h], 2 KiB more shared memory), and the V sum reads a key's eight weights as two float4, with the fmaf chain unchanged.
  • Other geometries (and the split kernel for 16 rows or fewer) take the old instance unchanged: putting the register path in the same instance made G = 4 and 6 about 40% slower (registers).
  • Kept out: the split (decode) kernel, the query batching across rows that share keys, __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, pinned 742bc191), --kv q8_0 --max-context 8192 unless said.

Passed:

  • A standalone harness with the kernel from main and from this branch side by side (96 rows, 5,000 cells, selections of 1, 63, 65, 2,051 and random counts, sel_cap 2,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, including qsa_kernels (split + batch sparse GQA against the host).
  • Engine: the same tokens from main and 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).
  • Nsight Compute (application replay), one launch of 1,024 rows: 34.34 → 12.91 ms; 58 → 128 registers, occupancy 67% → 33%; SM throughput 82% → 62%.
  • The main numbers in the table are from a build of main plus my feat/quant-iq4xs-q4k-rows CPU kernels, which give the same bits (that PR's own validation); the comparisons are against that build.

Failed:

  • The register path in the shared <IS_Q8, 8> instance (before G8): 0 bits differ, but G = 4 and 6 were 40-55% slower; hence the separate instance.

Not run:

  • HIP (the code is plain CUDA C++ with __shfl_xor_sync at width 32 like the rest of qwen4.cu, but I have no ROCm build), Metal (not taken there).
  • Other cards; prompts past 16,000 tokens; --batch serving.
  • Reproduction by anyone else; the whole test suite.

Limits

  • Only Qwen4's geometry (eight query heads a KV head) is faster; other QSA geometries are unchanged.
  • One card, one model file.

🤖 Generated with Claude Code

…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
…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
@VecSzn

VecSzn commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator

Integrated on main in commits a5063ce43 and 3e717f596 (491,520/491,520 bit-exact verified by poly_qwen4_test; included in upcoming v0.2.20-poly.11). Thanks @drakarah!

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