Relative positional embeddings with RoPE
In language models, the order of tokens is of critical importance. This post explains RoPE: a technique that ensures that the pairwise token attention weights only depend on their relative position.

A core component of modern transformer-based language models is self-attention. With self-attention, the embedding of a specific token (or word) in a sentence is transformed by the embeddings of surrounding tokens, capturing sentence context. For example, the word "station" has different meanings in the sentences "I am listening to a radio station" and "I just left the train station".
Self-attention by itself is order-agnostic and would compute the same results for the sentence "left train I station the just" or "I just left the train station". This is unfortunate, because word order is important in natural language. So, how do we ensure that self-attention will consider token positions?
Position as a sinusoidal signal
The trick presented in the original "Attention is all you need" paper is to encode the position of a token into the embedding before computing attention values (Vaswani et al. 2017). These position encodings, or positional embeddings, can take various forms, and a popular approach is the sinusoidal positional embedding. The d-dimensional positional encoding for a position n is defined as follows:
Let's plot these values for varying n and i to obtain a better sense of what this encoding represents.

The encoding values lie in the range [-1, 1], with the values in the lower embedding dimensions changing more frequently than those in the higher dimensions. This is similar to binary numbers, where the least significant bits change much more frequently than the most significant bits (Bishop, 2023). This positional embedding vector is then simply added to a token embedding before computing attention:
A downside of this approach is that it encodes the absolute position into the token embedding. In many sequences, the relative ordering of words or tokens is more important. For example, in the sentences "I just left the train station" and "Because I was late, I left the train station," the relationship between "train" and "station" is the same, even though they occur at different absolute positions in each sentence.
This is where "Rotary Positional Encodings" (RoPE) come in (Su et al. 2023). RoPE similarly uses a sinusoidal signal to encode the position. However, instead of adding this signal to the original input vector, RoPE multiplies the sinusoidal signal with the input after projection into query or key space. For example, to compute a position-adjusted query vector from the original token at position , with the embedding dimension :
Those familiar with linear algebra might recognize the rotation matrix. The input thus gets rotated, with the rotation angle being dependent on the token position . We generalize to embedding dimensions greater than two by expanding matrix as follows:
Pairs of features get rotated with varying base frequencies . is defined similarly as in the absolute positional embeddings, with lower embedding dimensions rotating more quickly than higher embedding dimensions:
The benefit of using rotated vectors is that the angle between two position-adjusted vectors at and will only depend on their relative position, not on the absolute positions and .

Interpreting input features as complex numbers to derive the RoPE equation
How was RoPE's rotation equation obtained? To explain its derivation, let's recall that the attention weights are computed by taking the softmax of the inner product between query and key vectors. In the case of self-attention, the query and key vectors are derived from different tokens in the sequence:
When using absolute positional encodings, the query and key vectors will be derived from the position-adjusted vector . For RoPE, we aim to find an alternative function to encode positional information, such that the inner product between two position-adjusted vectors only depends on the relative position between two tokens. Mathematically, the following should hold:
To derive such a function, we turn to the complex domain. A d-dimensional vector can be interpreted as a complex (d/2)-dimensional vector . For example:
A complex number can alternatively be written in polar form , where and are called the radial and angular component of a complex number, respectively, and which are defined as and .
The standard inner product on is defined as , i.e., taking the complex conjugate and transpose of before multiplication with . Using this definition, and writing the complex numbers returned by and in polar form, we can obtain the following:
Here we have explicitly written the inner product between two complex numbers on the left-hand side, equating it to a complex number on the right-hand side, representing . Additionally, we have defined functions , , , and , representing the radial and angular components of the complex numbers returned by and , respectively.
The conjugate of a complex number in polar form is . With this definition, we can further simplify the left-hand side of Equation 2 by combining the product of two complex numbers:
Equating the radial and angular components in Equation 3, we obtain:
To make functions and more concrete, we start with a few simple initial conditions. Let's require that a token at the start of a sequence is unmodified by our positional embedding: . Here can refer to or . Additionally, we define , i.e., , and , where is the angular component of , i.e., the angular component of the reinterpreted values of or as a complex number.
Now, let's assess the case when :
Here, we have applied Equation 3 to obtain the result , and applied it another time to obtain . We use our initial condition definitions to obtain the final result.
Note that Equation 4 should hold for all token positions ; the final result, however, doesn't depend on . Thus the general solution for .
What can we infer for the radial component when ? Applying Equation 3 similarly to what we did for the radial component, we obtain:
Rearranging terms of the first and last components of Equation 5, we obtain:
This holds for all and , which implies that the change in the angular component of the position-adjusted complex number is independent of the or .
This insight gives us some freedom to define . For example, we can define it as follows:
In other words, we simply add a position-dependent value to the original angular component . But what would be? We can gain some insight by analyzing the difference between two subsequent positions, i.e., the case when .
Here we moved and to the left-hand side to obtain an expression for . Furthermore, by applying Equation 3, we transformed the left-hand side to an expression that does not depend on . This implies that the difference does not depend on either, and can be constant. We can define as a simple arithmetic progression:
In this definition, and are free to choose. For example, we can set to an embedding-dimension-specific value, and set .
Combining all pieces, we arrive at the following:
Through our constraint on relative position and a few simple initial conditions, we were able to define a simple expression that encodes the sequence position into a query or key vector. Finally, using Euler's formula, we obtain the rotation matrix we discussed at the beginning of this post:
Conclusion
Token order is critical for accurately interpreting natural language and biological sequences. By constraining the inner product between position-adjusted query and key vectors to be only dependent on their relative position, RoPE derives a simple transformation, rotating input vectors in the complex domain. This ensures the attention weights for two tokens are also influenced by their relative position.
References
Bishop, C. M., Bishop, H. & Cham, S. (ed.) (2023). Deep Learning - Foundations and Concepts. ISBN: 978-3-031-45468-4
Su, Jianlin, Yu Lu, Shengfeng Pan, Ahmed Murtadha, Bo Wen, and Yunfeng Liu. “RoFormer: Enhanced Transformer with Rotary Position Embedding.” arXiv:2104.09864. Preprint, arXiv, November 8, 2023. https://doi.org/10.48550/arXiv.2104.09864.
Vaswani, Ashish, Noam Shazeer, Niki Parmar, et al. “Attention Is All You Need.” arXiv:1706.03762. Preprint, arXiv, August 2, 2023. https://doi.org/10.48550/arXiv.1706.03762.


