Data Engineering for Scaling Language Models to 128K Context

Yao Fu, Rameswar Panda, Xinyao Niu, Xiang Yue, Hannaneh Hajishirzi, Yoon Kim, Hao Peng · DOI

Abstract

This paper presents a practical and efficient methodology for extending the context window of Large Language Models (LLMs) to 128K tokens. By employing a "per-source length upsampling" strategy on just 1-5 billion tokens, the authors demonstrate that long-context capabilities can be "unlocked" from existing pretrained models. The resulting LLaMA-2 7B model matches or exceeds GPT-4’s performance on long-context retrieval tasks while maintaining strong performance on standard short-context benchmarks.

The Challenge of Long Context

As LLMs are increasingly deployed for multi-document QA, long-history dialogue, and autonomous agents, the context window has become a critical performance bottleneck. Two primary challenges exist:

  1. The Retrieval Gap: Many models claim support for 100K+ contexts but fail the "Needle-in-a-Haystack" test, where they must retrieve a specific fact buried within a massive document.
  2. The Stability-Efficiency Trade-off: Extending context often requires massive retraining or skewed data mixtures (e.g., training exclusively on books) that can degrade the model's general capabilities in areas like coding or logic.

Methodology: Unlocking Intrinsic Capabilities

The authors hypothesize that the ability to model long-range dependencies is an intrinsic capability acquired during large-scale pretraining, even if the initial training used shorter sequences (e.g., 4K). To "unlock" this capability, they employ several key techniques:

Per-source Length Upsampling

This is the core innovation of the paper. Instead of simply adding more long-form content like books, the authors take the original pretraining data mixture (SlimPajama) and upsample the longest documents within each domain (Web, Code, Arxiv, etc.). This approach preserves the model's original knowledge distribution while exposing it to longer sequences.

Comparison of data source and sequence length distributions across different upsampling strategies.

Continual Pretraining and Optimization

The authors perform continual pretraining on base LLaMA-2 models (7B and 13B) using sequences of 64K to 80K tokens. They adjust the base of the Rotary Positional Embedding (RoPE) to handle expanded coordinates. By utilizing hardware optimizations like DeepSpeed Zero 3 and FlashAttention 2, they demonstrate that training on 80K context is only approximately 3x slower than 4K, making it feasible for researchers with modest academic-level resources.

Key Insights

  • The "Unlock" Hypothesis: Only 500 million to 5 billion tokens are required to unlock long-context capabilities, rather than the hundreds of billions often assumed.
  • Domain Balance: Maintaining the original domain ratio while increasing sequence length is the most stable strategy for preserving general model performance.
  • Validation Loss Limitations: The authors found that validation loss is a poor predictor of retrieval performance; two models with identical loss can have vastly different "Needle-in-a-Haystack" scores.
  • Overfitting Risks: Training beyond 5B tokens on 80K length sequences actually caused performance to decrease on 128K generalization, suggesting the model begins to overfit to the specific training length.

Results and Performance

The proposed LLaMA-2 7B model achieved an 88.0% accuracy on the Needle-in-a-Haystack test at 128K, outperforming GPT-4 Turbo (87.1%) and significantly exceeding open-source baselines like YaRN Mistral and LongLoRA.

Needle-in-a-Haystack performance comparison between the proposed method, open-source baselines, and GPT-4.

Furthermore, the models maintained stable MMLU scores, indicating that context extension did not degrade general intelligence. On real-world tasks like BookQA, the 13B model significantly closed the performance gap with GPT-4, achieving a score of 31.1 compared to GPT-4's 37.4.

Limitations and Future Directions

Despite these successes, challenges remain. Standard setups hit a "memory wall" at 80K context, suggesting that moving to 200K+ will require advanced sequence parallelism. Additionally, this study focused on base models; instruction tuning (SFT) for long-context tasks remains a critical area for future exploration. Finally, while IO optimizations help, the quadratic complexity of attention remains a long-term hurdle for scaling.

Summary updated

BibTeX
@inproceedings{fu2024data,
  author = {Yao Fu and Rameswar Panda and Xinyao Niu and Xiang Yue and Hannaneh Hajishirzi and Yoon Kim and Hao Peng},
  title = {{Data Engineering for Scaling Language Models to 128K Context}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=TaAqeo7lUh},
}
Figure