Extending LLMs' Context Window with 100 Samples

Yikai Zhang, Junlong Li, Pengfei LiuarXiv 2024 · DOI

Abstract

Large Language Models (LLMs) typically struggle with inputs exceeding their pre-trained context window. We present entropy-aware ABF, a highly efficient method that extends the context window of models like LLaMA-2 from 4,096 to 16,384 tokens using as few as 100 training samples. By stabilizing attention entropy through adjusted base frequencies and layer-dependent logit scaling, this approach achieves state-of-the-art performance while requiring significantly fewer resources than existing interpolation methods.

Introduction

The fixed sequence length of LLMs (e.g., 4,096 tokens for LLaMA-2) is a major bottleneck for long-document processing. This limitation stems largely from Rotary Position Embeddings (RoPE), which fail to generalize to unseen token distances. While solutions like Position Interpolation (PI) and YaRN exist, they are often computationally expensive and data-intensive. This research introduces a theoretically grounded, resource-efficient alternative that leverages the relationship between attention entropy and model focus.

The Entropy-Aware ABF Method

The proposed method, entropy-aware ABF, integrates two primary components to maintain model performance over extended sequences:

  1. Adjusted Base Frequency (ABF): By increasing the RoPE base frequency (e.g., from 10,000 to 500,000), the positional encoding space is "stretched." This ensures that distant tokens appear more familiar to the model, resembling the distances encountered during initial pre-training.
  2. Dynamic, Layer-Dependent Logit Scaling: To counteract the natural increase in "attention entropy"—the tendency for attention to become more uniform and less focused as context grows—the authors apply a scaling factor $t$ to attention logits before the Softmax layer.

Crucially, this scaling is dynamic, adjusting based on token position ($t = \max(\log_c i, 1)$), and layer-dependent. Analysis revealed that the first two layers of LLMs naturally exhibit high entropy as they perform global scans. Applying scaling to these layers would disrupt their function; thus, they are left unscaled.

Information entropy across different layers and token positions. Note the distinct behavior of the first two layers compared to deeper layers.

Experimental Setup and Results

The model was fine-tuned on a curated subset of ShareGPT conversations, filtered for lengths up to 16,384 tokens. Remarkably, the method proved effective with just 100 samples, reaching a 16k window in approximately 6 minutes on four A100 GPUs.

Evaluation was conducted using LongBench, which spans 12 tasks including single/multi-document QA and summarization. The entropy-aware ABF model ("Ours") achieved a Macro average score of 39.1, significantly outperforming the baseline Llama-2-7B-Chat (33.0) and other extension methods like YaRN (35.7).

Performance comparison across LongBench tasks. Our method achieves the highest Macro average score.

Key Insights

  • The Entropy Hypothesis: LLM performance is tied to stable attention entropy. Spikes in entropy at long contexts lead to a loss of focus. Logit scaling serves as an inductive bias that helps the model maintain concentration.
  • Efficiency through Inductive Bias: Because the method provides a strong structural hint via scaling, the model does not need to re-learn attention patterns from scratch, explaining the extreme data efficiency.
  • Extrapolation Robustness: Unlike PI or YaRN, which often fail when tested beyond their fine-tuned window, entropy-aware ABF demonstrates better stability at 32k tokens, even when only trained on 16k.

Conclusion

Entropy-aware ABF offers a path to long-context LLMs that is both high-performing and accessible. While the method excels at general long-context tasks, future work will address specific challenges like short-answer retrieval where current fine-tuning objectives may be suboptimal. Nevertheless, starting with lengthy general conversations appears to be a highly effective baseline for context extension.

Summary updated

BibTeX
@misc{zhang2024extending,
  author = {Yikai Zhang and Junlong Li and Pengfei Liu},
  title = {{Extending LLMs' Context Window with 100 Samples}},
  year = {2024},
  eprint = {2401.07004},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2401.07004},
}
Figure