Randomized YaRN Improves Length Generalization for Long-Context Reasoning

Manas Mehta, Fangcong Yin, Greg DurrettarXiv 2026

TL;DR

Randomized YaRN is a new training recipe that enables Large Language Models (LLMs) to reason over context lengths up to 128K, even when trained only on much shorter sequences. By combining YaRN-based positional interpolation with randomized position sampling and a progressive length curriculum, the authors significantly outperform standard fine-tuning methods on challenging long-context reasoning benchmarks.

What Problem Does This Paper Address?

Most LLMs are pretrained on relatively short sequences (e.g., 4K or 8K tokens) due to the quadratic cost of attention. While techniques like RoPE (Rotary Positional Embeddings) allow for some context extension, models often fail to generalize to sequences significantly longer than what they saw during fine-tuning—a problem known as the "length generalization" gap.

Existing solutions like YaRN provide a way to interpolate positional embeddings to handle longer contexts, but applying them zero-shot often leads to poor reasoning performance. Conversely, fine-tuning on massive long-context datasets is computationally expensive and often just shifts the "distribution" rather than teaching the model true generalization. The authors ask: Can we train a model on short data in a way that makes it robust to much longer contexts at inference time?

Methods

The authors introduce Randomized YaRN, which builds on the YaRN (Yet another RoPE extension) method. It consists of two primary components:

  1. Randomized Positional Encoding (RPE): During training on a short sequence of length $L$, the model doesn't just see positions $0$ to $L-1$. Instead, it samples $L$ indices from a much larger range (up to a target length $L'$) and assigns them to the tokens in sorted order. This exposes the model to the "rotations" and features of distant positional encodings while still training on computationally efficient short sequences.
  2. Length-Generalization Curriculum: Rather than jumping to the maximum target length immediately, the authors use a curriculum. They gradually increase the maximum sampling range ($L_t$) over the course of training epochs. This allows the model to first master moderate extrapolations before tackling extreme ones.
Figure 1: Randomized YaRN trains LLMs for length generalization by: (1) Random position sampling: ran- domly sampling positions from a longer length distribu- tion, assigning the corresponding YaRN encodings to each token in the short-context training data to expose models to OOD positional encodings; and (2) Length- generalization curriculum: gradually increasing the sam- pling length distribution as a training curriculum.

At inference time, the model uses standard YaRN encoding with a fixed scale factor, benefiting from the OOD (out-of-distribution) positional robustness developed during training.

Datasets

The method was evaluated on three benchmarks designed to test actual reasoning, not just simple retrieval:

  • BABILong: A "needle-in-a-haystack" style benchmark that requires multi-hop reasoning over facts hidden in long documents.
  • Multi-Round Coreference Resolution (MRCR): A task requiring the model to track entities and resolve references across long, multi-turn conversations.
  • LongBench v2: A collection of four-way multiple-choice questions based on real-world long documents (QA, code, etc.).

Key Insights and Ideas

  • Training is Mandatory: Simply adding YaRN at inference time (zero-shot) provides negligible gains in reasoning accuracy. The model must be exposed to the interpolated positional distribution during training to use it effectively.
  • Curriculum is the Secret Sauce: Ablation studies showed that without the length curriculum, performance drops significantly (e.g., a 22.9% drop for RPE on MRCR). Gradually increasing the "difficulty" of the positional shifts is crucial for stability.
  • Simulating Distance: By sampling random indices, the model learns that the relative distance between tokens is more important than their absolute position, effectively "simulating" the experience of being in a 128K token document while only processing 8K tokens.

Key Results

Randomized YaRN consistently outperformed standard LoRA fine-tuning and other interpolation baselines like PoSE across all models tested (Qwen2.5-7B, Olmo3-7B, and Qwen3-14B).

  • BABILong: On Qwen2.5-7B, Randomized YaRN achieved an OOD average of 90.3%, compared to just 81.3% for standard Trained YaRN and 83.6% for vanilla LoRA. Notably, at the 128K context mark, it maintained 83.9% accuracy while LoRA plummeted to 63.0%.
Table 1: Performance on BABILong. Randomized YaRN shows superior stability as context length increases to 128K.
  • MRCR: The gains were even more pronounced here. For Qwen3-14B, Randomized YaRN hit 91.4% OOD average, whereas the standard Trained YaRN baseline only managed 78.3%.
Table 2: Performance on MRCR. Randomized YaRN significantly outperforms baselines at extreme OOD lengths.

Limitations and Future Work

  • Evaluation Scale: The study focused on 7B and 14B parameter models. Whether these findings hold for 70B+ or frontier-scale models remains to be seen.
  • Language Scope: The experiments were conducted exclusively on English-language data.
  • Training/Inference Gap: The method is most effective when the inference context is significantly longer than the training context. If the training data is already very long, the relative benefits of randomization might diminish.
  • Future Directions: The authors suggest exploring how this method interacts with different attention implementations (like sliding window attention) and whether it can be combined with data-centric approaches for even greater gains.

Summary updated

BibTeX
@misc{mehta2026randomized,
  author = {Manas Mehta and Fangcong Yin and Greg Durrett},
  title = {{Randomized YaRN Improves Length Generalization for Long-Context Reasoning}},
  year = {2026},
  eprint = {2606.23687},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2606.23687},
}
Figure