In transformer models like GPT, during autoregressive decoding (i.e., generating text one token at a time), caching is used to improve efficiency. Specifically, we cache the key and value projections from previous time steps. The reason we cache keys and values but not queries stems from how self-attention operates in these models.
Understanding Self-Attention in Decoding:
In the self-attention mechanism, for each position ( i ), we compute:
- Query vector (( q_i )) from the embedding of the current token at position ( i ).
- Key vectors (( k_j )) and Value vectors (( v_j )) from the embeddings of tokens at all positions ( j ) (up to position ( i ) due to causality constraints in autoregressive models).
The attention output at position ( i ) is computed using the query ( q_i ) and all previous keys and values (( k_j, v_j ) for ( j \leq i )):
[
\text{Attention}(q_i, K, V) = \sum_{j=1}^{i} \text{softmax}\left(\frac{q_i \cdot k_j}{\sqrt{d_k}}\right) v_j
]
Why We Cache Keys and Values:
-
Keys and Values Depend on Previous Tokens: The keys and values at each position ( j ) are computed from the embeddings of the tokens at those positions. When generating the next token, these embeddings (and thus their keys and values) do not change—they are fixed once the tokens are generated.
-
Reusing Keys and Values: Since the same keys and values from previous positions are needed at every subsequent time step, caching them avoids redundant computation. This is especially beneficial for long sequences, as recomputing keys and values for all previous tokens at each step would be computationally expensive.
Why We Don't Cache Queries:
-
Queries Depend on the Current Token: The query vector ( q_i ) at position ( i ) is computed from the embedding of the current token at that position. During decoding, this token is the one we are currently generating, so we cannot compute ( q_i ) ahead of time.
-
Queries Are Not Reused: Each query vector ( q_i ) is used only once—to compute the attention output at its own position ( i ). Unlike keys and values, previous query vectors ( q_j ) for ( j < i ) are not needed when computing attention for the current position. Therefore, caching queries from previous positions provides no benefit.
Clarifying the Misconception:
While it's true that both queries and keys/values are computed using linear projections of token embeddings, their roles in the self-attention mechanism differ during decoding:
-
Queries (Current Position Only): For each new token being generated, we compute its query vector from its embedding. This query is unique to the current time step and is not reused in future computations.
-
Keys and Values (All Previous Positions): The keys and values from all previous tokens are required at each time step to compute the attention output for the current token. Hence, caching them saves significant computation.
Illustrative Example:
Imagine we're generating a sequence and have already produced tokens at positions 1 to ( i - 1 ). When generating token at position ( i ):
- We compute the embedding ( e_i ) for the current token (which is unknown until this step).
- We compute the query ( q_i ) by projecting ( e_i ).
- To compute attention, we need to calculate ( q_i \cdot k_j ) for all ( j \leq i ).
- Keys ( k_j ) and values ( v_j ) for ( j < i ) are from previous tokens—we cache these.
- Key ( k_i ) and value ( v_i ) for the current token are computed and added to the cache for future steps.
Conclusion:
Caching queries isn't helpful in autoregressive decoding because queries from previous positions aren't needed when generating the next token—they're not part of the computation for future time steps. Only the keys and values from previous tokens are necessary to compute attention for the current token, which is why we cache them. This strategy optimizes efficiency without altering the model's output.
TL;DR: We cache keys and values because they're reused at each time step to compute attention with the current query. Queries depend on the current token being generated and aren't reused, so caching them doesn't provide a computational benefit.