Ever wonder why a 24 GB GPU suddenly runs out of memory when serving a language model? The answer lies in the hidden KV cache that grows with every token you process.
The Simple Version
Think of inference as a restaurant: the model’s weights are the kitchen, and the KV cache is the growing order list that must fit on the counter.
How It Actually Works
KV Cache Math
Each token in the KV cache for Mistral‑7B consumes about 131 KB. With 16 000 tokens of context and 80 concurrent users, the cache alone needs roughly 42 GB of GPU memory, which exceeds a 24 GB card.
Attention Layer
The attention mechanism stores queries, keys, and values for every token, and these are the biggest contributors to cache size.
GPU Memory Split
GPUs allocate a fixed chunk for model weights and a growing chunk for the KV cache; once the cache exceeds the free portion, new requests fail.
Technical Deep Dive
Model Optimization
Speakers discuss quantization, multi‑head and multi‑query attention, grouped‑query, latent attention, and flash attention as ways to shrink model footprints.
Serving Optimizations
They introduce paged attention (borrowed from OS paging), continuous batching, prefix caching, and KV quantization, each measured against a naive baseline.
Engine Benchmarking
Tests show vLLM and SGLang perform similarly on standard workloads, but a 3‑4× gap appears when agentic branching is added.
Why This Matters
Memory bottlenecks force teams to over‑provision GPUs or accept slower response times, raising costs and limiting real‑world LLM adoption.
Understanding KV cache behavior helps engineers pick the right hardware, apply the right optimizations, and avoid costly outages.
Key Takeaways
- KV cache size grows linearly with context length and concurrent requests.
- 131 KB per token for Mistral‑7B can quickly exceed 24 GB GPU limits.
- Attention internals dominate cache memory; optimizing attention reduces footprint.
- Techniques like flash attention and tiling dramatically improve throughput.
- Paged attention and continuous batching let you serve many requests on a single GPU.
- vLLM and SGLang are comparable for simple workloads, but differ under complex branching.
- Accurate capacity calculators prevent costly over‑provisioning.