Why anything is needed
Self-attention is permutation-equivariant: shuffle the input and the outputs shuffle with it, unchanged.
So order has to be added to the representation itself, before attention ever sees it.
- : the position index.
- : a vector of the same width as the embedding.
It is added, not concatenated. That keeps the width fixed and lets the model decide how much of each dimension to spend on position.
The sinusoidal scheme
- : which pair of dimensions, from 0 to .
- : the model width.
Each pair of dimensions is a sine and cosine of the same frequency, and the frequency falls geometrically across the pairs.
The fastest pair alternates every couple of positions. The slowest has a wavelength of about 60,000, longer than any sequence it will see.
Together they encode position the way a binary counter does, with each dimension flipping at a different rate.
Why sinusoids rather than just the index
Feeding in raw would be unbounded and would dominate the embedding at long positions.
The real argument is that a fixed offset becomes a linear transformation.
- : a rotation matrix depending only on , not on .
That follows from the angle-addition identities, and it means the model can learn to attend "three positions back" as one linear operation that works everywhere in the sequence.
It also extrapolates in principle: the function is defined for any , including positions longer than anything in training. In practice that extrapolation degrades quickly.
One forward pass, in order
- Look up the token embedding for each position.
- Compute or look up the positional vector for that index.
- Add the two together.
- Pass the sum into the first attention layer.
- Do this once at the bottom, not at every layer.
What replaced it
Learned absolute embeddings simply train a vector per position, which works as well within the training length and not at all beyond it.
Rotary embeddings rotate the queries and keys instead of adding to the input, which makes the dot product depend on relative position directly.
That last property is why almost every recent model uses rotary rather than sinusoidal, and why sinusoidal is now mostly of historical interest.