mlprep
mlprep/ML Breadthhard12 min

Walk me through what KV cache is, why LLM generation would be extremely slow without it, what exactly gets stored, how much memory it consumes at scale, and what tradeoffs it creates for serving systems.

formulate your answer, then —

tldr

KV cache stores K and V projections for all past tokens so each decode step only computes Q for the new token and reads cached K, V — cutting per-step attention from O(n²) to O(n). Memory cost grows with sequence length and layers, often exceeding model weights at long contexts. GQA reduces KV heads (Llama-3: 8 KV heads, 32 Q heads) to cut cache 4×. Decode is memory-bandwidth-bound; batching amortizes this. PagedAttention manages fragmentation and enables prefix sharing.

follow-up

  • How does PagedAttention in vLLM manage KV cache memory across many concurrent requests with varying context lengths?
  • What is prefix caching and when does it provide the biggest throughput wins?
  • Why is the decode phase memory-bandwidth-bound rather than compute-bound, and how does continuous batching exploit this?