NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Scale-invariant attention

Ben Anson, Xi Wang, Laurence Aitchison

Abstract

Large Language Models (LLMs) often struggle to maintain performance when the context length at inference time exceeds what they saw during training. This paper introduces scale-invariant attention, a novel logit transformation that ensures models maintain a consistent balance between local and global information. By mathematically enforcing scale invariance, the authors achieve superior zero-shot length generalization and near-perfect long-context retrieval (Needle-in-a-Haystack) where traditional methods fail.

Introduction

The "context window" is a major frontier in LLM research. While we want models to process massive documents, training on extremely long sequences is computationally prohibitive. Consequently, models are often trained on short contexts (e.g., 4k tokens) but expected to perform on much longer ones (e.g., 128k tokens) during deployment.

The core challenge is length generalization. Standard attention mechanisms tend to "smear" or become overly diffuse as the context grows; the model essentially loses its ability to pick out the "needles" of relevant information from the "haystack" of the past. Existing solutions like Rotary Position Embeddings (RoPE) or ALiBi handle the positional aspect but don't necessarily fix the distribution of attention weights. Other tricks, like LogN scaling, help maintain sparsity but often do so at the expense of the model's ability to focus on the immediate local context.

Methods

The authors propose a new attention modification inspired by the scale-invariant statistics of natural images. They define two key properties for "healthy" long-context attention:

  1. Scale-invariant total attention: The model should allot roughly equal total attention to different "scales" of the past (e.g., the last 10 tokens, the last 100, the last 1,000, etc.).
  2. Scale-invariant attention sparsity: Attention should become increasingly sparse for more distant tokens to prevent the "smearing" effect.

To achieve this, they derive a position-dependent transformation for the attention logits ($L_t$): $$L_t = a_t \bar{L}_t + m_t$$ Where $\bar{L}_t$ are the standard query-key products, $a_t$ is a multiplicative scale, and $m_t$ is an additive bias. Unlike previous methods that apply a single global scale, these parameters change based on how far back ($t$) the token is from the current query.

Figure 6: Theoretical and empirical expected entropy for different scales, validating the mathematical foundations of the scale-invariant transformation.

Under the assumption that attention logits are roughly Gaussian (which the authors empirically verify in models like Llama and Gemma), they provide a closed-form solution for $a_t$ and $m_t$ that provably satisfies their scale-invariance criteria.

Key Insights and Ideas

The authors realized that the problem with long context isn't just about where the model looks, but the entropy of the attention distribution. If the entropy grows linearly with context length, the model's focus becomes too diluted to be useful. Scale-invariant attention is unique because it explicitly protects the "local" context (the most recent tokens) while ensuring the "global" context (distant tokens) remains accessible but sparse.

Figure 2: Comparison of entropy and attention distribution across different scaling methods for both synthetic IID Gaussian and real logits. Note how scale-invariant attention maintains a stable balance as context size increases.

A surprising finding is how well the Gaussian assumption holds for attention logits in massive production models. This suggests that the mathematical foundations of their transformation are grounded in the actual behavior of trained Transformers.

Experimental Results

The researchers evaluated their method across several scales, including FineWeb for pretraining 162M and 304M parameter models, and Llama-2 7B for continual pretraining.

  • Superior Generalization: In a "Train @ 4k / Val @ 64k" setup, scale-invariant p-RoPE achieved significantly lower validation loss than standard RoPE, ALiBi, or LogN scaling.
  • Needle-in-a-Haystack Mastery: At a 64k context length, almost all baseline methods (including RoPE+NTK and YaRN) failed completely, dropping to 0% accuracy. Scale-invariant p-RoPE maintained 96.9% accuracy, matching the performance of the much more complex LogN+p-RoPE hybrid.
  • Zero-Shot Success: The method showed the strongest "zero-shot" capability, meaning it could be applied to a model trained on short sequences and immediately improve its performance on long sequences without further training.

Limitations and Future Work

While promising, the authors primarily tested models up to 304M parameters from scratch and 7B via continual pretraining. Testing at the 70B+ scale from scratch remains a goal for future research. Additionally, experiments were capped at 64k tokens due to hardware limits; empirical verification at 1M+ tokens is still needed. Future work will also explore how scale-invariance interacts with non-dense attention mechanisms like linear attention or state-space models (e.g., Mamba).

Summary updated

BibTeX
@inproceedings{anson2025scale,
  author = {Ben Anson and Xi Wang and Laurence Aitchison},
  title = {{Scale-invariant attention}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=ONE9LYBQYS},
}
Figure