Glossary/ Memory & Interconnect

KV Cache

The KV cache stores intermediate key and value tensors from previous tokens during autoregressive generation, enabling subquadratic computation by avoiding redundant re-encoding of the full context.

The KV cache (Key-Value cache) is a memory-based optimization essential to efficient transformer inference. During autoregressive text generation, each new token's attention scores must be computed against every previous token in the sequence. Without caching, this would require recomputing the key and value projections for the entire past context on every step-a quadratic cost in sequence length. The KV cache stores the key and value matrices from each earlier time step in GPU memory, so that only the new token's query needs to be projected. This reduces attention computation from O(n^2) to O(n) per token, and the total generation cost becomes roughly O(n^2) over the full sequence (since cache grows linearly). However, the KV cache imposes a significant memory footprint: for a 70B parameter model with 80 layers and 8-head group, the cache can consume over 30 GB for a 4096-token sequence. This often becomes the bottleneck for long-context inference, as it scales linearly with batch size and sequence length. Techniques like PagedAttention (used in vLLM) and sliding window attention manage cache more efficiently by storing it in non-contiguous blocks and evicting stale entries. In quantized inference with ExL2 or GPTQ, the KV cache is typically kept in FP16 to maintain numerical precision, further increasing memory pressure. AI builders must carefully profile KV cache size when selecting GPU hardware-models with high token-to-parameter ratios (e.g., MoE) can be especially demanding. Newer architectures like Mamba attempt to eliminate the KV cache entirely, but at the cost of reduced output quality for certain tasks.

Formula
cache_size = 2 * batch_size * sequence_length * num_layers * num_key_value_heads * head_dim * bytes_per_element
Common misconception

The KV cache is not a hold-all for raw token embeddings; it only stores the linear projection outputs used by the attention mechanism, not the input embeddings themselves.

Canonical reference
https://arxiv.org/abs/2309.06180

Related terms

Concepts you'll usually encounter alongside this one.

Used in benchmarks

Workloads on MyAI Bench that involve KV Cache.

Mentioned in tutorials

Hands-on guides that put this concept to work.

VRAM Calculator

See how this term affects your memory budget.

MyAI Bench

Real numbers, real chips, every workload.

Buying guides

Picks for builders, by use case.

Stay Ahead of the AI Curve

Get weekly AI hardware news, benchmark updates, and deals in your inbox. Founding-subscriber list, be one of the first.

✓ No spam✓ Weekly digest✓ Unsubscribe anytime