4.1.5 · D5Transformer Architecture

Question bank — Multi-head attention

1,985 words9 min readBack to topic
Figure — Multi-head attention

What to observe in the figure above: the left grid is with one row per token ( rows) and its columns sliced into coloured blocks of each — one block per head; the right grid is a single head's score matrix, square and (one cell per pair of tokens, not per feature). Carry that "rows = tokens, score matrix = token × token" picture into every item below.

Figure — Multi-head attention

What to observe in the figure above: the pink measured curve (spread of for random vectors) lands exactly on the yellow dashed line as grows from 1 to 128 — confirming that the typical logit magnitude is , so dividing by that constant (blue arrow) restores a spread of about 1.


True or false — justify

Each line: statement, then the verdict with a real reason.

Multi-head attention costs roughly times more compute than one full-width head.
False — one head does an score step costing and an value-weighting step also ; multiplying by heads gives , the same total FLOPs as one full-width head.
If every head used the same projection matrices , MHA would still help because you average more attention maps.
False — identical projections produce identical heads, so concatenating copies and blending with is no richer than one head. The diversity comes entirely from the projections being different.
The scaling factor changes between single-head and multi-head attention.
True in value — inside a head you scale by , not , because each head's dot products live in the smaller -dimensional subspace.
Concatenating the heads already lets them share information with each other.
False — concatenation just stacks them side by side into separate channels; no head has yet "seen" another. The mixing happens only when multiplies the concatenation.
The output projection is optional — you could feed the raw concatenation to the next layer.
False in practice — without the heads stay in fixed, non-interacting slots, so the model cannot learn to combine syntactic + semantic + positional signals. It also fixes which output dimensions each head "owns".
Multi-head attention and single-head attention with the same have the same number of parameters in their projection matrices.
True — the per-head matrices each of shape stack to one matrix, identical in parameter count to one full-width projection.
Each head must always attend most strongly to a token's own position.
False — self-attention weight is just softmax over learned scores; a head can legitimately put near-zero weight on the diagonal if the useful information lives elsewhere (e.g. a co-reference head looking backward).
In Cross-Attention, multi-head attention still splits into heads the same way it does in Self-Attention.
True — the head-splitting is independent of where , , come from; cross-attention only differs in that come from the encoder while comes from the decoder.
Any works as long as it is at most .
False — must divide so that is a whole number; e.g. fails for even though .

Spot the error

Each line states a flawed claim; the reveal names the specific mistake. (Recall from the definition box: , with = number of tokens.)

"With and heads, each head gets ."
The error is choosing an that does not divide is not an integer, so concatenation would not return exactly . must divide .
"The scores matrix has shape ."
Wrong shape — are (one row per token), so is : one score per pair of token positions, not per pair of feature dimensions.
"Softmax is applied over the whole scores matrix at once so all entries sum to 1."
The error is the axis — softmax is applied per row, so each query token's weights over the keys sum to 1, giving separate probability distributions.
"We sum the head outputs to keep dimension at ."
The error is summing instead of concatenating — summing vectors of size gives one -vector and destroys the distinct patterns. Concatenation gives while preserving each head.
" has shape ."
Wrong input width — multiplies the concatenation of width , so .
"Adding more heads always improves the model."
The error ignores the trade-off — pushing up shrinks , so each head has too few dimensions to represent anything useful, and quality falls.
"Positional patterns can't be captured because attention is permutation-agnostic, so MHA is useless for order."
Half-true premise, wrong conclusion — attention is order-blind by itself, which is exactly why Positional Encoding is added to inputs first; a head can then learn positional patterns from those encodings.

Why questions

Why do we use different projection matrices per head instead of one big shared projection?
Because different send the same input into different subspaces, so each head can specialise on a different relation type (grammar, meaning, position). Shared matrices would collapse all heads into one.
Why divide by rather than or nothing at all?
As the intuition box shows, a dot product of two random -dimensional vectors (mean 0, variance 1 per entry, independent) has variance , hence typical magnitude ; dividing by rescales this back to variance . Dividing by would instead give variance — the logits shrink toward , softmax becomes nearly uniform, and the head loses its ability to focus. Dividing by nothing leaves variance , so softmax saturates and gradients vanish.
Why concatenate the heads before the final projection rather than projecting each head and adding?
Concatenating first lets learn arbitrary cross-head mixtures — every output dimension can draw on all heads. Projecting-then-adding would fix each head's contribution independently and forbid cross-head interaction.
Why does MHA use the same scaled dot-product attention formula in every head instead of different mechanisms?
The richness is meant to come from the learned projections, not the mechanism. Reusing one formula keeps every head interpretable in the same way and keeps the implementation a single batched operation. (See Linear Projections in Neural Networks.)
Why is needed even though each head already produced a good context vector?
Because the heads sit in disjoint channels of the concatenation; only can blend their specialised outputs into a single unified representation for the next layer.
Why can two heads still learn different things even though they start from the identical input?
Their projection matrices are initialised differently and trained by gradient descent, so they descend into different local specialisations — the shared input is filtered through distinct learned lenses. (See Attention Visualization to observe this.)

Edge cases

What happens when ?
MHA reduces exactly to single-head attention: , the "concatenation" is just the one head, and becomes an ordinary output projection.
What happens as (each head one-dimensional)?
Each head has , so its scores come from a single scalar feature — heads become extremely weak and can barely encode a relationship, degrading quality despite maximal "diversity".
If a query token's scores are all equal across positions, what does that head output?
Softmax gives a uniform distribution, so the head returns the plain average of all value vectors — a "no-preference" state that ignores content and just pools everything.
If one score is vastly larger than the rest, what does softmax do?
It saturates toward a one-hot weight, so the head effectively copies a single token's value — hard attention as a limiting case of soft attention.
In cross-attention where the encoder has tokens (a second sequence length, defined in the symbol box) and the decoder has , what shape are the per-head scores?
is (decoder) and is (encoder), so is — non-square, one row per decoder token attending over encoder tokens. (See Cross-Attention.)
What if (heads project values to a different width)?
Attention still works — only controls the score computation and scaling, while controls the output width. Then the concatenation is , so must have that as its input dimension.
At the very first layer, before any attention has run, where do come from?
From the token embeddings plus Positional Encoding, each linearly projected — there is no "previous layer" yet, so the raw embedded sequence is the shared input to all heads.
Recall Fast self-test

One-sentence answers — cover and recall. Does adding heads increase total attention FLOPs? ::: No — shrinking keeps it . What single matrix lets heads talk to each other? ::: , applied after concatenation. Why not shared projections across heads? ::: They would produce identical, redundant heads. Softmax normalises along which axis? ::: Each query row, over the keys. Which numbers must divide? ::: , so that is a whole number.