Notes

Rotary Position Embedding (RoPE)

Attention, on its own, has no sense of order — shuffle the tokens and the arithmetic is unchanged. Position information has to be injected deliberately, and RoPE is the method nearly every current open-weight model uses to do it.

Rotation instead of addition

Earlier transformers added a position vector to the token embedding. RoPE instead rotates the query and key vectors by an angle proportional to the token's position, in pairs of dimensions, each pair at its own frequency. Because a dot product between two rotated vectors depends only on the difference of their angles, attention scores become a function of relative distance — token 500 attending to token 480 computes the same geometry as token 20 attending to token 0. Nothing is added to the values, nothing is learned, and the whole operation is a cheap elementwise multiply inside every attention layer.

Why it won
  • Relative positioning generalises: the model learns about distance, not about absolute slots in a fixed-length sequence.
  • It is compatible with the KV cache, since the rotation is applied when a key is created and stays correct for every later query.
  • The window can be stretched after training by rescaling the frequencies — position interpolation, NTK-aware scaling, YARN — which is how 4K-context models became 32K and beyond without pretraining from scratch.

It is in LLaMA, Mistral, Qwen, Gemma and most of what you will download from Hugging Face. Its one hard edge: quality degrades sharply past the trained range unless those frequencies are explicitly rescaled, so a long-context claim is a property of the scaling recipe, not of RoPE itself.

RoPE encodes token position by rotating the query and key vectors by an angle proportional to their position before attention is computed. Because a dot product depends only on the angle between vectors, attention scores become a function of the relative distance between tokens, with nothing learned and no extra parameters. It generalises better than absolute embeddings, works with the KV cache, and can be rescaled after training to extend a model's context window.

Words mean different things depending on where they sit in a sentence, so a language model needs some way of knowing the order of what it is reading. Oddly, its core machinery does not — to that machinery, a sentence is just a bag of words. RoPE is the trick that fixes this. It nudges each word's internal representation by an amount that depends on its position, like turning a dial one notch per word. The clever part is that only the gap between two words ends up mattering, not their absolute slots. That is why models using it cope with text far longer than the sentences they practised on.