Identify reusable keys and values during decoding
Type the indices of each value in their original order. · 171 words
Autoregressive decoding builds a response one token at a time. For each new token, the model computes a query, a key, and a value. Earlier tokens already have keys and values, so a key-value cache stores them. Keys are like labels for stored information; values hold the content. For a prompt [Red, ball], the query for a generated token [bounces] checks the cached keys for Red and ball, plus its own key, then combines the selected values. The new key and value for bounces are added to the cache.
Suppose the prompt is [The, cat] and the model next generates [sat]. The query for sat checks the keys for The and cat, plus its own key: 3 query-key comparisons. The keys and values for The, cat, and sat are then cached. If the next token is on, its query is compared with all 4 cached keys rather than recomputing the first three tokens' keys and values. Caching saves repeated projections. It does not remove the need to attend to earlier tokens.
Check your recall
During autoregressive decoding of token 5, how many earlier cached keys does its query inspect, and what is stored afterward?
Check your answer
It inspects 4 earlier cached keys. The model then adds token 5's new key and value, leaving 5 cached entries.
Sources and credits
Original KeyTouch lesson. Background reading · Vaswani et al. · Attention Is All You Need.