











Rotary Position Embedding, usually called RoPE, is one of the most common positional encoding methods in modern decoder-only language models. It appears in models such as LLaMA and many of its descendants, and it is also the positional encoding used in CS336 Assignment 1.
RoPE is often introduced with one sentence: rotate the query and key vectors according to their positions. That description is correct, but it hides the most interesting part. The important question is not merely how to implement a rotation matrix. It is why rotating queries and keys makes the attention score depend on relative position, what mathematical invariants this creates, and why the same mechanism eventually becomes difficult to extrapolate to very long contexts.
The main idea of this post is:
absolute position in Q,K⟹relative position in QTK\text{absolute position in } Q,K \quad\Longrightarrow\quad \text{relative position in } Q^TK
We will derive this result and then study the main properties and patterns of RoPE:
Self-attention compares token representations through dot products. Without any positional signal, the mechanism has no direct notion of order. The sequence
1 | the cat chased the mouse |
and a reordered version contain the same token vectors, even though their meaning is different.
A causal mask helps, but it solves a different problem. It tells position ii that it may attend only to positions j≤ij\leq i. It does not explicitly tell the model how far away position jj is, nor does it create a continuous representation of relative distance.
This distinction is important:
RoPE is a way to inject position into the attention computation without adding a separate positional vector to the hidden state.
Start with a two-dimensional vector x=(x1,x2)Tx=(x_1,x_2)^T. Rotating it by angle ϕ\phi gives:
R(ϕ)x=[cosϕ−sinϕsinϕcosϕ][x1x2]R(\phi)x = \begin{bmatrix} \cos\phi&-\sin\phi\\ \sin\phi&\cos\phi \end{bmatrix} \begin{bmatrix} x_1\\x_2 \end{bmatrix}
Equivalently, if we identify (x1,x2)(x_1,x_2) with the complex number z=x1+ix2z=x_1+ix_2, the same operation is simply:
z⟼eiϕzz\longmapsto e^{i\phi}z
RoPE applies this operation to pairs of dimensions in a query or key vector. At sequence position mm, the rotation angle is proportional to mm:
ϕm=mω\phi_m=m\omega
where ω\omega is a frequency assigned to that pair of dimensions.
The position does not produce an additive vector. It determines how far the content vector is rotated in its two-dimensional plane.
Let the head dimension be dd, where dd is even. Pair the coordinates:
(x1,x2),(x3,x4),…,(xd−1,xd)(x_1,x_2),(x_3,x_4),\ldots,(x_{d-1},x_d)
The kk-th pair receives its own frequency ωk\omega_k. At position mm, its angle is:
ϕm,k=mωk\phi_{m,k}=m\omega_k
The full rotation is a block-diagonal matrix:
Rm=diag(R(mω1),R(mω2),…,R(mωd/2))R_m= \operatorname{diag} \left( R(m\omega_1),R(m\omega_2),\ldots,R(m\omega_{d/2}) \right)
and the rotated vector is:
xm′=Rmxmx_m'=R_mx_m
In the standard RoPE parameterization used by many LLMs, the frequencies are geometrically spaced:
ωk=Θ−2k/d\omega_k=\Theta^{-2k/d}
or, depending on whether indexing starts at zero or one,
θm,k=mΘ2k/d\theta_{m,k}=\frac{m}{\Theta^{2k/d}}
Here Θ\Theta is commonly set to 10,00010{,}000. The exact indexing convention varies between implementations, but the essential pattern is the same: the frequencies form a geometric progression from fast to slow.
The model does not learn these frequencies in standard RoPE. They are determined by the hyperparameter Θ\Theta and the head dimension.
Let qmq_m be the content-dependent query at position mm and knk_n be the key at position nn. RoPE produces:
qm′=Rmqm,kn′=Rnknq_m'=R_mq_m,\qquad k_n'=R_nk_n
The attention score is their dot product:
(qm′)Tkn′=(Rmqm)T(Rnkn)(q_m')^Tk_n' =(R_mq_m)^T(R_nk_n)
Using (AB)T=BTAT(AB)^T=B^TA^T:
(qm′)Tkn′=qmTRmTRnkn(q_m')^Tk_n' =q_m^TR_m^TR_nk_n
For a rotation matrix, RmT=R−mR_m^T=R_{-m}. Rotations also compose additively:
RaRb=Ra+bR_aR_b=R_{a+b}
Therefore:
RmTRn=R−mRn=Rn−mR_m^TR_n=R_{-m}R_n=R_{n-m}
and the score becomes:
(qm′)Tkn′=qmTRn−mkn(q_m')^Tk_n' =q_m^TR_{n-m}k_n
This is the core property of RoPE. The query and key are individually transformed using absolute positions mm and nn, but their interaction depends on the relative displacement n−mn-m.
The content vectors qmq_m and knk_n still matter. RoPE does not replace content similarity with distance-only attention. Instead, it gives the content interaction a position-dependent transformation.
Because the full RoPE matrix is block diagonal, the same derivation applies independently to every two-dimensional frequency plane:
(qm′)Tkn′=∑kqm,kTR((n−m)ωk)kn,k(q_m')^Tk_n' =\sum_k q_{m,k}^T R((n-m)\omega_k) k_{n,k}
The final attention score is a sum of relative-position interactions evaluated at multiple frequencies.
Every rotation matrix is orthogonal:
RmTRm=IR_m^TR_m=I
Therefore:
∥Rmx∥22=xTRmTRmx=xTx=∥x∥22\lVert R_mx\rVert_2^2 =x^TR_m^TR_mx =x^Tx =\lVert x\rVert_2^2
RoPE changes direction but not magnitude.
This is useful for optimization. The positional transformation itself cannot amplify or shrink a vector. It only changes its phase in each two-dimensional plane. Of course, the overall Transformer can still change the norm through linear layers, RMSNorm, residual connections, and nonlinearities; norm preservation applies specifically to the rotation.
Rotating first by aa and then by bb is equivalent to rotating once by a+ba+b:
RaRb=Ra+bR_aR_b=R_{a+b}
This makes position shifts easy to reason about. If every position is shifted by the same offset cc:
qm′=Rm+cqm,kn′=Rn+cknq_m' = R_{m+c}q_m,\qquad k_n'=R_{n+c}k_n
then their relative interaction is still governed by:
Rm+cTRn+c=Rn−mR_{m+c}^TR_{n+c}=R_{n-m}
The shared offset cancels. This translation structure is one reason RoPE is naturally compatible with relative positions.
Because a common shift cancels inside RmTRnR_m^TR_n, the attention score does not fundamentally depend on where the sequence starts. It depends on the distance between the two positions, together with their content vectors.
This does not mean that an entire Transformer becomes perfectly translation-invariant. Causal boundaries, finite context windows, padding, document delimiters, and the learned content representations can all break simple global invariance. The statement is specifically about the positional part of the QTKQ^TK interaction.
RoPE encodes the signed displacement n−mn-m, not only the absolute distance ∣n−m∣|n-m|.
In general:
Rn−m≠Rm−nR_{n-m}\neq R_{m-n}
because the two matrices rotate in opposite directions. Therefore, the mechanism can distinguish “the key is 10 positions to the right” from “the key is 10 positions to the left,” subject to the causal mask and the content learned by the model.
If every pair of dimensions used the same frequency, all pairs would repeat with the same period. The model would have only one positional clock.
RoPE instead uses a geometric frequency schedule:
ωk=Θ−2k/d\omega_k=\Theta^{-2k/d}
The corresponding period of one frequency is:
Tk=2πωkT_k=\frac{2\pi}{\omega_k}
This produces a collection of clocks:
The model can combine these signals to distinguish both nearby and distant positions.
The frequency spectrum is similar to using several measurement rulers:
RoPE gives the attention mechanism many such rulers at once. A particular relative distance is represented by the vector of phases across all frequency pairs.
Consider a simplified setting where the content vectors are fixed and focus only on the positional contribution. In one two-dimensional plane, the relative-position term contains sine and cosine:
qTR(Δω)kq^TR(\Delta\omega)k
As Δ=n−m\Delta=n-m changes, this term oscillates. Across all frequency planes, the total positional interaction is a sum of oscillations:
K(Δ)≈∑kcos(Δωk)K(\Delta)\approx\sum_k \cos(\Delta\omega_k)
The exact expression depends on the query and key components, but this simplified kernel is useful for understanding the pattern.
The original RoPE analysis highlights a decaying tendency of relative-position dependence as the distance increases. More precisely, the aggregate kernel often has a strong central peak and decreasing average correlation at larger distances, but it is not strictly monotonic. Because it is a sum of periodic functions, it can oscillate and produce secondary peaks.
This distinction matters:
RoPE therefore favors nearby interactions without imposing a hard local window. The model can still attend to distant positions when the content-dependent query-key interaction makes them useful.
Every frequency is periodic:
R(ϕ+2π)=R(ϕ)R(\phi+2\pi)=R(\phi)
For a single frequency ωk\omega_k, positions separated by its period TkT_k have the same phase:
(m+Tk)ωk≡mωk(mod2π)(m+T_k)\omega_k\equiv m\omega_k\pmod{2\pi}
This is phase wrapping. A high-frequency component wraps quickly; a low-frequency component wraps slowly.
The full vector of frequencies makes the combined representation much less ambiguous than any single frequency. However, at sufficiently long distances, multiple frequency components can become difficult to distinguish, especially when the model is evaluated far beyond the position range seen during training.
This is the connection between RoPE and aliasing. The model does not receive an unbounded, perfectly unique coordinate. It receives a collection of periodic phases.
For a fixed frequency, the phase difference is:
Δϕ=Δωk\Delta\phi=\Delta\omega_k
As Δ\Delta grows, high-frequency components move through many cycles. Small changes in position can cause large phase changes, while large changes can land on similar phases after wrapping.
Low-frequency components are smoother and more stable over long distances, but they provide less local resolution. Long-context methods therefore often modify the frequency allocation or rescale the position index to make the learned phase range usable at a larger context length.
Attention has two conceptually different stages:
RoPE is applied to QQ and KK because position should affect the matching score:
score(m,n)=(Rmqm)T(Rnkn)\operatorname{score}(m,n) =(R_mq_m)^T(R_nk_n)
The value vector carries the content that will be transmitted after the matching decision. Keeping VV unrotated means the message itself is not transformed into a position-dependent coordinate system.
In standard RoPE:
Q′=RoPE(Q),K′=RoPE(K),V′=VQ'=\operatorname{RoPE}(Q),\qquad K'=\operatorname{RoPE}(K),\qquad V'=V
This separation is not mathematically inevitable; it is an architectural choice. The key property of standard RoPE is that relative position enters the attention logits through QTKQ^TK, while the value path remains position-agnostic.
A natural question is: if position should affect the representation, why not rotate XX first and then compute Q=XWQQ=XW_Q and K=XWKK=XW_K?
Suppose we rotate the input:
xm′=Rmxmx_m'=R_mx_m
Then the query becomes:
qm′=WQRmxmq_m'=W_QR_mx_m
This is not generally the same as:
RmWQxmR_mW_Qx_m
because the learned projection WQW_Q does not generally commute with the rotation matrix:
WQRm≠RmWQW_QR_m\neq R_mW_Q
The clean relative-position derivation depends on rotating the projected QQ and KK vectors:
(RmWQxm)T(RnWKxn)(R_mW_Qx_m)^T(R_nW_Kx_n)
not on rotating the input before arbitrary learned projections.
Rotating XX would entangle position with the input representation before the model decides how to form queries, keys, and values. RoPE instead injects position at the exact point where relative position is used: the attention matching operation.
Standard RoPE rotates all dimensions of a head. Partial RoPE, also called half RoPE in some implementations, applies rotation only to a subset of the head dimension.
Let the head dimension be dd and let the rotary dimension be dr≤dd_r\leq d. We split:
x=[xrot;xpass]x=[x_{rot};x_{pass}]
and apply:
RoPE(x)=[Rmxrot;xpass]\operatorname{RoPE}(x) = [R_mx_{rot};x_{pass}]
The rotated part carries the relative-position mechanism. The pass-through part is not rotated and can preserve content features without phase modulation.
Why might this help? Full RoPE forces every query and key dimension to participate in the periodic positional transformation. Partial RoPE gives the model a mixture of:
This can be useful when the model benefits from reserving some dimensions for content matching that is less affected by position. The tradeoff is that the positional signal has lower dimensional capacity.
Partial RoPE is not the same as applying a smaller RoPE to every vector by truncating the model. The unrotated dimensions remain part of the attention dot product and still contribute content similarity.
RoPE is especially important during autoregressive inference because keys are usually cached.
Suppose a prompt occupies positions 00 through T−1T-1, and the next generated token is at position TT. The new query must be rotated with position TT:
qT′=RTqTq_T'=R_Tq_T
Each cached key must retain the rotation corresponding to the position where it was originally created:
kj′=Rjkj,0≤j<Tk_j'=R_jk_j,\qquad 0\leq j<T
The score is then:
(qT′)Tkj′=qTTRTTRjkj=qTTRj−Tkj(q_T')^Tk_j' =q_T^TR_T^TR_jk_j =q_T^TR_{j-T}k_j
The important implementation consequence is that a cached key should not be re-rotated using the current decoding position. Its position is part of its identity.
This creates a common source of bugs: the position index used during decoding must continue from the prompt length, rather than restarting at zero for every generated token or every chunk.
RoPE itself does not require a KV cache, but the cache makes its position convention operationally important. In long-running generation, a position offset error changes every subsequent attention score.
The full d×dd\times d rotation matrix is almost never materialized. Instead, implementations cache the cosine and sine values for every position and every rotary dimension.
For a vector split into two halves x1x_1 and x2x_2, one common representation is:
rotate_half(x)=[−x2;x1]\operatorname{rotate\_half}(x)=[-x_2;x_1]
Then:
RoPE(x)=x⊙cosθ+rotate_half(x)⊙sinθ\operatorname{RoPE}(x) =x\odot\cos\theta +\operatorname{rotate\_half}(x)\odot\sin\theta
The exact pairing convention varies. Some implementations interleave the pairs as (x1,x2),(x3,x4),…(x_1,x_2),(x_3,x_4),\ldots; others split the vector into two halves and pair the corresponding coordinates. Both can be correct, but the cosine/sine layout must match the chosen rotation convention.
The essential implementation invariants are:
head_dim or rotary_dim must be even;RoPE has no learnable parameters in its standard form. The model learns how to use the rotated representations, but the rotation frequencies themselves are fixed by the configuration.
RoPE is often trained with a maximum context length LtrainL_{train} and later evaluated at a longer length LtestL_{test}. If Ltest≫LtrainL_{test}\gg L_{train}, several problems can appear.
During training, the model sees angles in the range:
0≤mωk<Ltrainωk0\leq m\omega_k<L_{train}\omega_k
At a longer context, it sees a phase range that may be far outside training. The model may not know how to interpret those unseen phase combinations.
High-frequency dimensions rotate many times over a long context. Their phases change rapidly and wrap around frequently. Low-frequency dimensions extrapolate more smoothly but may not resolve nearby positions as precisely.
A long-context extension often tries to rescale the position indices or alter the frequency schedule so that the new positions remain within a phase range that resembles training.
The broad strategies include:
The common principle is not “make the context length larger for free.” It is “change how positions map to phases so the model sees a more manageable distribution.” Long-context capability depends on both the position encoding and the training distribution.
A learned absolute embedding adds a vector pmp_m to the token representation:
hm=xm+pmh_m=x_m+p_m
This is simple and expressive within the trained position range, but the model must store a representation for each position, and extrapolation beyond the learned table is difficult.
The original Transformer uses fixed sine and cosine vectors. They can represent positions without learned parameters and have useful algebraic structure, but the positional signal is added to the hidden representation rather than inserted directly into the QKQK matching operation.
ALiBi adds a distance-dependent linear bias to attention logits:
score(m,n)=qmTkn−bh∣m−n∣\operatorname{score}(m,n) =q_m^Tk_n-b_h|m-n|
for head-specific slope bhb_h. It directly biases attention toward nearby positions and has a different extrapolation behavior from RoPE.
RoPE multiplies QQ and KK by position-dependent rotations. Its distinctive properties are:
There is no universally best positional encoding. The right choice depends on the model architecture, training context, inference length, and desired inductive bias.
| Property | Mathematical reason | Practical implication |
|---|---|---|
| Norm preserving | RmTRm=IR_m^TR_m=I | Position changes direction, not magnitude |
| Relative interaction | RmTRn=Rn−mR_m^TR_n=R_{n-m} | Attention can depend on relative displacement |
| Translation structure | Rm+cTRn+c=RmTRnR_{m+c}^TR_{n+c}=R_m^TR_n | A shared position offset cancels in the score |
| Multi-scale resolution | Geometric frequencies ωk\omega_k | Fast and slow positional clocks coexist |
| Periodicity | R(ϕ+2π)=R(ϕ)R(\phi+2\pi)=R(\phi) | Phase wrapping and aliasing can occur |
| Content preservation in VV | RoPE is applied to Q,KQ,K only | Position changes matching, not the transmitted value |
| Parameter-free standard form | Frequencies are fixed | No positional weights are learned |
| Partial RoPE | Rotate only dr<dd_r<d dimensions | Mix position-aware and pass-through features |
RoPE does not prevent access to future tokens. The causal mask does that. RoPE provides a position-dependent transformation; the mask controls visibility.
Standard RoPE rotates QQ and KK, not VV. Rotating VV changes the value path and is a different architectural choice.
The clean relative-position derivation applies to RmWQxmR_mW_Qx_m and RnWKxnR_nW_Kx_n. In general, WQRm≠RmWQW_QR_m\neq R_mW_Q, so rotating XX before the learned projections is not equivalent.
When decoding after a prompt, the next token starts at the prompt length. Restarting positions from zero can silently corrupt every subsequent attention score.
Interleaved pairs and half-split pairs are both possible. The error comes from using one convention for xx and another for the cached sine/cosine layout.
RoPE uses a sum of periodic functions. Its average dependence may decay with relative distance, but the exact score can oscillate and have secondary peaks.
Even if the hardware can store a longer context, the model may not know how to interpret the new phase distribution. Context extension is also a positional-distribution and training problem.
RoPE is elegant because a simple local operation creates a useful global property. Rotate the query at position mm and the key at position nn:
qm′=Rmqm,kn′=Rnknq_m'=R_mq_m,\qquad k_n'=R_nk_n
Then their attention interaction becomes:
(qm′)Tkn′=qmTRn−mkn(q_m')^Tk_n'=q_m^TR_{n-m}k_n
Absolute positions disappear from the positional part of the interaction, leaving relative displacement.
The rest of RoPE follows from this algebra: rotations preserve norms, compose additively, create a multi-scale frequency spectrum, and introduce periodic phase behavior. Those same properties explain both its strengths and its limitations. RoPE gives Transformers a clean relative-position inductive bias, but long-context inference must account for frequency resolution, phase wrapping, aliasing, and position-index consistency.
For LLM systems, RoPE is therefore more than a small positional-encoding layer. It is part of the contract between the model architecture and the inference runtime: the query, cached keys, sequence offsets, rotary dimension, and frequency schedule must all agree.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。