A dot product doesn't know where or sat in the sentence — it's the same number regardless. If attention is built entirely out of dot products, how does it ever learn that "dog bites man" and "man bites dog" aren't the same sentence?
Swap which token comes first. The score computed from content alone never budges — it can't, since nothing about it mentions position at all. The moment position gets added in, the swap changes the number.
Sinusoidal positional encoding builds a vector out of sines and cosines at geometrically spaced frequencies, one pair of dimensions per frequency, and adds it directly to the token's content embedding before anything else happens:
- — the token's position in the sequence:
- — which frequency pair of dimensions this is, out of total pairs.
- — the embedding dimension: how many numbers make up the encoding vector.
- Every dimension pair spins at its own rate
- Pair has a rate of , so it completes a full rotation roughly every positions — fast.
- Pair divides by first, so for the same it's barely turned at all.
Stacking fast and slow pairs together lets nearby positions look different in the fast dimensions while far-apart positions still differ in the slow ones.
- Added, not appended
is added elementwise to the content embedding, so a query·key dot product afterward mixes content and position inseparably — exactly what the intuition demo just showed happening to the score.
- A relative scheme skips the embedding entirely
Instead of writing where a token is into its vector, a relative scheme (used by T5 and Transformer-XL) adds a bias term straight onto the attention score, looked up by the distance between the query and key positions. Slide the whole sequence over and every distance stays the same, so the bias — and the score — doesn't move at all.
The bold curve is the sinusoidal score for a query and key that are always exactly one position apart, as the pair's starting offset slides from 0 to 20 — it wanders, because it reads each token's absolute position. The dashed line is the relative-bias score for that same distance of 1: flat, because distance is all it ever looks at.
Tokens and , content score , with (one fast frequency pair, one slow one):
- Position 0 is the trivial case
and at every frequency, so exactly, regardless of .
- Position 10, then 11 — the sinusoidal score
- At : rate is , rate is , so .
- At : .
Elementwise sums:
Dot product: — close to, but not equal to, the relative score below.
- The relative score for the same pair, distance 1
. This number doesn't depend on which two positions were 1 apart — only that they were.
Find the offset, among the candidates, where the sinusoidal score for a pair one apart lands within 0.05 of the relative score (-0.80).
Both schemes exist to break the symmetry a plain dot product can't break on its own. Sinusoidal encoding bakes absolute position into the content itself, at a range of frequencies chosen so nearby and far-apart positions both stay distinguishable. Relative encoding skips the embedding and biases the score by distance directly, which is what makes it naturally shift-invariant — a property that turns out to matter enormously once a model needs to handle sequences longer than the ones it was trained on. The next two chapters build on that absolute/relative split directly: first rotating query and key vectors so relative position falls out of the dot product for free, then stretching that rotation scheme to contexts far beyond what it ever saw during training.