KV Cache
KV Cache (Key-Value Cache) is an optimization mechanism used during Transformer model inference to store the key and value vectors corresponding to each token in the attention computation. During autoregressive generation, the model reuses cached key-value pairs from previous tokens rather than recomputing them at each new generation step, significantly reducing redundant computation and improving inference efficiency. The mechanism trades increased GPU memory usage for faster inference speed and is a core optimization technique in large language model deployment.
Core Facts
The KV cache is a core data structure in Transformer autoregressive inference, and its size is approximately 2 × number of layers × number of attention heads × head dimension × sequence length × precision bytes.
KV Cache avoids redundant computation by caching already-computed Key and Value matrices, and advanced techniques such as PagedAttention further optimize cache memory management efficiency.
KV Cache reduces the computational complexity of self-attention from O(N²) to O(N) by caching already-computed Key-Value pairs in GPU memory, but memory usage grows linearly with sequence length.
Timeline (last 90 days)
Multi-tenant inference platforms need to implement isolation at multiple levels, including request queue isolation, KV Cache cleanup in GPU memory, access control for logging systems, and correctness of batching logic.
DeepSeek stores the KV cache in a shared SSD pool of more than 100 PB, with the cache retained for 72 hours, and the hit price is only one-fiftieth of the miss price.
Common AI inference optimization techniques include model quantization, operator fusion and graph optimization, KV cache management, continuous batching and speculative decoding.
Quantization does not mean single-card deployment is possible: 4-bit quantization can roughly only compress the size to a quarter of the original, and a model of hundreds of GB still has tens of GB or more after quantization; plus the KV Cache and activation overhead during inference, it may still not fit on a single card.
MoE-Context lets all blocks share a single key-value cache; hidden blocks mask the participation of their key/value via attention masks, so they can be recalled in subsequent steps without re-encoding.
Common methods for controlling the cost of KV Cache transfer across pools include tiered transfer, KV Cache compression/quantization, and placing prefill and decode within the same high-speed interconnect domain.
Disaggregated decode does not solve the communication overhead of model parallelism and pipeline parallelism; KV Cache transfer across nodes will become a new bandwidth bottleneck under ultra-long contexts, and for small and medium-scale deployments the operational complexity may exceed the benefits.
If the sparsity pattern cannot reduce the amount of KV data actually read, the speedup in the decode phase will be very limited.
The decode phase generates autoregressively token by token, processing only one new token per step but needing to read the entire historical KV Cache; it is memory-bandwidth-intensive, and the bottleneck is data movement.
Transformer KV Cache memory grows linearly with context length, and a 1-million-token context brings significant memory challenges.
40 more timeline events
All Facts (20)
The KV cache is a core data structure in Transformer autoregressive inference, and its size is approximately 2 × number of layers × number of attention heads × head dimension × sequence length × precision bytes.
90%VerifiedKV Cache avoids redundant computation by caching already-computed Key and Value matrices, and advanced techniques such as PagedAttention further optimize cache memory management efficiency.
90%VerifiedKV Cache reduces the computational complexity of self-attention from O(N²) to O(N) by caching already-computed Key-Value pairs in GPU memory, but memory usage grows linearly with sequence length.
90%VerifiedTime to First Token (TTFT) refers to the time interval between a user submitting a request and the model outputting the first token, and is primarily constrained by the time cost of the prefill phase in which the model processes the input prompt.
90%VerifiedThe KV cache VRAM usage of ultra-long context windows grows linearly with context length, causing inference latency and costs to rise significantly.
90%VerifiedKV Cache persistently stores already-computed key-value pairs so that subsequent requests sharing the same prompt prefix can directly reuse them, skipping redundant matrix operations.
90%VerifiedThe KV Cache of a 70B-parameter model with 128K context support can require hundreds of gigabytes of VRAM at full load, exceeding the 80GB VRAM capacity of a single H100.
90%VerifiedKV Cache size is approximately equal to 2 × number of layers × number of attention heads × head dimension × sequence length × precision bytes.
90%VerifiedKV Cache caches previously computed key-value matrices in memory, reducing the complexity of autoregressive generation from quadratic growth with sequence length to linear.
90%VerifiedThe VRAM usage of KV Cache grows linearly with sequence length; for a 70-billion-parameter model using GQA, the KV Cache usage can reach tens of GB when processing 100,000 tokens.
90%VerifiedThe VRAM occupied by KV Cache grows linearly with sequence length.
90%VerifiedWhen large language models process long contexts, the computational complexity of the attention mechanism grows proportionally to the square of the sequence length.
90%VerifiedTaking LLaMA-3 70B (80 layers, 64 heads, 128 dimensions per head) as an example, the KV Cache for a single token occupies approximately 2.6 MB (FP16 precision), and the total KV Cache for a 32K context reaches approximately 83 GB.
90%VerifiedPagedAttention is the core technology of the vLLM framework, which draws on the idea of operating system virtual memory management to store KV Cache in pages.
90%VerifiedFor a model with 70B parameters, the KV Cache for an 8K context may occupy several additional gigabytes of VRAM
90%VerifiedKV Cache reduces inference complexity from O(n²) to O(n) by caching the already-computed Key/Value matrices.
90%VerifiedPagedAttention divides the KV cache into fixed-size pages allocated on demand, and can improve VRAM utilization by 2–4×.
85%VerifiedTaking LLaMA-2 70B as an example, when processing a 4096-token context with a batch size of 32, the KV Cache can consume over 80 GB of memory.
85%VerifiedPagedAttention improves KV Cache memory utilization from below 40% to close to 100%.
85%VerifiedPrompt caching saves the KV state of reusable prefixes (the tokens at the beginning of a prompt that remain unchanged); if the next request starts with an identical prefix, the provider reuses the saved computation and only processes the new portion.
80%