Start studying
Module 5 · Transformers Module contents
Module 5  ·  Card 9 / 19advanced

Rotary Embeddings

Rotary embeddings encode position by rotating each token's vector by an angle.

The size of the angle depends on where the token sits in the sequence.

Because attention compares rotations, the model sees relative distance rather than absolute position.

That extends to longer sequences far better than adding a fixed position vector.

Most recent open language models use it instead of the original scheme.

Learn in depth ↓
Module 05 · Transformers · 19 concepts
In depth  /  15 lines

Rotary Embeddings, in more detail

Rotary embeddings encode position by rotating a token's vector rather than adding to it.

Pairs of dimensions are treated as coordinates on a plane and turned by an angle.

The angle is proportional to the token's position in the sequence.

Different pairs rotate at different rates, from very fast to very slow.

The clever part is what happens when two rotated vectors are compared.

Attention takes a dot product, and rotating both vectors leaves only the difference in angle.

So the score depends on how far apart the tokens are, not where they sit absolutely.

Relative distance falls out of the geometry rather than being bolted on.

That is what the model actually needs, since adjacency matters and absolute index rarely does.

It also degrades gracefully on sequences longer than those seen in training.

Interpolating or rescaling the frequencies extends a model's context window after the fact.

That trick is how many models gained much longer windows without full retraining.

It adds no parameters, since the rotation is computed rather than learned.

It is applied inside each attention layer to the queries and keys, not once at the input.

Llama, Mistral, Qwen and most recent open models use it.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath Rotary Embeddings

Position as a rotation, not an addition

xm′=xm+pm\mathbf{x}'_m = \mathbf{x}_m + \mathbf{p}_m

The original transformer adds a position vector to the token embedding, which mixes what a token is with where it sits, in the same coordinates.

qm′=RΘ,mqm,kn′=RΘ,nkn\mathbf{q}'_m = R_{\Theta, m} \mathbf{q}_m, \qquad \mathbf{k}'_n = R_{\Theta, n} \mathbf{k}_n

Rotary embeddings instead rotate the query and key vectors by an angle proportional to their position, leaving the embedding itself untouched.

Rθ=[cos⁡θ−sin⁡θ sin⁡θcos⁡θ]R_{\theta} = \begin{bmatrix} \cos \theta & -\sin \theta \ \sin \theta & \cos \theta \end{bmatrix}

The vector is taken two dimensions at a time and each pair is rotated in its own plane, so the operation is a block-diagonal rotation matrix.

A rotation preserves length, so no information about the token is scaled away, which addition cannot promise.

Why the dot product becomes relative

(Rmq)⊤(Rnk)=q⊤Rm⊤Rnk=q⊤Rn−mk\big(R_m \mathbf{q}\big)^{\top} \big(R_n \mathbf{k}\big) = \mathbf{q}^{\top} R_m^{\top} R_n \mathbf{k} = \mathbf{q}^{\top} R_{n-m} \mathbf{k}

Two rotations composed give a rotation by the difference of the angles, so the attention score depends only on n−mn - m.

That is the whole result. Absolute positions go in and only the relative distance comes out, without any extra parameters or a bias table.

θi=10000−2i/d\theta_i = 10000^{-2i/d}

Each pair of dimensions rotates at a different frequency, fast in the early dimensions and slow in the later ones.

The slow frequencies encode coarse position across a long document and the fast ones encode fine local order, which is the same reasoning as the sinusoidal scheme it replaced.

One attention step, in order

  1. Project the token to queries and keys as usual.
  2. Split each into consecutive pairs of dimensions.
  3. Rotate pair ii by mθim \theta_i, where mm is the token's position.
  4. Take the dot product of the rotated query and key.
  5. Softmax and apply to the values, which are not rotated at all.

Values carry content rather than being compared, so rotating them would distort what is retrieved without changing what is matched.

What it buys in practice

There are no learned position parameters, so nothing has to be resized when the context length changes.

It is applied inside every attention layer rather than once at the input, so the position signal does not have to survive fifty layers of mixing.

Scores decay as ∣n−m∣|n - m| grows.

Distant tokens interfere across many frequencies at once, which produces a gentle decay with distance that matches how language actually behaves.

Extending the context

θi→θis\theta_i \to \frac{\theta_i}{s}

Position interpolation divides every frequency by the scaling factor, so a model trained at 4,000 tokens can read 32,000 by rotating more slowly.

It works because the model has seen every angle already. It is being asked to interpolate rather than to extrapolate, and a short fine-tune closes the remaining gap.

NTK-aware scaling refines this.

The refinement scales the slow dimensions harder than the fast ones, so long-range position stretches while local word order stays exactly as trained.

That is how almost every open model published with a long context reached it, and it is why RoPE is now near-universal.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
What advantage do rotary embeddings have over fixed added position vectors?
Transformers

19 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close