KV Cache Growth: The Hidden Memory Hog

The Key-Value (KV) cache is crucial for the attention mechanism in LLMs, storing intermediate computations. However, its size grows linearly with the input sequence length, rapidly consuming GPU memory. This limits the number of concurrent requests and maximum context window, leading to a "capacity-bound" inference regime, especially with long prompts.

The top 3

  1. Top 3 Techniques to Combat KV Cache Memory Bloat: The most impactful optimizations include PagedAttention, which reduces KV cache memory waste to under 4% and boosts throughput by 2-4x, along with KV cache quantization (e.g., FP8/INT8) that can halve memory usage. Architectural attention compression like Grouped-Query Attention (GQA) also significantly cuts cache size, with Llama 3 using GQA to achieve an 8x reduction versus standard Multi-Head Attention (MHA).
  2. When KV Cache Outweighs Model Weights in Memory: For long-context LLM inference, the KV cache can become the dominant memory consumer, surpassing the model's static weights. For example, a Llama 70B model with a 1M token context can require approximately 135 GB for its FP16 KV cache, easily exceeding the model's own parameter memory. At 128K context with 8 concurrent users, the KV cache can be 2.4x the model size.
  3. Self-Attention's Role in KV Cache Memory Scaling: The KV cache grows because the self-attention mechanism in Transformers stores Key (K) and Value (V) tensors for all previously processed tokens. This allows the model to avoid recomputing these states at each autoregressive generation step, reducing computational complexity from quadratic to linear. However, this caching mechanism means the memory footprint for the KV cache scales linearly with both the sequence length and the batch size.

Sources

Open the full topic