Shifting Mechanisms: How Positional Encoding Choice Shapes In-Context Retrieval
Organizations: Cornell University
Abstract
Language models increasingly use architectures that vary attention span and positional encoding across layers, such as applying RoPE with sliding-window attention and NoPE with global attention (SWA NoPE). However, how these choices shape in-context retrieval remains unclear. To study this question, we take a mechanistic view, tracing how positional encoding (PE) choice shapes the internal mechanisms models use for in-context retrieval. Across 22 open-weight models spanning eight families, we find that standard RoPE models rely primarily on positional retrieval, while PE hybrids shift toward semantic retrieval. We further show on a controlled pre-training ablation that confining positional encoding to local layers produces this semantic shift, degrading representations of positional information. Finally, we show that the reported long-context gains of PE hybrids mask a retrieval trade-off: SWA NoPE improves over RoPE on multiple-target retrieval and QA, but degrades when distinguishing competing keys. We show that these behavioral differences better track the mechanism shift from positional toward semantic mechanisms than a uniform improvement in long-context retrieval.
Figures & tables
| Model | PE Configuration | Window Size | Local:Global |
|---|---|---|---|
| RoPE Architectures | |||
| Gemma-2 ( Riviere et al., 2024 ) | RoPE (10K) | 4096 | 1:1 |
| Llama-3.1 ( Grattafiori et al., 2024 ) | RoPE (500K) | — | — |
| Qwen-2.5 ( Yang et al., 2024 ) | RoPE (1M) | — | — |
| Qwen-3 ( Yang et al., 2025a ) | RoPE (1M) | — | — |
| PE Hybrid Architectures | |||
| Architecture | Positional | Lexical | Reflexive | Unknown | ||
|---|---|---|---|---|---|---|
| 16K | RoPE | 17.6 | 56.6 | 1.6 | 24.1 | 0.30 |
| SWA RoPE | 19.4 | 46.6 | 2.1 | 31.9 | 0.40 | |
| SWA NoPE | 10.0 | 68.6 | 1.4 | 20.0 | 0.14 | |
| 32K | RoPE | 15.9 | 60.6 | 1.4 | 22.1 | 0.26 |
| SWA RoPE | 16.2 | 57.5 | 1.8 | 24.6 | 0.27 | |
| SWA NoPE | 9.5 | 71.9 | 1.3 | 17.3 | 0.13 |
| Context | Model | Ordering ID | Semantic Key | ||
|---|---|---|---|---|---|
| 16K | RoPE | 94.8 (0.2) | 99.0 (0.2) | ||
| SWA RoPE | 91.6 (0.1) | 98.8 (0.3) | |||
| SWA NoPE | 82.7 (0.3) | 99.4 (0.1) | |||
| 32K | RoPE | 94.9 (0.2) | 99.1 (0.2) | ||
| SWA RoPE | 92.7 (0.2) | 99.0 (0.3) | |||
| SWA NoPE | 83.9 (0.3) | 99.5 (0.1) | |||
| Context | Model | Overall (13) | NIAH mk1 (1) | VT + Aggr. (3) | NIAH rest (7) | QA (2) |
|---|---|---|---|---|---|---|
| 16K | RoPE | 47.1 | 91.2 | 16.9 | 56.1 | 38.7 |
| SWA RoPE | 47.2 | 84.8 | 18.5 | 57.2 | 36.7 | |
| SWA NoPE | 58.3 | 78.0 | 17.5 | 77.2 | 43.4 | |
| 32K | RoPE | 43.3 | 92.4 | 18.1 | 50.8 | 30.7 |
| SWA RoPE | 45.7 | 81.6 | 23.0 | 55.1 | 28.8 | |
| SWA NoPE | 53.9 | 82.2 | 15.6 | 69.6 | 42.0 |
| Confusable keys | Multi-item | ||||||
|---|---|---|---|---|---|---|---|
| Context | Model | multivalue | multiquery | ||||
| 16K | RoPE | 97.0 | 90.8 | 84.6 | 70.8 | 35.0 | 35.0 |
| SWA RoPE | 92.6 | 80.4 | 75.4 | 60.2 | 44.0 | 38.4 | |
| SWA NoPE | 88.2 | 73.0 | 57.4 | 52.6 | 80.8 | 85.7 | |
| 32K | RoPE | 96.8 | 91.8 | 82.6 | 67.6 | 25.2 | 30.5 |
| SWA RoPE | 82.6 | 75.6 | 57.4 | 46.8 | 26.4 | 37.9 | |
Appendix figures & tables13 assets
Supplementary material from the paper’s appendix.
Appendix
| RoPE | PE Hybrid | ||||
|---|---|---|---|---|---|
| Model | Layer | Positions | Model | Layer | Positions |
| Gemma-2 2B | 18 | Gemma-3 4B | 23 | ||
| Gemma-2 9B | 28 | Gemma-3 12B | 29 | ||
| Gemma-2 27B | 25 | Gemma-3 27B | 41 | ||
| Qwen2.5 3B | 31 | Gemma-4 2B | 24 | ||
| Qwen2.5 7B | 22 | Gemma-4 4B | 29 | ||
| Model | Positional | Lexical | Reflexive | Unknown | No effect | |
|---|---|---|---|---|---|---|
| RoPE | ||||||
| Qwen2.5 72B | 46.0 | 8.4 | 16.4 | 21.9 | 7.3 | 1.85 |
| Qwen2.5 7B | 36.0 | 16.7 | 11.4 | 33.0 | 2.7 | 1.28 |
| Qwen2.5 32B | 37.9 | 12.8 | 17.0 | 29.1 | 3.2 | 1.27 |
| Llama-3.1 70B | 28.8 | 16.7 | 10.8 | 28.5 | 15.2 | 1.05 |
| Gemma-2 27B | 33.0 | 15.7 | 17.8 | 25.1 | 8.2 | 0.98 |
| NIAH | VT + Aggr. | QA | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Context | Model | S1 | S2 | S3 | MK1 | MK2 | MK3 | MV | MQ | VT | CWE | FWE | QA1 | QA2 |
| 16K | RoPE | 88.2 | 97.4 | 50.0 | 91.2 | 83.0 | 4.0 | 35.0 | 35.0 | 0.1 | 0.6 | 50.1 | 50.6 | 26.8 |
| SWA RoPE | 100.0 | 94.6 | 36.6 | 84.8 | 75.2 | 11.6 | 44.0 | 38.4 | 6.1 | 5.9 | 43.4 | 45.8 | 27.6 | |
| SWA NoPE | 100.0 | 100.0 | 48.0 | 78.0 | 84.2 | 41.6 | 80.8 | 85.7 | 0.2 | 3.5 | 48.7 | 54.8 | 32.0 | |
| 32K | RoPE | 100.0 | 99.6 | 18.8 | 92.4 | 81.2 | 0.2 | 25.2 | 30.5 | 14.4 | 1.1 | 38.7 | 33.6 | 27.8 |
| SWA RoPE | 100.0 | 99.8 | 27.2 | 81.6 | 74.0 | 20.2 | 26.4 | 37.9 | 14.9 | 9.1 | 45.0 | 30.8 | 26.8 | |
| Context | Model | Comp. key | Neither | Comp. key | Neither | Comp. key | Neither | Comp. key | Neither |
|---|---|---|---|---|---|---|---|---|---|
| 32K | RoPE | 94% | 6% | 83% | 17% | 86% | 14% | 76% | 24% |
| SWA RoPE | 43% | 57% | 39% | 61% | 52% | 48% | 53% | 47% | |
| SWA NoPE | 94% | 6% | 86% | 14% | 82% | 18% | 83% | 17% | |