RoFormer
Introduced Rotary Position Embedding (RoPE), a method that mathematically integrates absolute positional information with relative distances, becoming the standard for modern LLMs.
Paper: RoFormer: Enhanced Transformer with Rotary Position Embedding
Authors: Jianlin Su, Yu Lu, Shengfeng Pan, Ahmed Murtadha, Bo Wen, Yunfeng Liu · 2021
Read the paperThe Problem
Standard Transformers natively have no sense of sequence order; they treat text as a "bag of words." The original Attention Is All You Need paper solved this by adding absolute sine/cosine positional encodings to the token embeddings before the first layer.
However, in natural language, the relative distance between words is often much more important than their absolute position. A noun directly following an adjective is highly relevant regardless of whether it happens at position 5 or position 500. Existing relative positional encodings (like those in T5 or Transformer-XL) required modifying the attention matrix directly, which was slow, complex, and incompatible with fast linear attention variants.
The Idea
The authors sought a mathematical way to encode absolute position such that when the model calculates the attention score (the dot product of the Query and Key vectors), the result depends only on the relative distance between them.
Their breakthrough was Rotary Position Embedding (RoPE). Instead of adding a vector to the embeddings, they treat the Query and Key vectors as complex numbers and rotate them in a 2D plane. The angle of rotation is proportional to the absolute position of the token.
Because of the properties of rotation matrices and complex dot products, when a rotated Query is multiplied by a rotated Key, the absolute angles cancel out, and the resulting dot product mathematically depends exactly on the angle difference—i.e., their relative distance.
How It Works
- The standard embedding vector is split into pairs of 2D coordinates.
- For a token at position , each 2D pair is rotated by an angle , where is a pre-defined frequency for that specific pair.
- This rotation is applied to the Query and Key vectors right before they are multiplied in the attention mechanism at every layer, rather than just at the input.
- The Value vectors are not rotated.
Why It Mattered
RoPE was elegant, mathematically sound, and computationally cheap. It provided the best of both worlds: the model knew the absolute position of every token, but the attention mechanism natively reacted to relative distances. It also showed promise for length extrapolation—the ability to train on short sequences and evaluate on longer ones.
What Came After
RoPE is arguably the most successful specific architectural tweak since the Transformer itself. While RoFormer as a model was just a proof of concept, RoPE superseded almost all other positional encodings. It was adopted by Google (PaLM), Meta (LLaMA, Llama 2, Llama 3), Mistral, DeepSeek, and countless others. It is the de facto standard positional encoding for modern Large Language Models.