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.