Resonance RoPE: Improving Context Length Generalization of Large Language Models

Suyuchen Wang, Ivan Kobyzev, Peng Lu, Mehdi Rezagholizadeh, Bang Liu

Abstract

Modern Large Language Models (LLMs) often face performance degradation when processing sequences longer than their training window, a challenge known as Train-Short-Test-Long (TSTL) generalization. This paper identifies a "feature gap" in standard Rotary Position Embeddings (RoPE) caused by non-integer wavelengths, which leads to phase shifts at out-of-distribution (OOD) positions. We propose Resonance RoPE, a simple, zero-overhead modification that rounds RoPE wavelengths to the nearest integer. This ensures that features "resonate" with the discrete token grid, eliminating the phase shift. Our results show that Resonance RoPE significantly enhances the long-context capabilities of models like LLaMA and Mistral, particularly when paired with scaling techniques like YaRN.

Introduction: The Challenge of Long Contexts

Most state-of-the-art LLMs utilize Rotary Position Embeddings (RoPE) to encode positional information. While effective, these models are typically trained on relatively short sequences (e.g., 4,096 tokens). When deployed on much longer sequences, performance often collapses.

Existing solutions typically involve interpolation (scaling indices to fit the training range) or extrapolation. However, the authors identify a more fundamental issue: RoPE features are continuous functions, but tokens are discrete. Because wavelengths ($\lambda_i = 2\pi / \theta_i$) are usually not integers, a model might learn a pattern at position $m$ during training that looks different at a much later position $n$ in a long sequence, even if the relative distance is identical. This "feature gap" prevents the model from recognizing familiar patterns at OOD positions.

Methodology: Harmonizing with the Token Grid

Resonance RoPE addresses the feature gap by aligning RoPE wavelengths with the discrete nature of tokens.

  1. Wavelength Calculation: For each dimension $i$, the standard wavelength is $\lambda_i = 2\pi / \theta_i$.
  2. Integer Rounding: We round this wavelength to the nearest integer: $\tilde{\lambda}_i = \text{round}(\lambda_i)$.
  3. Frequency Adjustment: A new "resonant" frequency is derived: $\tilde{\theta}_i = 2\pi / \tilde{\lambda}_i$.

By ensuring wavelengths are integers, each feature repeats exactly every $\tilde{\lambda}_i$ tokens. This eliminates the phase shift for "pre-critical" dimensions (those with wavelengths shorter than the training length), allowing the model to generalize its learned patterns to much larger positions.

Comparison of rotation angles in standard RoPE vs. Resonance RoPE. Resonance RoPE eliminates the phase shift between training and OOD positions.

This modification is performed offline and introduces zero computational overhead during training or inference.

Experimental Evaluation

The effectiveness of Resonance RoPE was evaluated using a combination of synthetic benchmarks and real-world datasets.

Synthetic Benchmark: POSGEN

The authors introduced POSGEN, a suite of three tasks designed to isolate position recognition:

  • Recursive: Fibonacci-style dependencies.
  • Chain-of-Thought (CoT): Step-by-step reasoning dependencies.
  • Semi-recursive: Variable-distance dependencies.

On the challenging "Semi-Recursive" task, applying Resonance RoPE to the YaRN scaling method improved OOD accuracy from 33.70% to 48.46%.

Real-World Performance

We evaluated LLaMA2-7B models fine-tuned on the PG19 dataset. Performance was measured using perplexity (PPL) on long-form documents from the GovReport and Proofpile datasets, as well as the comprehensive L-Eval benchmark.

Perplexity vs. Context Length on GovReport and Proofpile. Resonance YaRN consistently achieves lower perplexity across extended context lengths.

As shown in the figure above, Resonance YaRN consistently outperforms standard YaRN and other scaling techniques like Dynamic NTK, maintaining lower perplexity as context length increases up to 32k tokens.

Key Insights and Discussion

  • The Integer Wavelength Hypothesis: The core insight is that position embeddings should align with the discrete token grid. Non-integer wavelengths cause the embedding to "slide," creating features unseen during training.
  • Synergy with Scaling: Resonance RoPE is complementary to global scaling methods like YaRN. While YaRN handles the extension of the overall range, Resonance RoPE fixes the "local" feature gap within that range.
  • Limitations: Resonance RoPE primarily targets "pre-critical" dimensions. For "post-critical" dimensions (wavelengths longer than the training length), extrapolation techniques remain necessary.

Conclusion

Resonance RoPE provides a robust and efficient solution to the context length generalization problem in LLMs. By simply rounding wavelengths to the nearest integer, we can significantly bridge the gap between training and OOD performance without any additional parameters or computational cost.

Summary updated

BibTeX
@inproceedings{wang2024resonance,
  author = {Suyuchen Wang and Ivan Kobyzev and Peng Lu and Mehdi Rezagholizadeh and Bang Liu},
  title = {{Resonance RoPE: Improving Context Length Generalization of Large Language Models}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  url = {https://openreview.net/forum?id=X44iekHCjG},
}
Figure