How does Rotary Position Embedding (RoPE) inject position, and why is its effect described as relative?
- It adds a learned position vector to the token embedding once at the input, so deeper layers infer distance from it
- It rotates query and key vectors in 2D subspaces by a position-dependent angle, so the dot product depends on relative distance
- It multiplies the attention logits by a fixed distance penalty per head, so far-apart tokens are down-weighted linearly
- It appends the absolute integer position as an extra feature dimension that the query and key projections then learn to read
Why
RoPE encodes position by rotating each query and key vector within pairs of dimensions by an angle proportional to the token's absolute position, using the same geometric frequencies as sinusoidal encoding. The key algebraic property is that after rotating a query at position i and a key at position j, their dot product depends only on the difference i minus j, so the attention score is a function of relative distance even though the rotation itself uses absolute position. The learned-input-vector option describes additive learned positional embeddings, a different and now largely superseded method. The distance-penalty option describes ALiBi, which biases logits by distance rather than rotating vectors. The append-integer option is fabricated: RoPE does not add a raw position feature for projections to read; it transforms the existing query and key dimensions by rotation. Because RoPE is applied at every attention layer rather than once at the input, each layer receives fresh position information instead of relying on a signal surviving many residual connections. This combination of relative-distance behavior and cheap element-wise implementation is why RoPE dominates current large models.