Blog - KV Caching
Standard Attention Process
- You start with the previous layer outputs: $X$
- You down-project / matrix multiply by weight matrices, $W^Q, W^K, W^V \in \mathbb{R}^{512 \times 64}$ (8 heads).
- You compute attention $= \text{softmax}\left(\frac{QK^T}{\sqrt{64}}\right)V$
- Additionally a mask is applied - “We implement this inside of scaled dot-product attention by masking out (setting to $-\infty$) all values in the input of the softmax which correspond to illegal connections.”
Let’s say we’re generating a string with 4 tokens
<bos> I like pizza
First decoding step: We condition the model on <bos>
At every layer of the model the representation is built from one single Query and Key and Value (representing <bos> itself)
Second decoding step: The model is now conditioned on <bos> and “I”
Due to the causal mask, the BOS representation is still only composed from the single query and key, and value representing <bos>, but now the “I” representation is based on
(note we are ignoring the softmax & scaling here)
Third decoding step: The model is now conditioned on <bos>, “I” and “like”
The BOS representation is based on the single query and key, and value (not affected by I or like’s rep)
The I representation is based only on I and <bos> representations, but now the “like” representation is
(note we are ignoring the softmax & scaling here)
Why is the Query not cached?
The query vector is only needed to compute the representation for a particular token. Since we already have the key and value vectors (since they’re cached) the earlier query vectors are not needed in later steps.
When a new input token is added why don’t the input vectors change?
Because of the causal mask. In attention, each token representation is only based on the previous tokens and itself. All other parts of the transformer (e.g. feedforwards, skip connections) operate on each token representation independently.
(Typeset with Claude)
Citations
- Attention Is All You Need: 3.2.3
- Hugging Face Blog, KV Caching: https://huggingface.co/blog/not-lain/kv-caching