NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Farewell to Length Extrapolation, a Training-Free Infinite Context with Finite Attention Scope

Xiaoran Liu, Qipeng Guo, Yuerong Song, Zhigeng Liu, Kai Lv, Hang Yan, Linlin Li, Qun Liu, Xipeng Qiu · OpenReview

Abstract

The long-context capability of Large Language Models (LLMs) has made significant breakthroughs, but the maximum supported context length remains a critical bottleneck limiting their practical applications. The constraint of context length in LLMs arises from the self-attention mechanism, which cannot effectively and efficiently capture the semantic relationships within infinitely long contexts via the limited pre-trained positional information and attention scope. In this work, we propose ReAttention, a training-free approach enabling LLMs based on the self-attention mechanism to support an infinite context with a finite attention scope under sufficient memory resources. ReAttention performs position-agnostic top-$k$ attention before ordinary position-aware self-attention, freeing LLMs from the length extrapolation issue. We validate the performance of ReAttention on LongBench, L-Eval, and InfiniteBench and demonstrate that it is on par with traditional methods. Furthermore, we apply ReAttention to mainstream LLMs, including LLaMA3.1-8B and Mistral-v0.3-7B, enabling them to support context lengths of at least 1M and even expanding the context length of LLaMA3.2-3B-chat by 128$\times$ to 4M without any further training in Needle-In-A-Haystack tests. We also improve the efficiency of ReAttention with Triton and achieve efficient extrapolation without additional overhead.

Introduction

While modern LLMs have significantly advanced long-context capabilities, they frequently encounter a "length extrapolation" barrier. When models are exposed to sequences longer than those seen during training, performance typically degrades. This collapse is attributed to three primary factors:

  1. Out-of-Distribution (OOD) Positions: Positional embeddings, such as Rotary Positional Embeddings (RoPE), are not trained for extremely high indices.
  2. Attention Dilution: Increasing context length leads to higher attention entropy, making it difficult for the model to focus on critical information.
  3. Computational Complexity: The quadratic scaling of standard self-attention makes processing million-token contexts prohibitively expensive.

Existing methods like StreamingLLM utilize a sliding window approach but often lose information from the middle of the document. ReAttention addresses these challenges by decoupling semantic selection from positional awareness.

Methodology: The ReAttention Framework

ReAttention introduces a two-stage attention mechanism that mimics a "thinking before acting" process, allowing models to handle virtually infinite contexts within a fixed attention window.

The ReAttention architecture featuring position-agnostic TopK-Attention followed by standard Self-Attention.

1. Position-Agnostic Cache Selection

The first stage involves a top-$k$ search across the entire Key-Value (KV) cache. Crucially, this search is position-agnostic; it identifies segments of the past that are semantically most similar to the current query based on raw dot-product similarity, ignoring positional embeddings. This ensures that the selection process is not hindered by OOD positional information.

2. Finite Attention Scope

Once the most relevant segments are identified, they are concatenated with the "global" context (the prompt start) and the "local" context (the most recent tokens). Standard position-aware self-attention is then performed only on this manageable subset. Because the final attention is restricted to a fixed number of tokens, positional indices stay within pre-trained limits, effectively bypassing the extrapolation issue.

3. Triton Optimization

To ensure computational efficiency, the authors developed a custom GPU kernel using Triton. This kernel fuses top-$k$ selection with attention score computation, significantly reducing memory I/O and enabling the processing of long sequences hundreds of times faster than naive implementations.

Experimental Evaluation

The researchers evaluated ReAttention across several rigorous benchmarks, including LongBench, L-Eval, and InfiniteBench. They also utilized the Needle-In-A-Haystack (NIAH) retrieval test to verify the model's ability to find specific facts buried in massive documents, and the RULER benchmark for complex, multi-key retrieval tasks.

Key Results and Insights

ReAttention demonstrates remarkable scalability and performance across various mainstream LLMs.

Needle-In-A-Haystack (NIAH) results for various models using ReAttention, showing high accuracy across massive context lengths.

Massive Context Expansion

ReAttention successfully extended LLaMA 3.1-8B (128k context) to over 1 million tokens and LLaMA 3.2-3B to 4 million tokens—a 128x expansion—without any additional training or fine-tuning.

Performance and Efficiency

On LongBench, ReAttention achieved results comparable to full-attention models, outperforming other sparse methods like StreamingLLM and InfLLM. The Triton-optimized kernel allows handling 64k sequences with minimal memory overhead, preventing VRAM exhaustion that would occur with standard PyTorch implementations.

Semantic Manifolds

A key insight of this work is that positional information is optional for selection. Natural language forms a coherent "linear manifold" in high-dimensional space. ReAttention leverages this by using raw semantic similarity to filter noise more effectively than position-aware products, which can become "distracted" in deeper layers.

Limitations and Future Work

The primary limitation of ReAttention lies in its performance on synthetic or chaotic text. On the RULER benchmark (specifically MultiKey tasks), the model struggled. Synthetic data, such as random UUIDs, creates "overlapping manifolds" that confuse the position-agnostic selection process. Without original positional anchors, the model finds it difficult to distinguish between different branches of high-entropy data. Future research will focus on developing selection strategies that can handle non-natural data structures while maintaining the training-free benefits of ReAttention.

Summary updated

BibTeX
@inproceedings{liu2025farewell,
  author = {Xiaoran Liu and Qipeng Guo and Yuerong Song and Zhigeng Liu and Kai Lv and Hang Yan and Linlin Li and Qun Liu and Xipeng Qiu},
  title = {{Farewell to Length Extrapolation, a Training-Free Infinite Context with Finite Attention Scope}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=KDGP8yAz5b},
}
Figure