Skip to content

CUDA: store K/V in the cache from the kernel that makes the rows (rotated q8_0/q4_0 cache, Gemma 4 / Inkling 2 V norm) - #152

Merged
rafasmour merged 2 commits into
masterfrom
opt/kv-store-fusion
Dec 4, 2026
Merged

rafasmour merged 2 commits into
masterfrom
opt/kv-store-fusion

Conversation

@rafasmour

Copy link
Copy Markdown
Owner

A K/V row stored in a rotated q8_0 / q4_0 cache (the Hadamard rotation that upstream applies to quantized K/V) took three launches per attention layer and K/V: the Hadamard transform (MUL_MAT with the Hadamard hint, fwht_cuda), the quantized SET_ROWS, and for the V of Gemma 4 / Inkling 2 an RMS norm without weight in front. With an F16 cache the V of Gemma 4 took two (norm, SET_ROWS). Now each chain is one kernel.

Change

  • fwht.cu: fwht_set_rows_q (FWHT of the rows, q8_0 / q4_0 blocks written to the cache rows; one warp per FWHT row as fwht_cuda, block j of a row is register j of the 32 lanes) and rms_norm_fwht_set_rows_q (the RMS norm of the head first; heads of 128 / 256 / 512 with rotation blocks of 64).
  • norm.cu: rms_norm_set_rows_f32 (RMS norm without weight stored in an F16 / F32 / BF16 cache).
  • The arithmetic is that of the unfused kernels (rms_norm_f32 vector path with the same block size, fwht_cuda, k_set_rows_quant / k_set_rows), so the cache holds the same bytes.
  • fusion.cu: MUL_MAT (Hadamard), RESHAPE, VIEW, [RESHAPE,] SET_ROWS, RMS_NORM, RESHAPE, MUL_MAT (Hadamard), RESHAPE, VIEW, [RESHAPE,] SET_ROWS and RMS_NORM, VIEW, [RESHAPE,] SET_ROWS. GGML_CUDA_NO_KV_STORE_FUSION=1 turns them off.
  • llama-graph.cpp: with the K/V cache on the device the attention builders store V right after it is made (K was already last), so that V and its store are next to each other in the graph. Only the order of independent nodes changes.
  • test-backend-ops: FWHT_SET_ROWS (q8_0 / q4_0, rotation 64..512, 1..64 tokens, with and without the norm) and RMS_NORM_SET_ROWS (F16 / F32 / BF16, heads 128..4096).

Results

RX 7900 XT (gfx1100), llama-bench -fa on -lm dio -p 0 -n 128 -r 3. A/B in one build, GGML_CUDA_NO_KV_STORE_FUSION=1 for "off", two rounds interleaved:

model (K / V cache) fused stores tg128 off tg128 on
Gemma 4 31B UD-IQ3_XXS (q8_0 / q8_0) K, V with its norm 37.26 / 37.21 37.69 / 37.76 +1.4%
Inkling 2 Mini UD-IQ4_XS (q4_0 / q4_0) K, V with its norm 44.38 45.34 +2.2%
Qwen 3.8 27B Q4_0 (q8_0 / q4_0) K, V 37.08 / 37.11 37.32 / 37.20 +0.4%
Nemotron 3.5 Bolt E4B UD-Q5_K_XL (f16 / f16) V norm 98.44 / 98.47 99.59 / 99.47 +1.1%

(The first Inkling "off" round overlapped a build on the host and is left out.) Gemma pp512: 807.7 -> 809.3 t/s, no change.

End to end with the production speculative presets (llama-server, 6 completions x 256 tokens, greedy): Nemotron DFlash 181.7 / 182.3 -> 182.5 / 181.9 t/s, no change. An earlier experimental build of the same kernels gave Inkling 2 DSpark 55.4 / 55.4 -> 56.2 / 56.1 t/s (+1.4%), and Gemma 4 MTP and Qwen 3.8 MTP within +-1%. The text of every completion is the same with and without the fusions.

Numerics, the kernels write the same bytes as the unfused path:

  • KL divergence against fusion off (llama-perplexity, 4 x 512 tokens, ubatch 512): Gemma 4 mean KLD 0.000000 (max 0.000046), Qwen 3.8 0.000000 (max 0.000004), Nemotron 0.000001 (max 0.000067, which two runs of one unchanged build also give).
  • Inkling 2 Mini: the perplexity of every chunk is identical for master, fusion off and fusion on, two runs each (6.8862, 6.2117, 5.5344, 6.4183 at ubatch 512; 6.8482, 6.2056, 5.5130, 6.3881 at ubatch 256). One fusion-off run in an earlier batch gave different chunks 2 and 3 (final 6.4313); that did not reproduce in these eight runs.
  • test-backend-ops on ROCm0: FWHT_SET_ROWS 62/62, RMS_NORM_SET_ROWS 29/29, SET_ROWS 211/211, MUL_MAT_HADAMARD 32/32, RMS_NORM 121/121.

What is left of the rotation cost: the Q rotation before the attention and the rotation of its output are still separate launches. Leaving out every Hadamard transform and every quantized SET_ROWS (timing only, wrong results) gave Gemma +5.4%, Inkling +6.0% and Qwen 3.8 +2.0%, against the +1.4% / +2.2% / +0.4% here.

Assisted-by: Claude Opus 5.5

…tated quantized cache, Gemma 4 V norm)

A K/V row stored in a rotated q8_0 / q4_0 cache was three launches: the Hadamard transform (MUL_MAT with the
Hadamard hint), the quantized SET_ROWS, and for the V of Gemma 4 / Inkling 2 an RMS norm without weight before it.
With an F16 cache the V of Gemma 4 was two: the norm and the SET_ROWS. Each chain is now one kernel:

- fwht_set_rows_q: FWHT of the rows, q8_0 / q4_0 blocks written to the cache rows (one warp per row as fwht_cuda)
- rms_norm_fwht_set_rows_q: the same with the RMS norm of the head first (heads of 128 / 256 / 512, rotation of 64)
- rms_norm_set_rows_f32: RMS norm without weight stored in an F16 / F32 / BF16 cache

The arithmetic is that of the unfused kernels (rms_norm_f32 vector path with the same block size, fwht_cuda,
k_set_rows_quant / k_set_rows), so the cache holds the same values. The attention builders store V right after
it is made when the K/V cache is on the device, so that V and its store are next to each other in the graph.
GGML_CUDA_NO_KV_STORE_FUSION=1 turns the fusions off. Tests FWHT_SET_ROWS and RMS_NORM_SET_ROWS.

Assisted-by: Claude Opus 5.5
@rafasmour
rafasmour merged commit 928d721 into master Dec 4, 2026
9 of 27 checks passed
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