Answers · How models work
Why does attention need positional encoding if it already looks at the whole sequence at once?
In shortAttention treats tokens as an unordered set, so position has to be added to the vectors; otherwise a sentence and its anagram get the same representations.
Attention sees the whole sequence at once, but it sees it as a set: every score is a dot product and every output is a weighted sum, and neither operation refers to an index. Strip out position and "dog bites man" and "man bites dog" give the same three representations, merely placed in whichever slots the words occupy, so order has to be injected into the vectors themselves.
The computation has no index in it
Write the token vectors as rows of . The score from token to token is ; it uses the two vectors and nothing else. Softmax normalises those scores across , and a sum does not care in what order its terms are added. The output for token is then , another sum. Reorder the rows of by a permutation and the scores are the same numbers with rows and columns relabelled, the same softmax weights follow, and the outputs are the original outputs with rows relabelled.
| Order | Position 1 | Position 2 | Position 3 |
|---|---|---|---|
| Order A | dog | bites | man |
| Order B | man | bites | dog |
Take the two orders in the table. If the three content vectors are for dog, for bites and for man, bare attention returns for order A and for order B. The vector attached to dog is identical in both, so nothing downstream can tell whether dog is the biter or the bitten. The three vectors are not scrambled; they are shuffled exactly as the input was. That is what permutation equivariance means, and it is why the mechanism cannot tell a sentence from an anagram of it.
Why the obvious repairs fail
Appending the raw index as one extra component looks like a direct fix and is not. Suppose the content has three dimensions with components of size at most 1, so the content part of a dot product is at most 3. Append the position to both query and key; the position part of the score is . When both sides carry 500 that term is , which is times the largest possible content contribution. At positions 1 and 500 it is , still times as large. The position dimension swamps the content, and because has no upper bound a longer sequence makes the swamping worse without limit.
Dividing the index by the sequence length fixes the scale but destroys meaning. With positions counted 0 to divided by , the value 0.5 is token 5 in a ten-token sentence () and token 250 in a five-hundred-token document (). One step forward is in the first and in the second, so "the previous token" has no fixed representation. Learning one embedding per position avoids both problems and is used in practice, but it has a hard ceiling: position 1024 has no embedding if training stopped at 1023, and nothing about it can be inferred from the other independent parameters.
What a working encoding does
Sinusoidal encodings add a vector of fixed sines and cosines to each token embedding. Every component lies in , so position cannot dominate content, and the construction is defined for every integer position, so no maximum length is baked in. The property that earns its place is this: for any fixed offset there is a matrix , depending on alone, such that . For one frequency pair this is the angle addition formula written as a rotation. Take frequency radians per position and offset ; then radians, with and . Moving three positions forward rotates that pair by a fixed radians, the same rotation everywhere.
Rotary encodings apply the rotation later, to the query and key vectors themselves, rather than adding anything to the input. Rotations preserve lengths and angles under a shared rotation, and the score between positions and becomes . Absolute position has cancelled: the score for two tokens five apart is computed the same way whether they sit at positions 3 and 8 or at positions 3003 and 3008, since and . That is why relative distance is built in rather than learned separately at every absolute position. It does not make length generalisation free. Models trained on short contexts still degrade on much longer ones, and the usual fixes rescale or interpolate the rotation frequencies; the difference is that the failure is a matter of degree, not the complete absence of a representation for unseen positions.
Worth keeping
- Attention's output is a weighted average of value vectors, so permuting the input permutes the output and changes nothing else.
- Position must enter through the token vectors or through a rotation of the queries and keys, because no index appears in the score or the softmax.
- Sinusoidal encodings make a fixed offset a fixed rotation, which lets a head express the rule "k tokens back" as one linear operation.
Where this goes next
This question is one step of The Mathematics of Attention, a course that works through its subject from the beginning. The page that teaches this part of it is Position Without Recurrence, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- What do multiple attention heads actually give you that one big head doesn't?Several heads let a model track several relationships at once, each in its own lower-dimensional subspace, instead of squeezing every relationship into one weighted average that destroys information.
- Why do we divide the attention scores by the square root of the key dimension?The square root of the key dimension is the typical size of an unscaled attention score, so dividing by it stops softmax from becoming a hard pick-one.
- Why does attention use softmax instead of just dividing each score by the sum of all the scores?Dividing by the sum can give negative weights and fails when scores cancel; exponentiating first makes weights positive and turns score gaps into ratios.
- How does masking actually make attention causal?The mask is applied to the scores before they become weights, so forbidden positions end up with exactly zero weight and the rest are renormalised automatically.
- Why does the model get stuck repeating the same sentence over and over?The model is not stuck in a memory loop: once it has written a phrase, that phrase is in its context and makes writing it again more likely, so a decoder that always picks the top word cannot leave.
- Why does diffusion add Gaussian noise instead of any random corruption?Diffusion adds Gaussian noise because Gaussian errors combine exactly: each step keeps the spread fixed and gives a closed-form noisy picture at any later step, which makes the reverse step learnable.