LongRoPE: Extending LLM Context Window Beyond 2 Million Tokens

October 28, 2025 ยท View on GitHub

LongRoPE is an effective approach that extends LLM context window beyond 2048k tokens by non-uniformly rescaling RoPE positional embeddings. LongRoPE is accepted by ICML 2024 and has been integrated into Microsoft Phi-3. Learn more about the work LongRoPE: Extending LLM Context Window Beyond 2 Million Tokens:

๐Ÿค— Huggingface Daily Paper

Microsoft Research Official Blog

Microsoft Research Blog

News

  • [2025/02/27] "LongRoPE2: Near-Lossless LLM Context Window Scaling" is now available paper, code.

LongRoPE in Phi3-128k LLMs

LongRoPE currently supports the following Phi3-128k LLMs with 128k context window.

RULER

ModelContext Window4k8k16k32k64k128kAvg
Gemini-1.5-pro1M96.795.89695.995.994.495.8
GPT-4-1106-preview128k96.696.395.293.28781.291.6
GradientAI/LLaMA3 (70B)1M95.293.493.489.482.67287.7
Phi3-mini-128k (3.8B)128k92.391.290.887.779.865.384.5
Mixtral-8x22B64k95.694.993.490.984.731.781.9
ChatGLM (6B)128k87.883.478.669.956.042.069.6
LongChat (7B)32k84.779.970.859.30049.1

Long context code understanding (RepoQA)

ModelContext WindowPythoncppjavatypescriptrustavg
GPT-4o-2024-05-13128k958085969790.6
Gemini-1.5-pro-latest1M918191949690.6
claude-3-opus-20240229200k938388959490.6
Phi3-mini-128k-Instruct128k866473947177.6
GPT-4-turbo-2024-04-09128k847975895576.4
Mixtral-8x22B-Instruct-v0.164k606774835567.8

More short tasks

ModelMMLUGSM8KMedQAAGIEvalBBH-HardHumanEval
Phi3-mini-128k-Instruct68.183.655.336.971.557.9
Mistral-7B61.746.449.635.157.328
Gemma 7B63.659.85042.159.634.1
LLaMA3-Instruct-8B66.577.460.54251.560.4
Mixtral 8x7B68.464.762.245.269.737.8

Multi-modality long context support

ModelMMMUMMBenchScienceQAMathVistaInterGPSChartQA
Phi3-vision 128k-instruct40.480.590.844.538.181.4
LLaVA 1.6-vicuna-7B34.276.370.631.520.555.0
QWEN-VL Chat39.075.867.229.422.350.9
LLaMA3-LLaVA Next-8B36.479.473.734.824.665.8
Claude-3-Haiku40.762.472.033.232.159.3
Gemini 1.0 Pro V42.080.079.735.028.658.0
GPT-4V Turbo55.586.175.747.541.062.3

What does LongRoPE do?

The LongRoPE algorithm is built upon the two forms of non-uniformities in positional interpolation: varying RoPE dimensions and token positions. In order to achieve the best performance on long context windows using non-uniform positional embeddings, LongRoPE:

  • Exploit the best positional embedding rescaling parameters through an efficient search, providing a better initialization for fine-tuning and enabling an 8x extension in non-fine-tuning scenarios;
  • Introduce a progressive extension strategy that first fine-tunes a 256k length LLM and then conducts a second positional interpolation on the fine-tuned extended LLM to achieve a 2048k context window;
  • Readjust scaling factors and retained start tokens on 8k length to recover the short context window performance.

Due to policy restrictions, only evolution search part is now released. Any LLM training techniques such as EasyContext and nnScaler can be applied to the fine-tuning stage.

Quick Start

Build Environment

conda create -n longrope python==3.10
conda activate longrope
# flash-attn needs cuda >= 11.7
pip install -r requirements.txt

Tokenize Data

Tokenize PG19 as evolution search validation dataset and Proof-Pile as evaluation dataset.

bash ./examples/llama3/tokenzie-data.sh

Run evoluation search on Llama-3-8B model to sequence length of 128k:

bash ./examples/llama3/search.sh

The default evolution search hyperparameters are located in evolution/default_hyper_params/*.json. Users can customize the number of iterations, population size, number of parents, number of mutation and crossover operations in each iteration. These parameters will affect the convergence time and robustness of searching results.

Evaluation

Evaluate long-context perplexity and passkey accuracy:

bash ./examples/llama3/evaluate.sh

Citation

If you find that LongRoPE/LongRoPE2 helps your research, please consider citing it:

@misc{ding2024longrope,
      title={LongRoPE: Extending LLM Context Window Beyond 2 Million Tokens}, 
      author={Yiran Ding and Li Lyna Zhang and Chengruidong Zhang and Yuanyuan Xu and Ning Shang and Jiahang Xu and Fan Yang and Mao Yang},
      year={2024},
      eprint={2402.13753},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}

@misc{shang2025longrope2nearlosslessllmcontext,
      title={LongRoPE2: Near-Lossless LLM Context Window Scaling}, 
      author={Ning Shang and Li Lyna Zhang and Siyuan Wang and Gaokai Zhang and Gilsinia Lopez and Fan Yang and Weizhu Chen and Mao Yang},
      year={2025},
      eprint={2502.20082},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2502.20082}, 
}