DL0052 Rotary Positional Embedding

What is Rotary Positional Embedding (RoPE)?

Answer

Rotary Positional Embedding (RoPE) encodes position by rotating each pair of query/key dimensions by an angle proportional to the token’s position, instead of adding a position vector. Because a rotation at position m composed with one at position n depends only on the difference, the attention dot product between tokens automatically reflects their relative distance, with no learned parameters and good length generalization. RoPE is the default in GPT-NeoX, LLaMA, PaLM, Qwen, and most modern LLMs.

(1) Geometric Encoding: Each 2D slice of a query or key vector is rotated by m\theta_i, where m is the token position and \theta_i the per-dimension frequency.
(2) Relative From Absolute: The dot product of two rotated vectors depends on (m - n)\theta_i only: absolute rotations yield relative-position attention.
(3) Parameter-Free and Efficient: No embedding table, no attention bias, just a fixed trigonometric transform applied to Q and K.

Mathematical Formulation:
f(x, m) = \begin{pmatrix} \cos(m\theta) & -\sin(m\theta) \\ \sin(m\theta) & \cos(m\theta) \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \end{pmatrix}
\theta_i = 10000^{-2i/d}

Where:

  • m is the token’s absolute position in the sequence; x_1, x_2 are one 2D pair of the query/key vector.
  • \theta_i is the rotation frequency of the i-th dimension pair, geometrically decreasing across pairs so low dimensions capture short-range and high dimensions long-range offsets.
Line chart of attention similarity versus relative token distance, with RoPE similarity decaying smoothly as distance grows while sinusoidal absolute positional encoding stays flat.

Figure 1: Similarity vs relative distance: RoPE decays smoothly with distance (a built-in recency prior), while sinusoidal absolute PE stays roughly flat.

Why It Won: RoPE is shift-invariant (the same rotation rule works at any position), adds zero parameters or memory, and, unlike learned absolute embeddings, degrades gracefully beyond the training length, which is why context-extension methods (NTK scaling, YaRN) all build on rescaling its frequencies.


Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *