LongRoPE Guide: Extending Context to 2M Tokens via Search

The Problem: The Out-of-Distribution Rotational Shock of 2M-Token Contexts
In enterprise knowledge management, automated code repository ingestion, and multi-year financial audits in platforms like AiFetchly, foundation models are increasingly tasked with ingesting massive continuous sequences: analyzing entire software microservices, multi-thousand-page regulatory filings, or encyclopedic technical literature in a single prompt.
While modern transformer architectures comfortably support $4\text{k}$, $8\text{k}$, or $32\text{k}$ token contexts, expanding context windows past $100\text{k}$ tokens encounters a fundamental mathematical barrier: the out-of-distribution rotational shock of Rotary Position Embeddings (RoPE).
In standard RoPE (Su et al., 2024), token position $m$ is encoded by rotating Query and Key vectors in 2D dimensional planes using sinusoidal frequencies:
$$\theta_i = b^{-2i/d}, \quad i \in \left[0, \frac{d}{2}-1\right]$$
Where $b$ is the base frequency (typically $10,000$ or $500,000$), $d$ is the attention head dimension, and $i$ indexes the 2D subspace.
When an LLM trained on an $8\text{k}$ context is prompted with $128\text{k}$ or $2,048\text{k}$ ($2\text{M}$) tokens without positional adjustments, the rotational angle $m \cdot \theta_i$ visits unseen, out-of-distribution values:
- Attention dot-products fluctuate wildly, causing attention entropy to explode.
- The model suffers immediate representation collapse, outputting garbled text or repetitive loops.
The Limitations of Classical RoPE Extension Techniques:
┌───────────────────────────┬────────────────────────────────────────────────────────┐
│ Extension Mechanism │ Structural Limitation & Point of Failure │
├───────────────────────────┼────────────────────────────────────────────────────────┤
│ Position Interpolation │ Scales all frequencies uniformly by s = L'/L. Downscales│
│ (PI - Chen et al.) │ high-frequency local syntax, destroying grammar. │
├───────────────────────────┼────────────────────────────────────────────────────────┤
│ NTK-Aware RoPE │ Scales the base b to b'. Fails at >32k contexts due to │
│ (bloc97 / dynamic NTK) │ catastrophic phase misalignment on long sequences. │
├───────────────────────────┼────────────────────────────────────────────────────────┤
│ YaRN (Yet another RoPE) │ Hand-crafted 3-zone heuristic (ramp function). │
│ │ Plateaus at 128k; cannot stretch to 1M or 2M tokens. │
├───────────────────────────┼────────────────────────────────────────────────────────┤
│ Uniform SFT Brute-Force │ Full pretraining on 2M tokens requires millions of GPU │
│ │ cluster FLOPs; destroys baseline 4k/8k performance. │
└───────────────────────────┴────────────────────────────────────────────────────────┘ 1. The Uniform Scaling Fallacy
All earlier positional interpolation paradigms share a critical flaw: they assume positional scaling factors should be uniform across hidden dimensions or conform to simplistic hand-crafted curves.
In reality, modern transformer attention heads do not treat rotary dimensions uniformly:
- Low-Frequency Dimensions (Early Head Indices): Rotate slowly, encoding coarse, long-range global sequence ordering. These dimensions require heavy interpolation to compress long distances into the model’s familiar window.
- High-Frequency Dimensions (Late Head Indices): Rotate rapidly within short spans, capturing precise local lexical syntax, adjacent word dependencies, and intra-phrase grammar. Aggressively scaling high-frequency dimensions blurs local distinctions, destroying the model’s basic linguistic fluency.
2. Token Position Non-Uniformity: The Attention Sink Crisis
Furthermore, positional scaling needs vary across token positions:
- Empirical research confirms that initial prompt tokens act as “attention sinks” (StreamingLLM), soaking up excess attention probability mass to stabilize softmax normalizations.
- Applying interpolation to initial positions alters the attention sink geometry, triggering severe perplexity spikes even when the subsequent text is within normal sequence lengths.
3. The Short-Context Penalty
When engineering teams aggressively tune RoPE frequencies to accommodate massive sequence lengths (e.g., $128\text{k}$ tokens):
- The model’s performance on standard short benchmarks ($4\text{k}$ and $8\text{k}$ tasks like MMLU, GSM8K, and HumanEval) degrades significantly.
- Enterprises are forced into a costly compromise: maintaining separate checkpoints for short-context queries and long-context documents.
AI systems needed an automated, principled architecture: Can we mathematically search the exact, non-uniform scaling configuration required to stretch context windows to 2 Million tokens with minimal compute?
The Architecture: LongRoPE and Non-Uniform Evolutionary Search
To break through the $128\text{k}$ barrier and achieve $2,048,000$ token context windows, researchers at Microsoft Research introduced LongRoPE (Ding et al., 2024 / 2025).
LongRoPE eliminates manual heuristics. Instead, it formulates positional interpolation as an optimization problem solved via an Evolutionary Search Algorithm, discovering non-uniform scaling factors tailored to both dimensional channels and token positions.
The LongRoPE Architectural Overview:
┌────────────────────────────────────────────────────────────────────────────────────────┐
│ Stage 1: Evolutionary Search on 8x Context (e.g., 8k ──► 256k Window) │
│ - Searches non-uniform dimension multipliers: λ_i ∈ [1.0, 8.0] │
│ - Searches attention sink position threshold: n_hat │
│ - Minimizes token perplexity over 256k document slices │
└───────────────────────────────────┬────────────────────────────────────────────────────┘
│
▼
┌────────────────────────────────────────────────────────────────────────────────────────┐
│ Stage 2: Minimal Fine-Tuning at 256k Context Window │
│ - Applies Stage 1 non-uniform factors directly to model rotary embeddings │
│ - Fine-tunes model on 256k sequences for ONLY 1,000 steps (< 1% full SFT compute!) │
│ - Solidifies long-range associative attention while preserving core representations │
└───────────────────────────────────┬────────────────────────────────────────────────────┘
│
▼
┌────────────────────────────────────────────────────────────────────────────────────────┐
│ Stage 3: Second Evolutionary Search to 2,048k (2 Million Tokens) │
│ - Operates on top of the 256k fine-tuned checkpoint │
│ - Searches secondary non-uniform scaling multipliers: λ'_i ∈ [1.0, 64.0] │
│ - ZERO ADDITIONAL FINE-TUNING REQUIRED! Instantly extends context to 2M tokens! │
└───────────────────────────────────┬────────────────────────────────────────────────────┘
│
▼
┌────────────────────────────────────────────────────────────────────────────────────────┐
│ Stage 4: Short-Context Performance Recovery (Dual-Mode Serving) │
│ - Dynamically re-scales RoPE factors for sequences ≤ 4,096 tokens │
│ - Preserves 100% baseline accuracy on MMLU, GSM8K, and HumanEval benchmarks │
└────────────────────────────────────────────────────────────────────────────────────────┘ 1. Formulating Non-Uniform Positional Scaling
In LongRoPE, rather than dividing the position $m$ by a scalar $s$, each dimension $i \in [0, d/2 - 1]$ is scaled by a dedicated factor $\lambda_i \in \mathbb{R}^+$:
$$\theta_i’ = \theta_i \cdot \frac{1}{\lambda_i}$$
Furthermore, LongRoPE incorporates positional thresholding:
- For tokens at positions $m \le \hat{n}$ (the attention sink threshold, typically the first $32$ to $128$ tokens), no interpolation is applied ($\lambda_i = 1.0$), ensuring that critical initial prompt anchors remain pristine.
- For tokens beyond $\hat{n}$, dimension-wise scaling factors $\lambda_i$ smoothly interpolate positional angles.
2. Why Evolutionary Search Outperforms Manual Heuristics
The search space for non-uniform scaling factors is vast:
- For an attention head dimension of $d = 128$, there are $d/2 = 64$ independent frequency parameters.
- Finding optimal values analytically is impossible because transformer cross-layer attention dynamics are non-convex and non-monotonic.
LongRoPE employs a genetic evolutionary algorithm:
- Individual Representation: A vector $\mathbf{\Lambda} = (\lambda_0, \lambda_1, \dots, \lambda_{d/2 - 1}, \hat{n})$.
- Fitness Objective: The negative log-likelihood (perplexity) evaluated on long-context text chunks.
- Genetic Operations: Mutation (adding Gaussian noise to $\lambda_i$) and Crossover (swapping dimensional slices between high-performing parent configurations).
Comparing RoPE Scaling Paradigms:
+--------------------------+-------------------+-------------------+--------------------+
| Architecture | Scaling Profile | Search Technique | Max Context Window |
+--------------------------+-------------------+-------------------+--------------------+
| Position Interpolation | Uniform (Scalar s)| None (Analytic) | 16k - 32k tokens |
| NTK-Aware RoPE | Base Shift (b') | Empirical Tuning | 32k - 64k tokens |
| YaRN | 3-Zone Ramp Curve | Hand-Crafted Grid | 128k tokens |
| LongRoPE (Microsoft) | Non-Uniform (λ_i) | Evolutionary Alg. | 2,048,000 tokens |
+--------------------------+-------------------+-------------------+--------------------+ Deep Dive: The Progressive Scaling Curriculum and Short-Context Recovery
Directly stretching a model’s context window by $256\times$ (from $8\text{k}$ to $2\text{M}$ tokens) in a single jump fails: the search space is too sparse, and the initial perplexity landscape is overwhelmingly noisy.
LongRoPE overcomes this through a two-stage progressive extension curriculum.

1. Two-Stage Progressive Expansion Pipeline
- Stage 1 (Extending 8x from 8k to 256k Tokens):
- LongRoPE initializes an evolutionary search population bounded within $[1.0, 8.0]$.
- The search identifies the optimal non-uniform configuration $\mathbf{\Lambda}_{256\text{k}}$ that minimizes perplexity across $256\text{k}$-token document slices.
- The base model is fine-tuned with $\mathbf{\Lambda}_{256\text{k}}$ for only 1,000 steps on $256\text{k}$ sequences. This brief training aligns the self-attention projection weights with the expanded positional subspace without causing catastrophic forgetting.
- Stage 2 (Extending 64x from 256k to 2,048k Tokens):
- Using the $256\text{k}$ fine-tuned checkpoint as the new base, LongRoPE launches a secondary evolutionary search targeting a context window of $2,048,000$ tokens ($2\text{M}$).
- The search space is expanded to $[1.0, 64.0]$.
- The Breakthrough: Because the $256\text{k}$ fine-tuning already taught the model to utilize extended context, the secondary non-uniform factors $\mathbf{\Lambda}_{2\text{M}}$ achieve low perplexity with zero additional fine-tuning steps!
2. Short-Context Recovery via Dual-Scale Normalization
A notorious side effect of extreme context scaling is the “dilution” of positional distances at short ranges. If two words are $10$ tokens apart, scaling by $256\times$ compresses their positional angle difference to almost zero, making the model blind to local word order.
LongRoPE implements Dual-Scale Positional Readjustment:
- During inference, if the input prompt length $L \le 4,096$ tokens, the model automatically switches to a short-context scaling vector $\mathbf{\Lambda}_{\text{short}}$ discovered via a complementary search objective.
- For prompts exceeding $4,096$ tokens, the full long-context scaling vector $\mathbf{\Lambda}_{2\text{M}}$ is applied.
- This dynamic switching completely eliminates short-context performance degradation, achieving 100% parity with the un-modified base model on standard reasoning benchmarks.
End-to-End Implementation: Building a LongRoPE Rotary Embedding Module
The following standalone PyTorch script demonstrates how to construct a complete, executable LongRoPE module featuring non-uniform dimension-wise scaling, attention sink thresholding, and verification across sequences up to $2,048,000$ tokens.
import torch
import torch.nn as nn
import math
from typing import Optional, Tuple, List
class LongRoPEEmbedding(nn.Module):
"""
Non-Uniform Rotary Positional Embedding (LongRoPE) module.
Supports dimension-wise scaling factors (lambda_i) and attention sink thresholding.
"""
def __init__(
self,
dim: int = 128,
max_position_embeddings: int = 2048000,
base: float = 10000.0,
attention_sink_threshold: int = 64
):
super().__init__()
self.dim = dim
self.max_seq_len = max_position_embeddings
self.base = base
self.sink_threshold = attention_sink_threshold
# 1. Base RoPE frequencies: theta_i = base^(-2i/d) for i in [0, d/2 - 1]
half_dim = dim // 2
inv_freq = 1.0 / (base ** (torch.arange(0, half_dim, dtype=torch.float32) * 2 / dim))
self.register_buffer("inv_freq", inv_freq, persistent=False)
# 2. LongRoPE Non-Uniform Dimension Scaling Factors (lambda_i)
# Default initialization: all 1.0 (standard RoPE)
self.register_buffer("dimension_factors", torch.ones(half_dim, dtype=torch.float32))
def set_non_uniform_factors(self, factors: torch.Tensor, sink_threshold: Optional[int] = None):
"""Injects non-uniform scaling factors discovered via evolutionary search."""
assert factors.shape[0] == self.dim // 2, "Factors must match half head dimension"
self.dimension_factors.copy_(factors)
if sink_threshold is not None:
self.sink_threshold = sink_threshold
def forward(self, x: torch.Tensor, seq_len: int) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Computes cosine and sine embedding matrices for positions up to seq_len.
Applies thresholding: positions <= sink_threshold use unscaled RoPE.
"""
device = x.device
positions = torch.arange(seq_len, device=device, dtype=torch.float32)
# Scale frequencies by dimension factors: inv_freq_scaled = inv_freq / lambda_i
scaled_inv_freq = self.inv_freq.to(device) / self.dimension_factors.to(device)
# Outer product: [seq_len, half_dim]
# Position-dependent modulation for attention sinks
freqs = torch.outer(positions, scaled_inv_freq)
# For positions <= sink_threshold, enforce standard unscaled frequencies
if self.sink_threshold > 0:
unscaled_freqs = torch.outer(positions[:self.sink_threshold], self.inv_freq.to(device))
freqs[:self.sink_threshold] = unscaled_freqs
# Concatenate for full head dimension: [seq_len, dim]
freqs_full = torch.cat([freqs, freqs], dim=-1)
cos = torch.cos(freqs_full)
sin = torch.sin(freqs_full)
return cos, sin
def rotate_half(x: torch.Tensor) -> torch.Tensor:
"""Rotates half the hidden dimensions of the input."""
half = x.shape[-1] // 2
x1 = x[..., :half]
x2 = x[..., half:]
return torch.cat((-x2, x1), dim=-1)
def apply_longrope(x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor:
"""Applies rotary transformation: (x * cos) + (rotate_half(x) * sin)"""
# x shape: [B, num_heads, seq_len, head_dim]
return (x * cos.unsqueeze(0).unsqueeze(1)) + (rotate_half(x) * sin.unsqueeze(0).unsqueeze(1))
if __name__ == "__main__":
torch.manual_seed(42)
print("==================================================================")
print("LONGROPE NON-UNIFORM ROTARY EMBEDDING VERIFICATION")
print("==================================================================")
head_dim = 128
half_dim = head_dim // 2 # 64 independent rotational planes
test_context_len = 2048000 # 2 Million Tokens!
longrope = LongRoPEEmbedding(dim=head_dim, max_position_embeddings=test_context_len)
# Mock non-uniform scaling factors discovered via evolutionary search
# Notice: early (low-freq) dimensions scaled heavily (e.g., 32x-64x),
# while late (high-freq) dimensions scaled lightly (e.g., 1.2x-3.0x) to preserve syntax!
mock_factors = torch.linspace(48.0, 1.5, steps=half_dim)
longrope.set_non_uniform_factors(mock_factors, sink_threshold=64)
# Mock queries: 1 sequence, 8 attention heads, sequence length 4 (sampled at distance 2M)
mock_q = torch.randn(1, 8, 4, head_dim)
# Compute embeddings at boundary (pos = 2,047,996 to 2,048,000)
cos, sin = longrope(mock_q, seq_len=test_context_len)
# Slice embeddings for our test positions
cos_slice = cos[test_context_len - 4 : test_context_len]
sin_slice = sin[test_context_len - 4 : test_context_len]
q_rotated = apply_longrope(mock_q, cos_slice, sin_slice)
print(f"Target Max Context Length: {test_context_len:,} tokens (2 Million)")
print(f"Rotary Head Dimension: {head_dim} (64 complex 2D planes)")
print(f"Attention Sink Threshold: First 64 tokens preserved unscaled")
print(f"Min Dimension Scaling Factor: {mock_factors.min().item():.2f}x (High-Frequency)")
print(f"Max Dimension Scaling Factor: {mock_factors.max().item():.2f}x (Low-Frequency)")
print(f"Rotated Tensor Shape: {list(q_rotated.shape)}")
print(f"Finite Values Check: {torch.all(torch.isfinite(q_rotated)).item()}")
print("\nSUCCESS: LongRoPE rotary embeddings computed stably at 2,048,000 token horizon!") Production Benchmarks: Empirical Accuracy Across 2 Million Tokens
LongRoPE was rigorously evaluated across open-weights foundation models (LLaMA-2-7B/13B, LLaMA-3-8B, and Mistral-7B) on the Needle In A Haystack (NIAH) retrieval evaluation and standard short-context reasoning benchmarks.
1. Needle In A Haystack Pass Rates Across Sequence Lengths
Needle In A Haystack (NIAH) Retrieval Accuracy (%):
┌────────────────────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
│ Model / Scaling Method │ 128k Tokens │ 512k Tokens │ 1,024k (1M) │ 2,048k (2M) │
├────────────────────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
│ LLaMA-2 + Position Interp. │ 14.2% │ 0.0% │ 0.0% │ 0.0% │
│ LLaMA-2 + NTK-Aware │ 38.6% │ 0.0% │ 0.0% │ 0.0% │
│ LLaMA-2 + YaRN (128k SFT) │ 98.4% │ 22.1% │ 0.0% │ 0.0% │
├────────────────────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
│ LongRoPE (LLaMA-2-7B) │ 100.0% (SOTA)│ 99.8% (SOTA) │ 99.5% (SOTA) │ 98.9% (SOTA) │
│ LongRoPE (Mistral-7B) │ 100.0% (SOTA)│ 100.0% (SOTA)│ 99.8% (SOTA) │ 99.2% (SOTA) │
└────────────────────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
* Evaluates 100 random depth and position insertion trials. The empirical findings confirmed three core conclusions:
- Flawless 2M Retrieval: LongRoPE achieves a near-perfect 99.2% pass rate on the 2,048k Needle-in-a-Haystack evaluation on Mistral-7B, maintaining associative recall across distances previously inaccessible to open models.
- Extreme Compute Efficiency: Reaching $2\text{M}$ context required only 1,000 fine-tuning steps at $256\text{k}$ context, slashing pretraining compute costs by over $99.5%$ compared to standard context extension regimes.
- Generalization Beyond Fine-Tuned Windows: The second evolutionary search extended the model from $256\text{k}$ to $2,048\text{k}$ with zero training, proving that non-uniform frequency alignment unlocks latent extrapolation capacity already present within pre-trained transformer weights.
2. Preservation of Short-Context Benchmark Accuracy
Short-Context Benchmark Retention (4,096-token Tasks):
┌────────────────────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
│ Scaling Strategy │ MMLU (5-shot)│ GSM8K (8-shot│ HumanEval │ ARC-Challenge│
├────────────────────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
│ Base Model (LLaMA-2-7B) │ 45.3% │ 16.8% │ 12.8% │ 43.6% │
│ Standard 128k YaRN SFT │ 41.2% (-4.1%)│ 13.5% (-3.3%)│ 9.8% (-3.0%) │ 39.4% (-4.2%)│
├────────────────────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
│ LongRoPE (Dual-Scale Mode) │ 45.4% (+0.1%)│ 16.9% (+0.1%)│ 12.8% (0.0%) │ 43.5% (-0.1%)│
└────────────────────────────┴──────────────┴──────────────┴──────────────┴──────────────┘ Thanks to LongRoPE’s dual-scale recovery mechanism, the model suffers zero degradation on standard knowledge and reasoning benchmarks, completely solving the catastrophic short-context penalty.
Strategic Recommendations: Scaling to 2M Context in 2026
When engineering enterprise long-context retrieval pipelines, software teams should adopt the following architectural principles:
1. Exploit Evolutionary Search Over Hand-Crafted Curves
Avoid hard-coded heuristics (such as simple linear interpolation or basic NTK multipliers) when extending sequences beyond $64\text{k}$ tokens:
- Use evolutionary algorithms to search dimension-specific factors $\lambda_i$.
- Preserve high-frequency dimensions ($i \approx d/2$) with minimal scaling to prevent grammar and syntax destruction.
- Anchor attention sinks by strictly exempting initial tokens ($n \le \hat{n}$) from interpolation.
2. Pair LongRoPE with Inference Acceleration Kernels
Stretching context to 2 million tokens creates massive memory bandwidth demands during autoregressive generation:
- Combine LongRoPE with FlashDecoding (as explored in our FlashDecoding Guide) to parallelize the KV cache across GPU Streaming Multiprocessors.
- Deploy dynamic KV compression algorithms like SnapKV (see our SnapKV Guide) or Infini-Attention (see our Infini-Attention Guide) to keep VRAM footprints bounded within physical GPU memory.
3. Deploy Dual-Scale Normalization in Production Gateways
In client-facing production serving environments:
- Inspect prompt token length at the API gateway layer.
- Dynamically dispatch the short-context scaling parameters $\mathbf{\Lambda}{\text{short}}$ for user turns under $4\text{k}$ tokens, and activate the full long-context parameters $\mathbf{\Lambda}{2\text{M}}$ for massive multi-document analysis.
By combining non-uniform frequency search with a progressive two-stage curriculum, Microsoft LongRoPE establishes the modern blueprint for extending foundation model horizons to 2 million tokens without sacrificing short-range reasoning precision.