KV Cache: Context Window's Hidden VRAM Tax

The Key-Value (KV) cache is a primary VRAM bottleneck, growing linearly with batch size and quadratically with context length, with a Llama 3.1 70B model needing approximately 40 GB for a single 128K context request.

The top 3

  1. Long Context's Hidden VRAM Tax: Top Models' KV Cache: For a Llama 3 70B model, a 128K token context window demands approximately 40-64 GB of VRAM for the KV cache alone, often exceeding the model's weights. Even for an 8B model, 128K context can consume 17 GB. This linear scaling with context length makes KV cache a dominant VRAM consumer.
  2. Top 3 KV Cache VRAM Reduction Techniques: **Quantization** (e.g., INT8/INT4) can reduce KV cache memory by 2x-4x with minimal quality impact. **PagedAttention** (used by vLLM) eliminates fragmentation, recovering 20-40% of wasted VRAM and boosting throughput by 2-4x. **Prefix caching** reuses KV states for shared prompts, reducing input cost by 60-90%.
  3. KV Cache's Genesis: Why It's Essential for LLM Speed: KV cache was introduced to transform LLM inference from quadratic (O(n^2)) to linear (O(n)) complexity; it stores pre-computed Key and Value matrices from previous tokens, preventing redundant recalculations of attention for the entire sequence at each generation step, enabling significantly faster text generation.

Sources

Open the full topic