TL;DR
The global attention layers in Gemma 4 models feature a unique mathematical redundancy where the Key and Value are nearly identical. This paper introduces a "store once, read twice" technique that reduces KV cache memory usage by up to 37.5% in these layers without requiring any changes to existing, highly-optimized attention kernels. By using overlapping strided views, the authors achieved a 1.38x throughput boost in vLLM for long-context prompts.
What Problem Does This Paper Address?
In large language model (LLM) serving, the KV cache is the primary bottleneck for scalability. It stores the keys and values for all previous tokens in a sequence to avoid recomputing them, but as context lengths grow (e.g., to 32K or 64K tokens), the cache quickly consumes all available GPU memory. This limits the number of requests a single GPU can handle simultaneously (the batch size).
Gemma 4 was designed with a special structure called attention_k_eq_v in its global attention layers. In these layers, the Value is simply a normalized version of the Key projection, and the Key is that same Value scaled by a weight and modified by Rotary Position Embeddings (RoPE). While the Gemma 4 technical report noted this could save 37.5% of memory, popular serving engines like vLLM still store a full, separate Key and Value for every token. This paper provides the implementation "missing link" to actually realize these savings in production-grade engines.
Methods
The authors propose a method to store the redundant data in a single compact row of 640 values, rather than the standard 1,024 values (512 for Key + 512 for Value).
The core "trick" relies on overlapping strided views:
- Compact Storage: They store a row containing the rotated part of the Key ($k_R$), the shared unrotated part ($v_U$), and the remaining part of the Value ($v_R$).
- The "Two-View" Read: Instead of modifying the CUDA kernels that perform attention, they use the engine's memory management to create two different "views" (pointers with specific offsets and strides) of the same 640-value row. One view looks like a standard 512-dimension Key, and the other looks like a 512-dimension Value.
- Query Folding: To account for the scaling weight ($w$) that exists in the Key but not the Value, they "fold" that weight into the Query vector before the attention step. This ensures the mathematical output remains identical to the original model.
This approach is "zero-copy"—no data is moved or duplicated during the actual attention calculation, allowing the system to use standard, unmodified PagedAttention kernels.
Datasets
The authors evaluated their vLLM prototype using:
- GSM8K: A benchmark of 400 grade-school math word problems used to verify model accuracy and ensure the compact storage didn't degrade performance.
- Needle-Retrieval Prompts: 32 custom prompts of 7.7K tokens each, designed to test the model's ability to find specific information in long contexts.
- Synthetic Long Contexts: Prompts ranging from 8K to 64K tokens were used to measure memory footprint and throughput gains.
Key Insights and Ideas
- Kernel Agnosticism: The most significant insight is that you don't need to write new, complex CUDA kernels to save memory if you can manipulate how the engine views existing memory. This allows the method to benefit from future optimizations in standard kernels (like FlashAttention).
- Exact Arithmetic: Unlike quantization (which trades precision for memory), this method is exact in real arithmetic. The authors proved this by showing bit-identical log-probabilities between their compact version and the stock version.
- Global vs. Sliding Window: Gemma 4 uses both sliding-window and global attention layers. The authors correctly identified that at long contexts, the global layers dominate the memory footprint, making them the most impactful target for optimization.
Key Results
The authors implemented their method in a vLLM prototype and tested it on NVIDIA A100 GPUs:
- Memory Savings: The steady-state KV memory per request dropped by a factor of 1.34x to 1.40x at context lengths of 32K to 64K tokens.
- Throughput Increase: For 16K-token prompts, the compact engine processed 1.38x more tokens per step compared to the stock engine because it could fit larger batches into the same 15 GiB memory pool.
- Accuracy: The prototype achieved an accuracy of 0.9769 on GSM8K, nearly identical to the stock engine's 0.9722 (the slight difference was within the range of run-to-run noise).
Limitations and Future Work
- Architecture Specificity: This method only works for models using the
attention_k_eq_vdesign (currently Gemma 4 31B, 26B, and 12B). - FP8 Quantization: The current prototype does not yet "stack" with 8-bit KV cache quantization. When using FP8, the scaling factors for Keys and Values are typically different, which breaks the overlapping view trick.
- Eager Mode Overhead: In "eager mode" (without CUDA graphs), the additional Python-level transforms make individual steps 4% to 9% slower. However, this is usually offset by the ability to run larger batches.
- Padding Waste: Due to vLLM's block-size requirements, some memory (about 6.25%) is lost to padding in the compact layout, though the authors suggest a "packed block stride" could fix this in the future.
Summary updated