Positional Embeddings
In the transformer reading, we let each token draw on the tokens before it. The causal mask tells us which words came earlier. To recognize a phrase, we often want to know which earlier word came immediately before the one we’re reading. The mask gives a preceding word and a distant one the same permission to contribute.
We’ll start by giving each position its own vector. Then we’ll look at a way to put the distance between two positions directly into their attention score.
Absolute Position
A word gets the same embedding wherever it appears. How can we distinguish its first occurrence from its second? In Dr. Seuss’s One Fish Two Fish Red Fish Blue Fish, each fish has the same word embedding, even though a different word describes it each time.
An absolute positional embedding gives position \(i\) a vector \(\mathbf p_i\in\mathbb R^d\). We add it to the word embedding \(\mathbf x_i\) before the first transformer block:
\[ \underbrace{\mathbf x_i}_{\text{word}}+\underbrace{\mathbf p_i}_{\text{position}}. \]
The first fish gets \(\mathbf p_2\) and the next one gets \(\mathbf p_4\). The model can learn these vectors during training, as GPT-2 does, or we can choose them using a fixed formula. A learned table needs an entry for each position the model will use.
Probing Position
We only add the position vector once. After several blocks have updated the residual stream, can we still tell where a word appeared? We can test this with a linear probe. Given a token’s vector \(\mathbf h\) at one stage of GPT-2, the probe gives a score to each of the \(n\) possible positions and chooses the largest:
\[ \widehat i=\underset{1\leq j\leq n}{\arg\max}\;(\mathbf w_j^\top\mathbf h+b_j). \]
We freeze GPT-2 and fit a separate probe at each stage by regularized least squares, using the one-hot position as the target. We train on shuffled lists of eight words and test on new words. Each word appears equally often in every position. That last detail matters. If the probe only sees a word’s embedding, it has to make the same guess every time that word appears, so it gets exactly one in eight right.
Each row gives the predictions for words at one position. Correct position predictions lie on the diagonal.
Choose Input to see what happens when we add the position vector. Each row of the matrix shows the probe’s guesses for one true position, so a correct guess falls on the diagonal. Adding position raises the accuracy on held-out words from \(12.5\%\) to \(88.3\%\). At the final stage the probe still recovers the position \(49.2\%\) of the time.
Relative Position
Knowing the position of each fish is one way to find its description. There’s a simpler rule that works throughout the line. From fish, look back one word. Whether we find red or blue, the relative position of the description is the same: if fish is at \(i\) and its description is at \(j\), then \(j-i=-1\).
We’d like an attention head to learn this rule once and use it anywhere in the sequence. How could we make its score depend on \(j-i\) when the query knows only its own position \(i\) and the key knows only \(j\)?
Rotary Embeddings
Recall that a dot product depends on the angle between two vectors. If we turn both vectors by the same angle, their dot product stays the same. Rotary positional embeddings, or RoPE, use this fact to put the offset into the query-key comparison.
Let’s start with a query and key in two dimensions. Choose an angle \(\theta\) in radians per position, and write \(\mathbf R_i\) for a rotation by \(i\theta\). At position \(i\) we rotate the query, and at position \(j\) we rotate the key. We leave the values alone. The rotation and the resulting score are
\[ \mathbf R_i=\begin{bmatrix}\cos(i\theta)&-\sin(i\theta)\\\sin(i\theta)&\cos(i\theta)\end{bmatrix}, \qquad s_{ij}=\frac{1}{\sqrt{2}}(\mathbf R_i\mathbf q_i)^\top(\mathbf R_j\mathbf k_j). \]
Claim: For fixed query and key vectors, the rotary score depends only on their relative position: \[ s_{ij}=\frac{1}{\sqrt{2}}\mathbf q_i^\top\mathbf R_{j-i}\mathbf k_j. \]
Proof
The transpose of a rotation undoes it. Multiplying by \(\mathbf R_i^\top\mathbf R_j\) first turns a vector by \(j\theta\) and then turns it back by \(i\theta\). The net angle is \((j-i)\theta\), so \[ (\mathbf R_i\mathbf q_i)^\top(\mathbf R_j\mathbf k_j) =\mathbf q_i^\top\mathbf R_i^\top\mathbf R_j\mathbf k_j =\mathbf q_i^\top\mathbf R_{j-i}\mathbf k_j. \] Dividing by \(\sqrt{2}\) gives the score. Setting \(j=i\) gives \(\mathbf R_i^\top\mathbf R_i=\mathbf I_2\), so the rotations also preserve vector lengths.
In the figure, the query starts at the fish after red, with the key one position to its left. Shift both positions twice to reach blue fish. Both arrows turn, but the angle between them stays the same. The matrix uses the same query and key vectors at every position so we can isolate the effect of distance. Follow any diagonal: \(j-i\) stays constant, so the score does too. A matrix with constant diagonals is called Toeplitz.
Rotating a query by iθ and a key by jθ makes their dot product depend on (j − i)θ. With identical content, equal offsets give equal matrix entries.
In the Seuss line, red and blue supply different keys. Comparing each with the fish that follows uses the same rotation \(\mathbf R_{-1}\), but the scores can differ because the keys themselves differ. The demo compares fixed and changing content.
Rotation Frequencies
How much should a vector turn for each step along the sequence? A large turn makes neighboring positions easy to distinguish, but the angle soon wraps around. A small turn changes little from one word to the next, but accumulates over a longer passage. A wider head lets us use both.
For an even head width \(d\), group the coordinates into \(d/2\) pairs. Each pair rotates in its own plane. A common choice gives pair \(k\) the angular frequency \[ \theta_k=B^{-2(k-1)/d},\qquad k=1,\ldots,d/2,\quad B>1. \] The first pair turns by one radian per position. Each successive pair turns more slowly. The frequencies fall on a straight line in the figure because the vertical scale is logarithmic.
Let \(\boldsymbol\Theta_i\) collect the rotations at position \(i\) into a block-diagonal matrix. Since each pair rotates separately, the same angle subtraction holds in every block: \[ \boldsymbol\Theta_i^\top\boldsymbol\Theta_j=\boldsymbol\Theta_{j-i}, \qquad s_{ij}=\frac{1}{\sqrt d}\mathbf q_i^\top\boldsymbol\Theta_{j-i}\mathbf k_j. \]
The dot product adds a contribution from each pair. During training, the query and key projections learn which features to put in the fast and slow pairs. We can compute these rotations at any distance. How well a trained head uses distances beyond those it saw in training is a separate question we can test.