Rotary Position Embeddings (RoPE)

Notation

NotationDescription
๐’’๐‘š๐‘š-th query vector without positional information
๐’Œ๐‘›๐‘›-th key vector without positional information
๐’’๐‘šโ€ฒ๐‘š-th query vector with positional information
๐’Œ๐‘›โ€ฒ๐‘›-th key vector with positional information

Motivations: We Pursue Relative Positional Embedding

To incorporate positional information into the attention mechanism, we need to transform the original query and key vectors. The functions ๐‘“(โ‹…,โ‹…) encode the position indices ๐‘š and ๐‘› into the query and key vectors respectively, resulting in position-aware representations ๐’’๐‘šโ€ฒ and ๐’Œ๐‘›โ€ฒ.

๐’’๐‘šโ€ฒ=๐‘“(๐’’๐‘š,๐‘š),๐’Œ๐‘›โ€ฒ=๐‘“(๐’Œ๐‘›,๐‘›)

Typically, the attention score between a query at position ๐‘š and a key at position ๐‘› can be represented as a function ๐‘” that depends on both the content vectors (๐’’๐‘š and ๐’Œ๐‘›) and their absolute positions (๐‘š and ๐‘›) as follows:

Attn(๐’’๐‘šโ€ฒ,๐’Œ๐‘›โ€ฒ)=๐‘”(๐’’๐‘š,๐’Œ๐‘›,๐‘š,๐‘›)

but we want the attention score to only depend on the relative position (๐‘šโˆ’๐‘›) rather than absolute positions ๐‘š and ๐‘›, as relative position is easier to generalize to unseen sequence lengths.

Goal of relative positional embedding: Thus our goal is to find a function ๐‘” which is only a function of ๐’’๐‘š, ๐’Œ๐‘›, and ๐‘šโˆ’๐‘›, instead of ๐‘š and ๐‘› themselves as follows:

Attn(๐’’๐‘šโ€ฒ,๐’Œ๐‘›โ€ฒ)=๐‘”(๐’’๐‘š,๐’Œ๐‘›,๐‘šโˆ’๐‘›)

The RoPE is a solution to this goal.

RoPE (Rotational Position Embedding)

RoPE is a positional embedding that is a function of the relative position ๐‘šโˆ’๐‘›, which rotates the query and key vectors and then computes the attention score, which is a function of the relative position ๐‘šโˆ’๐‘›.

The base idea of RoPE is to rotate the query and key vectors by a certain angle, thus their dot product is a function of the relative position ๐‘šโˆ’๐‘›. RoPE used the property of rotation matrix multiplication. When we need to rotate a 2-dimensional vector (๐‘ฅ๐‘ฆ) by an angle ๐œƒ, we can multiply a rotation matrix (cos๐œƒโˆ’sin๐œƒsin๐œƒcos๐œƒ) to the vector (๐‘ฅ๐‘ฆ), to get the rotated vector (๐‘ฅcos๐œƒโˆ’๐‘ฆsin๐œƒ๐‘ฅsin๐œƒ+๐‘ฆcos๐œƒ).

In the following, suppose the query and key vectors are 2-dimensional vectors. We can rotate the query and key:

๐’’๐‘šโ€ฒ=๐‘“(๐’’๐‘š,๐‘š)=(cos๐‘š๐œƒโˆ’sin๐‘š๐œƒsin๐‘š๐œƒcos๐‘š๐œƒ)(๐‘ž๐‘š1๐‘ž๐‘š2)๐’Œ๐‘›โ€ฒ=๐‘“(๐’Œ๐‘›,๐‘›)=(cos๐‘›๐œƒโˆ’sin๐‘›๐œƒsin๐‘›๐œƒcos๐‘›๐œƒ)(๐‘˜๐‘›1๐‘˜๐‘›2)

The attention score is then:

Attn(๐’’๐‘šโ€ฒ,๐’Œ๐‘›โ€ฒ)=๐’’๐‘šโ€ฒ๐‘‡โ‹…๐’Œ๐‘›โ€ฒ=[(cos๐‘š๐œƒโˆ’sin๐‘š๐œƒsin๐‘š๐œƒcos๐‘š๐œƒ)(๐‘ž๐‘š1๐‘ž๐‘š2)]๐‘‡โ‹…[(cos๐‘›๐œƒโˆ’sin๐‘›๐œƒsin๐‘›๐œƒcos๐‘›๐œƒ)(๐‘˜๐‘›1๐‘˜๐‘›2)]=(๐‘ž๐‘š1๐‘ž๐‘š2)[(cos๐‘š๐œƒโˆ’sin๐‘š๐œƒsin๐‘š๐œƒcos๐‘š๐œƒ)๐‘‡(cos๐‘›๐œƒโˆ’sin๐‘›๐œƒsin๐‘›๐œƒcos๐‘›๐œƒ)](๐‘˜๐‘›1๐‘˜๐‘›2)=(๐‘ž๐‘š1๐‘ž๐‘š2)[(cos๐‘š๐œƒsin๐‘š๐œƒโˆ’sin๐‘š๐œƒcos๐‘š๐œƒ)(cos๐‘›๐œƒโˆ’sin๐‘›๐œƒsin๐‘›๐œƒcos๐‘›๐œƒ)](๐‘˜๐‘›1๐‘˜๐‘›2)=(๐‘ž๐‘š1๐‘ž๐‘š2)(cos((๐‘›โˆ’๐‘š)๐œƒ)โˆ’sin((๐‘›โˆ’๐‘š)๐œƒ)sin((๐‘›โˆ’๐‘š)๐œƒ)cos((๐‘›โˆ’๐‘š)๐œƒ))(๐‘˜๐‘›1๐‘˜๐‘›2)=๐‘ž๐‘š1[๐‘˜๐‘›1cos((๐‘›โˆ’๐‘š)๐œƒ)โˆ’๐‘˜๐‘›2sin((๐‘›โˆ’๐‘š)๐œƒ)]+๐‘ž๐‘š2[๐‘˜๐‘›1sin((๐‘›โˆ’๐‘š)๐œƒ)+๐‘˜๐‘›2cos((๐‘›โˆ’๐‘š)๐œƒ)]=(๐‘ž๐‘š1๐‘˜๐‘›1+๐‘ž๐‘š2๐‘˜๐‘›2)cos((๐‘›โˆ’๐‘š)๐œƒ)+(๐‘ž๐‘š2๐‘˜๐‘›1โˆ’๐‘ž๐‘š1๐‘˜๐‘›2)sin((๐‘›โˆ’๐‘š)๐œƒ)=๐‘”(๐’’๐‘š,๐’Œ๐‘›,๐‘šโˆ’๐‘›)

thus we have shown that the attention score is a function of ๐’Œ๐‘›, ๐’’๐‘š, and their relative position ๐‘šโˆ’๐‘›, not the absolute positions ๐‘š and ๐‘› themselves.

RoPE Implementation (Half-and-Half Pairing)

This is the method used in your Python code and in many popular implementations like LLaMA. It is chosen for its extreme efficiency with vectorized operations. Instead of pairing adjacent dimensions (which is intuitive), this method pairs the first half of the dimensions with the second half.

For a vector ๐’’ with dim=๐‘‘:

  • Pair 0: dimension 0 is paired with dimension ๐‘‘2. (๐‘ž0,๐‘ž๐‘‘2)
  • Pair 1: dimension 1 is paired with dimension ๐‘‘2+1. (๐‘ž1,๐‘ž๐‘‘2+1)
  • โ€ฆ
  • Pair ๐‘‘2โˆ’1: dimension ๐‘‘2โˆ’1 is paired with dimension ๐‘‘โˆ’1. (๐‘ž๐‘‘2โˆ’1,๐‘ž๐‘‘โˆ’1)

In practice, the query and key vectors are not 2-dimensional vectors, but ๐‘‘-dimensional vectors (๐‘‘%2=0).

๐’’๐‘š=(๐‘ž๐‘š1๐‘ž๐‘š2โ‹ฎ๐‘ž๐‘š๐‘‘),๐’Œ๐‘›=(๐‘˜๐‘›1๐‘˜๐‘›2โ‹ฎ๐‘˜๐‘›๐‘‘)

we then group the query and key vectors into ๐‘‘2 pairs, and rotate each pair by a different angle ๐œƒ๐‘– (๐‘–=1,2,โ€ฆ,๐‘‘2). Usually we make the following pairs: [(๐‘ž๐‘š0๐‘ž๐‘š๐‘‘2),(๐‘˜๐‘š0๐‘˜๐‘š๐‘‘2)],[(๐‘ž๐‘š1๐‘ž๐‘š๐‘‘2+1),(๐‘˜๐‘š1๐‘˜๐‘š๐‘‘2+1)],โ€ฆ,[(๐‘ž๐‘š๐‘‘2โˆ’1๐‘ž๐‘š๐‘‘โˆ’1),(๐‘˜๐‘š๐‘‘2โˆ’1๐‘˜๐‘š๐‘‘โˆ’1)].

For each pair ๐‘– (๐‘–=0,1,โ€ฆ,๐‘‘2โˆ’1), we rotate by an angle ๐œƒ๐‘–=1๐œ”2๐‘–๐‘‘=1100002๐‘–๐‘‘, where ๐œ” is a base frequency (typically 10000), ๐‘– is the ๐‘–-th dimension ๐‘–โˆˆ[0,1,2,โ€ฆ,๐‘‘2โˆ’1], and ๐‘‘ is the dimension of the query and key vectors.

This means higher dimensions get rotated by larger angles, creating a spectrum of different frequencies in the positional encoding.

The rotated query vector becomes:

๐’’๐‘šโ€ฒ=(๐‘ž๐‘š1cos(๐œƒ0๐‘š)โˆ’๐‘ž๐‘š๐‘‘2+1sin(๐œƒ0๐‘š)๐‘ž๐‘š2cos(๐œƒ2๐‘‘๐‘š)โˆ’๐‘ž๐‘š๐‘‘2+2sin(๐œƒ2๐‘‘๐‘š)โ‹ฎ๐‘ž๐‘š๐‘‘2cos(๐œƒ๐‘‘โˆ’2๐‘‘๐‘š)โˆ’๐‘ž๐‘š๐‘‘sin(๐œƒ๐‘‘โˆ’2๐‘‘๐‘š)๐‘ž๐‘š1sin(๐œƒ0๐‘š)+๐‘ž๐‘š๐‘‘2+1cos(๐œƒ0๐‘š)๐‘ž๐‘š2sin(๐œƒ2๐‘‘๐‘š)+๐‘ž๐‘š๐‘‘2+2cos(๐œƒ2๐‘‘๐‘š)โ‹ฎ๐‘ž๐‘š๐‘‘2sin(๐œƒ๐‘‘โˆ’2๐‘‘๐‘š)+๐‘ž๐‘š๐‘‘cos(๐œƒ๐‘‘โˆ’2๐‘‘๐‘š))=(๐‘ž๐‘š1cos(๐œƒ0๐‘š)โˆ’๐‘ž๐‘š๐‘‘2+1sin(๐œƒ0๐‘š)๐‘ž๐‘š2cos(๐œƒ2๐‘‘๐‘š)โˆ’๐‘ž๐‘š๐‘‘2+2sin(๐œƒ2๐‘‘๐‘š)โ‹ฎ๐‘ž๐‘š๐‘‘2cos(๐œƒ๐‘‘โˆ’2๐‘‘๐‘š)โˆ’๐‘ž๐‘š๐‘‘sin(๐œƒ๐‘‘โˆ’2๐‘‘๐‘š)๐‘ž๐‘š๐‘‘2+1cos(๐œƒ0๐‘š)+๐‘ž๐‘š1sin(๐œƒ0๐‘š)๐‘ž๐‘š๐‘‘2+2cos(๐œƒ2๐‘‘๐‘š)+๐‘ž๐‘š2sin(๐œƒ2๐‘‘๐‘š)โ‹ฎ๐‘ž๐‘š๐‘‘cos(๐œƒ๐‘‘โˆ’2๐‘‘๐‘š)+๐‘ž๐‘š๐‘‘2sin(๐œƒ๐‘‘โˆ’2๐‘‘๐‘š))

Similarly for the key vector:

๐’Œ๐‘›โ€ฒ=(๐‘˜๐‘›1cos(๐œƒ0๐‘›)โˆ’๐‘˜๐‘›๐‘‘2+1sin(๐œƒ0๐‘›)๐‘˜๐‘›2cos(๐œƒ2๐‘‘๐‘›)โˆ’๐‘˜๐‘›๐‘‘2+2sin(๐œƒ2๐‘‘๐‘›)โ‹ฎ๐‘˜๐‘›๐‘‘2cos(๐œƒ๐‘‘โˆ’2๐‘‘๐‘›)โˆ’๐‘˜๐‘›๐‘‘sin(๐œƒ๐‘‘โˆ’2๐‘‘๐‘›)๐‘˜๐‘›1sin(๐œƒ0๐‘›)+๐‘˜๐‘›๐‘‘2+1cos(๐œƒ0๐‘›)๐‘˜๐‘›2sin(๐œƒ2๐‘‘๐‘›)+๐‘˜๐‘›๐‘‘2+2cos(๐œƒ2๐‘‘๐‘›)โ‹ฎ๐‘˜๐‘›๐‘‘2sin(๐œƒ๐‘‘โˆ’2๐‘‘๐‘›)+๐‘˜๐‘›๐‘‘cos(๐œƒ๐‘‘โˆ’2๐‘‘๐‘›))=(๐‘˜๐‘›1cos(๐œƒ0๐‘›)โˆ’๐‘˜๐‘›๐‘‘2+1sin(๐œƒ0๐‘›)๐‘˜๐‘›2cos(๐œƒ2๐‘‘๐‘›)โˆ’๐‘˜๐‘›๐‘‘2+2sin(๐œƒ2๐‘‘๐‘›)โ‹ฎ๐‘˜๐‘›๐‘‘2cos(๐œƒ๐‘‘โˆ’2๐‘‘๐‘›)โˆ’๐‘˜๐‘›๐‘‘sin(๐œƒ๐‘‘โˆ’2๐‘‘๐‘›)๐‘˜๐‘›๐‘‘2+1cos(๐œƒ0๐‘›)+๐‘˜๐‘›1sin(๐œƒ0๐‘›)๐‘˜๐‘›๐‘‘2+2cos(๐œƒ2๐‘‘๐‘›)+๐‘˜๐‘›2sin(๐œƒ2๐‘‘๐‘›)โ‹ฎ๐‘˜๐‘›๐‘‘cos(๐œƒ๐‘‘โˆ’2๐‘‘๐‘›)+๐‘˜๐‘›๐‘‘2sin(๐œƒ๐‘‘โˆ’2๐‘‘๐‘›))

When we compute the attention score between these rotated vectors, each pair contributes a term that depends on the relative position (๐‘šโˆ’๐‘›), similar to the 2D case we analyzed earlier. The different frequencies ๐œ”2๐‘–๐‘‘ allow the model to capture position-dependent patterns at different scales.

Thus we have:

๐’’๐‘šโ€ฒ๐‘‡โ‹…๐’Œ๐‘›โ€ฒ=[(๐‘ž๐‘š1๐‘˜๐‘›1+๐‘ž๐‘š๐‘‘2๐‘˜๐‘›๐‘‘2)cos((๐‘›โˆ’๐‘š)๐œƒ)+(๐‘ž๐‘š๐‘‘2๐‘˜๐‘›1โˆ’๐‘ž๐‘š1๐‘˜๐‘›๐‘‘2)sin((๐‘›โˆ’๐‘š)๐œƒ)]+โ€ฆ+[(๐‘ž๐‘š๐‘‘2โˆ’1๐‘˜๐‘›๐‘‘2โˆ’1+๐‘ž๐‘š๐‘‘โˆ’1๐‘˜๐‘›๐‘‘โˆ’1)cos((๐‘›โˆ’๐‘š)๐œƒ)+(๐‘ž๐‘š๐‘‘โˆ’1๐‘˜๐‘›๐‘‘2โˆ’1โˆ’๐‘ž๐‘š๐‘‘2โˆ’1๐‘˜๐‘›๐‘‘โˆ’1)sin((๐‘›โˆ’๐‘š)๐œƒ)]=๐‘”(๐’’๐‘š,๐’Œ๐‘›,๐‘šโˆ’๐‘›)

Example:

Letโ€™s walk through how the code achieves this with a concrete example where dim =8. The input query vector is ๐’’=[๐‘ž0,๐‘ž1,๐‘ž2,๐‘ž3,๐‘ž4,๐‘ž5,๐‘ž6,๐‘ž7].

Pairing: The pairs will be: (๐‘ž0,๐‘ž4),(๐‘ž1,๐‘ž5),(๐‘ž2,๐‘ž6),(๐‘ž3,๐‘ž7).

Frequency: There will be ๐‘‘2=4 unique angles for a given position ๐‘š: ๐œƒ0,๐œƒ1,๐œƒ2,๐œƒ3. These angles are calculated based on the position ๐‘š and the pair index ๐‘–โˆˆ[0,1,2,3]:

๐œƒ๐‘š๐‘–=1100002๐‘–๐‘‘โ‹…๐‘š

where ๐‘‘ is the dimension. For example, ๐œƒ๐‘š0=1100002โˆ—08โ‹…๐‘š=1100000โ‹…๐‘š=1โ‹…๐‘š=๐‘š

We construct the angles for a given position ๐‘š as [๐œƒ๐‘š0,๐œƒ๐‘š1,๐œƒ๐‘š2,๐œƒ๐‘š3,๐œƒ๐‘š0,๐œƒ๐‘š1,๐œƒ๐‘š2,๐œƒ๐‘š3].

The final cos and sin tensors will therefore have this duplicated structure. For position m:

cos๐‘š=[cos(๐œƒ๐‘š0),cos(๐œƒ๐‘š1),cos(๐œƒ๐‘š2),cos(๐œƒ๐‘š3),cos(๐œƒ๐‘š0),cos(๐œƒ๐‘š1),cos(๐œƒ๐‘š2),cos(๐œƒ๐‘š3)]

sin๐‘š=[sin(๐œƒ๐‘š0),sin(๐œƒ๐‘š1),sin(๐œƒ๐‘š2),sin(๐œƒ๐‘š3),sin(๐œƒ๐‘š0),sin(๐œƒ๐‘š1),sin(๐œƒ๐‘š2),sin(๐œƒ๐‘š3)]

Now, letโ€™s see how the rotation is applied to ๐’’=[๐‘ž0,๐‘ž1,๐‘ž2,๐‘ž3,๐‘ž4,๐‘ž5,๐‘ž6,๐‘ž7].

We construct half-rotated query vector as ๐’’rotated=[โˆ’๐‘ž4,โˆ’๐‘ž5,โˆ’๐‘ž6,โˆ’๐‘ž7,๐‘ž0,๐‘ž1,๐‘ž2,๐‘ž3].

We then construct the query vector ๐’’๐‘šโ€ฒ as:

๐’’๐‘šโ€ฒ=(๐’’โ‹…cos)+(๐’’rotatedโ‹…sin)

If we let our 2D vector be (๐‘ž0,๐‘ž4) and the rotation angle be ๐œƒ๐‘š0, the standard 2D rotation formulas are:

  • ๐‘ž0โ€ฒ=๐‘ž0cos(๐œƒ๐‘š0)โˆ’๐‘ž4sin(๐œƒ๐‘š0)
  • ๐‘ž4โ€ฒ=๐‘ž0sin(๐œƒ๐‘š0)+๐‘ž4cos(๐œƒ๐‘š0)

As you can see, the code perfectly implements the 2D rotation for the pair (๐‘ž0,๐‘ž4). This same logic applies simultaneously to all other pairs: (๐‘ž1,๐‘ž5), (๐‘ž2,๐‘ž6), (๐‘ž3,๐‘ž7).

  • ๐‘ž1โ€ฒ=๐‘ž1cos(๐œƒ๐‘š1)โˆ’๐‘ž5sin(๐œƒ๐‘š1)
  • ๐‘ž5โ€ฒ=๐‘ž1sin(๐œƒ๐‘š1)+๐‘ž5cos(๐œƒ๐‘š1)
  • ๐‘ž2โ€ฒ=๐‘ž2cos(๐œƒ๐‘š2)โˆ’๐‘ž6sin(๐œƒ๐‘š2)
  • ๐‘ž6โ€ฒ=๐‘ž2sin(๐œƒ๐‘š2)+๐‘ž6cos(๐œƒ๐‘š2)
  • ๐‘ž3โ€ฒ=๐‘ž3cos(๐œƒ๐‘š3)โˆ’๐‘ž7sin(๐œƒ๐‘š3)
  • ๐‘ž7โ€ฒ=๐‘ž3sin(๐œƒ๐‘š3)+๐‘ž7cos(๐œƒ๐‘š3)

Extending RoPE to Longer Contexts

Vanilla RoPE is trained with a fixed maximum context length ๐ฟ. For positions ๐‘š>๐ฟ, the phase values ๐‘šโ‹…๐œƒ๐‘– exceed the range the model encountered during training, degrading performance. Three principal strategies address this: modifying the base frequency (ABF), interpolating positions uniformly (PI), and combining dimension-wise interpolation with temperature scaling (YaRN).

Recall that the phase (rotation angle) for dimension pair ๐‘– at position ๐‘š is:

๐œƒ๐‘–โ‹…๐‘š=๐‘š๐œ”2๐‘–๐‘‘,๐œ”=10000

The goal of context extension is to keep phase values at positions ๐‘šโ‰ค๐ฟโ€ฒ within the range the model learned during training (๐‘šโ‰ค๐ฟ). We write ๐›ผ=๐ฟโ€ฒ๐ฟ for the context extension ratio throughout.

We can express all extension methods in a general interpolation framework. Given the original RoPE encoding ๐‘“(๐’™๐‘š,๐‘š,๐œƒ๐‘–), each method defines a modified encoding:

๐‘“โ€ฒ(๐’™๐‘š,๐‘š,๐œƒ๐‘–)=๐‘“(๐’™๐‘š,๐‘”(๐‘š),โ„Ž(๐œƒ๐‘–))

where ๐‘” transforms positions and โ„Ž transforms frequencies. The choice of ๐‘” and โ„Ž distinguishes the methods.

ABF (Adjusted Base Frequency)

ABF extends the context by replacing the base frequency ๐œ” with a larger value ๐œ”โ€ฒ=๐‘ โ‹…๐œ” (๐‘ >1), directly reducing the angular frequencies and slowing phase accumulation. In the general framework, ABF sets ๐‘”(๐‘š)=๐‘š (positions unchanged) and โ„Ž(๐œƒ๐‘–)=๐œƒ๐‘–๐‘ 2๐‘–๐‘‘ (frequencies reduced).

Derivation

With the scaled base ๐œ”โ€ฒ=๐‘ โ‹…๐œ”, the phase for dimension pair ๐‘– at position ๐‘š becomes:

๐‘š(๐‘ โ‹…๐œ”)2๐‘–๐‘‘=๐‘š๐‘ 2๐‘–๐‘‘โ‹…๐œ”2๐‘–๐‘‘

Historical Context: From NTK Theory to Base Scaling

The idea of modifying the base frequency to extend context did not originate in a traditional research lab. In late June 2023 โ€” just days after Chen et al. published Position Interpolation โ€” a pseudonymous Reddit user, u/bloc97, posted โ€œNTK-Aware Scaled RoPEโ€ on the r/LocalLLaMA forum. The post demonstrated that by changing only three lines of code (replacing ๐œ”=10000 with a larger base), LLaMA 7B could handle 8K+ token sequences without any fine-tuning and with minimal perplexity degradation. This sparked a rapid wave of community-driven innovation: u/emozilla proposed Dynamic NTK scaling shortly after, and bloc97 followed up in July with NTK-by-parts โ€” each refinement building on the last within weeks rather than months. By August, Metaโ€™s Code Llama had adopted a base of 1000000, validating the approach at production scale. Peng et al. formalized the full method as YaRN in September 2023 (published at ICLR 2024).

The name โ€œNTK-awareโ€ comes from Neural Tangent Kernel theory (Jacot et al., 2018). The key theoretical connection is this: RoPEโ€™s position encoding [cos(๐‘š๐œƒ๐‘–),sin(๐‘š๐œƒ๐‘–)] is essentially a random Fourier feature mapping, and NTK theory predicts that such Fourier features must preserve a spectrum of frequencies matching the target functionโ€™s complexity. When PI compresses all frequencies uniformly by 1๐›ผ, it narrows the entire spectral bandwidth โ€” destroying the high-frequency components that the network relies on for fine-grained local position discrimination. Base scaling, by contrast, distributes the interpolation pressure non-uniformly: dimensions with small ๐‘– (high frequency, encoding local patterns) are barely changed, while dimensions with large ๐‘– (low frequency, encoding global patterns) absorb most of the compression. This preserves the modelโ€™s sensitivity to nearby token relationships while still preventing out-of-distribution phase values at long distances.

How Modifying the Base Changes Phase Growth

We can factor the ABF phase as:

๐‘š๐‘ 2๐‘–๐‘‘โ‹…๐œ”2๐‘–๐‘‘=1๐‘ 2๐‘–๐‘‘โ‹…๐‘š๐œ”2๐‘–๐‘‘โŸvanilla phase

Thus the phase reduction factor compared to vanilla RoPE is 1๐‘ 2๐‘–๐‘‘, which is dimension-dependent:

  • For ๐‘–=0: reduction =1๐‘ 0=1 โ€” the highest-frequency component is unchanged.
  • For ๐‘–=๐‘‘2โˆ’1: reduction =1๐‘ ๐‘‘โˆ’2๐‘‘โ‰ˆ1๐‘  โ€” the lowest-frequency component sees the largest reduction.

ABF primarily compresses the low-frequency (high-dimensional) pairs while leaving the high-frequency (low-dimensional) pairs nearly intact, because the exponent 2๐‘–๐‘‘ amplifies the effect of ๐‘  for larger ๐‘–.

Figure 1. ABF phase reduction factor 1๐‘ 2๐‘–๐‘‘ across dimension pairs (๐‘‘=128). Larger base scaling factor ๐‘  produces stronger compression, concentrated in higher-dimensional (low-frequency) pairs. All curves start at 1 for ๐‘–=0.

Choosing ๐‘ : NTK-Aware Scaling

To extend from context length ๐ฟ to ๐ฟโ€ฒ, we require the lowest-frequency pair (๐‘–=๐‘‘2โˆ’1) at position ๐ฟโ€ฒ to not exceed its vanilla phase at position ๐ฟ:

๐ฟโ€ฒ๐‘ ๐‘‘โˆ’2๐‘‘โ‹…๐œ”๐‘‘โˆ’2๐‘‘โ‰ค๐ฟ๐œ”๐‘‘โˆ’2๐‘‘๐‘ ๐‘‘โˆ’2๐‘‘โ‰ฅ๐ฟโ€ฒ๐ฟ๐‘ โ‰ฅ(๐ฟโ€ฒ๐ฟ)๐‘‘๐‘‘โˆ’2

The minimum value gives the NTK-aware base:

๐œ”โ€ฒ=๐œ”โ‹…(๐ฟโ€ฒ๐ฟ)๐‘‘๐‘‘โˆ’2

Practical Base Frequency Values

The theoretical analysis above predicts the minimum base frequency needed for a target context length. In practice, several prominent models adopt large base values that align with these bounds:

  • Code Llama: ๐œ”=1000000 (extending from 4K to 100K tokens).
  • Meta long-context models: ๐œ”=500000 (extending LLaMA-family models to 32Kโ€“128K tokens).

Men et al. (2024) establish a tighter theoretical bound by requiring the cumulative rotation ๐ต๐‘š,๐œƒ=โˆ‘๐‘–cos(๐‘š๐œƒ๐‘–)โ‰ฅ0 for effective positional discrimination. Their analysis yields minimum base requirements that grow rapidly with context length: approximately 4.3ร—103 for 4K context, 6.4ร—105 for 32K, and 7.8ร—106 for 128K. The values used in practice (500000โ€“1000000) sit comfortably above these theoretical minima.

A key advantage of ABF over pure position interpolation is that it preserves local positional resolution โ€” nearby tokens at low dimension pairs still see approximately the same phase differences as in vanilla RoPE, since the high-frequency components are nearly unchanged. The cost is that inter-dimensional phase ratios are distorted, as we will see when comparing with PI and YaRN below.

Limitations of Large Scaling Factors

While ABF successfully extends context length, increasing the scaling factor ๐‘  introduces growing distortions that limit its effectiveness. Recall from the stability analysis that ABF modifies the inter-dimensional phase ratio by an extra factor of ๐‘ 2(๐‘—โˆ’๐‘–)๐‘‘. As ๐‘  grows large, this factor becomes substantial for dimension pairs that are far apart in index โ€” meaning the model encounters inter-dimensional relationships that deviate significantly from what it learned during pre-training. In the extreme, with ๐‘ =100 and ๐‘‘=128, the ratio between the lowest and highest frequency pairs is distorted by a factor exceeding ๐‘ ๐‘‘โˆ’2๐‘‘โ‰ˆ97ร—, a regime well outside the modelโ€™s training distribution.

Research by Liu et al. (2023) on scaling laws for RoPE-based extrapolation reveals a counterintuitive finding: the default base of ๐œ”=10000 actually yields the worst extrapolation performance when models are fine-tuned for longer contexts. Performance improves when the base is adjusted in either direction. Smaller bases (e.g., ๐œ”=500) produce smooth, gradual perplexity degradation that enables nearly unlimited extrapolation potential, while larger bases (e.g., ๐œ”=1000000) produce excellent performance within a well-defined range but exhibit a sharp perplexity cliff beyond it. This non-monotonic behavior arises from a critical dimension ๐‘‘extra โ€” the dimension index beyond which wavelengths are shorter than the training context ๐ฟ. For LLaMA 2 7B with ๐‘‘=128, this critical dimension is approximately ๐‘‘extra=92. Dimensions beyond ๐‘‘extra have already completed many rotation cycles during training and can extrapolate safely, while dimensions below it have not seen a full cycle and require interpolation. The interplay between these two regimes explains why the default base sits at a local pessimum: it places too many dimensions in the ambiguous intermediate zone.

Men et al. (2024) reinforce this picture with their finding of โ€œsuperficial long-context ability.โ€ Models with an insufficiently large base can achieve low perplexity on long sequences โ€” appearing to handle extended contexts โ€” while actually failing at tasks that require precise long-range retrieval. The perplexity metric masks the modelโ€™s inability to attend to distant tokens with high specificity, because the cumulative rotation bound ๐ต๐‘š,๐œƒ=โˆ‘๐‘–cos(๐‘š๐œƒ๐‘–) approaches zero before the perplexity fully degrades. This gap between perplexity and retrieval accuracy is an important caveat when evaluating any base-scaling approach.

PI (Position Interpolation)

Position Interpolation takes the simplest possible approach: instead of modifying frequencies, it scales down all positions to fit within the trained range. In the general framework, PI sets ๐‘”(๐‘š)=๐‘š๐›ผ (positions compressed) and โ„Ž(๐œƒ๐‘–)=๐œƒ๐‘– (frequencies unchanged).

Derivation

Given the context extension ratio ๐›ผ=๐ฟโ€ฒ๐ฟ, PI maps each position:

๐‘šโ†’๐‘š๐›ผ

The frequencies ๐œƒ๐‘– remain unchanged. The phase at position ๐‘š becomes:

(๐‘š๐›ผ)โ‹…๐œƒ๐‘–=๐‘š๐›ผโ‹…๐œ”2๐‘–๐‘‘

How Position Interpolation Changes Phase

We can factor the PI phase as:

๐‘š๐›ผโ‹…๐œ”2๐‘–๐‘‘=1๐›ผโ‹…๐‘š๐œ”2๐‘–๐‘‘โŸvanilla phase

The reduction factor is 1๐›ผ, which is uniform across all dimensions.

At the extended boundary ๐‘š=๐ฟโ€ฒ:

(๐ฟโ€ฒ๐›ผ)โ‹…๐œƒ๐‘–=๐ฟโ‹…๐œƒ๐‘–

The phase at position ๐ฟโ€ฒ under PI equals the phase at position ๐ฟ under vanilla RoPE for every dimension ๐‘–. The model sees the same range of phase values it was trained on, just at a finer positional granularity.

Stability of Phase Ratios

Why PI preserves inter-dimensional relationships. Under vanilla RoPE, the inter-dimensional phase ratio between pairs ๐‘– and ๐‘— is:

๐œƒ๐‘–โ‹…๐‘š๐œƒ๐‘—โ‹…๐‘š=๐œ”2(๐‘—โˆ’๐‘–)๐‘‘

Under PI, this ratio is preserved exactly (the 1๐›ผ cancels):

(๐‘š๐›ผ)โ‹…๐œƒ๐‘–(๐‘š๐›ผ)โ‹…๐œƒ๐‘—=๐œƒ๐‘–๐œƒ๐‘—=๐œ”2(๐‘—โˆ’๐‘–)๐‘‘

Under ABF, the modified frequencies 1(๐‘ ๐œ”)2๐‘–๐‘‘ change this ratio to:

(๐‘ ๐œ”)2(๐‘—โˆ’๐‘–)๐‘‘=๐‘ 2(๐‘—โˆ’๐‘–)๐‘‘โ‹…๐œ”2(๐‘—โˆ’๐‘–)๐‘‘

The extra factor ๐‘ 2(๐‘—โˆ’๐‘–)๐‘‘ distorts the learned inter-dimensional relationships. Since PI preserves these ratios exactly, it typically requires less fine-tuning and is more stable when extending context length.

Limitations of Uniform Interpolation

While PI elegantly preserves inter-dimensional phase ratios, the uniform 1๐›ผ compression has a significant drawback. High-frequency dimension pairs (small ๐‘–) already have short wavelengths โ€” they encode fine-grained local position differences. Compressing these by 1๐›ผ reduces the modelโ€™s ability to distinguish nearby positions, effectively losing high-frequency positional information.

Low-frequency pairs (large ๐‘–), on the other hand, have wavelengths much longer than the training context ๐ฟ. For these dimensions, the model has never seen a full rotation cycle during training, so the phase values encountered at positions up to ๐ฟ occupy only a small arc. These dimensions genuinely need interpolation to avoid out-of-distribution phase values at positions beyond ๐ฟ. But high-frequency dimensions already complete many full rotations within [0,๐ฟ], so they can tolerate positions beyond ๐ฟ without extrapolation problems.

This observation โ€” that different dimensions require different amounts of interpolation โ€” motivates the more sophisticated YaRN method.

YaRN (Yet Another RoPE ExtensioN)

YaRN addresses the limitations of both ABF and PI by combining dimension-wise interpolation (NTK-by-parts) with attention temperature scaling. Rather than applying a single strategy uniformly, YaRN classifies each dimension pair according to how much interpolation it needs and blends PI with identity scaling accordingly.

Wavelength Analysis

Each dimension pair ๐‘– has a characteristic wavelength โ€” the number of positions for a full 2๐œ‹ rotation:

๐œ†๐‘–=2๐œ‹๐œƒ๐‘–=2๐œ‹โ‹…๐œ”2๐‘–๐‘‘

We define the ratio of the training context length to this wavelength:

๐‘Ÿ(๐‘–)=๐ฟ๐œ†๐‘–=๐ฟ2๐œ‹โ‹…๐œ”2๐‘–๐‘‘

This ratio ๐‘Ÿ(๐‘–) tells us how many full rotation cycles dimension ๐‘– completes within the training window:

  • High ratio (๐‘Ÿ(๐‘–)โ‰ซ1): the model has seen many full cycles during training. This dimension encodes local positional patterns and can tolerate extrapolation to positions beyond ๐ฟ without problems. No interpolation is needed.
  • Low ratio (๐‘Ÿ(๐‘–)โ‰ช1): the model has seen only a small arc of the full rotation. Positions beyond ๐ฟ would produce phase values the model has never encountered. Full interpolation (as in PI) is needed.
  • Intermediate ratio: a blend of interpolation and identity is appropriate.

NTK-by-Parts Interpolation

YaRN uses a ramp function ๐›พ(๐‘Ÿ) to smoothly transition between full interpolation and no interpolation, based on the ratio ๐‘Ÿ(๐‘–):

๐›พ(๐‘Ÿ)={0if๐‘Ÿ<๐›ผlo1if๐‘Ÿ>๐›ผhi๐‘Ÿโˆ’๐›ผlo๐›ผhiโˆ’๐›ผlootherwise

where ๐›ผlo and ๐›ผhi are hyperparameters controlling the transition boundaries. For the LLaMA model family, the recommended values are ๐›ผlo=1 and ๐›ผhi=32.

The choice of these boundaries has a precise physical interpretation tied to rotation cycles. A dimension pair with ๐‘Ÿ(๐‘–)<1 โ€” that is, ๐‘Ÿ(๐‘–)<๐›ผlo โ€” has a wavelength longer than the training context ๐ฟ. The model has seen less than one full rotation cycle for this dimension during pre-training, so any position beyond ๐ฟ pushes the phase into entirely uncharted territory. These dimensions are the most vulnerable to extrapolation failure and must receive full PI-style interpolation. At the other extreme, ๐‘Ÿ(๐‘–)>32 means the model has observed 32 or more complete rotation cycles during training. The positional patterns at these frequencies are thoroughly learned; the model can reliably extrapolate because the periodic structure is well established. Leaving these dimensions unchanged preserves the fine-grained local position sensitivity that PI would destroy.

This connects directly to the critical dimension concept from Liu et al. (2023): the boundary ๐‘Ÿ(๐‘–)=1 corresponds roughly to the index ๐‘‘extra where dimensions transition from having seen partial rotations to having seen full cycles. YaRNโ€™s ramp function effectively operationalizes this theoretical boundary, with ๐›ผlo=1 marking the onset of safe extrapolation and ๐›ผhi=32 marking the point of full confidence. The linear transition between them provides a smooth blend that avoids abrupt changes in the frequency spectrum โ€” a design choice validated empirically across the LLaMA model family.

The modified frequency for dimension pair ๐‘– is then:

โ„Ž(๐œƒ๐‘–)=(1โˆ’๐›พ(๐‘Ÿ(๐‘–)))โ‹…๐œƒ๐‘–๐›ผ+๐›พ(๐‘Ÿ(๐‘–))โ‹…๐œƒ๐‘–

We can factor this to understand the per-dimension behavior:

โ„Ž(๐œƒ๐‘–)=๐œƒ๐‘–โŸvanilla freq.โ‹…[๐›พ(๐‘Ÿ(๐‘–))+1โˆ’๐›พ(๐‘Ÿ(๐‘–))๐›ผ]

The three regimes are:

  • When ๐›พ=0 (low-frequency dimensions, ๐‘Ÿ(๐‘–)<๐›ผlo): โ„Ž(๐œƒ๐‘–)=๐œƒ๐‘–๐›ผ, identical to PI โ€” full interpolation.
  • When ๐›พ=1 (high-frequency dimensions, ๐‘Ÿ(๐‘–)>๐›ผhi): โ„Ž(๐œƒ๐‘–)=๐œƒ๐‘–, the frequency is unchanged โ€” no interpolation at all.
  • When 0<๐›พ<1 (transition region): โ„Ž(๐œƒ๐‘–)=๐œƒ๐‘–โ‹…[๐›พ+1โˆ’๐›พ๐›ผ], a linear blend between PI and identity.
Figure 2. YaRN NTK-by-parts interpolation with ๐›ผ=4, ๐ฟ=4096, ๐œ”=10000, ๐‘‘=128, ๐›ผlo=1, ๐›ผhi=32. The ramp function ๐›พ (blue) transitions from 0 to 1 across dimension pairs. The effective frequency scaling factor (red) interpolates between 1๐›ผ=0.25 (full PI, dotted gray) and 1 (no change, dashed gray). Low-frequency dimensions (small ๐‘–) receive full interpolation; high-frequency dimensions (large ๐‘–) are left unchanged.

Temperature Scaling

NTK-by-parts interpolation solves the dimension-wise frequency problem, but it introduces a subtler issue: the modified frequencies change the entropy of the attention distribution. When we interpolate some dimensions but not others, the effective magnitude of the dot-product attention scores shifts, causing the softmax to become either too sharp or too diffuse compared to the original model.

YaRN corrects this with a temperature parameter ๐‘ก applied to the attention logits:

Attn(๐’’๐‘šโ€ฒ,๐’Œ๐‘›โ€ฒ)=softmax(๐’’๐‘šโ€ฒ๐‘‡๐’Œ๐‘›โ€ฒ๐‘กโ‹…๐‘‘)

The temperature is derived from the extension ratio ๐›ผ via an empirically calibrated formula:

1๐‘ก=0.1โ‹…ln(๐›ผ)+1

Solving for ๐‘ก:

๐‘ก=1(0.1โ‹…ln(๐›ผ)+1)2

Since ๐›ผ>1, we have 0.1โ‹…ln(๐›ผ)+1>1, which gives ๐‘ก<1. Dividing by ๐‘ก<1 scales up the attention logits, sharpening the attention distribution. This counteracts the entropy increase caused by the modified frequencies, restoring the attention pattern to a distribution similar to what the model learned during training.

For example, with ๐›ผ=4: 1๐‘ก=0.1โ‹…ln(4)+1โ‰ˆ1.139, so ๐‘กโ‰ˆ0.771.

The formula 1๐‘ก=0.1โ‹…ln(๐›ผ)+1 was found by empirical fitting, not theoretical derivation. Peng et al. applied NTK-by-parts with various scaling factors ๐›ผ to LLaMA 7B, 13B, 33B, and 65B models without fine-tuning, sweeping over temperature values ๐‘ก for each configuration to find the one minimizing perplexity. When they plotted 1๐‘ก against ln(๐›ผ), the relationship turned out to be remarkably linear โ€” and consistent across all four model sizes. They validated this across 896 documents from RedPajama at different token positions, confirming that the optimal temperature depends primarily on the extension ratio rather than on model size or input content.

To understand why NTK-by-parts increases entropy in the first place, consider what happens to the dot-product attention scores. Interpolated dimensions (those with ๐›พโ‰ˆ0) have their frequencies reduced by 1๐›ผ, which compresses the phase differences between tokens โ€” positions that were once far apart in rotated space now appear closer. These dimensions contribute smaller dot products to the attention score. Meanwhile, unchanged dimensions (those with ๐›พโ‰ˆ1) contribute normal-magnitude dot products. The net effect is that the range of attention scores across different key positions shrinks: the gap between the most-attended and least-attended tokens narrows. When softmax operates on these compressed scores, it produces a flatter, more uniform distribution โ€” higher entropy. The model attends to everything more equally, losing its ability to focus on the most relevant tokens. Dividing by ๐‘ก<1 is equivalent to multiplying the attention logits by 1๐‘ก>1, which stretches the score range back out, restores the sharpness of the attention distribution, and brings the entropy profile closer to what the model learned during pre-training.

We note that the temperature formula is architecture-dependent. The coefficient 0.1 in 1๐‘ก=0.1โ‹…ln(๐›ผ)+1 was fit specifically for LLaMA models. For Mistral-7B, the optimal coefficient shifts to approximately 0.07, reflecting differences in head dimension, number of attention heads, and the learned attention patterns. In the general parametric form 1๐‘ก=๐‘Žโ‹…ln(๐›ผ)+๐‘, both ๐‘Ž and ๐‘ may need re-fitting when applying YaRN to a new architecture โ€” though the logarithmic relationship with ๐›ผ appears to be universal.

Dynamic NTK

In practice, the extension ratio need not be fixed at inference time. Dynamic NTK computes the scaling factor on the fly based on the current sequence length ๐‘™โ€ฒ:

๐›ผ=max(๐‘™โ€ฒ๐ฟ,1)

When ๐‘™โ€ฒโ‰ค๐ฟ, we have ๐›ผ=1 and YaRN reduces to vanilla RoPE โ€” no modification is applied. As ๐‘™โ€ฒ exceeds ๐ฟ, the interpolation gradually activates. This allows a single model to handle both short and long sequences without committing to a fixed extension ratio at deployment time.

Empirical Performance

We can now examine how YaRN performs in practice relative to other extension methods. The following table summarizes perplexity results on the Proof-pile evaluation set using a sliding window of ๐‘†=256 tokens, drawn from the YaRN paper (Peng et al., 2023):

ModelMethodPerplexity (65K)Perplexity (128K)
LLaMA 2 7BYaRN (๐‘ =16)2.42โ€”
LLaMA 2 7BYaRN (๐‘ =32)โˆผ2.372.37
LLaMA 2 13BYaRN (๐‘ =32)โ€”2.24
Code Llama 7BABF (๐œ”=106)2.552.54
Together AI 7BPI>32K blow-upโ€”

YaRN achieves the lowest perplexity at 128K tokens with the 13B model, outperforming Code Llama despite the latter using a much larger base frequency of ๐œ”=1000000. PI, while theoretically elegant, fails catastrophically beyond its trained extension range โ€” perplexity explodes past 32K tokens for the Together AI model. On passkey retrieval โ€” a stricter test of whether the model can actually attend to a specific token at arbitrary distances โ€” YaRN (๐‘ =32, 128K) achieves 99.4% accuracy, matching Code Llamaโ€™s 100K result.

The training efficiency comparison is equally striking. NTK-aware scaling from Xiong et al. requires approximately 64,000 A100 GPU-hours of continued pre-training. PI (Chen et al.) brings this down to roughly 640 GPU-hours. YaRN requires only 384 GPU-hours total โ€” 256 for the initial 64K extension plus 128 for the additional 128K stage โ€” using approximately 0.1% of the original pre-training data and converging in just 400 optimization steps for the first stage. This makes YaRN the most compute-efficient method by a significant margin while achieving superior or comparable results.

An important caveat emerges from these results: perplexity alone is not a reliable indicator of long-context capability. YaRN with ๐‘ =32 achieves similar perplexity to ๐‘ =16, yet the ๐‘ =32 model scores meaningfully higher on passkey retrieval, demonstrating better ability to attend to distant tokens. This gap suggests that perplexity primarily captures local language modeling quality, while retrieval tasks probe whether the model can genuinely leverage the full extended context. When evaluating RoPE extension methods, we recommend testing with retrieval-based benchmarks (such as passkey retrieval or needle-in-a-haystack) alongside perplexity.

From a practical standpoint, YaRN incurs zero computational overhead at inference time. The modified frequencies are pre-computed and cached, just as in vanilla RoPE, and the temperature scaling amounts to a single scalar multiplication on the attention logits. YaRN is natively supported in HuggingFace Transformers via rope_type: "yarn", and has been adopted by production models including Qwen, DeepSeek, and later LLaMA variants โ€” making it the de facto standard for RoPE-based context extension.

Comparison of Extension Methods

MethodPhase at position ๐‘šReduction factorScaling type
Vanilla RoPE๐‘š๐œ”2๐‘–๐‘‘โ€”โ€”
Base scaling (๐œ”โ†’๐›ผ๐œ”)๐‘š(๐›ผ๐œ”)2๐‘–๐‘‘1๐›ผ2๐‘–๐‘‘Dimension-dependent
ABF (๐œ”โ†’๐œ”๐›ผ๐‘‘๐‘‘โˆ’2)๐‘š๐œ”2๐‘–๐‘‘โ‹…๐›ผ2๐‘–๐‘‘โˆ’21๐›ผ2๐‘–๐‘‘โˆ’2Dim.-dependent (calibrated)
PI (๐‘šโ†’๐‘š๐›ผ)๐‘š๐›ผโ‹…๐œ”2๐‘–๐‘‘1๐›ผUniform
YaRN (NTK-by-parts)๐‘šโ‹…โ„Ž(๐œƒ๐‘–), with โ„Ž per-dim.๐›พ+1โˆ’๐›พ๐›ผDimension-adaptive + temp.

where ๐›ผ=๐ฟโ€ฒ๐ฟ is the context extension ratio and ๐›พ=๐›พ(๐‘Ÿ(๐‘–)) is the ramp function evaluated at each dimension.

We can summarize the key trade-offs as follows:

  • Base scaling and ABF modify the frequency spectrum in a dimension-dependent way, preserving high-frequency (local) positional resolution at the cost of distorting inter-dimensional phase ratios.
  • PI applies a uniform reduction that perfectly preserves phase ratios but sacrifices high-frequency resolution.
  • YaRN achieves the best of both worlds: it preserves high-frequency dimensions (where the model can extrapolate) while interpolating only the low-frequency dimensions (where extrapolation would fail), and corrects the resulting entropy shift via temperature scaling.
Figure 3. Comparison of phase reduction factors with ๐›ผ=4, ๐‘‘=128. Vanilla RoPE (dashed gray) has no reduction. PI (green) applies a uniform 14 reduction. Base scaling (blue) and ABF (red) reduce higher dimensions more aggressively. YaRN (orange) transitions smoothly from 1๐›ผ at low-frequency dimensions to 1 at high-frequency dimensions, combining the strengths of PI and ABF.