Found in the Middle: How Language Models Use Long Contexts Better via Plug-and-Play Positional Encoding

Zhenyu Zhang, Runjin Chen, Shiwei Liu, Zhewei Yao, Olatunji Ruwase, Beidi Chen, Xiaoxia Wu, Zhangyang Wang

Introduction

While modern Large Language Models (LLMs) boast massive context windows—spanning 32k, 128k, or even millions of tokens—they often suffer from a frustrating quirk known as the "lost-in-the-middle" phenomenon. Models are generally excellent at retrieving information from the very beginning or the very end of a prompt, but struggle significantly when the relevant "needle" is hidden in the middle of the "haystack."

To overcome this, researchers have introduced Multi-scale Positional Encoding (Ms-PoE), a "plug-and-play" technique that significantly improves how LLMs handle information buried in the middle of long documents. By simply rescaling positional indices across different attention heads—without requiring any extra training or fine-tuning—Ms-PoE achieves an average accuracy boost of up to 3.8 points on rigorous long-context benchmarks.

The Root of the Problem

The persistent difficulty LLMs face in identifying middle-situated information stems from two primary culprits:

  1. Casual Attention Bias: Transformer architectures naturally favor initial tokens because they undergo more attention operations during the generation process.
  2. RoPE Decay: Most open-source LLMs utilize Rotary Positional Encoding (RoPE), which includes a "long-term decay" property. This mathematically predisposes the model to prioritize nearby tokens over distant ones, effectively causing the model to "forget" the middle as the sequence grows.

Methodology: Multi-scale Positional Encoding (Ms-PoE)

The core idea behind Ms-PoE is surprisingly simple: if the model ignores the middle because it feels "too far away," the solution is to mathematically compress the perceived distance of those tokens.

Multi-scale Positional Encoding (Ms-PoE) applies varying scaling ratios across different attention heads.

The approach relies on three key components:

  • Index Rescaling: The position index $m$ is replaced with $m/r$, where $r$ is a scaling ratio. This effectively "compresses" the perceived distance between tokens, pushing back against the mathematical decay inherent in RoPE.
  • Multi-scale Assignment: Instead of applying a single scaling ratio to the entire model, Ms-PoE assigns different ratios to different attention heads. This creates a "multi-scale" view, where some heads focus on short-range dependencies while others are forced to look further across the context.
  • Position-Awareness Scoring: The authors discovered that some attention heads are naturally "position-aware" (able to track information even when it moves), while others are "position-unaware." By developing a scoring metric ($S_P$) during the prefilling stage, they assign smaller scaling ratios (closer to 1.0) to position-aware heads to preserve their precision, and larger ratios to unaware heads to help them capture middle-context information.

Crucially, Ms-PoE is entirely plug-and-play. It requires zero fine-tuning and introduces no computational overhead during inference.

Experimental Setup

The researchers evaluated Ms-PoE across several rigorous long-context benchmarks:

  • Zero-SCROLLS: A suite of seven tasks including document summarization (GovReport) and long-form QA (Qasper).
  • MDQA (Multi-Document Question Answering): Designed to test retrieval from various positions in a 10-document stack.
  • Key-Value Retrieval: A synthetic "needle-in-a-haystack" test requiring the model to find a specific value associated with a key among many pairs.
  • LongBench-EN: A comprehensive benchmark consisting of 13 different long-context tasks.

Key Results and Insights

The results demonstrate that Ms-PoE acts as a rare "free lunch" for long-context language modeling, offering consistent improvements across different model sizes (7B and 13B) and base context lengths.

Performance comparison on MDQA and Key-Value Retrieval tasks, demonstrating how Ms-PoE flattens the 'U-shaped' accuracy curve.
  • Flattening the Curve: In MDQA and Key-Value retrieval tests, the traditional "U-shaped" performance curve—where middle-context accuracy drops into a valley—became significantly flatter. The performance gap between the best and worst retrieval positions was reduced by approximately 2% to 4%.
  • Zero-SCROLLS Gains: Ms-PoE achieved an average accuracy gain of up to 3.8 points across various models, including Llama-2, Vicuna, and StableBeluga.
  • Specific Task Wins: On the Qasper task (Question Answering on Scientific Papers), Ms-PoE boosted Vicuna-7B's performance by a staggering 13.4 points.
  • The "Sweet Spot" for Scaling: Empirical testing revealed that a scaling ratio between 1.5 and 2.0 is the "Goldilocks zone" for mitigating the lost-in-the-middle effect without degrading the model's original capabilities.

Limitations and Future Work

While highly effective, the current study focuses exclusively on models using Rotary Positional Encoding (RoPE). It remains to be seen how these principles apply to other encoding schemes like ALiBi or T5-style relative biases. Additionally, while the linear range of 1.2 to 1.8 worked well for scaling ratios, the authors suggest that exploring more sophisticated, non-linear assignment strategies could yield even better results. Finally, a deeper theoretical understanding of why certain attention heads are inherently more position-aware than others remains a promising avenue for future research.

Summary updated

BibTeX
@inproceedings{zhang2024found,
  author = {Zhenyu Zhang and Runjin Chen and Shiwei Liu and Zhewei Yao and Olatunji Ruwase and Beidi Chen and Xiaoxia Wu and Zhangyang Wang},
  title = {{Found in the Middle: How Language Models Use Long Contexts Better via Plug-and-Play Positional Encoding}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=fPmScVB1Td},
}
Figure