🎯 Goal: Give you an intuition for “KV caching” without assuming you’ve taken a deep dive into Transformers.
(If anything sounds fuzzy, just ask-away and we can zoom in on that part!)
1⃣ A 60-second refresher on what matters
• A Transformer is a type of neural network often used for text generation (think ChatGPT, code-assistants, etc.).
• Its super-power is a mechanism called “self-attention.”
– Every time the model looks at a word (or token) it asks: “Which earlier words should I pay attention to, and by how much?”
Under the hood, that “attention” is done with three little data structures for each token:
| What | Nickname | Rough analogy |
|---|
| Query | Q | A question you’re about to ask (“Who relates to me?”) |
| Key | K | A label on each past word (“Here’s what I’m about.”) |
| Value | V | The actual content you want to retrieve if the key matches |
The model compares Query (the new token’s question) to all stored Keys, then mixes the corresponding Values to produce its answer.
2⃣ The practical headache
When you let a language model “write” something, you feed it one token at a time:
Step 1 ➜ token 1 ➜ model outputs token 2
Step 2 ➜ token 2 ➜ model outputs token 3
… and so on.
At every step the naïve method recomputes ALL the Keys and Values for EVERY previous token, even though those haven’t changed. That’s like rewriting yesterday’s notes from scratch every morning—super wasteful.
3⃣ KV caching to the rescue
KV caching = “Keep those Keys and Values around; reuse them next step.”
Workflow with KV caching:
-
Step 1
• Compute Q₁, K₁, V₁ for token 1
• Store K₁, V₁ in a cache (usually GPU memory)
-
Step 2
• Only compute Q₂ (the new query for token 2)
• Retrieve K₁, V₁ from cache, add new K₂, V₂
• Attention now uses [K₁,K₂] and [V₁,V₂]
• Append K₂, V₂ to cache
-
Repeat… Each new step does O(1) extra work instead of O(sequence length).
4⃣ A household analogy 🗄️
Picture a librarian (the model) who must cite earlier books (tokens).
• Without caching: Each time a new customer arrives, the librarian re-scans the entire library catalog from scratch.
• With KV caching: After the first scan, the librarian keeps an index card (Key) and a quick summary (Value) of each book on her desk. When a new customer asks, she just looks at the pile of cards and adds one more for the latest book. Way faster!
5⃣ Why you should care
✅ Speedups of 2×–10× during text generation (depends on hardware & sequence length)
✅ Lower compute cost means lower energy bills and quicker user responses
✅ Makes long interactive chats feasible on smaller GPUs/CPUs
6⃣ Fine print / limitations
• Primarily used during inference/generation, not during full-sequence training (because during training you already process the whole sequence in parallel).
• Consumes GPU memory: you’re saving K and V tensors, which scale with sequence length.
• When you exceed memory, you may need to truncate (drop early tokens) or off-load part of the cache to slower memory.
🔑 TL;DR
“KV caching” is just the trick of storing a Transformer’s previously computed Keys & Values so that each new token only needs to compute its own Query (plus its own Key & Value) instead of recomputing everything from scratch. Same answers, dramatically less work.