- Key idea
- Self-attention is permutation-equivariant; fix it by adding a position vector to each token (learned, as in GPT-2, or sinusoidal) or by encoding relative position inside attention (RoPE, ALiBi)
- Real world
- Scrabble tiles in a bag. The bag holds the same letters whether they spell “listen” or “silent”. To read a word you need the tiles laid out in order on the board.
- In an LLM
- “dog bites man” and “man bites dog” have exactly the same tokens. GPT-2 tells them apart by adding a learned vector for position 0, 1, 2 to each token’s embedding before the first layer.
Attention ignores order
Look again at what attention computes. Each token’s output is a weighted average of the other tokens’ values, with weights from query–key dot products. Nothing in that recipe refers to where a token is. If you shuffle the input rows, every query, key and value is shuffled the same way, the scores are shuffled, and each token’s output row is exactly what it was before, just in a new place. Mathematicians call this permutation equivariance; for a language model it means the input is a bag of words.
See it with the toy attention head:
The causal mask does break the symmetry a little, because a token only sees the tokens before it, so moving it changes what it can see. Models trained with no position information at all (“NoPE”) can learn some sense of position this way. But it is indirect, and every major model adds a explicitly.
Option 1: learn a vector per position
The simplest fix: a second embedding table, with one row per position instead of one per token. GPT-2 has 1,024 positions, so its position table is 1,024 × 768. The input to the first layer is the sum of the two lookups:
Now “ cat” at position 1 and “ cat” at position 5 are different vectors, and so are their queries and keys. Here is GPT-2’s real position table:
GPT-2 was never told that position 21 follows position 20; it learned 1,024 independent vectors. Training still made them smooth: neighbouring positions are very similar, and similarity fades with distance. Some columns drift slowly with position, like waves.
Learned positions have one hard limit: there is no row for position 1,024. GPT-2 simply cannot read longer texts, and positions near the end of the table, seen less often in training, are less well trained.
At a concert, everyone holds a ticket with a seat number. The people are the same whichever seats they sit in, but the numbers make the order clear. A learned position table is a sheet of seat labels that the model designs for itself; it just has no labels beyond the last row of the hall.
Without positions, GPT-2 would see the same three vectors for “dog bites man” and “man bites dog”, and would have to predict the same next word after both. With position vectors added, “ dog” at position 0 differs from “ dog” at position 2, so the attention heads can work out who did what to whom.
Option 2: sine waves
The original transformer (Vaswani et al., 2017) used fixed vectors instead of learned ones. Each position becomes numbers: pairs of sine and cosine at different speeds. Each pair is like a clock hand turning at its own rate as the position grows. The fastest hand turns 1 radian per position; each next one is slower, down to one that barely moves over thousands of positions. Together, the angles of all the hands identify the position, like the hour, minute and second hands of a clock.
Two properties made this attractive:
- It is defined for every position, even ones longer than any training text (though models did not actually work well beyond their training length).
- Moving by a fixed offset is a rotation. Shifting from position to turns every pair by an angle that depends only on , so the dot product between two position vectors depends only on how far apart they are. The similarity curve in the widget has the same shape wherever you put it.
Option 3: rotate the queries and keys
That second property is the seed of RoPE (rotary position embedding), which almost every model since 2023 uses: Llama, Qwen, Gemma, DeepSeek, Mistral. Instead of adding a position vector to the input, RoPE rotates each query and key by angles that depend on its position. Then the attention score between two tokens depends only on their relative distance, which is what language mostly cares about. It has its own page: RoPE: positions as rotations.
Another relative scheme, ALiBi (Press et al., 2022), adds no vectors at all. It subtracts a penalty from each attention score in proportion to the distance between the two tokens, so distant tokens get less attention by default. It was used in BLOOM and MPT.
| Method | Where position enters | Used in |
|---|---|---|
| Learned absolute | Added to the input, one vector per position | GPT-2, GPT-3, BERT |
| Sinusoidal | Added to the input, fixed waves | Original transformer (2017) |
| RoPE | Rotates queries and keys in every layer | Llama, Qwen, Gemma, DeepSeek, Mistral |
| ALiBi | Distance penalty on attention scores | BLOOM, MPT |
| None (NoPE) | Only through the causal mask | Some layers of some models; research |
Permutation equivariance, formally
Let be a permutation matrix and the input. Without a mask,
because and the row-wise softmax commutes with reordering rows and columns. The MLP and layer norm act on each row separately, so the whole unmasked transformer is equivariant too.
The sinusoidal formula
For position and pair :
Then , which depends only on .
In papers
- Sinusoidal encoding: Vaswani et al., 2017, section 3.5.
- Learned positions in GPT-2: Radford et al., 2019.
- RoPE: Su et al., 2021.
- ALiBi: Press, Smith and Lewis, 2022.
- Language models without positional encodings still learn position: Haviv et al., 2022.