4.1.4 · D5Transformer Architecture

Question bank — Scaled dot-product attention

1,470 words7 min readBack to topic

Before we start, one shared vocabulary so nothing here is used before it is defined:

Related notes you may want open: Softmax Function, Self-Attention, Cross-Attention, Multi-Head Attention, Query-Key-Value Model, Attention Mask, Gradient Descent, Positional Encoding.


True or false — justify

Scaling by changes which key wins the softmax.
False. Scaling multiplies every logit in a row by the same constant , so their order is preserved — the largest logit stays largest. It changes how sharp the distribution is, not who wins.
Softmax weights in one row of always sum to 1.
True by construction: dividing each by the row sum forces the row to add to 1. That is why each row is read as a distribution over keys.
If two keys have identical vectors, a query must attend to them equally.
True — identical keys give identical logits against any query, and softmax maps equal logits to equal weights. The value vectors behind them can still differ, so the outputs they contribute differ.
The scaling factor is a learned parameter tuned by gradient descent.
False. is a fixed constant chosen from the variance argument; nothing about it is learned. Only are learned.
Making larger always makes attention more expressive.
False. Larger adds parameters and widens the variance of dot products, inviting overfitting and more extreme outliers. Transformers prefer many heads with small over one head with huge .
Attention output has the same number of columns as the value dimension .
True. The output is where is and is , giving — the key/query dimension has already been "summed away" inside .
Removing the scaling but using float128 precision fully fixes the problem.
False. High precision stops overflow, but the real damage is gradient quality: large logits still push softmax into saturated flat regions where learning stalls. See Gradient Descent.
In self-attention, , , and are the same matrix.
False. They all come from the same input , but through different weight matrices: , , . So a token can ask a different question than it answers.
A dot product of zero between a query and key means they are opposites.
False. Zero means the vectors are orthogonal (perpendicular), a neutral "unrelated" score. Opposite directions give a large negative dot product.
Softmax is invariant to adding the same constant to every logit in a row.
True. , and the cancels between numerator and row-sum. This is why we can subtract the row max for numerical stability without changing the result.

Spot the error

"Var, so we divide by to normalise it."
The variance is but the standard deviation is , and we scale by standard deviation to bring the spread back to 1. Dividing by over-shrinks the logits, flattening them toward a uniform distribution.
" has shape when is and is ."
Wrong shape. is , so is — a score for every query–key pair. The dimension is contracted away by the dot product.
"We apply softmax to the whole score matrix at once as a single distribution."
Softmax is applied row-wise: each query gets its own distribution over the keys. A single global softmax would let unrelated queries compete against each other.
"Because dot product is symmetric, , so query and key roles are interchangeable."
and are transposes of each other, not equal, and they use different learned weights . The query/key roles are not interchangeable even in Self-Attention.
"An Attention Mask sets forbidden weights to 0 after softmax."
It sets forbidden logits to before softmax, so and the row still renormalises to sum to 1. Zeroing after softmax would break the distribution (rows would no longer sum to 1).
"Scaling and the number of heads are unrelated design choices."
They are linked: multi-head attention splits the model dimension into smaller per-head , and each head still scales by its own . See Multi-Head Attention.

Why questions

Why use a dot product instead of, say, Euclidean distance, for compatibility?
The dot product is cheap (), differentiable, and directly measures directional alignment in embedding space — large when vectors point the same way. It plugs straight into fast matrix multiplication ().
Why does softmax saturation hurt learning specifically?
A saturated softmax output one-hot has gradient , so backprop sends almost no signal to and . The model cannot tell that one key is slightly better than another.
Why does variance of grow with ?
It is a sum of independent zero-mean products, each contributing variance 1, and independent variances add. More terms means more accumulated spread, hence variance .
Why compute all queries at once as matrices rather than looping?
GPUs/TPUs are optimised for dense matrix multiplication, so batching every query into is dramatically faster than one-at-a-time loops — same maths, hardware-friendly form.
Why multiply attention weights by rather than by ?
Keys are the "address labels" used only to score relevance; values are the actual content to retrieve. The output is a soft-blended answer, so we mix the values weighted by how relevant each key was.
Why can the same architecture serve both self- and cross-attention?
Only the sources of , , change: Self-Attention draws them from one sequence, Cross-Attention draws from the decoder and from the encoder. The scaled-dot-product computation is identical.
Why is positional information needed on top of attention?
Attention weights depend only on content compatibility, so permuting the tokens permutes the outputs identically — order is invisible. Positional Encoding injects order so "dog bites man" differs from "man bites dog".

Edge cases

What are the attention weights if all logits in a row are equal?
Equal logits give a uniform distribution: each of the keys gets weight . The output is then a plain average of all value vectors.
What happens to a row that an Attention Mask blocks entirely?
Every logit becomes , so softmax is — undefined. In practice such rows must never occur (a query always sees at least one key, e.g. itself in causal masking).
What is the output when there is exactly one key ()?
Softmax of a single logit is always , so the output equals that one value vector regardless of the score. There is no competition when there is nothing to compete against.
As with unscaled logits, what does softmax approach?
The logit spread grows without bound, so softmax collapses toward a one-hot vector on the single largest score — near-total saturation. Scaling by is precisely what prevents this collapse.
What if a query vector is exactly the zero vector?
Its dot product with every key is 0, so all logits are equal and softmax gives a uniform average of the values — the query expresses no preference. This is the degenerate "no question asked" case.
If two rows of are identical, what about their outputs?
Identical queries produce identical logit rows, identical softmax weights, and therefore identical output rows. Two tokens asking the same question retrieve the same blended answer.
What does a very large negative logit contribute to its softmax weight?
, so that key receives essentially zero attention weight and its value barely enters the output. This is exactly how masking drives weights to 0.