English static mirror for SEO/GEO · AI-assisted translation · Read Chinese original

YaRN: Yet Another RoPE Extension Method for Efficient Context Window Expansion

Forum topic · 小凯 · 2026-05-10

Summary

YaRN (arXiv: 2309.00071, Quesnelle et al., 2023) is an efficient method for extending the context length of RoPE-based language models such as LLaMA without full pretraining. While RoPE theoretically supports length extrapolation, models trained at 2048 tokens often degrade badly at 4096. YaRN addresses this by modifying RoPE rotation frequencies: (1) frequency scaling compresses long sequences so the model perceives them as shorter; (2) temperature scaling counteracts the over-sharp attention distributions caused by frequency scaling; (3) NTK-aware scaling dynamically assigns smaller scaling factors to high-frequency components (local distinctions) and larger ones to low-frequency components (long-range relations). The method achieves effective context extension with roughly 10x fewer tokens and 2.5x fewer training steps than naive fine-tuning approaches, and can even extrapolate beyond the fine-tuning dataset. YaRN established context extension as a cheap post-training technique and influenced later models such as CodeLLaMA and LongLLaMA.

YaRN: Yet Another RoPE Extension (2023, Quesnelle et al.)

arXiv: 2309.00071

The Core Problem

RoPE theoretically supports length extrapolation, but in practice, models trained at a certain length collapse when fed longer sequences. LLaMA, trained at 2048 tokens, can start hallucinating when pushed to 4096. How can existing models be extended without retraining from scratch?

The Method

YaRN's core idea is to modify the rotation frequencies of RoPE, making the model "believe" sequences are shorter than they actually are.

Specifically:

1. Frequency Scaling: Multiply RoPE's rotation angles by a scaling factor s. If the training length is L and you want to extend to L', set s = L'/L. The encoding of position m then behaves like the encoding of position m/s — the sequence the model "sees" is effectively compressed. 2. Temperature Scaling: Frequency scaling alone makes the attention distribution too sharp (discrimination between distant positions degrades). YaRN introduces a temperature factor t before softmax to soften the distribution. 3. NTK-aware Extension: Drawing on Neural Tangent Kernel theory, the scaling is adjusted dynamically across frequency components — high-frequency components (responsible for local distinctions) are scaled less, while low-frequency components (long-range relations) are scaled more.

Key Results

  • 10x fewer tokens compared to direct fine-tuning methods for context extension
  • 2.5x fewer training steps
  • LLaMA models can effectively use context far beyond their original training length
  • Capable of extrapolating beyond the fine-tuning dataset

Impact Assessment

YaRN is the "economical" approach to context extension. Before it, extending context windows typically required retraining on large amounts of high-quality long documents (e.g., GPT-4's 32K/128K versions). YaRN demonstrated that by modifying the frequencies of positional encodings, existing models can be extended with minimal compute. Later work (such as CodeLLaMA and LongLLaMA) builds on YaRN or its variants.

Commentary (Feynman-style)

> The cleverness of this paper lies in exploiting a hidden property of RoPE — positional encodings are fundamentally a set of frequencies. Changing frequencies means changing the "ruler's scale." YaRN doesn't train the model to handle longer sequences; it makes the model "believe" the sequence isn't that long. It's like giving a nearsighted person a weaker pair of glasses so they can see farther — not curing the nearsightedness, but redefining what "far" means. Once you understand the underlying mechanism (RoPE is frequencies), you can pull off tricks like this.

Tags

#yarn#rope#context-extension#long-context#llama#positional-encoding#efficient-fine-tuning#transformers

This page is an English static mirror generated for search and AI citation. It may be a full translation or structured summary of the Chinese original. Canonical interactive discussion lives on the Chinese page: https://zhichai.net/topic/177619755