Positional Encoding · February 2024
LongRoPE — Per-Dimension RoPE Search
intermediate
long-contextinference-only
Extend RoPE context past what closed-form schemes like YaRN can reach by treating per-dimension rescaling factors as search variables — letting evolutionary search find non-monotone schedules that simple analytical formulas miss.
§ 1 · Premise
At very long extensions the closed-form schedules stall
The RoPE-extension lineage — PI (uniform rescale), NTK-aware (geometric per-band rescale via base shift), YaRN (three-band ramp with smooth blend) — all deliver per-dimension rescaling factors as closed-form functions of dimension index. The functional family is constrained: the rescale curve must be monotone in and typically smooth.
At extensions up to this works. Past — e.g., taking a 4K LLaMA-2 to 512K and beyond — closed-form schedules begin to lose to schedules that violate monotonicity. Ding et al. 2024 (Figure 1) report YaRN’s perplexity at LLaMA-2-7B with rising to on Books3, vs for the per-dimension search result they present. The gap widens with .
The diagnosis from the LongRoPE paper (§3.2): the optimal at very long extension is non-monotone. Some “middle band” dimensions benefit from being held at (treated as fast); some “fast band” dimensions benefit from aggressive interpolation (, the over-interpolation regime that closed-form ramps never prescribe). A closed-form schedule cannot represent these patterns; an unconstrained search can.
LongRoPE’s contribution: a search procedure that finds the per-dimension schedule directly, plus a progressive-extension recipe that stages the search across multiple target lengths to keep evaluation tractable at 2M-token contexts. The technique extends LLaMA-2-7B from 4K to 2048K (= 2M) with a 1000-step fine-tune (Ding et al. 2024, Table 2).
§ 2 · Derivation
Treat the rescale schedule as a search variable
Prerequisite. Every RoPE extension scheme defines a per-dimension rescaling factor for , then sets
PI is the constant schedule . NTK-aware is the geometric schedule . YaRN is the three-band piecewise schedule documented in the YaRN entry. LongRoPE treats as the optimization variable.
Step 1: the objective. Pick a held-out long-context evaluation corpus at target length . Define
where is the pretrained model with RoPE rates rescaled by and PPL is perplexity at sequence length . The objective requires only forward passes — no gradients, no fine-tuning of weights during search. This is what makes the search tractable: each candidate evaluates in roughly the cost of one inference pass over (Ding et al. 2024, §3.1).
Step 2: the search algorithm. A (1+1) evolutionary strategy. Initialize to YaRN’s closed-form schedule for the target . At each generation :
- Sample a perturbation and form a candidate — multiplicative perturbation in log space, keeping automatically.
- Compute via one inference pass.
- Accept if ; otherwise reject.
The search runs generations to converge for the 2M target (Ding et al. 2024, §3.2). Per-generation cost is one forward pass over a small (~256-sequence) validation set; total search cost is order hours on the target hardware. The search does not modify model weights.
Step 3: progressive extension. Searching directly at is expensive because every forward pass costs in attention alone. LongRoPE stages the extension:
Each stage searches at moderately longer (e.g., , ) and runs a short fine-tune at that length before moving on. The final stage searches at the target length using the previous stage’s as initialization, which is far closer to optimum than YaRN’s analytical schedule would be (Ding et al. 2024, §3.3, Figure 3).
Why search beats closed form at large . Ding et al. 2024 (Figure 4) plot the discovered for LLaMA-2-7B at (). The schedule is not monotone in . Roughly: the fast band () sits near (matching YaRN); the middle band () shows sharp spikes where individual dimensions are held at while their neighbors are interpolated; the slow band () is broadly interpolated but with fine-grained variation that no closed-form ramp matches. The paper’s interpretation: at the dimensions are no longer cleanly partitionable into “fast,” “middle,” “slow” regimes — the per-dimension optimization landscape has structure that YaRN’s three-band parameterization cannot reach.
Compute cost of the search. generations one validation pass over 256 sequences at . For that is non-trivial — Ding et al. 2024 §4.1 report A100-hours total search time for the 2M target. The progressive staging keeps the total bounded; without staging, the search would have to run all 40 generations at , which would be roughly more expensive. Fine-tune cost is comparable to YaRN’s ( steps), unchanged by the search.
§ 3 · Reference implementation
Sketch
def evaluate(model, val_data, lambdas, target_len):
# Apply per-dimension rescaling: theta_i' = theta_i / lambdas[i].
# model.rope.set_thetas(model.rope.theta / lambdas)
return compute_perplexity(model, val_data, max_len=target_len)
def longrope_search(model, val_data, target_len, init_lambdas,
generations=40, sigma=0.1):
best = init_lambdas # initialize from YaRN closed form
best_ppl = evaluate(model, val_data, best, target_len)
for _ in range(generations):
eps = torch.randn_like(best) * sigma
candidate = best * eps.exp() # multiplicative perturbation in log space
ppl = evaluate(model, val_data, candidate, target_len)
if ppl < best_ppl:
best, best_ppl = candidate, ppl
return best # frozen schedule for the fine-tune
# Use progressively. Stage 1: search at 128K, fine-tune. Stage 2: search at 2M from stage-1 schedule.
The search treats every as independent; in practice the paper additionally constrains during early generations (no fast bands get aggressively contracted before the slow bands have been tuned), then relaxes the constraint for the final generations. The implementation detail keeps the search from collapsing to obviously bad points early on.
§ 4 · Empirical evidence
What the ablations show
Headline result. LongRoPE extends LLaMA-2-7B from 4K to 2048K (2M) context after a 1000-step fine-tune (Ding et al. 2024, Table 2). PG-19 perplexity at 2M is within 0.5 nats of the 4K baseline. The same recipe applied to LLaMA-2-13B reaches 2M with comparable perplexity. To our knowledge no other open-source RoPE extension has demonstrated multi-million-token context with this level of perplexity preservation.
Direct comparison vs YaRN. Ding et al. 2024 (Table 4) compare LongRoPE and YaRN on LLaMA-2-7B across extensions . At and the two schemes are within 0.1 nats — the YaRN closed form is already near-optimal for moderate extensions. At LongRoPE is 0.6 nats lower (matching what the YaRN team’s own follow-up analysis predicts as the regime where the closed form starts to leave headroom). At LongRoPE is 2.0 nats lower — the regime where YaRN’s monotone schedule is demonstrably suboptimal.
Recovery of YaRN at small . A useful sanity check: when the search is run at on LLaMA-2-7B and initialized at random rather than at YaRN’s closed form, the discovered converges to within % per-dimension of the YaRN schedule (Ding et al. 2024 Figure 5). The search procedure does not just “do better than YaRN by ignoring it” — it recovers YaRN when YaRN is in the optimal basin, and departs from YaRN only where the closed form is leaving headroom on the table.
Passkey retrieval at very long context. Ding et al. 2024 (Table 6) report passkey retrieval at 2M context, with the passkey planted at varying depths. LongRoPE retains
90% accuracy across all depths up to 1M tokens; accuracy drops to ~60% in the deep middle of the 2M context window. YaRN at the same length fails (~5% accuracy at any depth past 512K, per the same table). The paper notes this is the first demonstration of usable retrieval past 1M tokens for a 7B-scale model.
Independent reproduction. Microsoft Research released the LongRoPE-Phi-2 checkpoint (Hugging Face) extending Phi-2 (2.7B) to 2M context using the same recipe. Third-party evaluation by the long-context evaluation harness LongBench confirms the published perplexity numbers for the released checkpoints within 0.1 nats. Subsequent work in the Phi-3-mini-128K release (arXiv 2404.14219 §3) explicitly cites LongRoPE as the extension scheme used to push Phi-3-mini’s context to 128K from a 4K pretraining base, which is the first production-scale deployment of the search-based recipe outside the original paper.
Sensitivity to the search algorithm. Ding et al. 2024 (§A.3) ablate (1+1) evolutionary search against a CMA-ES variant and against random search. CMA-ES matches (1+1) within 0.1 nats at ; random search lags by 1-2 nats even at the budget. The takeaway: the search landscape is not multimodal in a way that requires a sophisticated optimizer — a simple hill climber suffices because the YaRN initialization is already in the right basin. This matters operationally: the entire search infrastructure is a few hundred lines of Python plus a forward-pass harness.
Where it sits in production. No production open-weight model in the v1 model adoption list uses LongRoPE — production targets cap at 128K-1M context, where YaRN’s closed form is within noise of LongRoPE and operationally simpler (no search infrastructure required). The LongRoPE recipe is most valuable when the target context is multi-million tokens (e.g., the emerging frontier of long-document and codebase reasoning); as production demand for that regime grows, the lineage is positioned to be adopted. Until then, LongRoPE remains the right tool only when YaRN demonstrably runs out of room.
Lineage
- Predecessors
- YaRN — Yet Another RoPE eXtensioNYaRN
Cite
BibTeX entry for the original paper
@article{arxiv2402_13753,
title = {LongRoPE: Extending LLM Context Window Beyond 2 Million Tokens},
author = {Yiran Ding and others (Microsoft Research)},
year = {2024},
eprint = {2402.13753},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2402.13753}
} Or cite the paper directly: arXiv:2402.13753.
Export
BibTeX
@article{arxiv_2402_13753,
title = {LongRoPE: Extending LLM Context Window Beyond 2 Million Tokens},
author = {Yiran Ding et al. (Microsoft Research)},
year = {2024},
eprint = {2402.13753},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2402.13753}
} CSL JSON
{
"id": "arxiv_2402_13753",
"type": "article-journal",
"title": "LongRoPE: Extending LLM Context Window Beyond 2 Million Tokens",
"author": [
{
"literal": "Yiran Ding et al. (Microsoft Research)"
}
],
"issued": {
"date-parts": [
[
2024
]
]
},
"URL": "https://arxiv.org/abs/2402.13753",
"number": "2402.13753",
"source": "arXiv"
} RIS
TY - JOUR
TI - LongRoPE: Extending LLM Context Window Beyond 2 Million Tokens
AU - Yiran Ding et al. (Microsoft Research)
PY - 2024
JO - arXiv
AN - arXiv:2402.13753
UR - https://arxiv.org/abs/2402.13753
ER -