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 ๐‘›. This gives the model a useful relative-position inductive bias, although it does not by itself guarantee extrapolation 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 (Rotary 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).

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

We group each vector into ๐‘‘2 coordinate pairs. For the query these are (๐‘ž๐‘š๐‘–๐‘ž๐‘š๐‘–+๐‘‘2), and for the key they are (๐‘˜๐‘›๐‘–๐‘˜๐‘›๐‘–+๐‘‘2), where ๐‘–=0,1,โ€ฆ,๐‘‘2โˆ’1.

For each pair ๐‘– (๐‘–=0,1,โ€ฆ,๐‘‘2โˆ’1), define the angular frequency ๐œƒ๐‘–=1๐œ”2๐‘–๐‘‘=1100002๐‘–๐‘‘, where ๐œ” is the RoPE base (typically 10000). At position ๐‘š, the pair is rotated by the angle ๐‘š๐œƒ๐‘–.

Thus small pair indices have higher frequencies and larger rotation angles at a fixed position, while large pair indices have lower frequencies and smaller rotation angles.

The rotated query vector becomes:

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

Similarly for the key vector:

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

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:

๐’’๐‘šโ€ฒ๐‘‡โ‹…๐’Œ๐‘›โ€ฒ=โˆ‘๐‘–=0๐‘‘2โˆ’1[(๐‘ž๐‘š๐‘–๐‘˜๐‘›๐‘–+๐‘ž๐‘š๐‘–+๐‘‘2๐‘˜๐‘›๐‘–+๐‘‘2)cos((๐‘›โˆ’๐‘š)๐œƒ๐‘–)+(๐‘ž๐‘š๐‘–+๐‘‘2๐‘˜๐‘›๐‘–โˆ’๐‘ž๐‘š๐‘–๐‘˜๐‘›๐‘–+๐‘‘2)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 are ๐‘‘2=4 angular frequencies: ๐œƒ0,๐œƒ1,๐œƒ2,๐œƒ3. At position ๐‘š, the angle for pair ๐‘–โˆˆ[0,1,2,3] is:

๐œ‘๐‘š,๐‘–=๐‘š๐œƒ๐‘–=๐‘š100002๐‘–๐‘‘

where ๐‘‘ is the dimension. For example, ๐œ‘๐‘š,0=๐‘š100002โˆ—08=๐‘š.

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. Practical choices such as 500000โ€“1000000 are near or above the quoted 32K bound but below the quoted 128K bound, so they should not be described as satisfying the latter sufficient condition.

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 can give particularly poor extrapolation performance when models are fine-tuned for longer contexts. Performance can improve when the base is adjusted in either direction. Smaller bases produce a different, more gradual degradation regime, while sufficiently large bases can work well within a target range but may exhibit a sharper failure beyond it. Their analysis introduces a critical dimension separating frequencies that complete full cycles within the training context from those that do not. Under the indexing convention used here, ๐œƒ๐‘–=๐œ”โˆ’2๐‘–๐‘‘ decreases and the wavelength ๐œ†๐‘– increases with ๐‘–: small-๐‘– pairs have short wavelengths and complete more cycles, whereas large-๐‘– pairs have long wavelengths and may cover only a partial cycle. Any numerical critical index must be interpreted using the paperโ€™s precise dimension convention before being mapped to the pair index ๐‘– used here.

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 effective frequency scaling factor (red) interpolates between 1 (no change, dashed gray) at small-๐‘–, high-frequency pairs and 1๐›ผ=0.25 (full PI, dotted gray) at large-๐‘–, low-frequency pairs. The blue curve shows the corresponding ramp value ๐›พ(๐‘Ÿ(๐‘–)).

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.

The entropy change should be understood as an empirical property of the modified attention patterns, not as a consequence of every interpolated dimension contributing a smaller dot product. RoPE rotations preserve vector norms, and changing a phase does not make an individual dot-product contribution decrease monotonically. Empirically, however, NTK-by-parts changes the distribution of logits across key positions and can make the resulting softmax too diffuse. Dividing by ๐‘ก<1 is equivalent to multiplying the logits by 1๐‘ก>1, sharpening the distribution and bringing its entropy profile closer to the one observed 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 Scaling and Dynamic NTK

In practice, the extension ratio need not be fixed at inference time. A dynamic schedule can compute it from the current sequence length ๐‘™โ€ฒ:

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

When ๐‘™โ€ฒโ‰ค๐ฟ, we have ๐›ผ=1, so a dynamically scaled method can reduce to vanilla RoPE. As ๐‘™โ€ฒ exceeds ๐ฟ, its scaling gradually activates. Dynamic NTK specifically applies such a schedule to the NTK-aware base-scaling formula; it is distinct from YaRN, which combines NTK-by-parts interpolation with attention scaling. A YaRN implementation may also choose its scale dynamically, but that should be described as dynamic YaRN scaling rather than treating Dynamic NTK and YaRN as the same method. Dynamic scaling also complicates KV caching because changing the scale can require previously cached rotated keys to be recomputed or handled specially.

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.