KV Cache › Cache Memory Requirements

Michael BrenndoerferJanuary 6, 20266 min read
Language AI HandbookAll chapters
Part 42 of 67Inference Optimization
Chapter 313KV Cache › Cache Memory Requirements
Reading section 6 of 14 · Cache Memory Requirements · 6 minSection 6 of 14 · 6 min

Book contents

Section 6 of 14 · 6 min read

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 TT, we need to store keys and values for every layer and every head. The basic memory formula is:

Memory=2×L×H×dh×T×bytes per element\text{Memory} = 2 \times L \times H \times d_h \times T \times \text{bytes per element}

where:

  • 22: accounts for storing both keys and values, as each requires a separate tensor
  • LL: the number of transformer layers, each maintaining its own independent cache
  • HH: the number of attention heads per layer
  • dhd_h: the dimension of each head, determining the size of individual key and value vectors
  • TT: the sequence length in tokens, the dynamic factor that grows during generation
  • bytes per element\text{bytes per element}: 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 dmodel=H×dhd_{\text{model}} = H \times d_h, we can simplify this formula by substituting:

Memory=2×L×dmodel×T×bytes per element\text{Memory} = 2 \times L \times d_{\text{model}} \times T \times \text{bytes per element}

where:

  • dmodeld_{\text{model}}: the total model dimension, equal to H×dhH \times d_h
  • LL: the number of layers in the model
  • TT: the sequence length
  • bytes per element\text{bytes per element}: 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:

  • L=32L = 32 layers
  • dmodel=4096d_{\text{model}} = 4096
  • Typically stored in FP16 (2 bytes per element)

For a single sequence of length T=2048T = 2048, we can compute the cache size step by step:

Memory=2×32×4096×2048×2=1,073,741,824 bytes=1 GB\begin{aligned} \text{Memory} &= 2 \times 32 \times 4096 \times 2048 \times 2 \\ &= 1,073,741,824 \text{ bytes} \\ &= 1 \text{ GB} \end{aligned}

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:

Memory=8×2×32×4096×4096×2=17,179,869,184 bytes=16 GB\begin{aligned} \text{Memory} &= 8 \times 2 \times 32 \times 4096 \times 4096 \times 2 \\ &= 17,179,869,184 \text{ bytes} \\ &= 16 \text{ GB} \end{aligned}

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:

KV cache memory requirements for different model sizes at their respective context lengths in FP16 precision.
ModelLayersdmodeld_{\text{model}}ContextCache per Sequence
LLaMA 7B3240964K2 GB
LLaMA 13B4051204K3.1 GB
LLaMA 70B8081924K10.0 GB
GPT-4 (estimated)12012288128K590 GB

The cache memory scales linearly with both model size (via the L×dmodelL \times d_{\text{model}} 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.

Out[5]:
Visualization
Log-log line chart of KV cache memory in GB versus context length in thousands of tokens for LLaMA 7B, 13B, and 70B. Dashed gray reference lines mark 24 GB and 80 GB GPU limits.
KV cache memory scaling with context length for LLaMA 7B, 13B, and 70B models, plotted on a log-log scale. Memory grows linearly with context length (appearing as a straight line on this scale), and larger models require proportionally more cache memory. The dashed horizontal lines indicate typical GPU memory capacities. LLaMA 70B reaches 80 GB (the A100/H100 limit) at 32K tokens and exceeds it at longer contexts, making single-GPU serving beyond that point impossible without memory optimizations.