Skip to content

[w2] fa-decode-quant-kv: v_perm dequant, opt-in KV split cap and tile GQA padding for quantized decode - #173

Merged
rafasmour merged 1 commit into
masterfrom
opt/w2-fa-decode-quant-kv
Dec 4, 2026
Merged

rafasmour merged 1 commit into
masterfrom
opt/w2-fa-decode-quant-kv

Conversation

@rafasmour

Copy link
Copy Markdown
Owner

What / why

Decode attention with q8_0/q4_0 K/V on RDNA3 (Gemma, Qwen3.8, Qwen4 with dense attention) goes through the direct-dequant tile kernel (flash_attn_tile, nb <= 8, D256/D512). About 450 VALU per KV tile went to dequantization, and GQA 6 (Qwen3.8: 24 heads / 4 KV heads) launched 3 blocks per KV head that each dequantize the whole head. Three changes:

  1. v_perm dequant (compile-time, bit-identical, default on, HIP only). flash_attn_load_tile_quant uses the variant F body from kern-survey-variants-A-F.patch (hunks 1-2 only, no streamk hunk): bytes placed under 0x64 with ggml_hip_perm, offset subtracted, scaled by d. Same values as int8 -> half -> * d. Only the dequant body is touched (the row index / oob part is left alone for qwen4-qsa-sparse-fa).
  2. GGML_CUDA_FA_DECODE_SPLIT_CAP=<n> (opt-in). In launch_fattn's parallel-blocks branch: RDNA3_0, Q->ne[1] <= 8, quantized K or V, not sparse: parallel_blocks = min(parallel_blocks, n*nsm / ntiles_dst). Unset keeps today's behaviour. Changes the combine order slightly. Note it only has an effect when the current selection exceeds n blocks per CU in total (e.g. cap 1, or many-tile launches), so tonight's sweep also tells whether 2/4 matter at all.
  3. GGML_CUDA_FA_TILE_GQA_PAD=1 (opt-in). launch_fattn_tile_switch_ncols2: for quantized K/V and nb <= 8, GQA 3 -> ncols2 4, GQA 5/6/7 -> ncols2 8 (existing instances, no new template). The tile kernel now derives sequence/head_kv/head0 from gridDim.z and gqa_ratio at run time (identical values when gqa % ncols2 == 0), zeroes padded Q columns, and skips their Q read, sink read, dst and meta writes. Model check (GGUF headers): Qwen3.8 24/4 = GQA 6 (benefits), Gemma 32/16 and 32/4, MiniCPM5 32/2 = GQA 16 (f16 path), Qwen4 16/2 = GQA 8 (already aligned), Nemotron-Bolt 8/2 = GQA 4.

Not done: the D512 "V is a view of K, keep the dequantized K tile in LDS" stretch (needs LDS/occupancy measurement on the GPU first). The Inkling D128 bias route was skipped as planned (GGUF no longer on disk).

Expected gain (estimate, not measured)

Scout estimate: tg +4-7% at 32k and +12-25% at 90-96k for q8_0 K/V models. Static ISA (below) supports roughly -25% VALU on the decode instances.

Compile / static checks (no GPU used)

  • HIP gfx1100 object files compile clean (no warnings): fattn.cu.o, fattn-tile-instance-f16-f16-dkq256-dv256.cu.o, ...dkq512-dv512.cu.o. CPU build of test-backend-ops clean; the new perf case runs on the CPU backend.
  • ISA of dkq256 tile object, master vs branch, direct-quant decode instances (ncols1 x ncols2, K/V types):
instance instructions VALU v_bfe v_perm VGPRs
<1,2> q8_0/q8_0 (GQA 2, Qwen3.8 padded variant uses <1,8>) 2001 -> 1643 1369 -> 1009 (-26%) 130 -> 3 0 -> 64 190 -> 190
<1,8> q8_0/q8_0 2716 -> 2386 1947 -> 1579 (-19%) 130 -> 2 0 -> 64 unchanged
<1,2> q4_0/q4_0 2056 -> 1683 1473 -> 1048 (-29%) 131 -> 3 0 -> 64 190 -> 190
<2,1> q8_0/q8_0 5471 -> 4212 4013 -> 2668 (-34%) 514 -> 2 0 -> 256 151 -> 149

Summed over all 34 quantized instances of the file: VALU 153937 -> 127624 (-17%), v_bfe 8736 -> 102, v_cvt_f16 9093 -> 453, v_mov_b16 8569 -> 60. VGPR counts drop or stay equal on the decode instances (e.g. <1,2> q8_0/q4_0 228 -> 186). Spills: pre-existing spills in the large-ncols instances (<32,1>, <4,8>, <8,4>) are unchanged or removed, except two <4,8> q8_0-q4_0 / <8,4> q8_0-q4_0 mixed instances going from 14-15 to 17-18 spilled VGPRs and <16,2> q4_0 getting 3 spilled VGPRs (unused: direct path only runs for nb <= 8, <16,2> is a 32-column prompt tile on the converted-f16 path).

Switches

  • v_perm dequant: compile-time, bit-identical, no switch (compare builds).
  • GGML_CUDA_FA_DECODE_SPLIT_CAP=<n> (opt-in, numerics change slightly).
  • GGML_CUDA_FA_TILE_GQA_PAD=1 (opt-in).

Files

ggml/src/ggml-cuda/fattn-common.cuh, ggml/src/ggml-cuda/fattn-tile.cuh, tests/test-backend-ops.cpp (eval: nb 1..8 with GQA 3/6/7, D256/512, q8_0, q4_0, q8_0-q4_0, sinks, 1 KV head, 2 sequences; perf: nb=1 at kv 32768 / 98304, GQA 6/8/2, in commented [w2 fa-decode-quant-kv] blocks).

Risks

  • Padded Q columns must be skipped exactly: eval GQA 6/3/7 with sinks and a single KV head (padded group at the end of Q) covers it.
  • Split cap changes summation order (opt-in).
  • Textual conflicts expected with qwen4-qsa-sparse-fa in flash_attn_load_tile_quant and fattn-tile.cuh (kernel head0/sequence lines).
  • Rules followed: no GPU used, no production containers touched, nothing pushed to ggml-org, not merged.

GPU validation tonight (NOT YET RUN)

Preamble (P) from the wave plan.

Eval:
  GPURUN_OPTS='-e GGML_CUDA_FA_TILE_GQA_PAD=1 -e GGML_CUDA_FA_DECODE_SPLIT_CAP=2' ./gpusmall.sh ./test2.sh w2-dq $BA - FLASH_ATTN_EXT 'nb=[1-8],.*type_K=(q8_0|q4_0)'
Kernel perf (v_perm is compile-time, so compare builds):
  ./gpusmall.sh ./perf2.sh w2-dq-b0 $B0 - FLASH_ATTN_EXT 'nb=1,.*type_K=(q8_0|q4_0)'
  GPURUN_OPTS="$OFF" ./gpusmall.sh ./perf2.sh w2-dq-ba $BA - FLASH_ATTN_EXT 'nb=1,.*type_K=(q8_0|q4_0)'
  repeat the BA line with GPURUN_OPTS='-e GGML_CUDA_FA_DECODE_SPLIT_CAP=2', '...=4' and '-e GGML_CUDA_FA_TILE_GQA_PAD=1' (also cap=1)
tg at depth:
  ./gpublock.sh ./lb.sh g-dq-b0 $B0 $G $GA -p 0 -n 128 -d 0,32768,90112 -r 3
  GPURUN_OPTS="$OFF" ./gpublock.sh ./lb.sh g-dq-ba $BA $G $GA -p 0 -n 128 -d 0,32768,90112 -r 3
  repeat the BA line with -e GGML_CUDA_FA_DECODE_SPLIT_CAP=2 and =4
  same set for $Q38 $Q38A (-d 32768,90112; also with -e GGML_CUDA_FA_TILE_GQA_PAD=1, Qwen3.8 is GQA 6) and for $Q4 $Q4A (-d 32768, sparse off)
Greedy decode identity (v_perm must keep the hash identical):
  ./gpublock.sh python3 -I $TB/specbench.py g-dq-b0 $B0 --n 256 --modes greedy -- $(python3 -I $TB/preset2args.py gemma-4-31b-it --nospec)
  the same with $BA and SB_ENV unset
KLD for the opt-ins:
  ./gpublock.sh ./kl.sh g-dq-base $BA $G base $GA
  ./gpublock.sh env KL_ENV='GGML_CUDA_FA_DECODE_SPLIT_CAP=2 GGML_CUDA_FA_TILE_GQA_PAD=1' ./kl.sh g-dq $BA $G test $GA
  Pass: Mean KLD <= 0.001.

Assisted-by: Claude Sonnet 5.5

…nd tile GQA padding for quantized decode

- flash_attn_load_tile_quant: on HIP the half path (8 values per chunk) puts the bytes under 0x64 with v_perm and
  subtracts the offset, the values are bit-identical to the int8 -> half -> * d path
- GGML_CUDA_FA_DECODE_SPLIT_CAP=<n> (opt-in): at most n blocks per CU for RDNA3 decode (nb <= 8) with quantized K/V
- GGML_CUDA_FA_TILE_GQA_PAD=1 (opt-in): GQA 3 -> 4 and 5/6/7 -> 8 in the direct quantized K/V tile path, padded Q
  columns are zeroed and their dst/meta/sink accesses skipped; the tile kernel derives head0 from gridDim.z at run time
- test-backend-ops: eval cases nb 1..8 with GQA 3/6/7 (q8_0, q4_0, q8_0-q4_0, D256/512, sinks) and perf cases nb=1 at 32k/96k

Assisted-by: Claude Sonnet 5.5
@rafasmour
rafasmour merged commit 462f7d4 into master Dec 4, 2026
10 of 27 checks passed
@rafasmour

Copy link
Copy Markdown
Owner Author

Reverted from master on 2026-12-04 (w2 build crashed Qwen4-single at >20k ctx; no measured gain). Commit preserved on branch archive/w2-25f0def; will be re-landed individually after GPU validation.

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.

1 participant