Zubnet AIसीखेंWiki › KV Cache
Infrastructure

KV Cache

Key-Value Cache
एक memory optimization जो attention mechanism से पहले से compute किए गए key और value tensors को store करती है, ताकि हर नए token के लिए उन्हें recompute न करना पड़े। Autoregressive generation के दौरान, हर नया token सभी पिछले tokens पर attend करता है। Caching के बिना, आपको हर step पर पूरी sequence के लिए attention recompute करनी पड़ती। KV cache memory को speed के लिए trade करता है पहले से compute की गई चीज़ों को store करके।

यह क्यों matter करता है

KV cache ही वजह है कि LLM inference memory-bound है, compute-bound नहीं। Claude के साथ एक लम्बी conversation सिर्फ model weights के लिए memory use नहीं करती — एक 100K token context के लिए KV cache tens of GB की VRAM खा सकता है। यही वजह है कि providers longer contexts के लिए ज़्यादा charge करते हैं, “context window” का theoretical limit के आगे एक practical ceiling क्यों है, और paged attention और cache eviction जैसी techniques active research areas क्यों हैं।

Deep Dive

In a Transformer, the attention mechanism computes three matrices for each token: Query (Q), Key (K), and Value (V). The query of the current token is compared against the keys of all previous tokens to produce attention weights, which are then used to weight the values. During generation, the Q changes with each new token, but the K and V for all previous tokens stay the same. The KV cache stores these K and V matrices so they're computed once and reused.

The Memory Math

KV cache size = 2 (K and V) × num_layers × num_heads × head_dim × sequence_length × bytes_per_element. For a 70B model with 80 layers, 64 heads, head dimension 128, at FP16: that's 2 × 80 × 64 × 128 × 2 bytes = ~2.6 MB per token. A 100K context therefore needs ~256 GB of KV cache alone — more than the model weights themselves. This is the fundamental constraint on long-context inference.

Optimizations

Several techniques address KV cache pressure. Grouped Query Attention (GQA) shares key-value heads across multiple query heads, reducing cache size by 4–8x. Multi-Query Attention (MQA) goes further with a single KV head. PagedAttention (used by vLLM) manages cache memory like virtual memory pages, eliminating fragmentation. Sliding window attention limits how far back each token looks, capping cache growth. Quantizing the KV cache to FP8 or INT4 is another practical lever — some quality loss, but 2–4x memory savings.

संबंधित अवधारणाएँ

← सभी Terms
← Knowledge Graph LangChain →