Fall 2026
  • Discord
  • Gradescope
  • Syllabus
  • Spring 2026

On this page

  • Position Information
  • Rotary Embeddings
  • Rotation Frequencies

Positional Embeddings

“Dog bites man” and “man bites dog” contain the same words, but tell different stories. Our content-only attention calculation cannot tell those roles apart: reordering the input embeddings simply reorders the outputs. To use word order, we need to put it into the computation.

The causal mask already supplies one kind of order by restricting each token to its past. Within that visible context, we would also like to represent which token came immediately before another or how far apart two tokens are. Positional embeddings supply this information.

How can attention distinguish the same words in a different order?


We’ll start by representing positions directly, then use rotations to make an inner product depend on the distance between positions.

Position Information

An absolute positional embedding assigns a vector \(\mathbf p_i\in\mathbb R^d\) to position \(i\). We add it to the content embedding \(\mathbf x_i\in\mathbb R^d\):

\[ \mathbf x_i\leftarrow\mathbf x_i+\mathbf p_i. \]

The same word now produces different inputs at different positions. The vectors \(\mathbf p_i\) may be learned as part of the model or generated by a fixed formula. A learned table has entries for a specified range of positions. A fixed formula can be evaluated beyond the training range, though that alone does not tell us how well the model will use those new positions.

Absolute positions are straightforward to represent, but many useful relationships are relative. The preceding word is one position away whether we are at the beginning or the middle of a paragraph. If a query is at position \(i\) and a key is at position \(j\), their relative position is the signed offset \(j-i\). We would like the positional part of their comparison to depend on that offset.

Rotary positional embeddings, usually called RoPE, accomplish this through the geometry of an inner product. Each vector is rotated using its own absolute position. When we compare two rotated vectors, the difference between their rotations appears in the score.

Rotary Embeddings

Begin with a two-dimensional attention head, so \(\mathbf q_i,\mathbf k_j\in\mathbb R^2\) and the head width is \(d_k=2\). These are the query and key after the learned projections and before position is applied. Choose an angular frequency \(\theta>0\), measured in radians per position, and define

\[ \mathbf R_i=\begin{bmatrix}\cos(i\theta)&-\sin(i\theta)\\\sin(i\theta)&\cos(i\theta)\end{bmatrix}. \]

At position \(i\), rotate the query by \(i\theta\). At position \(j\), rotate the key by \(j\theta\). The values stay unchanged. The scaled attention score becomes

\[ s_{ij}=\frac{(\mathbf R_i\mathbf q_i)^\top(\mathbf R_j\mathbf k_j)}{\sqrt{d_k}}. \]

Rotating both vectors by the same angle preserves their inner product. We can therefore undo the query’s rotation in both vectors, leaving the key rotated by the difference of the two angles. Let’s write out that argument.

Claim: RoPE makes the positional part of the query-key score depend only on the offset \(j-i\):

\[ s_{ij}=\frac{\mathbf q_i^\top\mathbf R_{j-i}\mathbf k_j}{\sqrt{d_k}}. \]

The rotations preserve the lengths of both vectors.

Proof

Write \(\mathbf R(\phi)\) for rotation by angle \(\phi\), so \(\mathbf R_i=\mathbf R(i\theta)\). Transposing a rotation reverses its angle: \(\mathbf R(\phi)^\top=\mathbf R(-\phi)\). Matrix multiplication and the angle-difference identities give

\[ \mathbf R_i^\top\mathbf R_j=\begin{bmatrix}\cos((j-i)\theta)&-\sin((j-i)\theta)\\\sin((j-i)\theta)&\cos((j-i)\theta)\end{bmatrix}=\mathbf R_{j-i}. \]

Substitute this product into the score:

\[ s_{ij}=\frac{(\mathbf R_i\mathbf q_i)^\top(\mathbf R_j\mathbf k_j)}{\sqrt{d_k}}=\frac{\mathbf q_i^\top\mathbf R_i^\top\mathbf R_j\mathbf k_j}{\sqrt{d_k}}=\frac{\mathbf q_i^\top\mathbf R_{j-i}\mathbf k_j}{\sqrt{d_k}}. \]

Setting \(j=i\) gives \(\mathbf R_i^\top\mathbf R_i=\mathbf I_2\). Consequently,

\[ \|\mathbf R_i\mathbf q_i\|_2^2=\mathbf q_i^\top\mathbf R_i^\top\mathbf R_i\mathbf q_i=\|\mathbf q_i\|_2^2. \]

The same identity at position \(j\) preserves the key’s squared length. Taking square roots proves norm preservation.

The subscripts on \(\mathbf q_i\) and \(\mathbf k_j\) still matter: their content can differ from one pair to another. The result says that, for fixed query and key content, shifting both positions by the same amount leaves the score unchanged. It does not say that every pair of tokens at the same distance has the same score.

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.

The figure holds the content fixed while changing positions. The left panel shows how the two rotations change the alignment. The right panel repeats the same query and key content at every position, so each score depends only on \(j-i\). Entries along a diagonal have the same offset and therefore the same score. A matrix with this structure is called Toeplitz.

This is a controlled way to see the positional effect. In a sentence, changing content generally breaks those diagonal stripes. The demo compares the two cases.

RoPE changes alignment without increasing a vector’s length simply because it appears later. Values remain unchanged, so the position dependence enters through the attention weights used to combine them.

Rotation Frequencies

A single planar rotation gives us only one rate of change with distance. Its angle wraps around after a full turn, so distant integer offsets can produce nearly the same rotation. We would like several ways to distinguish distances, including both rapid changes over nearby positions and slower changes over longer ranges.

For an even head width \(d_k\), divide each query and key into \(d_k/2\) pairs of coordinates. Rotate each pair at its own frequency. With a base \(B>1\), a common choice for pair \(k\) is

\[ \theta_k=B^{-2(k-1)/d_k},\qquad k=1,\ldots,d_k/2. \]

The first pair rotates most rapidly. Successive pairs use smaller frequencies. For position \(i\), collect the planar rotations into a block-diagonal matrix \(\boldsymbol\Theta_i\in\mathbb R^{d_k\times d_k}\). Each block rotates just one pair of coordinates by \(i\theta_k\).

Products and transposes act separately on these blocks. Applying the two-dimensional result to every block gives

\[ \boldsymbol\Theta_i^\top\boldsymbol\Theta_j=\boldsymbol\Theta_{j-i},\qquad s_{ij}=\frac{\mathbf q_i^\top\boldsymbol\Theta_{j-i}\mathbf k_j}{\sqrt{d_k}}. \]

Setting \(j=i\) again gives the identity matrix, so the full transformation also preserves vector lengths. The score adds the contributions from all coordinate pairs. The learned query and key projections determine what information is placed in each pair and hence how much each frequency matters to a comparison.

RoPE frequencies descend geometrically across coordinate pairs, from fast rotations at the first pairs to slow rotations at later pairs.

The vertical axis is logarithmic because the frequencies decrease geometrically. Fast rotations distinguish nearby offsets through larger angle changes, while slow rotations change less over the same interval. Multiple frequencies give the model several scales of positional information.

The mask and the rotations play different roles. The causal mask specifies which positions are available. RoPE affects comparisons among those positions. Moving a whole sequence while keeping its content, visible pairs, and relative offsets fixed preserves these rotary comparisons. Changing the boundaries or the available context can still change the model’s output.

Longer contexts introduce offsets that may not have appeared in training. We can evaluate the rotation formula at those offsets, but extending a model’s useful context requires checking how it behaves there. The geometry supplies a representation of distance, not a guarantee about extrapolation.

We began with one rotation for each absolute position. The inner product combines two such rotations into their relative offset. This lets attention use both content and distance without making the position vectors larger as the sequence grows.