Skip to main content
CookLLM logoCookLLM Docs

Length Extrapolation

Premium

NTK-aware Scaling, YaRN, and other methods to let RoPE handle longer sequences

Get code access

The Rotation View: Understanding Extrapolation

Suppose the model is trained with max_seq_len = 4096. What happens if inference length is 8192?

A common intuition is “the position index is out of range, so it is OOD.” But that is imprecise. The position index mm is unbounded, while RoPE position embeddings are bounded trigonometric functions. The model interacts with embeddings, not indices. To understand OOD, we must analyze the embeddings.

Rotating on the Unit Circle

Recall the dot product after RoPE (complex form):

(Rmq)⊤(Rnk)=Re[∑i=0d/2−1q[2i:2i+1] k[2i:2i+1]∗ ei(m−n)θi](R_m q)^\top (R_n k) = \text{Re}\left[\sum_{i=0}^{d/2-1} q_{[2i:2i+1]} \, k^*_{[2i:2i+1]} \, e^{i(m-n)\theta_i}\right]

The key is ei(m−n)θie^{i(m-n)\theta_i}. By Euler’s formula, it is a point on the unit circle. As relative distance m−nm-n grows, this point rotates around the circle. Larger θi\theta_i rotates faster; smaller θi\theta_i rotates slower.

This is the core of the “rotation view”: whether m−nm-n is OOD is not important; what matters is whether the unit-circle points have been sufficiently covered during training.

High Frequency vs Low Frequency: Coverage Differences

Assume training length LtrainL_{\text{train}}, then m−n∈[0,Ltrain−1]m-n \in [0, L_{\text{train}} - 1]. For each dimension ii, the number of rotations during training is:

ri=θi⋅Ltrain2πr_i = \frac{\theta_i \cdot L_{\text{train}}}{2\pi}
  • High-frequency dims (θi\theta_i large, ii small): fast rotation, many turns during training, covering the circle. At test time, even larger m−nm-n just keeps rotating on already covered points → no OOD
  • Low-frequency dims (θi\theta_i small, ii large): slow rotation, may not complete one full circle during training, covering only a small arc. When test-time m−nm-n exceeds that arc, we enter unseen territory → true OOD
Unit Circle Coverage
4096
8192
High freq (i=0)
i = 0, θ = 1.0000
Rotations
651.9
Period
6
Full coverage — safe
Low freq (i=63)
i = 63, θ = 0.0001
Rotations
0.1
Period
54410
Arc only — OOD risk
Trained arcExtrapolation (OOD)

Concrete numbers (d=128d=128, base=10000\text{base}=10000, Ltrain=4096L_{\text{train}}=4096):

# Highest-frequency dim (i=0): θ₀ = 1.0
# Rotations: 1.0 × 4096 / (2π) ≈ 651 turns → full coverage, safe

# Lowest-frequency dim (i=63): θ₆₃ ≈ 0.00011
# Rotations: 0.00011 × 4096 / (2π) ≈ 0.07 turns → tiny arc, high OOD risk

The core issue is not “the rotation angle is too large,” but insufficient unit-circle coverage in low-frequency dimensions. High-frequency dims are the safest.

From Rotation to Solutions

With this view, the solution becomes clear:

  • Dims with enough rotations (high frequency) → no change, extrapolate directly
  • Dims with insufficient rotations (low frequency) → compress out-of-range angles back into the trained arc (position interpolation)
  • Middle range → smoothly transition between the two

All methods below are variations of how to compress and how much.

Log in to continue reading

This is premium content. Please log in to access the full article.