DEV Community

Cover image for Understanding Transformer Decoding with a KV Cache
matsuken92
matsuken92

Posted on

Understanding Transformer Decoding with a KV Cache

Hi, I'm Matsuken, a data scientist at a Japanese technology company.

In this article, I'll explain how decoding works in a Transformer that uses a key-value (KV) cache.

This is an English version of my original Japanese article, which I originally wrote and published on Qiita.

Transformer inference consists of two stages:

  1. Prefill: Process all input tokens—the prompt—in parallel. During this stage, the model computes the keys ( KK ) and values ( VV ) and stores them in a cache.
  2. Decode: Generate text one token at a time. At each step, the model reads and reuses the previously computed keys and values, then appends one new row—the key and value for the latest token—to the cache.

For an explanation of the first stage, prefill, see my previous article. The stored KK and VV tensors produced by this process are collectively called the KV cache.

Given an input XX , an attention layer computes the queries, keys, and values as follows:

Q=XWQK=XWKV=XWV \begin{aligned} Q &= XW_Q \cr K &= XW_K \cr V &= XW_V \end{aligned}

It then produces the output YY :

Y=softmax(QKTd+M)V Y = \operatorname{softmax}\left(\frac{QK^{\mathrm{T}}}{\sqrt{d}} + M\right)V

The process is illustrated below.1

01 The input token matrix X is projected into the query, key, and value matrices Q, K, and V.

Here, nn is the number of tokens and dd is the token representation dimension.

02 Attention scores are computed from Q and K, combined with a causal mask, normalized with softmax, and multiplied by V to produce Y.

In a decoder-only Transformer, this computation takes place in every Multi-Head Attention block: each block receives XX and produces YY .2

03 A decoder-only Transformer with the masked Multi-Head Attention block highlighted.

Prefill

During the initial pass, the input XX normally contains multiple tokens, so it is an n×dn \times d matrix. This is the prefill stage covered in my previous article.

The next stage—decode—is where the KV cache comes into play.

Decode

Prefill processes the prompt and determines the first generated token. In each subsequent decode step, the model receives the token generated in the previous step and autoregressively predicts the next token.

Conceptually, we could append every generated token to the original input sequence and process the entire sequence again. In practice, this would repeatedly perform the same computations. Instead, the model computes only the new query, key, and value vectors for the latest token. It reuses the keys and values for all previous tokens from the KV cache. This reuse is the purpose of the KV cache.

Suppose the prompt is:

[BOS] two kids are playing in a swimming pool with a green colored crocodile.

and the first generated token is “Then.”

04 The prompt is passed to the model, which generates the token “Then.”

Because nn tokens have already been processed, the model uses the representation of token n+1n+1 to compute three new vectors: qn+1q_{n+1} , kn+1k_{n+1} , and vn+1v_{n+1} .

05 The representation x at position n+1 is projected into q, k, and v vectors.

Next, the model updates KK and VV . It reuses the entries through position nn from the cache and appends kn+1k_{n+1} and vn+1v_{n+1} , respectively.

06 The cached K and V entries are reused, and the new k and v entries for “Then” are appended.

The new query qn+1q_{n+1} is multiplied by the transpose of the updated key matrix, which consists of the cached keys followed by kn+1k_{n+1} . This produces the attention-score vector sn+1s_{n+1} .

07 The new query q at position n+1 is multiplied by the updated transposed key matrix to produce attention scores s at position n+1.

The model applies softmax to sn+1s_{n+1} and multiplies the resulting attention weights by the updated value matrix—the cached values followed by vn+1v_{n+1} . The result is yn+1y_{n+1} .

08 The softmax-normalized attention scores are multiplied by the updated value matrix to produce y at position n+1.

We have now obtained the yn+1y_{n+1} required by this layer.

Although the explanation above follows the operations step by step, we can also view the computation as part of a conceptual full attention matrix. It has three regions: entries that were computed in previous steps, future positions excluded by the causal mask, and the newly computed row for the current step.

09 An attention matrix separating previously computed entries, future positions excluded by the causal mask, and the row newly computed at position n+1.

The complete decode-step computation can be summarized as follows:

10 The new query attends to the updated key cache

You might wonder whether yn+1y_{n+1} alone is enough, or whether the model also needs y0,,yny_0, \ldots, y_n . Recall that this step required only xn+1x_{n+1} because the earlier keys and values were already cached. Therefore, computing only yn+1y_{n+1} is sufficient.

As long as KK and VV are cached, each subsequent step needs only the representation of the newest token to produce the next-token output. That is what makes decoding with a KV cache so efficient.

03 A decoder-only Transformer with the masked Multi-Head Attention block highlighted.

The model maintains a separate KV cache for every Transformer layer. If the model has NN layers, it therefore has NN cache pairs:

(Kcache(1),Vcache(1)),,(Kcache(N),Vcache(N)) (K_{\mathrm{cache}}^{(1)}, V_{\mathrm{cache}}^{(1)}), \ldots, (K_{\mathrm{cache}}^{(N)}, V_{\mathrm{cache}}^{(N)})

This efficiency comes with a trade-off: as the number of tokens—the context-window length—increases, the memory required to store the key and value caches also grows. Several techniques address this issue, but I'll save those for another article.


  1. To keep the diagrams simple, they omit division by the scale factor d\sqrt{d}

  2. To focus on the KV-cache mechanism, the rest of this article represents Multi-Head Attention as a single attention head. 

Top comments (0)