Query, Key & Value: The Core Intuition¶
Overview¶
Every word in a sentence plays one of three roles inside attention: Query (Q), Key (K), or Value (V). The Query asks "what am I looking for?", the Key answers "what am I?", and the Value supplies "what do I contribute?". Attention is the process of matching Queries to Keys and blending the corresponding Values. Nothing about Q/K/V is pre-programmed โ all three are learned from data.
- Analogy: attention works like a database / search engine lookup
- Zero-setup: no hand-crafted rules; the model discovers the roles during training
- Leads to: 02 Scaled Dot Product Attention โ how Q, K, V combine mathematically
- First seen in: "Neural Machine Translation by Jointly Learning to Align and Translate" (Bahdanau et al., 2015) โ attention predates transformers!
The Search Engine Analogy¶
The simplest mental model for attention is a database query. Every word plays one of three roles โ Query, Key, or Value:
You run a search engine (this is attention!):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
QUERY = what I'm โ "best laptop under 1000$" โ
searching for โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
KEY = the labels โ "laptop reviews" โ page 3โ
of every stored โ "coffee machines" โ page 8โ
document โ "budget laptops" โ page 1โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
VALUE = the โ page 3 content, page 8 โ
actual content โ content, page 1 content โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
1. QUERY is compared against every KEY โ score (similarity)
2. Scores are normalized โ weights (how much to pay attention)
3. We fetch the VALUES weighted by scores โ output
Why is the search engine the right metaphor?
โ
Query โ Key: "what I want" is different from "what each item is"
โ
Query vs all: a query compares itself against EVERY key (all positions)
โ
Soft retrieval: results are mixed proportionally to relevance โ
not a hard "yes/no" retrieval
โ
Learned ranking: the "relevance function" is trained, not hand-written
The Three Roles, in Detail¶
In a transformer, every token projects to three vectors using learned weight matrices:
For token xแตข:
Qแตข = W_Q ยท xแตข โ "What am I looking for?" (query)
Kแตข = W_K ยท xแตข โ "What am I?" (key)
Vแตข = W_V ยท xแตข โ "What do I contribute?" (value)
Reading them in a concrete sentence¶
Example: in "The chef tasted the soup"
"soup" as Query: "is there anything modifying me? (adjectives, verbs...)"
"chef" sends a Key: "I am the subject, a person"
"tasted" sends a Key: "I am a past-tense verb"
"the" sends a Key: "I am a determiner"
The softmax weights decide which Keys match the Query best.
What each role is "about"¶
Role Question it answers Governed by Emerges as
โโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโ
Q "What am I seeking?" the token's own needs object-of-verb links,
modification needs
K "How should I be found?" the token's identity grammatical category,
topic, name
V "What content do I give?" the token's meaning the "payload" that
gets passed along
๐ก Key Insight: Q, K, V are learned during training. The model discovers which roles are useful for language understanding. Nobody tells "soup" to look for adjectives โ it learns to, because that improves next-token prediction.
Why Three Vectors? (Why Not One?)¶
A reasonable question: why not just compute similarity between raw token embeddings? Three reasons:
1. Separation of "Matching" from "Content"¶
Similarity is about MATCHING, output is about CONTENT.
"cat" and "kitten": high similarity (both noun-y, animal-y)
But when generating the word after "the ___", you want the
CONTENT of "cat"/"kitten" (the animal concept), not a blur.
Q/K decide WHO talks to WHOM. V decides WHAT is said.
Mixing them into one vector forces a compromise:
- a vector that is both a good label AND good content
- conflates "findability" with "meaning"
2. Different Projections โ Different Semantics¶
Q, K, V come from three DIFFERENT learned matrices:
Qแตข = W_Q xแตข (d_model โ d_k)
Kแตข = W_K xแตข (d_model โ d_k)
Vแตข = W_V xแตข (d_model โ d_v)
Each matrix warps the embedding space for its own purpose:
W_K warps space so that related items land close together
(two words can be "far apart" in K-space but close in V-space)
W_Q warps space relative to what the token needs at this position
W_V warps space so the payload is well-separated for the MLP
One embedding vector cannot serve all three purposes at once.
3. Keys Are Compared in a Lower-Dimensional Space¶
Typically d_k < d_model (e.g., d_model = 4096, d_k = 128).
Projecting to a smaller key space:
- cheap dot products (fewer FLOPs per head)
- forces the key to compress to "essentially relevant" features
- the compression is learned, so it keeps what matters for matching
The Projection Matrices¶
Token embeddings X: (seq_len, d_model) e.g., (6, 4)
Learned parameters (all trainable, shared across positions):
W_Q: (d_model, d_k) W_K: (d_model, d_k) W_V: (d_model, d_v)
Computing Q, K, V โ one matrix multiply each:
Q = X @ W_Q โ (seq_len, d_k)
K = X @ W_K โ (seq_len, d_k)
V = X @ W_V โ (seq_len, d_v)
Concrete (toy):
X = 6 tokens ร 4-dim embedding
W_Q: 4ร3, W_K: 4ร3, W_V: 4ร4
โ Q: 6ร3, K: 6ร3, V: 6ร4
Typical dimensions in real models¶
Model d_model d_k (head) heads
GPT-2 small 768 64 12
BERT base 768 64 12
LLaMA-2 7B 4096 128 32
GPT-3 12288 128 96
Common ratio: d_k = d_model / heads (128, 64, 80, ...)
Attention as a Biological/Perceptual Idea¶
Attention isn't an AI invention โ it mirrors natural attention:
๐ข Visual attention: your eyes scan a scene and "focus" on salient
regions; peripheral regions contribute less
๐ข Cognitive attention: when reading, you focus on words that resolve
ambiguity ("bank" โ depends on "river" or "money")
๐ข Machine translation origin: Bahdanau (2015) let the decoder "look back"
at relevant encoder words for each target word โ
instead of squeezing the whole source into
one context vector
Transformer = the same idea, applied at every layer, for every token,
in parallel, with learned Q/K/V.
Where Q, K, V Come From: The Full Pipeline¶
Raw text
โ tokenization
โผ
Token IDs โโโบ Embedding lookup โโโบ X (token embeddings, may already
โ include position info โ see
โ [05 Position Information](/02-llm-modeling/00-fundamentals/01-attention/05-position-information/))
โผ
Linear layer: XยทW_Q โ Q XยทW_K โ K XยทW_V โ V
โ
โผ
scaled_dot_product_attention(Q, K, V) โ [02 Scaled Dot Product Attention](/02-llm-modeling/00-fundamentals/01-attention/02-scaled-dot-product-attention/)
โ
โผ
Context-aware token representations (one per position)
Key Takeaways¶
๐ Q asks, K labels, V delivers โ three learned roles per token
๐ Fuzzy retrieval: relevance is soft (softmax weights), not binary
๐ง Three separate matrices: matching space (Q/K) โ content space (V)
๐ Fully learned: no hand-crafted linguistic rules anywhere
๐ Universal: self-attention, cross-attention and multi-head all reuse the same Q/K/V machinery
โก Parallel by design: XยทW_Q, XยทW_K, XยทW_V are one batched matmul each
Related Notes¶
- 00 Attention Mechanisms โ chapter overview and motivation
- 02 Scaled Dot Product Attention โ how Q/K/V combine mathematically
- 04 Multi Head Attention โ many Q/K/V triples in parallel
- 03 Types Of Attention โ where Q comes from (same or other sequence)
- 05 Position Information โ embeddings carry position info before projection