Computation Mechanism Behind LLM Position Generalization

Chi Han, Heng Ji

Abstract

Large Language Models (LLMs) exhibit a remarkable ability to handle scrambled word orders and generalize to context lengths far exceeding their training window. This paper investigates the internal computational mechanisms enabling this behavior. We reveal that LLMs learn to mathematically disentangle positional information from semantic meaning within their attention mechanisms, treating them as independent, additive components. This "disentanglement law" explains how models maintain performance under positional perturbations and extreme sequence lengths.

Introduction: The Mystery of Position Generalization

While LLMs are known to be robust to "position perturbations"—similar to the human ability to read text with transposed letters—and can be extended to handle long sequences, the internal logic has remained a "black box." The core challenge is understanding how the self-attention mechanism, which traditionally mixes position and semantics through complex rotations like Rotary Positional Encoding (RoPE), manages to keep these signals from interfering with each other.

The two primary challenges addressed: Order Transposition (robustness to shuffling) and Length Generalization (handling extreme contexts).

Methodology

The authors employ a mix of empirical decomposition and theoretical analysis to study models like Llama-3.2-7B:

  • 3-Axis Linear Approximation: Decomposing the attention logit matrix into three independent axes: token distance ($i-j$), query index ($i$), and key index ($j$).
  • Fake Distance Analysis: Replacing real token distances with controlled variables to observe semantic processing reactions to positional shifts.
  • RoPE Feature Analysis: Identifying specific "slow-rotating" dimensions that act as primary carriers of positional information, while faster-rotating dimensions handle semantic nuances.

The Disentanglement Law

Counterintuitively, LLMs learn to simplify the complex attention function into a near-linear sum:

$$w(i-j, q, k) \approx f(q, i-j) + g(q, k)$$

In essence, the "where" (position) and the "what" (semantics) are added together rather than being inextricably tangled. This disentanglement is a learned behavior; in randomly initialized models, the correlation between actual logits and this additive approximation is nearly non-existent (~0.03), whereas it reaches ~0.96 in trained models.

Conceptual diagram of the Disentanglement Law: the attention logit map is approximated as the sum of a distance-based pattern and semantic relevance.

Key Results

  • High Fidelity: The additive approximation shows a 0.959 linear correlation with actual attention logits.
  • Extreme Robustness: Shuffling up to 5% of tokens results in only a marginal 0.6% drop in accuracy on the Qasper dataset. Shuffling 1% of tokens across 1,000 positions increases perplexity by a negligible 0.02.
  • Distributional Stability: Length-generalization techniques succeed because they ensure attention output vectors stay within the "training-time distribution," as confirmed by PCA visualizations.

Conclusion and Future Work

This discovery provides a theoretical foundation for LLM robustness and suggests several practical applications, including more efficient attention calculation for low-importance regions, improved KV cache compression based on semantic importance, and the mitigation of positional biases like the "lost in the middle" effect.

Summary updated

BibTeX
@inproceedings{han2025computation,
  author = {Chi Han and Heng Ji},
  title = {{Computation Mechanism Behind LLM Position Generalization}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure