YaRN (Yet another RoPE extensioN method) extends RoPE-based Llama 2 models from 4K to 128K tokens using 10x fewer tokens and 2.5x fewer training steps than linear Position Interpolation. By splitting Rotary Position Embeddings (RoPE) into frequency bands that preserve high-frequency local order while interpolating low-frequency dimensions, and rescaling attention logits with a lightweight temperature factor, Bowen Peng and colleagues let models trained on 64K segments extrapolate cleanly to 128K context while losing less than 0.5% on short-context benchmarks.

  • 2.5x faster convergence: on Llama 2 7B (4K to 8K, s = 2), YaRN matches Position Interpolation’s Proof-pile perplexity (3.35 vs. 3.34) in 400 steps instead of 1,000 steps (under 0.1% of original pretraining data).
  • Train on 64K, run at 128K: Llama 2 7B and 13B fine-tuned on 64K PG19 chunks (s = 32 for 200 steps past the s = 16 checkpoint) reach 2.37 (7B) and 2.24 (13B) sliding-window perplexity at 128K and 99.4% passkey retrieval accuracy.
  • Minimal short-context regression: extending Llama 2 from 64K to 128K drops Hugging Face Open LLM Leaderboard averages by only 0.49% (7B MMLU 43.8 to 41.7, HellaSwag 77.8 to 78.4), whereas Code Llama’s uniform base-change NTK drops 7B MMLU to 31.1.
  • Zero-tuning dynamic mode: Dynamic-YaRN updates the scale factor s per forward pass, extending context past 2x without fine-tuning and preventing perplexity blow-up beyond 4K.
How YaRN Partitions RoPE Dimensions by Rotation Count r(d) = L / lambda_d
1. Low frequencies (r < 1)
Wavelength longer than pretrained context L.
Linearly interpolated: h(theta_d) = theta_d / s
Prevents out-of-distribution extrapolation on dimensions acting like absolute anchors.
2. Mid-band ramp (1 <= r <= 32)
Completes 1 to 32 rotations in L.
Blended via ramp gamma(r) = (r - 1) / 31
Smoothly transitions between PI scaling and unscaled frequencies (alpha = 1, beta = 32 for LLaMA).
3. High frequencies (r > 32)
Wavelength much shorter than L.
Untouched: h(theta_d) = theta_d + temp 1/t
Keeps local token distances intact while scaling q, k by sqrt(1/t) = 0.1 ln(s) + 1.

Source: Peng et al. (2023/2026), Sections 3.2 and 3.3.

Why Uniform Stretching Breaks Local Attention

Position Interpolation (PI) extends context by dividing every position index by scale factor s = L' / L, squeezing all angles back inside the pretrained range. That uniform compression works up to roughly s = 8, but destroys high-frequency rotary components, forcing adjacent tokens into nearly indistinguishable angles. Neural Tangent Kernel (NTK) theory shows networks struggle to relearn high-frequency distinctions once embeddings lose them.

Community “NTK-aware” scaling solved part of that problem by raising the rotary base b' = b * s^(|D|/(|D|-2)) (the method Code Llama called Adjusted Base Frequency with b = 1M), which stretches low frequencies more than high frequencies. However, base-changing extrapolates mid-range dimensions out of bounds and miscalibrates the effective scale s, requiring higher empirical scale factors and heavier fine-tuning (6,400 A100 hours for Code Llama 7B vs. 384 A100 hours for YaRN Llama 2 7B 128K).

YaRN replaces the blind base change with NTK-by-parts plus attention temperature scaling. Using r(d) = L / lambda_d (how many full 2*pi rotations dimension d makes over pretrained length L), YaRN leaves high-frequency relative dimensions (r > 32) untouched, interpolates low-frequency dimensions (r < 1) by 1/s, and blends the middle band linearly. To counter rising attention softmax entropy over longer contexts, YaRN divides pre-softmax dot products by temperature t (sqrt(1/t) = 0.1 ln(s) + 1, about 1.208 at s = 8). Multiplying both q and k rotary vectors by sqrt(1/t) implements this with zero runtime overhead and full FlashAttention-2 compatibility.

Llama 2 7B Long-Context Models: Short-Context MMLU and Compute Cost
5-shot MMLU accuracy (%, higher is better; baseline Llama 2 7B = 43.8%)
Llama 2 7B (4K)
43.8% (0h)
YaRN 64K (s=16)
42.5% (256h)
YaRN 128K (s=32)
41.7% (384h)
Together PI (32K)
43.3% (N/A)
Code Llama (100K)
31.1% (6,400h)

Source: Peng et al. (2023/2026), Tables 4 and 10. Training times in A100 GPU-hours.

Key Findings

Across Proof-pile and GovReport long-document evaluations, YaRN outperforms PI, NTK-aware, and NTK-by-parts at equal 400-step budgets on LLaMA 7B (2.77 Proof-pile perplexity at 32K vs. 3.57 for PI and 8.49 for NTK-aware). Unlike ALiBi’s distance penalties, which are fixed at pretraining, YaRN adapts already-trained RoPE checkpoints via short fine-tuning or zero-tuning Dynamic Scaling.

Even more practically, YaRN transfers across sequence lengths (“train short, test long”). Extending Llama 2 7B and 13B using only 64K-token PG19 chunks (400 steps at s = 16 plus 200 steps at s = 32) produces smooth perplexity declines all the way to 128K (2.37 at 7B; 2.24 at 13B), whereas the s = 16 model spikes above 10 past 64K. Both 7B and 13B 128K models hit 99.4% passkey retrieval accuracy across 8K to 128K contexts.

Caveats and Builder Takeaway

Three practical constraints govern YaRN in production. First, the band boundaries (alpha = 1, beta = 32) and temperature curve (0.1 ln(s) + 1) were fitted empirically on LLaMA and Llama 2; non-LLaMA architectures or different RoPE bases require verification. Second, Dynamic-YaRN changes s as sequence length grows, so inference engines must cache pre-RoPE key vectors and apply rotary angles on the fly rather than caching post-RoPE keys. Third, low perplexity and passkey recall do not guarantee multi-hop reasoning across 128K tokens (as the authors note, s = 16 showed lower passkey scores than s = 32 despite similar perplexity), and serving 128K contexts still pays full attention and KV-cache memory costs.

Use YaRN whenever extending a pretrained RoPE model beyond 4x its native context window, especially if your fine-tuning data is shorter than your target inference window or you need a drop-in, FlashAttention-compatible RoPE schedule.