Skip to content

HIP: enable more efficient kv cache decode on RDNA4 - #30803

Closed
clarkbox wants to merge 1 commit into
ggml-org:masterfrom
clarkbox:rdna4-fattn-vec-quantized-kv
Closed

clarkbox wants to merge 1 commit into
ggml-org:masterfrom
clarkbox:rdna4-fattn-vec-quantized-kv

Conversation

@clarkbox

Copy link
Copy Markdown

Overview

KV cache at q4 and q8 on RDNA4 was getting read up to 8 times a pass, once per query head. That meant quantized cache ran slower than f16 once the context got long.

This puts a full 32-value K block on one lane, loads it once, and checks the query heads that share that KV head in the same pass (2, 4, 6, or 8 heads). RDNA4 only, head size 128 and 256. f16 KV is unchanged, and other GPUs and head sizes stay on the old path.

R9700 (gfx1201), ROCm 10.1, 300 W. llama-bench, master vs this commit, -fa 1:

D = 256 (-n 64)

Model K type V type Test t/s master t/s this commit Speedup
qwen35 27B Q4_K_M f16 f16 tg64@d8192 28.03 28.06 1.00
qwen35 27B Q4_K_M f16 f16 tg64@d32768 25.62 25.64 1.00
qwen35 27B Q4_K_M f16 f16 tg64@d65536 22.98 22.99 1.00
qwen35 27B Q4_K_M f16 f16 tg64@d131072 19.06 19.08 1.00
qwen35 27B Q4_K_M q4_0 q4_0 tg64@d8192 27.60 28.30 1.03
qwen35 27B Q4_K_M q4_0 q4_0 tg64@d32768 24.88 27.63 1.11
qwen35 27B Q4_K_M q4_0 q4_0 tg64@d65536 22.03 26.76 1.21
qwen35 27B Q4_K_M q4_0 q4_0 tg64@d131072 17.81 25.17 1.41
qwen35 27B Q4_K_M q8_0 q8_0 tg64@d8192 26.51 28.20 1.06
qwen35 27B Q4_K_M q8_0 q8_0 tg64@d32768 22.52 27.03 1.20
qwen35 27B Q4_K_M q8_0 q8_0 tg64@d65536 18.53 25.72 1.39
qwen35 27B Q4_K_M q8_0 q8_0 tg64@d131072 13.53 23.47 1.73
qwen35moe 35B.A3B Q4_K_M f16 f16 tg64@d8192 81.96 81.82 1.00
qwen35moe 35B.A3B Q4_K_M f16 f16 tg64@d32768 76.36 76.34 1.00
qwen35moe 35B.A3B Q4_K_M f16 f16 tg64@d65536 70.33 70.45 1.00
qwen35moe 35B.A3B Q4_K_M f16 f16 tg64@d131072 61.17 61.13 1.00
qwen35moe 35B.A3B Q4_K_M q4_0 q4_0 tg64@d8192 79.53 81.79 1.03
qwen35moe 35B.A3B Q4_K_M q4_0 q4_0 tg64@d32768 70.70 79.68 1.13
qwen35moe 35B.A3B Q4_K_M q4_0 q4_0 tg64@d65536 61.44 77.10 1.25
qwen35moe 35B.A3B Q4_K_M q4_0 q4_0 tg64@d131072 48.49 72.16 1.49
qwen35moe 35B.A3B Q4_K_M q8_0 q8_0 tg64@d8192 75.65 81.54 1.08
qwen35moe 35B.A3B Q4_K_M q8_0 q8_0 tg64@d32768 62.32 78.29 1.26
qwen35moe 35B.A3B Q4_K_M q8_0 q8_0 tg64@d65536 49.93 74.20 1.49
qwen35moe 35B.A3B Q4_K_M q8_0 q8_0 tg64@d131072 36.13 66.22 1.83

D = 128 (-n 128)

Model K type V type Test t/s master t/s this commit Speedup
qwen3moe 30B.A3B Q4_K_M f16 f16 tg128@d8192 101.81 100.81 0.99
qwen3moe 30B.A3B Q4_K_M f16 f16 tg128@d16384 89.94 89.19 0.99
qwen3moe 30B.A3B Q4_K_M f16 f16 tg128@d32768 71.54 71.99 1.01
qwen3moe 30B.A3B Q4_K_M f16 f16 tg128@d65536 51.05 52.35 1.03
qwen3moe 30B.A3B Q4_K_M q4_0 q4_0 tg128@d8192 81.42 99.73 1.22
qwen3moe 30B.A3B Q4_K_M q4_0 q4_0 tg128@d16384 66.96 94.69 1.41
qwen3moe 30B.A3B Q4_K_M q4_0 q4_0 tg128@d32768 49.10 85.64 1.74
qwen3moe 30B.A3B Q4_K_M q4_0 q4_0 tg128@d65536 32.10 71.70 2.23
qwen3moe 30B.A3B Q4_K_M q8_0 q8_0 tg128@d8192 84.82 98.64 1.16
qwen3moe 30B.A3B Q4_K_M q8_0 q8_0 tg128@d16384 69.09 91.91 1.33
qwen3moe 30B.A3B Q4_K_M q8_0 q8_0 tg128@d32768 50.41 80.80 1.60
qwen3moe 30B.A3B Q4_K_M q8_0 q8_0 tg128@d65536 32.58 64.78 1.99
qwen3 8B Q4_K_M (GQA=4) q4_0 q4_0 tg128@d16384 66.23 81.87 1.24
qwen3 8B Q4_K_M (GQA=4) q8_0 q8_0 tg128@d16384 67.42 74.62 1.11
qwen3 14B Q4_K_M (GQA=5) q4_0 q4_0 tg128@d16384 43.17 47.11 1.09
qwen3 14B Q4_K_M (GQA=5) q8_0 q8_0 tg128@d16384 42.55 45.99 1.08
llama 3.1 8B Q4_K_M (GQA=4) q4_0 q4_0 tg128@d16384 70.29 86.03 1.22
llama 3.1 8B Q4_K_M (GQA=4) q8_0 q8_0 tg128@d16384 71.08 79.05 1.11

f16 KV is flat across these rows. test-backend-ops -o FLASH_ATTN_EXT on this GPU: 4030/4030.

Requirements

  • I have read the contributing guidelines
  • Self-reported review complexity: Medium
  • AI usage disclosure: Yes. Developed by a human with AI for benchmarks, profiling, prototype, testing.

KV cache at q4 and q8 on RDNA4 was getting read up to 8 times a pass,
once per query head. This puts a full 32-value K block on one lane and
checks all of those query heads in the same pass.

On an R9700 (ROCm 10.1), Qwen3-30B-A3B q4_0 at 64k goes from 32.1 to
71.7 tok/s, and Qwen3.6-35B-A3B q8_0 at 128k goes from 36.1 to 66.2 tok/s.
Full tables in the PR. FLASH_ATTN_EXT passes 4030/4030.
@clarkbox
clarkbox requested review from a team and ggerganov as code owners October 19, 2026 09:42
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Oct 19, 2026
@IMbackK IMbackK self-assigned this Oct 19, 2026
@JohannesGaessler

Copy link
Copy Markdown
Contributor

Thank you, these are good changes. However, I'm not sure this is the right way to go about it. The vector kernel suffers from massive register pressure and is very quickly outscaled by the tile kernel beyond 1 or 2 columns. So I would very much assume that rather than adding GQA support to the vector kernel you would get better performance by adding quantized KV cache support to the tile kernel. If you're up for it I would ask you to try implementing that first. There is this PR #30792 that kind of goes in that direction but I would preferably assign this to you instead if you're interested.

@clarkbox
clarkbox marked this pull request as draft October 25, 2026 22:49
@clarkbox

Copy link
Copy Markdown
Author

Ok I will dig in. Your approach looks better. Parking this as a draft for now.

@clarkbox

Copy link
Copy Markdown
Author

Tile version is up in #31234 @JohannesGaessler

@clarkbox

Copy link
Copy Markdown
Author

closing this in favor of #31234

@clarkbox clarkbox closed this Oct 27, 2026
station384 pushed a commit to station384/llama.cpp-rocm that referenced this pull request Nov 6, 2026
Working tree content carried over from the previous b12033-pr2 worktree, whose
git objects did not survive the consolidation: upstream 785322d plus the hipCUB
on HIP change and PRs ggml-org#30803, ggml-org#25940 and ggml-org#31234, plus the two rdna4 blocks that
are also on rocm-gfx1201.

Kept as a single commit so the carried changes are visible as one diff against
rocm-gfx1201 rather than being mixed into that branch's history.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants