KV Cache › Cache Memory Requirements
Language AI HandbookAll chapters KV Cache › Cache Memory RequirementsOne section · Sections in this chapter6 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Understanding KV cache memory consumption is needed for deployment planning, as the cache often becomes the primary memory bottleneck in production systems. Unlike model weights that have a fixed size once the model is loaded, the cache grows dynamically during generation and can easily exceed the model weights in memory usage for long sequences or large batches. A system that can comfortably load and run a 7B model may be unable to serve multiple concurrent users at long context lengths, not because the model is too large, but because the caches are.
Memory Formula
To derive the memory requirements, we must account for all the tensors that constitute the complete cache. For a single sequence of length , we need to store keys and values for every layer and every head. The basic memory formula is:
where:
- : accounts for storing both keys and values, as each requires a separate tensor
- : the number of transformer layers, each maintaining its own independent cache
- : the number of attention heads per layer
- : the dimension of each head, determining the size of individual key and value vectors
- : the sequence length in tokens, the dynamic factor that grows during generation
- : the memory size required for a single floating-point number (e.g., 2 bytes for FP16, 4 bytes for FP32)
Since the total model dimension is typically expressed as , we can simplify this formula by substituting:
where:
- : the total model dimension, equal to
- : the number of layers in the model
- : the sequence length
- : the numerical precision in bytes
This simplified formula reveals an important insight: the cache memory scales with the product of model depth and width, multiplied by sequence length. Doubling any of these factors doubles the memory requirement. This linear relationship is the cache's great advantage over the naive quadratic scheme, but it still produces very large absolute values for modern models.
Example: LLaMA 2 7B
To make these formulas concrete, let's calculate the cache requirements for a widely deployed model. The LLaMA 2 7B model has the following specifications:
- layers
- Typically stored in FP16 (2 bytes per element)
For a single sequence of length , we can compute the cache size step by step:
A single 2048-token sequence requires a full gigabyte just for the KV cache. This is separate from the memory needed for model weights and activations, along with the rest of the inference overhead.
For a batch of 8 sequences at the 4096-token context length, the memory requirement grows proportionally:
For comparison, the model weights themselves require about 14 GB in FP16. This means that for a batch of 8 long-context requests, the KV cache consumes more memory than the entire model. At longer context lengths or larger batch sizes, the cache can substantially exceed the model size, making memory management the necessary challenge for deployment.
Scaling with Modern Models
Larger models and longer contexts exacerbate the memory pressure, creating significant challenges for deployment at scale. The following table shows KV cache sizes for various model configurations, illustrating how the requirements grow across different architectural choices:
| Model | Layers | Context | Cache per Sequence | |
|---|---|---|---|---|
| LLaMA 7B | 32 | 4096 | 4K | 2 GB |
| LLaMA 13B | 40 | 5120 | 4K | 3.1 GB |
| LLaMA 70B | 80 | 8192 | 4K | 10.0 GB |
| GPT-4 (estimated) | 120 | 12288 | 128K | 590 GB |
The cache memory scales linearly with both model size (via the product) and context length. This linear scaling in context length, combined with the quadratic scaling of the attention computation itself, makes long-context inference particularly challenging. A model like GPT-4 with a 128K context window requires hundreds of gigabytes per sequence, necessitating distributed systems and advanced memory management strategies. Understanding this table should give you a visceral sense of why every modern LLM serving system devotes significant engineering effort to cache optimization.
