passdrill
AI & LLM Engineering · How LLMs Work: Transformers & Training · Card 019/024 medium

When an autoregressive Transformer decoder generates text one token at a time, naively recomputing self-attention from scratch at every step would repeat a large amount of work. What does key-value (KV) caching do to avoid this, and what does it not need to recompute?

  1. It stores the entire attention-score matrix from the very first generation step and reuses those exact scores unchanged for every later token, regardless of what new token is generated
  2. It skips computing new query vectors for later tokens, reusing the query vector from the first generated token for every subsequent generation step
  3. It discards the key and value vectors after each step and instead caches only the final output logits, replaying them directly for the next step
  4. It stores the key and value vectors computed for every previously generated token so that, at each new step, only the new token's query, key, and value need to be computed, with that new query then attended over the cached keys and values, rather than recomputing keys and values for the whole sequence so far
Next card → Shuffle