Sinusoidal Positions: A Ruler Made of Waves
A learned table stops at its last row. Waves offer a computable position rule. Different frequencies describe different scales, although evaluating a new position does not mean the model understands it.
#Section 21A Ruler Made of Waves
Part III ended with two complaints about the learned table: it stops dead at its last row, and it treats every position as unrelated to its neighbours. In 2017, the paper that introduced the Transformer, Attention Is All You Need by Ashish Vaswani and colleagues at Google, took a different route. Instead of learning position vectors, it calculated them with a formula. A formula can be evaluated at any position, so there is no table to run out of, and a well-chosen formula can build the relationship between neighbours in from the start.
The formula uses sine and cosine waves, the smooth up-and-down curves from school trigonometry. If those feel rusty, here is all you need. A sine wave rises from 0 to 1, falls through 0 to , and comes back, forever, always between and 1. The cosine wave is the same shape shifted by a quarter turn. How fast a wave wiggles is set by its frequency, written (the Greek letter omega). A wave with wiggles quickly; a wave with crawls.
Give each pair of numbers in the position vector its own wave, from very fast to very slow, and to get the vector for position , read off the height of every wave at the point .
Entries come in sine/cosine pairs. Each pair has a fixed frequency, and later pairs vary more slowly. Position determines the angle at which each pair is evaluated.
At one position, evaluate every pair's sine and cosine. Fast waves change strongly between neighboring rows; slow waves change little. The figure shows their combined signature.
The position vector is then added to the token vector, exactly as in Part III: . The only change is where the second vector comes from.
#Section 22The Famous Heatmap
If you search for "positional encoding" you will meet one picture over and over. It comes from drawing many position vectors as rows of coloured cells, one row per position and one column per dimension, with colour showing the value (orange for positive, blue for negative).
The left side flickers because those columns come from fast waves: going down one row moves the wave by a whole radian, which is enough to change its sign within a few positions. The right side looks like solid stripes because those waves are so slow that across 48 positions they barely move. Read any single row and you have one position's vector, the same kind of strip you read off the waves in Section 21. The fast columns tell nearby positions apart. The slow columns tell distant positions apart. Together they give every position a unique fingerprint.
#Section 23Clock Hands and Binary Counting
Treat each (sine, cosine) pair as the tip of a clock hand. The cosine is how far the tip is to the right of the centre and the sine is how far it is up, so the pair always sits on a circle of radius 1. Every time the position goes up by one, hand turns by radians. (A radian is a unit of angle; a full turn is radians.) The first hand spins fast, a whole radian per step. The fourth hand turns only 0.032 radians per step and needs about 200 steps to go once around.
Now compare it with counting in binary, the base-2 counting computers use. In binary, 7 is written 0111. The last digit flips on every step, the next one every two steps, the next every four, and the first every eight. Fast digits track small changes; slow digits track big ones; together they pin down any number. The clock hands do exactly the same job, but smoothly. Instead of jumping between 0 and 1, each hand glides around its circle. That smoothness matters, because a neural network learns more easily from quantities that change gradually.
#Section 24Why Waves, and Why These Speeds?
Why not just use the plain number, 0, 1, 2, 3, as the position? Two reasons. First, scale: by position 1,000 the number would be enormous compared to the token vector's entries, which are around 1, and it would swamp the meaning. Waves always stay between and 1. Second, information: a single number gives the network one dimension to work with, and comparing two positions would require the network to learn subtraction. Waves spread position across many dimensions and, as the next section shows, make comparison almost free.
The geometric ladder
Wavelength is the positional interval for one full cycle. Successive frequencies form a geometric progression, so the pairs cover several distance scales.
That range is the point. Some waves resolve "the word right next to me" and others resolve "something thousands of words back". The number 10,000 is called the base, and it sets how slow the slowest wave is. There is nothing magic about it. Models that need very long contexts raise it so the slowest waves stretch further; Unit II shows Llama 3 using 500,000 for exactly that reason.
#Section 25The Dot Product Only Cares About Distance
A sine/cosine pair compares two angles through their difference. Summing the pairs gives a position-vector comparison that depends on the gap rather than the starting point.
Look for on the right-hand side. It is not there. The similarity between two positions depends only on the gap , never on where you start. "Two words apart" produces the same similarity at the start of a text and deep inside it.
The figure shows similarity across gaps. Nearby positions tend to have similar signatures, but the oscillating waves do not imply strictly monotonic similarity.
A shift is a rotation
Why does cancel? Go back to the clock picture. Moving forward positions turns hand by , whatever angle it started at. So the angle between the hand at position and the hand at position is always . Spin both hands forward together and the angle between them does not change. The dot product of two unit arrows is the cosine of the angle between them, so it does not change either.
This picture leads to Part V. "Moving forward in position is a rotation" is exactly what RoPE takes and applies in a smarter place. Sinusoidal positions have the rotation property but then throw it partly away by adding the position vector to the token vector, which mixes the two and muddies the clean distance-only comparison once the vectors pass through the model's learned weights. RoPE keeps the rotation and never adds.
#Section 26”Works at Any Length” Is Only Half True
Since the formula can be evaluated at any number, sinusoidal positions were often described as working for sequences of any length. Sort of. The model can certainly compute a vector for position 10,000. But if it only ever trained on 1,024 positions, the combinations of wave heights that appear past 1,024 are combinations it has never seen, and every layer has learned to interpret only the ones it saw. In practice, quality degrades past the training length, often quickly. Being able to compute a vector is not the same as understanding it.
Sharp edges of sinusoidal positions
Position and meaning share lanes. The position wave is added on top of the token’s vector, in the same 512 numbers, so every layer has to untangle “what” from “where”. The model can do this, but it is a tax. RoPE avoids the mixing entirely.
Slow lanes are nearly constant, and that is fine. The slowest waves barely change across a normal text, so those entries are almost the same for every position. They are not wasted: the model can use those dimensions mostly for token meaning.
Interleaved or split halves. Some code places sine and cosine side by side (sin, cos, sin, cos, as in the formula). Other code puts all the sines first and all the cosines after. Both are fine, but loading weights trained one way into code written the other way scrambles every position.
Precision at huge positions. At position 100,000 the angle for the fastest wave is 100,000 radians. In 16-bit floating point, numbers that large carry very few digits after the decimal point, so the rounding error in the angle is bigger than the wave’s whole period and the sine comes out as noise. Compute angles in 32-bit precision and only convert the final sine and cosine.