Skip to content

hip : Qwen4-35B-A3B QSA attention for GQA 8 and q8_0 K/V (pp 409 -> 2254 at 128k) - #345

Open
dzannotti wants to merge 2 commits into
masterfrom
hb/qwen4-qsa-gqa8-q8
Open

dzannotti wants to merge 2 commits into
masterfrom
hb/qwen4-qsa-gqa8-q8

Conversation

@dzannotti

Copy link
Copy Markdown
Collaborator

Overview

Qwen4-35B-A3B (#343) loads and runs, but on HIP its prefill falls off a cliff past the QSA budget: pp2048 is 409 t/s at
128k depth against 3006 at depth 0. Two gaps in the HIP QSA kernels cause it, both specific to this model's shape and
cache type:

  1. GQA 8. qsa_prefill takes only GQA 12 (Qwen3.8-Flash-Next: 24 query heads over 2 KV heads). Qwen4-35B-A3B
    has 16 over 2. Once the cache holds more than indexer_top_k + ratio - 1 cells, every full-attention layer's
    selected-key attention is rejected by ROCm0 and the scheduler runs it on the CPU (10 CPU splits per ubatch).
  2. q8_0 K/V. Both QSA kernels take f16 K/V only, so with -ctk q8_0 -ctv q8_0 the selected-key probe in
    llama_memory_hybrid_idx fails and every QSA layer builds the masked top-k route (a [n_kv, n_tokens] mask and
    dense attention), which grows with the depth.

What's in it

  • qsa_prefill: the attention kernel is templated on the GQA ratio. A group of 4 queries is 4*GQA rows: 3 WMMA
    tiles at GQA 12 (the existing code path) and 2 at GQA 8. The merge kernel and the 16-bit block masks are unchanged.
  • qsa_prefill: a q8_0 K or V is converted to contiguous f16 (ggml_get_to_fp16_nc_cuda), then attended by the f16
    kernel.
  • qsa_decode: the scalar kernel reads K/V rows as f16 or q8_0 (lane l of dim block d is element l of q8_0
    block d). The WMMA kernel stays f16 / GQA 12.
  • test-backend-ops: GQA 8 cases for QSA_PREFILL and QSA_PREFILL_MASKLESS (incl. 2048 queries over 131072 keys),
    and q8_0 K/V cases at GQA 8 and 12.

HIP only. Vulkan, CUDA and the llama/ graph code are not touched.

Measurements

Device:   Ryzen AI Max+ 395, 128 GB LPDDR5X-8000, gfx1151
OS:       Ubuntu 26.04, kernel 7.0.0-44-generic
Backend:  ROCm 10.1 (HIP), -DGGML_HIP=ON -DGPU_TARGETS=gfx1151 -DCMAKE_BUILD_TYPE=Release
Base:     master 94ec9a3dd
Weights:  unsloth/Qwen4-35B-A3B-GGUF UD-Q4_K_XL
Command:  llama-bench -fa 1 -ngl 99 -ub 2048 -p 2048 -n 32 -d 0,32768,131072 -r 1
test KV master this PR
pp2048 f16 3006 2983
pp2048 @ d32768 f16 525 2635
pp2048 @ d131072 f16 409 2254
pp2048 @ d32768 q8_0 1286 2613
pp2048 @ d131072 q8_0 not run 2264
tg32 @ d131072 q8_0 29.7 39.6
tg128 f16 50.4 50.5

The q8_0 master cell at 128k was not run; at 32k the q8_0 path on master is the masked route described above.

Correctness

  • test-backend-ops -b ROCm0: QSA_PREFILL 32/32, QSA_PREFILL_MASKLESS 31/31, QSA_DECODE 92/92,
    QSA_DECODE_MASKLESS 56/56, FLASH_ATTN_EXT 4387/4387.
  • KLD, llama-perplexity, the four tests/corpus files concatenated, -c 8192 -b 2048 -ub 2048 (so most positions are
    past the QSA budget). Reference: master at ub2048, which runs these layers on the CPU (f16 KV) or on the masked
    route (q8_0 KV).
run KLD 99.9% KLD same top-1 PPL (base 2.3180 f16 / 2.3121 q8_0)
this PR, f16 KV 0.00730 +- 0.00024 0.421 97.32 % 2.3173
this PR, q8_0 KV 0.00871 +- 0.00033 0.619 n/a 2.3182
GPU dense masked route (this PR, LLAMA_QSA_SELECTED_KEY=0), f16 KV 0.00779 +- 0.00028 0.505 97.36 % 2.3194
master ub1024 vs ub2048, f16 KV 0.00061 0.011 98.96 % 2.3178

The PR's difference against master is the difference between GPU (f16 WMMA) and CPU attention: the GPU dense
masked route, which does not use the changed kernels, lands at the same KLD and top-1. PPL is equal within the error.

Not tested

  • Qwen3.8-Flash-Next (GQA 12) end to end. Its kernel path is the same code instantiated at GQA 12 and the GQA 12
    test cases pass, but no model run.
  • Vulkan and CUDA (not changed).
  • --parallel > 1 beyond the existing multi-stream prefill test case.

AI usage: the changes, measurements and this description were produced by an AI agent (Claude Code) under my direction, on my box.

🤖 Generated with Claude Code

https://claude.ai/code/session_0177uRMCLjJEx8S8sE4dQeXo

The QSA prefill kernel took only GQA 12 (Qwen3.8-Flash-Next). Qwen4-35B-A3B has 16 query heads over 2 KV heads, so
past the indexer budget every full-attention layer's selected-key attention fell back to the CPU.

The kernel is templated on the GQA ratio: a group of 4 queries is 4*GQA rows, 3 WMMA tiles at GQA 12 (unchanged)
and 2 at GQA 8.

Assisted-by: Claude Code
With a q8_0 cache the selected-key probe failed (both QSA kernels took f16 K/V only), so every qwen4exp QSA layer
built the masked top-k route: a [n_kv, n_tokens] mask and dense attention, which grows with the depth.

- decode: the scalar kernel reads K/V rows as f16 or q8_0 (lane l of dim block d is element l of q8_0 block d).
  The WMMA kernel stays f16.
- prefill: a q8_0 K or V is converted to contiguous f16 first, then attended by the f16 kernel.

Assisted-by: Claude Code

@Nathanw1014 Nathanw1014 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks. I read through the GQA templating, the q8_0 prefill conversion and the q8_0 decode lane mapping, and they check out.

Small, non-blocking notes:

  1. qsa-prefill.cu:556-571: when only one of K/V is q8_0, the f16 one is also given fake contiguous strides and data = nullptr in the recursive check, so its real stride and alignment checks are skipped. Rewriting only the quantized tensor would keep them.
  2. qsa-prefill.cu:255: the static_assert allows NT 1..8, but part[NT][8][64] is too small for the 16 KiB output staging at NT = 1 (GQA 4). Only 8 and 12 are instantiated, so this is latent. Tightening the assert (or sizing part as max(NT, 2)) would guard it.
  3. qsa-prefill.cu:661-680: q8_0 converts the whole cache to f16 per layer per ubatch. That's fine at pp2048. Small ubatches at deep context might feel it.
  4. A QSA_DECODE_MASKLESS q8_0 case at GQA 8 (Qwen4's decode shape) would be nice. The q8_0 decode cases are GQA 12/4 only.

This branch has not been deployed

No deployments
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