Manuscript Offprint

arXiv:2611.23035

Storing the Gemma 4 K=V Cache Once: Exact Global-KV Memory Savings in a Paged Serving Engine with Unchanged Attention Kernels

Kushagra Bainsla

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

In the global attention layers of Gemma 4 31B, the value is the normalized key projection, and the key is the value scaled by a fixed weight, with rotary position encoding on one quarter of its dimensions. The Gemma 4 report states that this design reduces the global KV cache by up to 37.5%, but the open serving engines that we checked still store a full key and a full value: 1,024 values per KV head and token. We store one 640-value row instead, exact in real arithmetic, and an unchanged paged attention kernel reads it through two overlapping strided views as its key cache and its value cache. In a vLLM prototype with the 16-bit 31B model, the outputs are bit-identical to an emulation of the same arithmetic on the stock cache, 2 of 432 final answers differ from the stock engine (within its run-to-run noise), and the steady decode KV memory per request falls by a factor of 1.34 to 1.40 at 32K to 64K tokens. On a prefill/decode pair of two A100 GPUs with NIXL transfer and CPU offload, a decode engine with a full 15 GiB pool ran 1.38 times the stock decode tokens per step for 16K-token prompts (repeated to 0.06%) and 1.24 times for 8K-token prompts; a model of the vLLM block pool predicted both ratios to within 1% (the engine was 0.7% to 0.9% higher) and matched the change between them. The eager decode throughput rose by 1.30 and 1.11 times, and on this path the compact engine gave the same output tokens as the emulation for 8 prompts at batch 1. The run-time prototype makes an eager step 4% to 9% slower at equal batch, and the method does not yet stack with an 8-bit KV cache at the default scale.

1 Introduction

The KV cache sets the number of long requests that one GPU can serve. Paged serving engines such as vLLM [1] keep the cache in fixed-size blocks and admit a request only when its blocks fit in the pool. A smaller cache per request therefore allows a larger batch and more decode tokens per second, until the GPU becomes compute bound.

Gemma 4 [2] uses two layer types. In the 31B model, 50 sliding-window layers attend to the last 1,024 tokens with 16 KV heads of 256 dimensions, and 10 global layers attend to the whole context with 4 KV heads of 512 dimensions. At long context the global layers hold most of the cache of a request. They also have a special structure, which the model configuration calls attention_k_eq_v: they have no value projection. The value is the RMS-normalized key projection, and the key is that value multiplied by the key-norm weight, with rotary position encoding (RoPE) [3] on 128 of the 512 dimensions. The Gemma 4 report states that this design reduces the global KV cache by up to 37.5% [2]. The 26B-A4B and 12B models use the same design; the E2B and E4B models do not.

The open serving engines that we checked do not realize this saving: they store 1,024 values per KV head and token in these layers (Section 7). This paper shows how to store 640 values that an unchanged paged attention kernel can read, and it measures what this gives in vLLM. The kernel reads the 640-value row through two views: a view is a window on the same memory with its own shape and strides, so no data is copied (Figure 1).

k_{\mathcal{R}}$$v_{\mathcal{U}} (384)vRv_{\mathcal{R}}row rr:K view: values 0 to 511 =[ kR∣vU ]=[\,k_{\mathcal{R}}\mid v_{\mathcal{U}}\,]V view: values 128 to 639 =[ vU∣vR ]=[\,v_{\mathcal{U}}\mid v_{\mathcal{R}}\,]

Figure 1: The compact row of one global KV head and token (640 values instead of 1,024). A stock attention kernel reads the K view and the V view as its two caches. The query carries wUw_{\mathcal{U}} on its unrotated part (Eq. (3)), and the output is permuted back to the model order.

We do not claim the saving, the shape of the stored row or the exact cache reduction as new. The Gemma 4 report states the saving. Multi-head latent attention (MLA) [4] caches a shared part plus a separate RoPE part, and its decode kernels read the key and the value from one cached row [5]. Slim attention [6] caches only the keys of multi-head attention layers and computes the values from them with a matrix product. Our two-view read is the MLA decode layout specialized to equal key and value widths, so that kernels that require equal key and value head sizes read the shared buffer unchanged; our searches targeted Gemma 4 and its engines, not this layout in general. Our contributions are:

  1. A compact row and a two-view read. One row of 640 values per KV head and token holds the rotated part of the key and the whole value. A stock paged attention kernel reads it through two overlapping strided views; the key-norm weight folds into the query, and a fixed permutation restores the output order. The method is exact in real arithmetic (Section 3).
  2. A checked vLLM prototype. Its outputs are bit-identical to an emulation of the same arithmetic on the stock cache, and its final answers stay within noise of the stock engine (Sections 4 and 5.1).
  3. Measurements and a speed bound. We measure the memory per request, the batch that a fixed pool holds and the step cost of the prototype. A byte count shows why the smaller cache gives little speed per step at these batch sizes: the weights are about 85% of the bytes that a decode step reads (Sections 5.2 to 5.4).
  4. A test in a serving decode node. The compact cache works with the vLLM CPU offload connector and, with a small patch for the pinned build, with NIXL transfer from a prefill engine. On a two-GPU prefill/decode pair whose decode pool stays full, the compact decode engine ran 1.38 and 1.24 times the stock decode tokens per step at two operating points. A model of the block pool, frozen before each run, predicted both ratios to within 1% (the engine was 0.7% to 0.9% higher) and matched the change between them. The eager decode throughput rose by 1.30 and 1.11 times, and the compact engine gave the same output tokens as the emulation for 8 prompts at batch 1 (Section 6).

An 8-bit (FP8) KV cache halves all KV bytes, but it is lossy. We tested the compact row with FP8 only at the kernel level (Appendix D), so we do not claim that the two savings stack.

References (31)