KV Cache › The Redundancy Problem in Autoregressive Generation

Michael BrenndoerferJanuary 6, 20268 min read
Language AI HandbookAll chapters
Part 42 of 67Inference Optimization
Chapter 313KV Cache › The Redundancy Problem in Autoregressive Generation
Reading section 2 of 14 · The Redundancy Problem in Autoregressive Generation · 8 minSection 2 of 14 · 8 min

Book contents

Section 2 of 14 · 8 min read

KV Cache › The Redundancy Problem in Autoregressive GenerationOne section · Sections in this chapter2 / 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

As we explored in Part XIII, decoder-only transformers generate text one token at a time. At each step, the model takes all tokens generated so far (including the prompt) and predicts the next token. The computational core of this process is the self-attention mechanism, which allows each token to gather information from all tokens that came before it. To understand why caching gives such large benefits, we must first examine exactly how attention operates during generation and identify where the redundant work occurs. The redundancy is not an accident or a design flaw; it is an inherent consequence of computing context-aware representations at every position.

How Attention Creates Redundancy

Recall from Part XIII that self-attention computes queries, keys, and values from the input. The basic insight behind these three components is that attention works like an information retrieval system: queries stand for what a token is looking for, keys stand for what each token offers, and values contain the actual information to be retrieved. Think of it like a library search: the query is your search term, the keys are the catalog entries for every book on the shelf, and the values are the books themselves. When you search, you compare your query against every catalog entry and retrieve the books whose entries best match. We express these three components through linear projections:

Q=XWQ,K=XWK,V=XWVQ = XW_Q, \quad K = XW_K, \quad V = XW_V

where:

  • XX: the input embedding matrix, containing the vector representations of all tokens in the sequence
  • WQ,WK,WVW_Q, W_K, W_V: the learned projection matrices that transform inputs into query, key, and value subspaces, each capturing a different aspect of the token's meaning
  • Q,K,VQ, K, V: the resulting matrices, where QQ contains vectors representing what each token seeks, KK contains vectors representing what each token can be matched against, and VV contains the actual content to be aggregated

Each of these projection matrices is learned during training to extract the most useful representations for the attention mechanism. Once training is complete, these matrices remain fixed during inference. This means that given the same input token, the projections will always produce the same key and value vectors. This determinism is the property that makes caching possible: if the input and the weight matrix are unchanged, the output cannot change either.

The attention mechanism then computes a weighted sum of values based on the compatibility between queries and keys. This compatibility is measured through dot products, which capture how well a query's "question" matches each key's "answer." The full attention formula is:

Attention(Q,K,V)=softmax(QKTdk)V\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V

where:

  • Q,K,VQ, K, V: the query, key, and value matrices computed from the input
  • KTK^T: the transpose of the key matrix, arranged so that the dot product between queries and keys can be computed efficiently through matrix multiplication
  • dkd_k: the dimension of the key vectors, used for scaling to prevent the dot products from growing too large
  • softmax\text{softmax}: the function that converts raw compatibility scores into probability weights summing to 1

The scaling factor dk\sqrt{d_k} is necessary. Without this normalization, the dot products between high-dimensional vectors can become very large in magnitude. When these large values pass through the softmax function, the result becomes extremely peaked, meaning almost all attention weight concentrates on a single token. This causes gradients to vanish for all other positions, making training unstable. The square root scaling keeps the variance of the dot products roughly constant regardless of the dimension, and we explored this phenomenon in depth in Part XIII.

Tracing the Redundancy Step by Step

During generation, let's trace what happens at each step to understand precisely where the wasted work accumulates. Suppose we're generating a response to a 10-token prompt. The model must produce one token at a time, with each new token depending on everything that came before it.

Step 1 (Prefill): The model processes all 10 prompt tokens simultaneously. It computes the QQ, KK, VV matrices, each of shape (10,dk)(10, d_k) or (10,dv)(10, d_v) depending on the specific projection. The attention mechanism allows each prompt token to attend to all previous prompt tokens, establishing the initial context representation.

Step 2: After generating the first new token, we now have 11 tokens total. Without any optimization, the model would need to process the entire 11-token sequence from scratch. This means computing QQ, KK, VV for all 11 tokens, even though 10 of them are unchanged from the previous step.

Step 3: With 12 tokens total, we compute QQ, KK, VV for all 12 tokens, once again recomputing the projections for the original prompt tokens that haven't changed at all.

Notice the wasteful pattern: at step tt, we recompute the keys and values for the first 10 prompt tokens, even though nothing about them has changed since step 1. The same tokens, passing through the same fixed projection matrices WKW_K and WVW_V, produce the same KK and VV vectors every single time. This redundancy stems from a basic property of the attention mechanism: the key and value for a given token depend only on that token's embedding and the projection weights, not on what comes after it in the sequence. The causal mask ensures that earlier tokens never attend to later ones, so later tokens can never influence the keys and values of earlier tokens.

This redundancy has concrete costs that grow sharply with sequence length. For a model with LL layers, HH attention heads, and head dimension dhd_h, generating TT tokens requires computing KK and VV projections repeatedly at every step. The total number of projection operations across all generation steps sums to:

∑t=1Tt=T(T+1)2≈T22\sum_{t=1}^{T} t = \frac{T(T+1)}{2} \approx \frac{T^2}{2}

where:

  • TT: the final sequence length, representing the total number of tokens processed including both the prompt and generated tokens
  • tt: the current step number, which also is the sequence length at that particular step
  • ∑t=1Tt\sum_{t=1}^{T} t: the sum of the arithmetic progression 1+2+3+…+T1 + 2 + 3 + \ldots + T, capturing the total number of token-projection operations across all steps

This quadratic scaling has severe implications for practical deployment. If generating 100 tokens requires 100×1012=5,050\frac{100 \times 101}{2} = 5,050 projection operations, then generating 1,000 tokens requires 1000×10012=500,500\frac{1000 \times 1001}{2} = 500,500 operations, a 100-fold increase in work for only a 10-fold increase in output length. This contrasts with the ideal case where each token's keys and values are computed only once, requiring TT projection operations. The KV cache reaches exactly this ideal.

Out[3]:
Visualization
Line chart showing total projection operations vs sequence length. The red quadratic curve (without KV cache) rises steeply to ~125,000 at T=500, while the green linear curve (with KV cache) remains flat near zero.
Computational complexity comparison between generation with and without KV cache. Without caching, the number of projection operations grows quadratically with sequence length. With KV cache, the growth is linear, giving large savings for longer sequences. The shaded red region is the eliminated redundant operations.