KV Cache

Also known as: Key-Value Cache

Category: Neural Networks & Architectures

A memory optimization that stores previously computed key and value tensors during autoregressive inference to avoid redundant computation.

Full Explanation

The KV (Key-Value) cache is an inference optimization for autoregressive transformer models that stores the key and value projections from all previous tokens at each layer. During generation, when computing attention for a new token, only the new token's query needs to be computed; the keys and values for all previous tokens are retrieved from the cache. Without the KV cache, every new token would require recomputing attention over the entire sequence from scratch. The KV cache trades memory for speed, with memory usage growing linearly with sequence length and number of layers. For long sequences and large models, KV cache management becomes a critical bottleneck. Techniques like multi-query attention, grouped-query attention, and paged attention (vLLM) have been developed to reduce KV cache memory footprint.

Related Terms

More in Neural Networks & Architectures

Explore More