LongRecipe: Recipe for Efficient Long Context Generalization in Large Language Models

Zhiyuan Hu, Yuliang Liu, Jinman Zhao, Suyuchen Wang, WangYan WangYan, Wei Shen, Qing Gu, Anh Tuan Luu, See-Kiong Ng, Zhiwei Jiang, Bryan Hooi

Abstract

Extending the context window of Large Language Models (LLMs) is traditionally a resource-intensive process, often requiring massive GPU clusters. This paper introduces LongRecipe, a highly efficient fine-tuning framework designed to extend context windows (e.g., from 8k to 128k) using only 30% of the target sequence length. By focusing on impactful tokens and simulating long-range position indices, LongRecipe reduces compute costs by over 85%, enabling GPT-4 level long-context performance on a single 80GB GPU in just one day.

Introduction

Standard LLMs are typically pretrained on short context windows (4k-8k tokens), leading to failure when processing massive documents for summarization or retrieval. While brute-force continual pretraining on longer sequences is a common solution, the quadratic scaling of memory and compute costs makes it prohibitive for most researchers. Existing efficient methods like YaRN or PoSE often either require full-length fine-tuning or introduce distortions in local sentence structures that degrade performance. LongRecipe addresses these challenges by training on long contexts without feeding full-length sequences, while preventing "catastrophic forgetting" of baseline reasoning skills.

Methodology

The LongRecipe framework is built upon three primary pillars: Impactful Token Analysis, Position Index Transformation, and Training Optimization.

Impactful Token Analysis

Rather than training on arbitrary text chunks, LongRecipe identifies tokens that exhibit the most significant logit changes when a model is extended to longer contexts. Analysis revealed that specific token types, particularly numerals (NUM), are highly sensitive to context extension—experiencing roughly 4x the logit change compared to conjunctions. The training corpus is subsequently filtered and upsampled to prioritize sentences containing these "anchor" tokens.

Impactful Token Analysis identifies sensitive tokens like numerals that drive long-context adaptation.

Position Index Transformation

To simulate a 128k context window using only a fraction of the tokens, LongRecipe employs a synthetic position index strategy. Extracted sentences are assigned non-continuous position indices. While local chunks remain continuous to preserve grammatical structure (averaging 88 tokens), random "skips" are introduced between chunks. This technique effectively trains the model's Rotary Position Embeddings (RoPE) to handle long-range dependencies without requiring the memory overhead of filler text.

Position Index Transformation uses index skipping to simulate long-range distances within short training sequences.

Training Optimization: Replay and Merging

To mitigate the degradation of general reasoning skills caused by position index manipulation, the framework utilizes two techniques:

  1. Pretraining Data Replay: Brief training on original short-context instruction data.
  2. Model Merging: Averaging the weights of the original short-context model with the newly trained long-context model to restore baseline performance.

Evaluation and Results

LongRecipe was evaluated using a diverse set of benchmarks, including Multi-Needle In A Haystack (NIAH-M), RULER, and LongBench for long-context tasks, alongside MMLU, GSM8K, and HumanEval for general capabilities.

The framework successfully extended Llama3-8B, Mistral-7B, and Qwen2-7B to 128k context windows. Key results include:

  • Efficiency: Training requires only 30% of the target context window size, reducing computational resources by over 85%.
  • Performance: On NIAH-M, LongRecipe outperformed baseline efficient methods like RPES and PoSE by 6.6% and 7.8%, respectively.
  • Accessibility: An 8k base model was extended to 128k in one day on a single H100 GPU.
  • Generalization: Models retained ~75% of math (GSM8K) and ~65% of coding (HumanEval) performance, with negligible impact on MMLU scores.

Conclusion

LongRecipe demonstrates that semantic coherence is not strictly necessary for long-context post-training; teaching the attention mechanism to handle vast distances via skipped indices is sufficient. Future work aims to extend these models to 512k and 1 million token capacities, further democratizing the development of long-context LLMs.

Summary updated

BibTeX
@inproceedings{hu2025longrecipe,
  author = {Zhiyuan Hu and Yuliang Liu and Jinman Zhao and Suyuchen Wang and WangYan WangYan and Wei Shen and Qing Gu and Anh Tuan Luu and See-Kiong Ng and Zhiwei Jiang and Bryan Hooi},
  title = {{LongRecipe: Recipe for Efficient Long Context Generalization in Large Language Models}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure