Repository navigation
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
Conversation
…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
…n of #155 and the K/V store fusions)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_MATwith the Hadamard hint,fwht_cuda), the quantizedSET_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 asfwht_cuda, blockjof a row is registerjof the 32 lanes) andrms_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).rms_norm_f32vector 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_ROWSandRMS_NORM, VIEW, [RESHAPE,] SET_ROWS.GGML_CUDA_NO_KV_STORE_FUSION=1turns 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) andRMS_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=1for "off", two rounds interleaved:(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:
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).test-backend-opson 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