LongAttn: Selecting Long-context Training Data via Token-level Attention

Longyun Wu, Dawei Zhu, Guangxiang Zhao, Zhuocheng Yu, Junfeng Ran, Xiangyu Wong, Lin Sun, Sujian Li

Abstract

Researchers have developed LongAttn, a framework that uses an LLM’s own self-attention mechanism to identify high-quality long-context training data. By analyzing token-level dependencies rather than coarse sentence-level metrics, LongAttn selects data so effectively that a model trained on just 5 billion "high-dependency" tokens can outperform models trained on 20 billion randomly sampled tokens. Furthermore, LongAttn is significantly faster to compute than previous state-of-the-art selection methods, making it a scalable solution for enhancing LLM context windows.

Introduction

As Large Language Models (LLMs) evolve, their context windows have expanded significantly. However, simply increasing the window size is insufficient; models require training on data that necessitates long-range reasoning. Most current long-context datasets are constructed naively or filtered using sentence-level linguistic metrics like perplexity. These approaches suffer from misalignment with the transformer's token-to-token architecture and computational inefficiency. For instance, methods like ProLong can run 16x slower than standard inference, hindering scalability.

Methodology

The authors propose LongAttn, which leverages the self-attention matrix of a pre-trained LLM to score how much a text segment relies on long-distance information. The framework calculates a Long-distance Dependency Score ($LDS_T$) based on two primary components:

  1. Dependency Strength ($DS_T$): Measures the proportion of attention scores assigned to tokens at least $k$ positions away (where $k$ is 1/4 of the sequence length). High $DS_T$ indicates the model is actively retrieving information from the distant past.
  2. Distribution Uniformity ($DU_T$): To mitigate the "Attention Sink" effect—where initial tokens collect high attention regardless of semantic relevance—the authors calculate the variance of the attention distribution. They prioritize data where dependencies are spread across the document.
The LongAttn Framework overview, showing the data processing pipeline from raw datasets to a long-context capable model.

A key technical insight is the use of the first decoder layer for scoring. Deeper layers are more prone to attention sinks, while the first layer effectively captures raw contextual aggregation at a fraction of the computational cost (~1/32 of a full forward pass).

Experimental Setup

The authors introduced LongABC-32K, a high-quality dataset filtered from ArXiv papers, literary books, and code repositories. Starting from a 36B token pool, LongAttn was used to select subsets of 1B, 3B, 5B, and 10B tokens. Evaluation was conducted using the RULER benchmark, NIAH (Needle In A Haystack), and LongBench.

Results and Insights

LongAttn demonstrates a "less is more" effect, where data quality significantly outweighs quantity. As shown in the performance comparison, a model trained on 5B tokens of LongAttn-selected data achieves better long-context retrieval than a model trained on 20B tokens of randomly sampled data.

Average NIAH-32K scores across different training token volumes, comparing LongAttn (LongABC-32K) against ProLong and random baselines.

Key Findings:

  • Performance: On the RULER benchmark (32k length), the LongAttn-trained model (5B tokens) achieved an average score of 82.30, outperforming the random baseline (78.20) and ProLong (81.20).
  • Efficiency: LongAttn is approximately 12x faster than sentence-level methods like ProLong. Selecting data from the LongABC-Raw dataset required only 50 GPU hours with LongAttn, compared to 600 GPU hours for ProLong.
  • Scalability: Using a larger "teacher" model (e.g., LLaMA-3-70B) to select data for a smaller model (LLaMA-3-8B) further improved results.
  • General Capability: The models maintained performance on short-context tasks like coding (HumanEval), though a slight dip in general knowledge (MMLU) was observed, likely due to the specialized nature of the long-context sources.

Conclusion

LongAttn provides a robust, efficient mechanism for identifying high-quality long-context training data by looking directly at the model's internal attention states. While the $O(N^2)$ complexity of attention maps remains a challenge for extremely long sequences, the use of efficient kernels like Flash-Attention could mitigate this. Future work will focus on refining the $LDS_T$ metric to better distinguish meaningful dependencies from noise and optimizing data mixing ratios to prevent the forgetting of general knowledge during continual pre-training.

Summary updated

BibTeX
@inproceedings{wu2025longattn,
  author = {Longyun Wu and Dawei Zhu and Guangxiang Zhao and Zhuocheng Yu and Junfeng Ran and Xiangyu Wong and Lin Sun and Sujian Li},
  title = {{LongAttn: Selecting Long-context Training Data via Token-level Attention}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
  url = {https://openreview.net/forum?id=2UKr2TnXmg},
}
Figure