YaRN: Efficient Context Window Extension of Large Language Models
TL;DR. RoPE models break beyond their training length. YaRN extends Llama 2 from 4K to 64K or 128K tokens with a few hundred fine-tuning steps on 64K-token PG19 chunks: 400 steps at s = 16, then 200 more at s = 32. It does this by changing the rotary frequencies per dimension, according to how many times each one rotates within the original window, and adding an attention temperature. The paper reports needing 10× fewer tokens and 2.5× fewer steps than previous methods.
Key idea
Section titled “Key idea”RoPE rotates dimension pair at frequency with wavelength . To stretch the context by , YaRN compares each wavelength with the original context through :
with for Llama. Attention logits are then divided by a temperature , where :
Results
Section titled “Results”- Perplexity (Table 1, sliding window S = 256, ten 128K Proof-pile documents): the 7B s = 32 model scores 3.56, 3.04, 2.70, 2.45 and 2.37 at 8K to 128K. The 13B model reaches 2.24 at 128K. The s = 16 models exceed 10 at 128K.
- Passkey (Table 9): 7B and 13B at s = 32 reach 99.4% at 128K. At s = 16 they reach 96.3% and 97.5% at 64K.
- Short context (Table 10): Llama 2 7B scores 53.1/77.8/43.8/39.0 and YaRN 7B at s = 32 scores 52.1/78.4/41.7/37.3. Code Llama’s NTK extension drops to 39.9/60.8/31.1/37.8. Going from s = 16 to s = 32 costs 0.49% on average.
Connections
Section titled “Connections”- Before: RoPE defines the frequencies that YaRN rescales.
- Does the window work? RULER, NoLiMa and Context Rot (which uses YaRN to extend Qwen) measure what a long window is worth beyond perplexity and passkey.
- Making it affordable: FlashAttention-2 is used in YaRN’s training, and GQA and PagedAttention handle the KV cache of 128K-token prompts.
- Alternative: Mamba replaces attention with a state-space model, so it has no positional rotation to extend.
Questions to think through
These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.
- Derive / compute
Take a Llama-style model with head dimension 128 (so d = 0 … 63 indexes the rotary pairs), base b = 10000, original context L = 4096, and YaRN with s = 16, α = 1, β = 32. Compute r(d) = L / λ_d for d = 0, 20, 21, 30, 45 and 46, give γ(r) and the effective frequency multiplier h(θ_d) / θ_d for each, and say which dimensions are left alone, ramped or fully interpolated. Then compute the recommended attention scaling for s = 16 and s = 32.
What a strong answer covers
- λ_d = 2π · 10000^(2d/128), so r(d) = 4096 / λ_d ≈ 652 (d = 0), 36.7 (d = 20), 31.7 (d = 21), 8.69 (d = 30), 1.004 (d = 45) and 0.87 (d = 46).
- γ = 1 when r > 32, so d ≤ 20 keep θ_d unchanged (multiplier 1): they rotate many times within L and carry fine local position.
- For 1 ≤ r ≤ 32, γ = (r − 1)/31: d = 21 gives γ ≈ 0.99 (multiplier ≈ 0.99); d = 30 gives γ ≈ 0.25, multiplier (1 − γ)/16 + γ ≈ 0.30; d = 45 gives γ ≈ 0, multiplier ≈ 1/16.
- r < 1 (d ≥ 46): wavelength longer than the training context, so these are interpolated exactly like PI, multiplier 1/16, which avoids positions the model never saw.
- Attention scaling √(1/t) = 0.1 ln s + 1 gives 1.277 at s = 16 and 1.347 at s = 32 (the paper quotes 1.208 at s = 8); q and k are both multiplied by it, so logits are scaled by its square, about 1.63 at s = 16.
- Mechanism
Compare Position Interpolation, NTK-aware scaling, NTK-by-parts and full YaRN: what each does to the RoPE frequencies, which failure of the previous method it fixes, and why YaRN adds an attention temperature on top. Why does dynamic scaling help a model used without fine-tuning?
What a strong answer covers
- PI squeezes positions so the extended window L′ maps onto the trained range L, equivalent to using θ_d/s in every dimension (the γ = 0 case of the by-parts formula), keeping all angles within the trained range, but it compresses the high-frequency dimensions too, so nearby tokens become hard to distinguish (loss of high-frequency information, argued via NTK / Fourier-feature theory).
- NTK-aware keeps positions but changes the base to b′ = b · s^(|D|/(|D|−2)) (about 167K for s = 16, |D| = 128), spreading the scaling: high frequencies barely change, low ones are interpolated. Some dimensions are slightly extrapolated to out-of-bound values, so fine-tuning with it is worse than PI, and s must be set above the target extension.
- NTK-by-parts decides per dimension using the wavelength-to-context ratio r(d): no interpolation when r > β, full PI when r < α, a linear ramp in between, h(θ_d) = (1 − γ)θ_d/s + γθ_d.
- YaRN = NTK-by-parts plus a temperature t in softmax(qᵀk / (t√|D|)), fitted for lowest perplexity on LLaMA as √(1/t) = 0.1 ln s + 1 (the paper ties it to increased attention entropy at longer lengths); it has zero overhead because scaling q and k folds into the precomputed rotary embeddings.
- Dynamic scaling sets s = max(1, l′/L) per forward pass from the current length l′, so short inputs run unmodified and longer ones degrade gradually rather than abruptly past L′.
- Critique
YaRN's evidence is sliding-window perplexity on Proof-pile, passkey retrieval and short-context benchmark scores. Argue whether these show that the 128K window is usable for a RAG reader, citing the numbers, and design the extra evaluation you would require.
What a strong answer covers
- Perplexity: the s = 32 7B model reaches 2.37 at 128K on 10 Proof-pile documents, while the s = 16 model exceeds 10 at 128K, showing it extrapolates only modestly beyond its scale. Low perplexity mostly reflects local prediction and says little about using distant evidence.
- Passkey: 99.4% for 7B s = 32 at 128K (Table 9) is a literal-match task, exactly what RULER and NoLiMa show to be too easy.
- Short context: Llama 2 7B 53.1/77.8/43.8/39.0 (ARC-c/HellaSwag/MMLU/TruthfulQA) vs YaRN s = 32 52.1/78.4/41.7/37.3, an average drop of 0.49% from s = 16 to s = 32, so short-context quality is mostly preserved.
- Missing: no effective-length measure, no position sweep, no distractors. Context Rot even uses YaRN to extend Qwen models and still finds degradation with length.
- Required: RULER-style effective length, a lost-in-the-middle sweep with real retrieved passages, and a RAG QA test with hard-negative padding, comparing YaRN against the same model given only top-k reranked passages.