Long-context language models often neglect information in the middle of the input, a phenomenon known as "lost-in-the-middle". Part of this gap may stem from the long-range decay of rotary position embedding (RoPE), which weakens the positional signal between distant tokens. Existing input compression deletes low-importance tokens, and attention reweighting acts only after RoPE has weakened this signal. RoPE-level methods modify positions directly but apply the same remapping to every query. To address these limitations, we propose QGPM (Query-Guided Positional Modulation), a training-free method that reshapes RoPE positions using query relevance from the model's own attention heads. QGPM builds a broad relevance map for lower layers and a sharp one for upper layers. Low-relevance tokens advance by less than one position on fast-rotating planes, pulling relevant evidence toward the query. Slow-rotating planes keep the original layout, with every token in place. Experiments on NoLiMa and RULER with multiple context lengths show that QGPM improves over base models and strong baselines, with the largest gains on retrieval tasks, and generalizes across multiple backbones. Our code is available at https://github.com/iZHpy/QGPM.
Figures & tables
Figure 1: Overview of QGPM. (a) A plain prefill reads the attention from the query to each context sentence in a few selected heads. The fused sentence ranking is shaped into a broad relevance map and a sharp score map. Context before the first relevant sentence is protected and keeps a score of 1. (b) Each map sets a positional stride for every token on every frequency plane. Low-relevance tokens advance by less than one position on fast planes. Slow planes, relevant sentences, and the query keep unit stride. Cumulative sums give the remapped positions. (c) In the answering pass, the lower layers use the broad map and the upper layers use the sharp map. Decoding continues in unit steps from the last position of each map.
Qwen2.5-7B
Qwen3-4B
Method
NoLiMa
QA-1
QA-2
MK
MV
Avg.
NoLiMa
QA-1
QA-2
MK
MV
Avg.
Vanilla
28.5
79.6
57.0
94.3
83.5
68.6
16.1
77.5
56.6
99.6
96.0
69.2
LLMLingua-2
19.7
51.0
40.0
0 2.2
65.3
35.6
10.4
49.1
36.6
0 2.0
75.9
34.8
Ms-PoE
32.9
77.2
52.0
36.8
68.5
53.5
16.6
75.4
55.3
88.2
84.7
64.0
Self-Extend
29.5
79.3
57.5
78.8
82.6
65.5
18.1
76.5
57.5
99.8
92.2
68.8
STRING
33.1
79.4
57.8
92.3
76.6
67.8
22.9
78.8
60.0
99.3
93.0
70.8
Table 1: Accuracy (%) averaged over context lengths, from 4K to 32K for NoLiMa and from 4K to 64K for RULER. MK and MV denote NIAH-MK and NIAH-MV, and Avg. is the mean over the five tasks. Bold marks the best result for each backbone. † Includes 64K, which exceeds the configured window of Ministral-8B. Appendix E gives the results at each length.
Figure 2: Accuracy (%) of all five tasks at each context length. Gray lines show LLMLingua-2, Ms-PoE, and Self-Extend. The shaded region marks 64K for Ministral-8B, which exceeds the configured window of this backbone.
QGPM vs.
NoLiMa
NIAH
QA
All
Vanilla
16/0/0
31/6/3
24/2/14
71/8/17
LLMLingua-2
16/0/0
40/0/0
40/0/0
96/0/0
Ms-PoE
16/0/0
40/0/0
38/1/1
94/1/1
Self-Extend
16/0/0
34/3/3
26/2/12
76/5/15
STRING
16/0/0
38/2/0
22/2/16
76/4/16
Table 2: Wins, ties, and losses (W/T/L) of QGPM against each method over settings of backbone, task, and context length. The four columns cover 16, 40, 40, and 96 settings.
Qwen2.5-7B
NoLiMa
Method
NoLiMa
QA-1
MV
Ministral
Granite
Vanilla
28.5
79.6
83.5
11.5
18.6
STRING
33.1
79.4
76.6
17.1
25.9
Top-K
41.1
80.6
91.1
n/a
n/a
Reorder
39.9
80.1
92.2
n/a
n/a
QGPM
46.1
80.4
93.4
24.1
35.6
Table 3: Average accuracy (%) of QGPM, input editing, and Oracle. MV denotes NIAH-MV, and n/a marks settings that are not run. Top-K and Reorder rely on Qwen3-Reranker-4B, whose tokenizer is not fully compatible with those of Ministral-8B and Granite-3.3-8B. Oracle (gray) uses evidence positions marked by hand, which are costly to obtain for every backbone. Bold marks the best result other than Oracle.
Figure 3: Accuracy (%) of QGPM, input editing, and Oracle at each context length on Qwen2.5-7B. Top-K and Reorder share one color as the two input editing variants. Oracle (dashed) uses annotated evidence and serves as a reference.
Appendix figures & tables4 assets
Supplementary material from the paper’s appendix.
Appendix
Backbone
Layers
Heads
θ
Window
Qwen2.5-7B
28
28 / 4
107
1,010,000
Qwen3-4B
36
32 / 8
5×106
262,144
Ministral-8B
36
32 / 8
108
32,768
Granite-3.3-8B
40
32 / 8
107
131,072
Appendix
Table 4: Backbone configurations from the HuggingFace releases. Heads lists query heads and key-value heads. Window is the configured maximum number of positions.
Qwen2.5-7B
Qwen3-4B
Task
Method
4K
8K
16K
32K
64K
Avg.
4K
8K
16K
32K
64K
Avg.
NoLiMa
Vanilla
0 36.7
0 32.9
0 27.2
0 17.2
–
0 28.5
0 30.6
0 19.4
0 10.3
00 4.0
–
0 16.1
LLMLingua-2
0 29.8
0 18.5
0 17.4
0 13.2
–
0 19.7
0 23.2
0 10.7
00 5.0
00 2.8
–
0 10.4
Ms-PoE
0 41.2
0 36.6
0 32.2
0 21.7
–
0 32.9
0 32.2
0 19.9
00 9.5
00 4.6
–
0 16.6
Self-Extend
0 36.2
0 31.7
0 27.1
0 22.8
–
0 29.5
0 30.7
0 19.9
0 13.9
00 8.0
–
0 18.1
STRING
0 38.0
0 34.4
0 33.1
0 26.7
–
0 33.1
0 35.6
0 29.5
0 18.7
00 7.9
–
0 22.9
Appendix
Table 5: Accuracy (%) at each context length on Qwen2.5-7B and Qwen3-4B. NoLiMa is evaluated up to 32K, and Avg. is the mean over the evaluated lengths. Bold marks the best result for each backbone, task, and length.
Ministral-8B
Granite-3.3-8B
Task
Method
4K
8K
16K
32K
64K †
Avg.
4K
8K
16K
32K
64K
Avg.
NoLiMa
Vanilla
0 25.8
0 13.0
00 4.5
00 2.6
–
0 11.5
0 31.8
0 21.7
0 12.9
00 7.9
–
0 18.6
LLMLingua-2
0 15.9
00 6.1
00 1.9
00 0.8
–
00 6.2
0 22.8
0 16.9
0 10.3
00 6.5
–
0 14.1
Ms-PoE
0 19.8
00 9.6
00 3.6
00 2.9
–
00 9.0
0 31.6
0 19.8
0 14.4
00 8.3
–
0 18.5
Self-Extend
0 25.8
0 11.5
00 4.4
00 2.5
–
0 11.1
0 29.0
0 18.7
0 12.0
00 6.5
–
0 16.6
STRING
0 32.9
0 19.8
0 10.6
00 5.1
–
0 17.1
0 40.3
0 30.0
0 20.8
0 12.4
–
0 25.9
Appendix
Table 6: Accuracy (%) at each context length on Ministral-8B and Granite-3.3-8B. NoLiMa is evaluated up to 32K, and Avg. is the mean over the evaluated lengths. Bold marks the best result for each backbone, task, and length. † Exceeds the configured window of Ministral-8B.
Task
Method
4K
8K
16K
32K
64K
Avg.
NoLiMa
Vanilla
36.7
32.9
27.2
17.2
–
28.5
STRING
38.0
34.4
33.1
26.7
–
33.1
Top-K
47.9
50.7
37.2
28.7
–
41.1
Reorder
50.4
44.7
38.2
26.4
–
39.9
QGPM
56.5
54.9
43.8
29.1
–
46.1
Oracle
57.9
50.8
45.8
34.8
–
47.3
Appendix
Table 7: Accuracy (%) of QGPM, input editing, and Oracle at each context length on Qwen2.5-7B. Oracle (gray) is shown for reference. Bold marks the best result other than Oracle.
We identify intrinsic limitations of Rotary Positional Embeddings (RoPE) in Transformer-based long-context language models. Our theoretical analysis abstracts away from the specific content of the context and depends only on its length. We prove that as context length increases, RoPE-based attention becomes unpredictable and loses two properties that are central to its effectiveness. First, it loses its locality bias: RoPE is no more likely to favor nearer positions than substantially farther ones. Second, it loses consistency in token relevance: a key vector that receives a higher attention score than an alternative at one position may receive a lower score at another. In both cases, the probability of failure approaches 0.5, no better than random guessing. We further prove that the attention score can remain unchanged when a key token is moved to a different position, or even replaced by a different token, indicating a failure to distinguish positions or tokens. Adjusting the RoPE base trades off distinguishing positions against distinguishing tokens but cannot preserve both at the same time. Increasing the RoPE base hyperparameter, a common practice in today's long-context models, helps distinguish different tokens, but inevitably sacrifices the ability to distinguish positions. Our empirical analysis shows that multi-head, multi-layer architectures are insufficient to overcome these limitations. Our findings suggest that fundamentally new mechanisms for encoding position and token order may be needed in future Transformer long-context language models.
Yufeng Du, Phillip Harris, Minyang Tian +5
University of Illinois at Urbana-Champaign · USA · University of Bonn +3
Long-context failures of RoPE-based language models can arise from RoPE's intrinsic tradeoff between maintaining stable token preferences and distinguishing nearby positions. Determining which weakness to address, and how, requires a more precise characterization of RoPE's behavior in trained models across context lengths. We address a key limitation of prior theory by allowing unequal query-key scales across RoPE frequencies, which aligns well with practical empirical observations. Our theory makes both vulnerabilities measurable for individual heads and inputs, and quantifies how high-frequency components support positional sensitivity while potentially disrupting semantic stability. We also derive a theoretical context-length bound beyond which, under specified conditions, a fixed attention-score comparison cannot jointly avoid semantic reversal and positional insensitivity. Guided by our fresh theoretical insights, we introduce RoPE Profiler, a lightweight, plug-and-play diagnostic toolkit that augments existing evaluations with zero additional forward passes by reusing cached query and key activations. Reusing activations collected during evaluation, the toolkit incurs little overhead. It supplements standard benchmark scores with two diagnostic scores that reveal semantic and positional weaknesses and help users prioritize which aspect to address. Crucially, our evaluations across 49 long-context task settings reveal a distinct pattern where reasoning tasks predominantly suffer from semantic reversal, whereas retrieval tasks are primarily vulnerable to positional insensitivity. Guided by our theory and diagnostic profiles, targeted high-frequency rescaling achieves immediate gains without additional training, improving task accuracy by up to 20 percentage points on Qwen3-8B and 25 percentage points on Llama-3.1-8B-Instruct.
Large language models (LLMs) increasingly operate in settings that require reliable long-context understanding, such as retrieval-augmented generation and multi-document reasoning. A common strategy is to fine-tune pretrained short-context models at the target sequence length. However, we find that standard long-context adaptation can remain brittle: model accuracy depends strongly on the absolute placement of relevant evidence, exhibiting high positional variance even when controlling for task format and difficulty. We propose RoPE-Perturbed Self-Distillation, a training regularizer that improves positional robustness. The core idea is to form alternative "views" of the same training sequence by perturbing its RoPE indices -- effectively moving parts of the context to different positions -- and to train the model to produce consistent predictions across views via self-distillation. This encourages reliance on semantic signals instead of brittle position dependencies. Experiments on long-context adaptation of Llama-3-8B and Qwen-3-4B demonstrate consistent gains on long-context benchmarks, including up to 12.04% improvement on RULER-64K for Llama-3-8B and 2.71% on RULER-256K for Qwen-3-4B after SFT, alongside improved length extrapolation beyond the training context window.
Zichong Li, Chen Liang, Liliang Ren +3
Work is done during internship at Microsoft. · Georgia Institute of Technology · Microsoft.