Transformers rely heavily on the softmax function to pay "sharp" attention to specific tokens, a capability that is crucial for logical reasoning and algorithmic tasks. However, as input sequences grow longer than those seen during training, this sharp attention inevitably "disperses," breaking the model's reasoning capabilities.
In the paper Softmax is not Enough (for Sharp Size Generalisation), researchers mathematically prove that this dispersion is a fundamental limitation of the softmax function. To counter this, they propose an "adaptive temperature" method that dynamically sharpens attention based on its entropy, significantly improving a model's ability to generalize to longer contexts.
The Problem: The Inevitability of Softmax Dispersion
Modern AI systems use the softmax function within their attention heads to route information. Mechanistic interpretability research has shown that models learn specific "circuits" (like induction heads) that rely on making sharp, precise decisions. To function correctly, these circuits must put almost all their attention weight on one or two critical tokens.
But what happens when a model is asked to reason over a much longer sequence than it saw during training—a challenge known as size or length generalization? The authors investigate why models fail at seemingly simple algorithmic tasks, like finding the maximum value in a list, when the list grows large.
They hypothesize that the culprit is the softmax function itself, which fundamentally struggles to maintain sharp attention as the number of input items increases. A reasoning circuit that looks perfectly sharp and robust on in-distribution data will inevitably blur and fail when exposed to longer out-of-distribution sequences.
Empirical Evidence and Theoretical Proof
The paper combines theoretical proofs with empirical observations to demonstrate that softmax dispersion is not just a training artifact or a bug; it is a mathematical certainty.
The authors mathematically prove that in any Transformer operating over a finite vocabulary, the pre-softmax values (logits) are strictly bounded. Because of these bounds, as the number of input tokens ($n$) grows, the softmax output for any single item must decay toward $1/n$. Furthermore, they prove that the only way a standard Transformer can keep softmax sharp on larger inputs is by drastically increasing the magnitude of its weight matrices. However, this encourages overfitting and is actively hindered by standard regularization practices like weight decay and Layer Normalization.
This dispersion can be measured as an increase in entropy. As the number of items grows, the attention distribution becomes more uniform, and the Shannon entropy of the attention heads rises accordingly.
The Solution: Adaptive Temperature
To mitigate this dispersion, the authors introduce a dynamic temperature scaling mechanism applied directly to the softmax function at inference time. Borrowing from statistical mechanics, they use the principle that decreasing temperature monotonically decreases entropy.
Their method, Adaptive Temperature, works by calculating the Shannon entropy of the standard softmax output:
- Entropy-Based Scaling: If the entropy is high (meaning attention is dispersed and "blurry"), the algorithm lowers the softmax temperature to force the distribution to become sharper. They fit a degree-4 polynomial to map observed entropy to an optimal temperature scale.
- Streaming Implementation: Because calculating entropy naively requires materializing the full $O(n^2)$ attention matrix, the authors derive a clever $O(n)$ memory streaming algorithm. This makes their adaptive temperature compatible with highly efficient, scalable implementations like FlashAttention.
Experimental Results
The authors tested their intervention on both synthetic and real-world algorithmic benchmarks:
- Max Retrieval Task (Synthetic): A custom toy dataset required a single-head attention model to identify the maximum value in a set. Trained on sets of $\le 16$ items, both the baseline and the adaptive temperature model achieved 98.6% accuracy in-distribution. However, on massive out-of-distribution sets (e.g., 1,024 items), the baseline's accuracy plummeted to 53.8%, while the adaptive temperature model retained a significantly higher 57.7%.
- CLRS-Text Benchmark: This challenging text-based algorithmic reasoning benchmark contains 30 classical algorithms (e.g., sorting, graph pathfinding). When the 2-billion parameter Gemma (Gemma 2B) model was fine-tuned and evaluated with adaptive temperature, it showed significant accuracy improvements across almost all 30 algorithmic tasks on larger problem sizes compared to the standard baseline.
Limitations and Future Work
The authors are highly transparent that adaptive temperature is an "ad-hoc" fix. It acts as a band-aid but does not escape the fundamental mathematical limitations of the softmax function—eventually, dispersion will still occur at extreme scales. Additionally, the polynomial used to map entropy to temperature was derived from specific context windows; applying it to massively larger windows caused slight underperformance on a few specific algorithms due to unintended distribution shifts.
For future work, the authors strongly advocate for researching hybrid architectures or alternative attention mechanisms—such as linear attention, sigmoidal attention, or selective attention—that do not rely entirely on normalized softmax, especially for building robust reasoning engines.
Summary updated