4.1.3 · D5Transformer Architecture

Question bank — Query, key, value matrices

2,087 words9 min readBack to topic

This bank hunts the mental slips around query, key, value matrices — the ideas that feel right but hide a wrong assumption. No heavy arithmetic here (see the worked-examples pages for that). Each line is a question ::: answer reveal: cover the right side, commit to an answer out loud with a reason, then check.


Symbols and shapes you need first

Before any trap, pin down every symbol these questions use. Nothing below is used before it appears here.

The picture below shows one token embedding being fanned out by three different learned matrices into three different roles. Notice the red value vector — it can be a different length () than the black query and key ().

Figure — Query, key, value matrices
Figure — Query, key, value matrices
Figure — Query, key, value matrices

True or false — justify

True or false: , , and are three different slices of the input matrix .
False. They are three separate linear projections , , of the same full — every projection sees all dimensions, nothing is sliced off.
True or false: since , , all come from the same , the three matrices carry identical information.
False. The learned weights are different, so each projection reshapes into a different subspace — matching-intent, matching-offer, and content — that emphasise different features.
True or false: the weight matrices are fixed by the architecture designer.
False. Only their shapes are fixed. The entries start random and are learned by backpropagation; a translation model tunes them differently than a summariser.
True or false: swapping and leaves the attention output unchanged.
False. The score is , which is asymmetric — swapping the roles makes token query with 's intent, changing every relevance score.
True or false: for self-attention, , , and all come from the same sequence.
True. Self-attention feeds one sequence into all three projections; cross-attention is where comes from one sequence and from another.
True or false: and must be equal.
False. sizes the matching space (queries and keys must share it to take a dot product); sizes the content space and is free to differ. They are often set equal only for convenience.
True or false: the dot product requires and to live in the same-dimension space.
True. A dot product pairs coordinates one-to-one, so queries and keys must both be -dimensional — this is exactly why and share output size .
True or false: making larger always improves attention quality.
False. Larger inflates dot-product magnitudes, pushing softmax toward saturation and killing gradients — the reason scaled dot-product attention divides by .
True or false: the value vector decides whether token is attended to.
False. Whether-to-attend is decided by the query–key score; only decides what information token contributes once it has been selected.

Spot the error

"Attention is symmetric: if attends strongly to , then attends strongly to ." — find the flaw.
The score uses 's query with 's key, while uses 's query with 's key — different vectors from different matrices, so scores are generally unequal.
"We can drop and just use the raw embedding as the value." — what's wrong?
Then the content space is forced to equal the raw semantic space, removing the model's freedom to separate "what made relevant" from "what information should pass on." exists precisely to decouple these.
", so ." — spot the error.
The multiplication order is wrong. With rows of being tokens, ; writing has mismatched inner dimensions and would mix tokens instead of projecting each independently.
"Since softmax normalises the scores, scaling by is redundant." — why is this wrong?
Softmax normalises the sum, not the spread. Large raw scores make softmax nearly one-hot with vanishing gradients; the scaling controls the spread before softmax, which normalisation cannot fix afterward.
"Because the same feeds all three, we only need to learn one matrix and reuse it for Q, K, V." — the flaw?
Reusing one matrix collapses query, key, and value into the same space, destroying the asymmetry and the query-vs-content decoupling that make attention expressive.
"The attention output for token is a weighted sum of the keys ." — correct it.
It is a weighted sum of the values : . Keys only produce the weights ; values carry the information.

Why questions

Why do we need a separate key projection at all instead of matching queries directly against raw embeddings?
Raw embeddings mix meaning, position and syntax on the same axes; the key projection carves out a subspace where "should I be attended to" is cleanly measurable, independent of raw semantics.
Why is the dot product a sensible measure of relevance rather than, say, elementwise difference?
The dot product measures alignment of direction in the matching space via — large-positive means the arrows point the same way, which is exactly "this key answers my query."
Why can be smaller than without losing the ability to attend?
Attention only needs a subspace rich enough to compare relevance, not to store all meaning. A smaller also cuts compute and reduces overfitting, since matching needs fewer degrees of freedom than full semantics.
Why is it useful that "cat" can query for verbs (Q–K space) yet supply noun features (V space)?
Because the feature that makes a token relevant differs from the feature it should deliver. Separate and let one token match on grammatical role and contribute semantic content — impossible with a single projection.
Why are the same linear layers applied to every token position identically?
The projection is content-based, not position-based; applying the same to each row keeps the operation permutation-consistent, and any position information must already be injected via positional encoding into .

Edge cases

What is a token's self-relevance score , and is it always the largest?
It is using 's own query and key. Because , this is not guaranteed to be the maximum — a token can attend more strongly to a different token than to itself.
If (all-zero query weights), what happens to attention?
Every is the zero vector, so all scores are zero, softmax outputs a uniform distribution, and each output becomes the plain average of all values — attention degenerates into mean-pooling.
For a sequence of length (a single token), what does attention compute?
The lone token attends only to itself; softmax over one score is , so the output is exactly its own value vector , regardless of the score value.
If two tokens have identical embeddings, must their query vectors be identical?
Yes. The projection is a deterministic function of , so equal embeddings give equal queries — any later difference in attention must come from differing positions encoded into beforehand.
What happens to the softmax weights if all relevance scores for token are equal?
Softmax returns a uniform distribution , and the output is the unweighted average of all value vectors — attention provides no selectivity in this degenerate case.
In multi-head attention, why can one head learn a focused on syntax while another focuses on coreference?
Each head owns independent with their own -slice, so they are optimised separately and can specialise on different relational patterns simultaneously.

Fast self-test — trace each trap to its root

Recall The one assumption behind most traps

Symmetry / sameness illusion ::: Treating as the same object ignores that they are three independent learned projections — see "True or false" items 1–2.

Recall The directionality trap

Why attention is not symmetric ::: uses ; uses — different vectors — see "Spot the error" item 1 and "Edge cases" self-relevance.

Recall The dimension trap

Why never breaks ::: vanishes inside the dot product, only sets output width — see the tiny worked-shapes example.

Recall The softmax trap

What scaling by actually fixes ::: The spread of scores before softmax, not the sum — see "Spot the error" item 4.