How Positional Encoding Works
Part 3 showed that self-attention cannot see word order, and that nothing later in the model can put it back. This part covers the fix. It works through the sine and cosine formula, where the result is added, and why it lets the model use the distance between two words. It ends with the rotary version that modern LLMs use instead.
4.1The sine and cosine solution
Part 3, Section 3.7 found two problems with a plain counter. Its numbers grow without limit. And it fails the requirement set out in that section: no part of its score can depend on the relative position \(m - n\) alone. The Transformer paper uses sine and cosine to avoid both problems.
Here is the formula for filling in one positional encoding. The formula looks hard, but in a moment you will fill in a whole vector by hand, and then it will look simple.
\[ \begin{aligned} \text{PE}_{\text{pos},\,2i}&=\sin\!\left(\text{pos}\,/\,10000^{2i/d_{\text{model}}}\right)\\[1ex] \text{PE}_{\text{pos},\,2i+1}&=\cos\!\left(\text{pos}\,/\,10000^{2i/d_{\text{model}}}\right) \end{aligned} \]Reading the formula
The formula names two different things, and they are easy to mix up.
\(\text{pos}\) is which word, counting from 0. In I love LLMs,
I is at \(\text{pos} = 0\), love at \(\text{pos} = 1\),
LLMs at \(\text{pos} = 2\).
\(2i\) and \(2i+1\) are which number inside that positional encoding, also counting from 0. These are the slots defined in Part 3, Section 3.2. A vector of 4 numbers has slots 0, 1, 2 and 3, and these are the labels across the top of Figure 2.
Why write "slot \(2i\)" and "slot \(2i+1\)" instead of just "slot \(j\)"? Because that notation answers two questions at once:
- Which function to use. \(2i\) is always an even number and \(2i+1\) is always odd, so: even-numbered slots get a sine, odd-numbered slots get a cosine.
- What \(i\) is. Whichever slot you are filling in, \(i\) is that slot number divided by 2, throwing away any remainder. Slots 0 and 1 both give \(i = 0\); slots 2 and 3 both give \(i = 1\).
For a vector of 4 numbers that works out as:
| slot | even or odd | function | so \(i\) is |
|---|---|---|---|
| 0 | even, so it is \(2i\) | \(\sin\) | \(0 / 2 = 0\) |
| 1 | odd, so it is \(2i+1\) | \(\cos\) | \(1 / 2 \to 0\) |
| 2 | even, so it is \(2i\) | \(\sin\) | \(2 / 2 = 1\) |
| 3 | odd, so it is \(2i+1\) | \(\cos\) | \(3 / 2 \to 1\) |
✗ Common mistake It is tempting to read \(i\) as the slot number, but that is wrong. \(i\) is the slot number divided by 2, with the remainder thrown away. So in a vector of 512 numbers the slots run from 0 to 511, but \(i\) only runs from 0 to 255. For the last slot, slot 511, the power of 10000 is \(2 \times 255/512\), not \(2 \times 511/512\).
Working out I love LLMs, number by number
Let \(d_{\text{model}} = 4\), so every embedding vector is 4 numbers long and every positional encoding has to match. Three words means three positional encodings to build, four numbers each.
Start with the divisor, \(10000^{2i/d_{\text{model}}}\), because it doesn't depend on the word at all — only on \(i\). You have two values of \(i\) to cover:
\[ \begin{aligned} i = 0:\quad & 10000^{2 \times 0 / 4} = 10000^{0} = 1\\[4pt] i = 1:\quad & 10000^{2 \times 1 / 4} = 10000^{0.5} = 100 \end{aligned} \]Reading that back through the table: slots 0 and 1 divide the position by 1, and slots 2 and 3 divide it by 100. Those divisors are fixed now, for every word in the sentence. The only thing left to change is \(\text{pos}\).
I, at \(\text{pos} = 0\):
love, at \(\text{pos} = 1\):
LLMs, at \(\text{pos} = 2\):
That is twelve numbers, each a sine or a cosine. Stack the three vectors and you have the positional encodings for the whole sentence:
\[ \begin{aligned} \texttt{I}\quad &\to\quad (\,0.00,\ \ 1.00,\ \ 0.00,\ \ 1.00\,)\\[4pt] \texttt{love}\quad &\to\quad (\,0.84,\ \ 0.54,\ \ 0.01,\ \ 1.00\,)\\[4pt] \texttt{LLMs}\quad &\to\quad (\,0.91,\ -0.42,\ \ 0.02,\ \ 1.00\,) \end{aligned} \]You have just built the first three rows of Figure 2, a little further down. Each slot keeps its own function and its own divisor for the whole sentence. The position is the only number that changes from row to row.
Think for a moment Compare slot 0 with slot 2 down the three words. Slot 0 goes 0.00, 0.84, 0.91. Slot 2 goes 0.00, 0.01, 0.02. Why is one moving so much faster than the other, when both are sines?
The divisor is the only difference. Slot 0 divides the position by 1, so its sine goes through a full cycle in about 6 words, because \(2\pi \approx 6.28\). Slot 2 divides the position by 100, so its sine needs about 600 words for the same cycle. Over three words it has hardly moved from 0.
Four numbers give only these two speeds. Figure 1 uses a vector of eight numbers instead, so that four pairs fit. It draws each pair as one hand of a clock. Each hand is labelled with a number \(\theta\), which says how far that hand turns each time the position goes up by one.
Each hand's \(\theta\) is 1 divided by that pair's divisor. Write \(\theta_i\) for the \(\theta\) of pair \(i\), the pair that holds slots \(2i\) and \(2i+1\):
\[ \theta_i = \frac{1}{10000^{2i/d_{\text{model}}}} \]The divisor is 1 for slots 0 and 1, and it grows towards 10000 as \(i\) grows. With our four slots it reaches 100, so \(\theta_0 = 1/1 = 1\) and \(\theta_1 = 1/100 = 0.01\). So each pair turns more slowly than the pair before it, and that is the whole purpose of the divisor.
"Fast" and "slow" describe pairs: how much a pair's angle changes when the position goes up by one. That speed decides which positions the pair can tell apart. Pair 0 turns 1 radian, about 57°, per step, so positions 3 and 4 are 57° apart on its circle and easy to tell apart. But it goes round once every 6.28 positions, so positions 3 and 9 are only 16° apart. Pair 1 turns only 0.01 radian per step: positions 3 and 4 are only 0.6° apart, but positions 3 and 300 are 170° apart. So the fast pair separates neighbouring positions, and the slow pair separates distant ones.
Dividing the position by the divisor is the same as multiplying it by \(\theta_i\). So the formula at the top of this section can also be written like this:
\[ \begin{aligned} \text{PE}_{\text{pos},\,2i}&=\sin(\text{pos} \times \theta_i)\\[1ex] \text{PE}_{\text{pos},\,2i+1}&=\cos(\text{pos} \times \theta_i) \end{aligned} \]In words: at position \(\text{pos}\), pair \(i\) has turned through the angle \(\text{pos} \times \theta_i\), and its two slots hold the sine and the cosine of that angle.
Sine and cosine always lie between \(-1\) and \(1\). So every value in a positional encoding stays between \(-1\) and \(1\), whatever the position: at position 3 and at position 3000 alike. Unlike a counter, the encoding never grows large enough to drown out the embedding it is added to. That solves the counter's first problem, and Section 4.3 shows how the same waves solve the second.
Key takeaway
- A positional encoding is built from sine and cosine waves. Slot \(2i\) gets a sine, slot \(2i{+}1\) gets a cosine, and the two slots of pair \(i\) use the same \(\theta_i\).
- \(\theta_i\) is 1 divided by \(10000^{2i/d_{\text{model}}}\). The first pair turns fast and tells neighbouring positions apart; the later pairs turn slowly and tell distant positions apart.
- Every value stays between \(-1\) and \(1\), whatever the position, so the encoding can be added to a word's embedding without drowning it out.
Try it (4 minutes): With \(d_{\text{model}} = 4\) there are two pairs of slots. Work out the encoding for position 3 by hand. Then check one thing about your answer: slots 0 and 1 are the sine and the cosine of the same angle, so squaring them and adding should give 1. Do the same for slots 2 and 3.
4.2Where and how is it added?
The Transformer uses the positional encodings in the simplest possible way: it adds each one to the embedding vector of the word at that position.
The addition happens once, at the very bottom of the model. Each word is first turned into its embedding vector, the positional encoding is added, and only then does anything enter the first self-attention layer.
The addition is done slot by slot. The embedding vector says what the word means,
the positional encoding says where the word is, and the two have exactly the same
number of slots. So slot 0 is added to slot 0, slot 1 to slot 1, and so on:
embedding vector + positional encoding.
The sum is the input vector of Part 3, Section 3.5: what the word means plus where it sits. The input vector is the only thing the first attention layer ever sees.
Why addition instead of concatenation?
Does adding the positional encoding to the embedding vector damage the word's meaning? And why not concatenate the two vectors instead, that is, place them side by side in one longer vector?
Concatenation is not wrong, but it is expensive. Put a 512-number positional encoding next to a 512-number embedding vector, and every vector in the model becomes 1024 numbers long instead of 512. Every weight matrix after that point doubles in width. A cheaper version keeps the total at 512 and splits it, say 384 numbers for meaning and 128 for position. That works too, but those 128 slots can then never be used for meaning.
Addition costs nothing extra. But does it damage the embedding vector? To answer that, look at what happens to the input vector next. The first thing that touches it is a weight matrix: \(\mathbf{W}^Q\), \(\mathbf{W}^K\) or \(\mathbf{W}^V\). Take \(\mathbf{W}^Q\).
With concatenation, the input vector is \([\mathbf{e};\ \mathbf{p}]\): the embedding vector \(\mathbf{e}\) followed by the positional encoding \(\mathbf{p}\). Split \(\mathbf{W}^Q\) into two blocks of rows, \(\mathbf{W}_e\) for the rows that meet \(\mathbf{e}\) and \(\mathbf{W}_p\) for the rows that meet \(\mathbf{p}\). Then
\[ [\mathbf{e};\ \mathbf{p}]\,\mathbf{W}^Q = \mathbf{e}\,\mathbf{W}_e + \mathbf{p}\,\mathbf{W}_p \]So concatenation is also an addition. The meaning is projected by one matrix, the position by another, and the two results are added.
With addition, the input vector is \(\mathbf{e} + \mathbf{p}\), and
\[ (\mathbf{e} + \mathbf{p})\,\mathbf{W}^Q = \mathbf{e}\,\mathbf{W}^Q + \mathbf{p}\,\mathbf{W}^Q \]The two lines have the same shape. The only difference is that concatenation gives meaning and position a matrix each, and addition makes them share one.
So the question "does addition damage the meaning?" becomes: how much does the model
lose by sharing one matrix? Figure 2 shows where to look. The first two columns change a
lot from one row to the next: 0.00, 0.84, 0.91,
0.14. In those slots, meaning and position really are mixed, and nothing can
separate them again. The last two columns hardly change: sin(pos/100) goes
only from 0.00 to 0.03, and cos(pos/100) stays at
1.00 the whole way down. In a real model with \(d_{\text{model}} = 512\),
the last pair changes even less: across a 50-word sentence its sine changes by about
0.005 and its cosine by about 0.00001. A slot that adds almost
the same number to every word does not change the differences between words, so it does
not damage the meaning; it also carries almost no position, so it is free for meaning.
The mixing happens only in the fast slots. Whether it does harm cannot be worked out on
paper; in practice it does little harm, and that is why addition is used. Figure 3 shows
the same pattern over fifty positions instead of four.
Figure 4 shows where in the model the addition happens.
✗ Common mistake The later slots look flat in Figure 2, so it is tempting to read them as all zeros, the empty space left for meaning, but that is wrong. Half of them are sines near0.00, but the other half are cosines near1.00. What is small is not the value but the change from one position to the next, and that is why these slots carry almost no position.
Key takeaway
- Positional encodings are added to the embedding vectors once, at the bottom of the model, before the first attention layer sees anything.
- Addition is chosen over concatenation because it costs no extra width. Concatenation followed by a weight matrix is also an addition, \(\mathbf{e}\,\mathbf{W}_e + \mathbf{p}\,\mathbf{W}_p\); plain addition makes the two share one matrix. Meaning and position are then mixed only in the fast slots, and Figure 2 shows that this costs little.
4.3Why sine and cosine make relative position easy
The original Transformer paper says the authors chose sine and cosine because they expected this choice to let the model "easily learn to attend by relative positions". What does that mean?
Write \(\mathbf{p}_{\text{pos}}\) for the positional encoding of the word at position \(\text{pos}\): the vector whose slots Section 4.1 filled in. Suppose you know \(\mathbf{p}_2\) and want \(\mathbf{p}_5\), the encoding three positions later. Because the encoding is made of sines and cosines, there is a fixed matrix that turns \(\mathbf{p}_2\) into \(\mathbf{p}_5\). The same matrix also turns \(\mathbf{p}_{10}\) into \(\mathbf{p}_{13}\) and \(\mathbf{p}_{100}\) into \(\mathbf{p}_{103}\): it moves any encoding 3 positions forward, wherever it starts. And that matrix is a rotation matrix.
The matrix moves the slots two at a time. Recall from Section 4.1 that the slots come in pairs: slots 0 and 1 are pair 0, slots 2 and 3 are pair 1, and in general pair \(i\) is slots \(2i\) and \(2i + 1\). Pair \(i\) holds \(\sin(\text{pos} \times \theta_i)\) and \(\cos(\text{pos} \times \theta_i)\). In our four-number encoding, pair 0 holds \(\sin(\text{pos})\) and \(\cos(\text{pos})\), and pair 1 holds \(\sin(\text{pos} \times 0.01)\) and \(\cos(\text{pos} \times 0.01)\).
Write \(\phi\) for the number of positions to move forward. Moving pair \(i\) forward by \(\phi\) positions is the same as multiplying it by this small matrix:
\[ \begin{pmatrix} \cos(\theta_i \phi) & \sin(\theta_i \phi) \\ -\sin(\theta_i \phi) & \cos(\theta_i \phi) \end{pmatrix} \]The check takes three lines, using the addition formulas for sine and cosine that you may remember from school, and it is in Appendix A. Every entry of this matrix is built from \(\phi\) and \(\theta_i\). The starting position \(\text{pos}\) does not appear anywhere, which is why one matrix works wherever you start. Each pair has its own matrix of this kind, with its own \(\theta_i\).
Why is this matrix called a rotation? Take pair 0 of our four-number encoding, where
\(\theta_0 = 1\), so its two numbers are \(\sin(\text{pos})\) and \(\cos(\text{pos})\).
Section 4.1 worked them out: \((0.00,\ 1.00)\) for I at position 0,
\((0.84,\ 0.54)\) for love at position 1, and \((0.91,\ -0.42)\) for
LLMs at position 2. Read each pair as a point \((x, y)\). All three points
lie on a circle of radius 1, because \(\sin^2 + \cos^2 = 1\); for love,
\(0.84^2 + 0.54^2 = 1.00\). From one position to the next, the point moves round the
circle by the same angle, \(\theta_0 = 1\) radian, about 57°.
Every pair works the same way. Pair \(i\) holds \((\sin(\text{pos} \times \theta_i),\ \cos(\text{pos} \times \theta_i))\), a point on the same circle, and moving \(\phi\) positions forward moves it round by the angle \(\theta_i \phi\). The point is the tip of one clock hand in Figure 1, and the matrix above does exactly that turn. A matrix that turns a point around a circle is called a rotation matrix. (Which way the hand turns makes no difference here; Appendix A says more.)
"Three steps back", now with sines
position score — the score is the number attention computes for a query and a key, \(\mathbf{q} \cdot \mathbf{k}\) (Part 1). The higher it is, the more attention the query's word gives the key's word. With the input \(\mathbf{e} + \mathbf{p}\), the score splits into four terms (Part 3, Section 3.7). The position score is the fourth, \(\mathbf{p}_m \mathbf{M} \mathbf{p}_n^\top\), where \(\mathbf{M} = \mathbf{W}^Q \mathbf{W}^{K\top}\).
In Part 3, Section 3.7, a counter failed the requirement on three examples: positions 3 and 0, 10 and 7, and 100 and 97. In each example the two positions are 3 apart, and the counter gave them position scores of \(0\), \(70c\) and \(9700c\). Now run the same three examples through a sine-cosine encoding. Look at the same part of the score as in that section: the fourth of its four terms, \(\mathbf{p}_m \mathbf{M} \mathbf{p}_n^\top\), the only one that holds both positions. This part calls it the position score, as Part 3 did. As in that section, \(m\) is the position of the query's word and \(n\) the position of the key's word. Take only the fastest pair of slots, where \(\theta_0 = 1\) and so \(\mathbf{p}_m = (\sin m,\ \cos m)\). Start with the simplest choice the model could make, \(\mathbf{M} = \mathbf{I}\). Then the position score is just the dot product of the two encodings:
\[ \mathbf{p}_m \cdot \mathbf{p}_n = \sin(m)\sin(n) + \cos(m)\cos(n) = \cos(m - n) \] \[ \begin{array}{lcr} \text{positions 3 and 0:} & \cos(3) & = -0.99 \\[4pt] \text{positions 10 and 7:} & \cos(3) & = -0.99 \\[4pt] \text{positions 100 and 97:} & \cos(3) & = -0.99 \end{array} \]The gap is the same, and so is the position score, wherever the two words sit. \(\cos(m - n)\) depends on the relative position \(m - n\) alone, so the sine-cosine encoding meets the requirement that the counter failed. The position score now tells the model how far apart two words are, wherever they sit, so a rule like "three apart" is something the model can learn.
Think for a moment The same position score for the same gap is needed, but it is not enough. If the model wants to attend to the word exactly 3 back, the position score at a gap of 3 also has to be the highest one. With \(\mathbf{M} = \mathbf{I}\) it is not: the highest position score is always at a gap of 0, a word with itself. What could the model do about that?
It can choose a different \(\mathbf{M}\). With the counter, that did not help: every choice of \(\mathbf{M}\) still gave \(c\,mn\), so the weights could only change the size of \(c\). With sine and cosine, a different \(\mathbf{M}\) can move the highest position score.
In the position score \(\mathbf{p}_m \mathbf{M} \mathbf{p}_n^\top\), \(\mathbf{p}_m\) is the positional encoding of the query's word and \(\mathbf{p}_n\) that of the key's word. The matrix \(\mathbf{M}\) acts on \(\mathbf{p}_m\) first, before the dot product with \(\mathbf{p}_n\). Let \(\mathbf{M}\) be the matrix from the start of this section that moves an encoding, set to move it 3 positions back instead of forward. Then \(\mathbf{p}_m \mathbf{M}\) is \(\mathbf{p}_{m-3}\): the positional encoding of the query's word is replaced by the encoding of the position three before it.
Say the query's word is at position 10. Its positional encoding \(\mathbf{p}_{10}\) becomes \(\mathbf{p}_7\), so with the fastest pair the position score for a key's word at position \(n\) is \(\cos(7 - n)\). That is highest at \(n = 7\), the word three back. If the query's word is at position 100, \(\mathbf{p}_{100}\) becomes \(\mathbf{p}_{97}\), and the highest position score is at 97, again three back. Both cases use the same \(\mathbf{M}\), because the matrix that moves an encoding does not depend on where the encoding starts. Figure 5 shows the position score for every gap, with and without this \(\mathbf{M}\), for a realistic 128-number encoding:
The highest position score moved from a gap of 0 to a gap of 3. That is what "easily learn to attend by relative positions" means in practice. The model learns one matrix, once, and the rule "look 3 words back" then works at position 4 and at position 400 alike. No matrix can do the same for a counter, because \(c\,mn\) only ever moves in one direction as \(n\) grows, so its highest point is always at the first or the last word, never at a gap of 3.
All the pairs together
So far we used only the fastest pair. A real encoding has many pairs, and the position score adds up one term from each. Keep \(\mathbf{M} = \mathbf{I}\), so the position score is the dot product of the positional encodings \(\mathbf{p}_m\) and \(\mathbf{p}_n\) of two positions. Each sine-cosine pair contributes two terms to the sum:
\[ \mathbf{p}_m \cdot \mathbf{p}_n = \sum_{i=0}^{d_{\text{model}}/2\,-\,1} \big[\, \sin(\theta_i m)\sin(\theta_i n) + \cos(\theta_i m)\cos(\theta_i n) \,\big] = \sum_{i=0}^{d_{\text{model}}/2\,-\,1} \cos\!\big(\theta_i (m - n)\big) \]The last step uses the cosine subtraction formula, \(\cos(A - B) = \cos A \cos B + \sin A \sin B\), once for every pair. The sum runs over all the pairs: \(i\) goes from 0, the fastest pair, to \(d_{\text{model}}/2 - 1\), the slowest. With \(d_{\text{model}} = 128\) that is 64 pairs, \(i = 0\) to \(63\).
As with the single pair above, the absolute positions \(m\) and \(n\) have gone, and only the gap \(m - n\) is left, now for all the pairs at once.
The sum has two more properties: it is the same in both directions, and it is larger for nearby positions than for distant ones. Appendix B shows both, and nothing later in this part depends on them.
These three properties (only the gap matters, the same in both directions, larger for nearby positions) hold for \(\mathbf{M} = \mathbf{I}\). A trained model learns its own \(\mathbf{M}\), and with a different \(\mathbf{M}\) they can change. Figure 5 already showed one case: with the rotation, the highest position score is at a gap of 3, not at 0, so it is no longer largest for nearby positions. Another \(\mathbf{M}\) can even break the first property. Keep only the sine slot of the fastest pair, \(\mathbf{M} = \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix}\): the position score is then \(\sin m \cdot \sin n\). That gives \(0\) for positions 3 and 0, but \(-0.54 \times 0.66 = -0.36\) for positions 10 and 7, although both gaps are 3.
Section 4.4 shows RoPE, which goes one step further: with RoPE, the score depends only on the two words and the gap between them, whatever weights the model learns.
Key takeaway
- With \(\mathbf{M} = \mathbf{I}\), the position score is the dot product of the two encodings, and it depends only on the gap \(m - n\): the fastest pair gives \(\cos(m - n)\), and the sum over all the pairs still depends on the gap alone. A counter gives \(c\,mn\), which grows with where the two words sit. So the encoding meets the requirement of Part 3, Section 3.7.
- Moving a positional encoding forward by a fixed number of steps is one fixed rotation. So by learning one matrix, the model can move the highest position score to a gap of 3, at every position.
- The first point holds for \(\mathbf{M} = \mathbf{I}\); a different learned \(\mathbf{M}\) can break it. The encoding makes the right pattern easy to learn; it does not force the model to learn it.
Try it (3 minutes): Work out slots 0 and 1 of the positional encodings for positions 10, 7, 100 and 97. Take the dot product of the slots for positions 10 and 7, and then for positions 100 and 97. The two answers should agree. Then do positions 10 and 6 and see the answer change.
4.4Modern LLMs: Rotary Position Embedding (RoPE)
Section 4.3 showed that moving a positional encoding forward is the same as multiplying it by a rotation matrix. But the original Transformer adds the positional encoding to the embedding vector (\(\mathbf{e} + \mathbf{p}\)), and the model has to discover the rotation for itself during training.
That raises a question. If a rotation is what carries the gap between two positions, why add anything at all? Why not rotate the vectors directly, by an angle that depends on their position?
That idea is Rotary Position Embedding (RoPE). It is the method used by almost every modern large language model whose design is public, including Llama, Mistral, Qwen and DeepSeek. Closed models such as GPT-4 have never published how they encode position, so nobody outside the companies that built them can say for certain which method those models use.
How RoPE works
RoPE does not add a positional encoding \(\mathbf{p}\) to the embedding vector \(\mathbf{e}\). Instead, it waits until \(\mathbf{W}^Q\) and \(\mathbf{W}^K\) have produced the Query vector \(\mathbf{q}\) and the Key vector \(\mathbf{k}\). Then it rotates each one by an angle that grows with its position. For now, picture a query of just two numbers, so the rotation happens in one flat plane. Real queries have more numbers, and we come back to that in a moment.
\[ \mathbf{q}_m = \mathbf{R}_m \mathbf{q}, \quad \mathbf{q} = (\mathbf{e}_m \mathbf{W}^Q)^\top \qquad\text{and}\qquad \mathbf{k}_n = \mathbf{R}_n \mathbf{k}, \quad \mathbf{k} = (\mathbf{e}_n \mathbf{W}^K)^\top \]Here \(\mathbf{e}_m\) is the embedding vector of the word at position \(m\); the \(m\) only says which word it is, and nothing about the position has been added to it. So \(\mathbf{q}\) has no subscript: it is the same vector wherever the word sits, and only \(\mathbf{q}_m\), after the rotation, depends on the position. \(\mathbf{R}_m\) is the rotation matrix for position \(m\). The vectors \(\mathbf{q}\) and \(\mathbf{k}\) are written as columns, with the matrix on the left, which is the usual way to write a rotation. For one pair, \(\mathbf{R}_m\) is the \(2 \times 2\) rotation by the angle \(m\theta\), where \(\theta\) is the turn per step of this one pair, as in Section 4.1:
\[ \mathbf{R}_m = \begin{pmatrix} \cos m\theta & -\sin m\theta \\ \sin m\theta & \cos m\theta \end{pmatrix}, \qquad \mathbf{R}_m \begin{pmatrix} q_0 \\ q_1 \end{pmatrix} = \begin{pmatrix} q_0 \cos m\theta - q_1 \sin m\theta \\ q_0 \sin m\theta + q_1 \cos m\theta \end{pmatrix} \]The angle is \(m \times \theta\), so it grows by one \(\theta\) for each step of the
position, and that is the whole method. At position 0 the angle is 0,
\(\cos 0 = 1\) and \(\sin 0 = 0\), so \(\mathbf{R}_0 = \mathbf{I}\) and the vector does
not move. For example, take \(\theta = 15^\circ\), as in Figure 6, and
\(\mathbf{q} = (1, 0)\). At position 1 the angle is \(15^\circ\) and
\(\mathbf{q}_1 = (0.97,\ 0.26)\). At position 3 it is \(45^\circ\) and
\(\mathbf{q}_3 = (0.71,\ 0.71)\). Figure 6
shows this for love and LLMs, first at positions 1 and 2 and
then three places later. Figure 7 shows the same thing as an animation.
love and LLMs are 1 step apart in both
sentences, so the angle between the two arrows is 40° on the left and 40° on
the right.A real query has more than two numbers
Figure 6 shows a plane, but a real Query vector is not two numbers. In Llama-style models, each attention head works on 128 numbers per token. There is no single plane in which to rotate 128 numbers, so what does RoPE actually do?
RoPE cuts the vector into neighbouring pairs and gives each pair its own plane. Slots 0 and 1 form the first pair, slots 2 and 3 the second, and so on up to slots 126 and 127. A 128-number vector becomes 64 separate 2D points, and each point gets its own small rotation. (Some implementations pair slot \(j\) with slot \(j + d/2\) instead. That is the same operation with the numbers in a different order, and nothing below changes.)
The rotations are not all the same, though. Pair \(i\) turns by \(m\theta_i\), where \(\theta_i\) is built exactly as in Section 4.1, with \(d\), the number of slots in one head's query, in place of \(d_{\text{model}}\):
\[ \theta_i = \frac{1}{10000^{2i/d}}, \qquad i = 0, 1, \dots, \tfrac{d}{2}-1 \]So the first pair turns fast, and every later pair turns more slowly. These are the speeds of Figure 1 again: the fast pairs tell nearby positions apart, and the slow pairs tell distant positions apart, as Section 4.1 showed. RoPE did not invent new speeds; it took the old ones and used them to turn the query and the key instead of adding them to the embedding. Figure 8 shows this for a toy vector of 8 numbers.
Written as one matrix, \(\mathbf{R}_m\) is block-diagonal: independent \(2 \times 2\) rotations sit along the diagonal, and every other entry is zero. Each \(\mathbf{r}(\alpha)\) is the same \(2 \times 2\) rotation as above, with \(\alpha = m\theta_i\) for pair \(i\).
\[ \mathbf{R}_m = \begin{pmatrix} \mathbf{r}(m\theta_0) & & & \\ & \mathbf{r}(m\theta_1) & & \\ & & \ddots & \\ & & & \mathbf{r}(m\theta_{d/2-1}) \end{pmatrix}, \qquad \mathbf{r}(\alpha) = \begin{pmatrix} \cos\alpha & -\sin\alpha \\ \sin\alpha & \cos\alpha \end{pmatrix} \]In practice nobody builds this matrix, because it is almost all zeros. The code just goes through the pairs and applies four multiplications, one subtraction and one addition to each. But the block-diagonal form is the useful way to picture \(\mathbf{R}_m\), and it is what makes the next proof work. The \(2 \times 2\) pieces never interact: slots 0 and 1 of the result come only from slots 0 and 1 of the input, slots 2 and 3 only from slots 2 and 3, and so on. With four slots, for example:
\[ \mathbf{R}_m \begin{pmatrix} q_0 \\ q_1 \\ q_2 \\ q_3 \end{pmatrix} = \begin{pmatrix} \mathbf{r}(m\theta_0) \begin{pmatrix} q_0 \\ q_1 \end{pmatrix} \\[4pt] \mathbf{r}(m\theta_1) \begin{pmatrix} q_2 \\ q_3 \end{pmatrix} \end{pmatrix} \]So rotating a 128-number vector is just 64 separate 2D rotations, one per pair. The proof below therefore checks each step for one \(2 \times 2\) rotation, with any angle; the step then holds for every pair, each with its own \(\theta_i\), and so for the whole matrix.
Why only the gap survives
Write out the attention score, the dot product, between a query at position \(m\) and a key at position \(n\), and compare the two methods.
With an absolute positional encoding, which is added to the embedding vector, the score is the four-term sum from Part 3, Section 3.7:
\[ (\mathbf{e}_m + \mathbf{p}_m)\,\mathbf{M}\,(\mathbf{e}_n + \mathbf{p}_n)^\top = \mathbf{e}_m \mathbf{M} \mathbf{e}_n^\top + \mathbf{e}_m \mathbf{M} \mathbf{p}_n^\top + \mathbf{p}_m \mathbf{M} \mathbf{e}_n^\top + \mathbf{p}_m \mathbf{M} \mathbf{p}_n^\top \]Here \(\mathbf{e}_m\) is the embedding vector of the word at position \(m\), and \(\mathbf{M} = \mathbf{W}^Q \mathbf{W}^{K\top}\). Only the last term holds both positions, so only the last term can depend on the gap. The middle two each know only one of the two positions. Those two terms are not useless, but any rule about the gap has to be carried by the fourth term. So with an added encoding, the score can depend on the positions in other ways than through the gap: through the middle two terms, which see \(m\) or \(n\) alone, and through the fourth term whenever \(\mathbf{M}\) is not a rotation, as the example at the end of Section 4.3 showed.
With RoPE, the query and the key are rotated instead. Write the dot product as a matrix product first:
\[ (\mathbf{R}_m \mathbf{q}) \cdot (\mathbf{R}_n \mathbf{k}) = (\mathbf{R}_m \mathbf{q})^\top (\mathbf{R}_n \mathbf{k}) = \mathbf{q}^\top \mathbf{R}_m^\top \mathbf{R}_n \mathbf{k} \]Everything now depends on the middle part, \(\mathbf{R}_m^\top \mathbf{R}_n\). This product simplifies in two steps, and both are short enough to check.
Step one: transposing a rotation reverses it. Write a \(2 \times 2\) rotation by \(\alpha\) and its transpose side by side:
\[ \mathbf{r}(\alpha) = \begin{pmatrix} \cos\alpha & -\sin\alpha \\ \sin\alpha & \cos\alpha \end{pmatrix}, \qquad \mathbf{r}(\alpha)^\top = \begin{pmatrix} \cos\alpha & \sin\alpha \\ -\sin\alpha & \cos\alpha \end{pmatrix} \]Cosine is even and sine is odd, so \(\cos(-\alpha) = \cos\alpha\) and \(\sin(-\alpha) = -\sin\alpha\). Substitute those into \(\mathbf{r}(-\alpha)\) and you get the matrix on the right, exactly. So \(\mathbf{r}(\alpha)^\top = \mathbf{r}(-\alpha)\): the transpose is a turn of the same size in the other direction.
Step two: rotations combine by adding angles. Turning by \(\beta\) after turning by \(\alpha\) is one turn of \(\alpha + \beta\). This follows from the same addition formulas Appendix A uses. Put the two steps together:
\[ \mathbf{R}_m^\top \mathbf{R}_n = \mathbf{R}_{-m}\,\mathbf{R}_n = \mathbf{R}_{n-m} \]Both steps are about one \(2 \times 2\) piece. The pieces never mix, so what holds for one holds for the whole matrix. The score becomes:
\[ (\mathbf{R}_m \mathbf{q}) \cdot (\mathbf{R}_n \mathbf{k}) = \mathbf{q}^\top \mathbf{R}_{n-m} \mathbf{k} \]On the right-hand side, \(m\) and \(n\) have gone and only \(n-m\) is left. There are no extra terms and no absolute positions: the score depends on the two words and on the gap between them, and on nothing else. (The gap appears as \(n - m\) here and as \(m - n\) in Section 4.3. The two say the same thing with the sign flipped.)
This is stronger than the requirement of Part 3, Section 3.7. That requirement asked only for a part of the score that can depend on the gap alone. With an added encoding, the score depends on the gap only if the model learns an \(\mathbf{M}\) that makes it so, and the middle two terms still see the absolute positions. With RoPE, the score depends only on the two words and the gap between them, whatever weights the model learns.
Why a rotation, and not something simpler?
Think for a moment The result above needed only one thing: a score that depends on the positions only through the gap. A rotation is not the only way to get that. Multiply the query at position \(m\) by \(1.01^m\) and the key at position \(n\) by \(1.01^{-n}\). The score becomes \(1.01^{m-n}\) times \(\mathbf{q} \cdot \mathbf{k}\), which also depends only on the gap. Why use a rotation, then?
Because a rotation matrix is orthogonal: \(\mathbf{R}^\top \mathbf{R} = \mathbf{I}\). That one property does two separate jobs.
Job one — one rule for both. In comparison, scaling needs two different rules: \(1.01^m\) for the query and \(1.01^{-n}\) for the key, and the minus sign in the second rule is what turns \(m\) and \(n\) into \(m - n\). Give the key the same rule as the query, \(1.01^n\), and the score becomes \((1.01^m \mathbf{q}) \cdot (1.01^n \mathbf{k}) = 1.01^{m+n}\,(\mathbf{q} \cdot \mathbf{k})\): the exponents add, and both absolute positions stay. RoPE, however, uses the same rule for both, \(\mathbf{R}_m\) for the query and \(\mathbf{R}_n\) for the key, and only the gap survives. The minus sign comes from the dot product itself: it transposes \(\mathbf{R}_m\), and \(\mathbf{R}_m^\top = \mathbf{R}_{-m}\), a turn of the same size in the other direction. That is step one of the proof above.
Job two — nothing grows. An orthogonal matrix never changes a vector's length: \(\|\mathbf{R}\mathbf{q}\| = \|\mathbf{q}\|\), at any position, in any pair. A rotated arrow points somewhere new and is exactly as long as before.
Job two is what rules out scaling. A query at position 4000 and a key at position 0 would give a score of \(1.01^{4000}\) times \(\mathbf{q} \cdot \mathbf{k}\), and \(1.01^{4000}\) is about \(2 \times 10^{17}\). Softmax reacts to the differences between scores, and it reacts to them exponentially. Part 1, Section 1.3.1 showed what that does: stretching the scores 1, 2, 3 by a factor of 8 turned the weights 0.09, 0.24, 0.67 into 0.0000, 0.0003, 0.9997, so one word took almost everything. A factor of \(2 \times 10^{17}\) is far worse. The weights would be 1 for one key and 0 for all the others, and a small change in any score would not move them at all. A model learns by making small changes and watching what they do (Part 9), so it would learn nothing from such a row.
The Transformer already guards against this. Part 1, Section 1.3.2 showed that raw scores grow with the vector length, to about ±8 at \(d_k = 64\), and that is enough to collapse softmax; so every score is divided by \(\sqrt{d_k}\) to bring it back to about ±1. Scaling by \(1.01^m\) would undo that: at position 4000 it multiplies the score by \(2 \times 10^{17}\), and the collapse the division was meant to prevent comes back far worse. The counter in Part 3, Section 3.7 had the same problem: numbers that grow with the position. There is a second problem: the length of the vector would now carry position, when the length should be part of how well the two words match.
A rotation avoids both problems. The length stays the same, so the largest score a query and a key can produce is the same at position 0 and at position 100,000. All of the position information is in the angle, and an angle cannot grow without limit.
Key takeaway
- RoPE does not add the position to the word's row. The query \(\mathbf{q}\) and the key \(\mathbf{k}\) are made from the embedding alone and carry no position; RoPE then rotates each one by an angle that grows with its position, inside the attention step.
- Rotating by \(m\) and by \(n\) and then taking the dot product leaves a single rotation by \(n-m\). The two absolute positions cancel, and only the gap is left.
- With an added encoding, the score depends on the gap only if the model learns an \(\mathbf{M}\) that makes it so. With RoPE, the score depends only on the two words and the gap between them, whatever weights the model learns.
- A rotation is used because it is orthogonal. One rule, \(\mathbf{R}_m\), serves both the query and the key, and a vector's length never changes, so the scores do not grow with the position.
Try it (4 minutes): Take the two slots of one pair, \((q_0,\ q_1)\), and rotate them by the angle for position 1. Then, starting again from \((q_0,\ q_1)\), rotate them by the angle for position 2. Check that the length \(\sqrt{q_0^2 + q_1^2}\) is the same in all three cases. Then say which of the two methods in this part, adding a wave or rotating, leaves the word's own row untouched, and which one mixes the position into it.
4.5RoPE by hand, and what it leaves alone
Working out the rotations, number by number
Section 4.1 filled in a whole positional encoding by hand. Do the same here, on the same three words, so you can see exactly what RoPE asks you to compute.
Take \(d = 4\), so each query and key is four numbers and splits into two pairs. Start with \(\theta_0\) and \(\theta_1\), the turn per step of each pair. They depend only on the pair, not on the word:
\[ \theta_0 = \frac{1}{10000^{2 \times 0/4}} = 1, \qquad \theta_1 = \frac{1}{10000^{2 \times 1/4}} = 0.01 \]Pair 0 (slots 0 and 1) turns by \(m \times 1\); pair 1 (slots 2 and 3) turns by \(m \times 0.01\). That gives one small table covering every rotation in the sentence:
| word | position \(m\) | pair 0 turns by (radians) | pair 1 turns by (radians) |
|---|---|---|---|
I | 0 | 0.00 | 0.00 |
love | 1 | 1.00 | 0.01 |
LLMs | 2 | 2.00 | 0.02 |
Now apply them. Take love's query, \((1, 2, 0, 1)\), which is the second row
of \(\mathbf{Q}\) in Part 3,
Section 3.2, written as a row here to save space. love sits at position 1.
Pair 0 is \((1, 2)\) and turns by 1.00 radians, where \(\cos 1 = 0.54\) and \(\sin 1 = 0.84\):
Pair 1 is \((0, 1)\) and turns by 0.01 radians, where \(\cos 0.01 = 1.00\) and \(\sin 0.01 = 0.01\):
\[ \begin{pmatrix} 1.00 & -0.01 \\ 0.01 & 1.00 \end{pmatrix}\begin{pmatrix} 0 \\ 1 \end{pmatrix} = \begin{pmatrix} (1.00)(0)-(0.01)(1) \\ (0.01)(0)+(1.00)(1) \end{pmatrix} = \begin{pmatrix} -0.01 \\ 1.00 \end{pmatrix} \]Put the two pairs back together and love's query becomes
\((-1.14,\ 1.92,\ -0.01,\ 1.00)\). The two pairs moved very differently: pair 0 turned by
a whole radian, while pair 1 hardly moved. These are the fast and slow speeds of Figure 1
again, now used to turn the vector instead of being added to it.
Do the same for all three words and you get:
\[ \begin{array}{lll} & \text{query, rotated} & \text{key, rotated}\\[4pt] \texttt{I}\ (m=0) & (2.00,\ 1.00,\ 0.00,\ 0.00) & (2.00,\ 0.00,\ 1.00,\ 0.00)\\[4pt] \texttt{love}\ (m=1) & (-1.14,\ 1.92,\ -0.01,\ 1.00) & (-1.68,\ 1.08,\ -0.01,\ 1.00)\\[4pt] \texttt{LLMs}\ (m=2) & (-0.42,\ 0.91,\ 0.96,\ 2.02) & (-0.42,\ 0.91,\ -0.04,\ 2.00) \end{array} \]I is at position 0, so it is rotated by zero, and both its query and its key
stay as they were. In general, the first word is never rotated, whatever the
sentence. Its position still counts: every other word is rotated relative to it.
Checking that the gap is all that survives
Section 4.4 proved that
\((\mathbf{R}_m \mathbf{q}) \cdot (\mathbf{R}_n \mathbf{k}) = \mathbf{q}^\top \mathbf{R}_{n-m} \mathbf{k}\).
Check it with these numbers: the left side first, then the right. Take the dot product of love's rotated query with I's rotated
key, using the rows above:
Now get the same number the short way. Leave love's query alone, unrotated, and
rotate I's key by the gap instead. I is at position 0 and
love at position 1, so the gap is \(n - m = -1\), and the key turns by
\(-1.00\) and \(-0.01\) radians:
Both ways give the same score, \(-2.29\). (With unrounded values both give \(-2.30\); the \(-2.29\) comes from the rounded numbers printed above.)
✗ Common mistake
The score came out the same, so it is tempting to expect the rotated vectors to look
similar to the originals, but that is wrong. love's query went from
\((1, 2, 0, 1)\) to \((-1.14, 1.92, -0.01, 1.00)\), and the first number even changed sign.
What stays the same is not the vectors but their lengths and the angle between
them, and those are all the dot product reads.
Think for a moment The short way rotated the key by \(-1\) because the two words are one apart. Suppose both words moved three places further into the sentence. What changes in that calculation?
Nothing changes. Put love at position 4 and I at position 3. The
gap is still \(-1\), so the key still turns by \(-1.00\) and \(-0.01\) radians, and the
score is still \(-2.29\). Both vectors were rotated much further, but the score they
produce stayed the same.
What RoPE leaves alone
The rotation was applied to \(\mathbf{q}\) and to \(\mathbf{k}\), and nowhere else. \(\mathbf{V}\) is never rotated.
This is deliberate. The score built from \(\mathbf{q}\) and \(\mathbf{k}\) decides
which words a word takes information from; \(\mathbf{V}\) is what it
takes. Position belongs in the first decision, not in the second. If bites
attends to dog, bites should collect what dog
means, not a version of dog that has been rotated because of where
dog happened to sit.
✗ Common mistake \(\mathbf{Q}\), \(\mathbf{K}\) and \(\mathbf{V}\) all come from the same input rows, one weight matrix each, so it is tempting to assume \(\mathbf{V}\) is rotated too, but that is wrong. They come from the same rows but do different jobs. \(\mathbf{Q}\) and \(\mathbf{K}\) decide where to look and carry position; \(\mathbf{V}\) is what gets collected and carries content only.
Where in the model this happens
An absolute encoding is added once, at the bottom, before the first layer; that is the addition shown in Figure 4. Every layer above works with whatever position information survived from there.
RoPE is applied somewhere else: inside each attention layer, after \(\mathbf{W}^Q\) and \(\mathbf{W}^K\) have produced \(\mathbf{q}\) and \(\mathbf{k}\), and just before the dot product. So it happens again in layer 2 and again in layer 3, each time on newly computed vectors.
✗ Common mistake Figure 4 marks where an absolute encoding is added, so it is tempting to picture RoPE as a different vector added at the bottom of the stack, in that same place, but that is wrong. Nothing is added at the bottom. Position enters each time a query and a key are about to be compared.
Key takeaway
- Worked by hand, RoPE gives the same score whether both vectors are rotated or only the key is rotated by the gap. Moving both words further into the sentence does not change that score.
- The word at position 0 is never rotated. Its position still shows, in how far every other word is rotated relative to it.
- RoPE rotates only the Query and the Key, never the Value, and it does so inside every attention layer, not once at the bottom.
Try it (3 minutes): Using the rotated rows above, take the dot product of
LLMs's rotated query with love's rotated key. Then do it the
short way: rotate love's unrotated key \((0, 2, 0, 1)\) by the gap
\(n - m = -1\), and take its dot product with LLMs's unrotated query
\((1, 0, 1, 2)\). The two answers should agree to within 0.01; the difference comes from
the rounding.
AAppendix: why a position shift is a rotation
This appendix belongs to Section 4.3. That section claimed that moving \(\phi\) positions forward, from position \(\text{pos}\) to position \(\text{pos} + \phi\), is the same as multiplying the positional encoding by a matrix. The matrix depends only on \(\phi\). Here is the check.
Start from the addition formulas for sine and cosine:
\[ \begin{aligned} \sin(x + y) &= \sin(x)\cos(y) + \cos(x)\sin(y) \\ \cos(x + y) &= \cos(x)\cos(y) - \sin(x)\sin(y) \end{aligned} \]Take pair \(i\) at position \(\text{pos}\), so its angle is \(x = \theta_i\,\text{pos}\), and shift it \(\phi\) positions forward, which adds \(y = \theta_i \phi\) to the angle. The two formulas then say that the shift is a \(2 \times 2\) matrix multiplication:
\[ \begin{pmatrix} \cos(\theta_i \phi) & \sin(\theta_i \phi) \\ -\sin(\theta_i \phi) & \cos(\theta_i \phi) \end{pmatrix} \begin{pmatrix} \sin(\theta_i\,\text{pos}) \\ \cos(\theta_i\,\text{pos}) \end{pmatrix} = \begin{pmatrix} \sin(\theta_i (\text{pos} + \phi)) \\ \cos(\theta_i (\text{pos} + \phi)) \end{pmatrix} \]Every entry of the \(2 \times 2\) matrix on the left is a sine or a cosine of \(\theta_i \phi\). The starting position \(\text{pos}\) appears only in the vector being multiplied, never in the matrix, and that is what Section 4.3 claimed. The matrix has the form \(\begin{pmatrix} \cos\alpha & \sin\alpha \\ -\sin\alpha & \cos\alpha \end{pmatrix}\) with \(\alpha = \theta_i \phi\), which is a rotation by \(\alpha\). The rotation is clockwise, because the pair is stored sine first. Section 4.4 writes the anticlockwise rotation as \(\mathbf{r}(\alpha)\), so in that notation this matrix is \(\mathbf{r}(-\alpha)\).
Section 4.4 uses this twice: once to motivate RoPE, and once for the fact that rotations by \(m\theta\) and \(n\theta\) combine into a rotation by \((n-m)\theta\).
BAppendix: what the full sum looks like
This appendix belongs to Section 4.3. That section added up all the pairs and found that the dot product of two positional encodings is
\[ \mathbf{p}_m \cdot \mathbf{p}_n = \sum_{i=0}^{d_{\text{model}}/2\,-\,1} \cos\!\big(\theta_i (m - n)\big) \]which depends only on the gap \(m - n\). This appendix shows two more properties of the sum: it is the same in both directions, and it is larger for nearby positions than for distant ones.
Because \(\cos(-x) = \cos(x)\), looking 4 words back gives exactly the same position score as looking 4 words forward. So the position score is symmetric: it is the same in both directions.
✗ Common mistake It is tempting to read that symmetry as the model can never tell three words back from three words forward. That part is true of this dot product alone, because this dot product is the case \(\mathbf{M} = \mathbf{I}\). What it misses is the rotation from Section 4.3. Once \(\mathbf{M}\) is the rotation that moves the query's encoding back 3 positions, the curve is symmetric around a gap of 3 instead of a gap of 0, and "three back" and "three forward" get different position scores.
To see how the position score changes as the gap grows, try it with our four-number encoding, which has two pairs: pair 0 with \(\theta_0 = 1\) and pair 1 with \(\theta_1 = 0.01\). Its position score is \(\cos(\text{gap}) + \cos(0.01 \times \text{gap})\):
| gap | pair 0: \(\cos(\text{gap})\) | pair 1: \(\cos(0.01 \times \text{gap})\) | position score |
|---|---|---|---|
| 0 | 1.00 | 1.00 | 2.00 |
| 1 | 0.54 | 1.00 | 1.54 |
| 2 | −0.42 | 1.00 | 0.58 |
| 3 | −0.99 | 1.00 | 0.01 |
| 4 | −0.65 | 1.00 | 0.35 |
| 5 | 0.28 | 1.00 | 1.28 |
| 6 | 0.96 | 1.00 | 1.96 |
| 7 | 0.75 | 1.00 | 1.75 |
| 8 | −0.15 | 1.00 | 0.85 |
| 9 | −0.91 | 1.00 | 0.09 |
| 10 | −0.84 | 1.00 | 0.16 |
At a gap of 0 both cosines are 1, so the position score is as large as it can be. As the gap grows, the angle of the fast pair grows by 1 radian per step and its cosine drops quickly. The angle of the slow pair grows by only 0.01 per step, so its cosine stays at 1.00. Up to a gap of 3 the position score falls. Then it rises again, to 1.96 at a gap of 6, almost back to 2.00. The fast pair has turned once around its circle, which takes about 6.28 positions, so its cosine is close to 1 again. With only two pairs, a gap of 6 looks almost the same as a gap of 0.
This is why a real encoding uses many pairs, with speeds between these two. Their cosines do not all come back to 1 at the same gap, so the sum does not climb back to its value at a gap of 0. Figure 9 shows the result for \(d_{\text{model}} = 128\), divided by the number of pairs so that a gap of 0 reads 1.00. Along a row it falls from 1.00 at a gap of 0 to 0.69 at a gap of 9, with no rise.
Figure 9 also shows that the fall slows down: 1.00, 0.97, 0.90, 0.82, 0.76, and then 0.74, 0.74, 0.73. The slow pairs, whose cosines stay close to 1, keep the position score from falling to zero.
✗ Common mistake Figure 9 looks like an attention grid, so it is tempting to read the figure as the attention pattern itself, but that is wrong. It is the pattern in the raw positional encodings, before \(\mathbf{W}^Q\) and \(\mathbf{W}^K\) act on them. Those weights can stretch or scramble the pattern in whatever way training finds useful. The encoding makes the right pattern easy to learn; it does not force the model to learn it.