Abstract
Recent research has established a mathematical equivalence between the self-attention mechanism in Transformers and the update rules of Modern Hopfield Networks (MHNs). However, this equivalence typically relies on an adiabatic approximation, assuming internal dynamics reach equilibrium instantaneously. This paper introduces Modern Hopfield Attention (MHA), a mechanism that incorporates the previously neglected hidden states of MHNs into the Transformer architecture. By propagating attention scores across layers as a running memory, MHA mitigates the pervasive problem of rank collapse in deep models. Our results demonstrate that MHA improves performance in both Vision Transformers (ViT) and Large Language Models (LLMs) without introducing additional training parameters.
Introduction
As Transformer architectures scale in depth, they encounter a fundamental limitation known as rank collapse or token uniformity. In deep networks, the representations of distinct tokens tend to converge, losing their unique expressive power. Theoretical analysis suggests that in pure attention networks, this decay in diversity occurs at a double-exponential rate.
While the link between Modern Hopfield Networks and Transformers is well-documented, standard implementations treat each attention layer as an isolated event. This paper explores the potential of the "hidden states" within the Hopfield framework. By moving beyond the adiabatic approximation, we can treat the network's depth as a dynamical process where attention patterns are refined and inherited rather than rediscovered at every layer.
Methods: Modern Hopfield Attention (MHA)
The core contribution of this work is the Modern Hopfield Attention (MHA) layer. Unlike standard Transformers that calculate attention scores ($QK^T$) independently for each layer, MHA maintains a persistent hidden state ($H$) that acts as a temporal memory across the network's depth.
The Mechanism
- State Accumulation: MHA maintains an exponential moving average of the attention scores. Instead of relying solely on the current layer's Query-Key product, the model integrates historical attention patterns.
- Information Propagation: The hidden state $H$ is passed sequentially from one layer to the next. The final attention weights for any given layer are derived from the softmax of this accumulated state.
- Parameter Efficiency: MHA is a structural modification of how existing tensors are combined. It requires no additional weight matrices ($W_Q, W_K, W_V$), making it a "drop-in" replacement for standard self-attention.
Theoretical Justification
The authors utilize a rigorous mathematical framework to show that the standard Transformer is merely one specific discretization of underlying Hopfield dynamics. By applying a different discretization scheme—specifically using forward derivatives for visible states and backward derivatives for hidden states—the architecture naturally evolves into the MHA structure, which is theoretically more robust against representation decay.
Experimental Results
The efficacy of MHA was evaluated across diverse benchmarks in natural language processing and computer vision.
Language Modeling
In experiments with GPT-2 Small on the WikiText-103 dataset, MHA reduced perplexity from 22.87 to 20.70. Similar performance gains were observed when integrating MHA into LLaMA-based architectures, suggesting that the benefits of hidden state propagation scale across different transformer variants.
Computer Vision
For Vision Transformers (ViT-Base) trained on ImageNet-1k, MHA improved top-1 accuracy from 76.07% to 77.06%. The model also showed superior performance in transfer learning tasks across datasets such as Oxford Flowers 102 and Stanford Cars.
Mitigating Rank Collapse
The most significant insight involves the preservation of token diversity. In standard GPT-2 models, cosine similarity between tokens often approaches 1.0 in deeper layers, indicating total rank collapse. MHA-equipped models maintain significantly lower similarity scores, preserving the "rank" of the feature matrix even in very deep configurations. In "skip-free" networks—which typically fail beyond a few layers—MHA allowed models to maintain functional accuracy where standard models collapsed to chance-level performance.
Discussion and Limitations
MHA represents a significant step in bridging neuroscience-inspired associative memory with scalable deep learning. By allowing the network to "remember" attention patterns across depth, we achieve more stable and expressive representations.
However, several challenges remain:
- Scale: Current experiments were conducted at academic scales (up to GPT-2 Medium). Further validation on multi-billion parameter models is required.
- Hyperparameters: MHA introduces two new hyperparameters ($\alpha$ and $\alpha'$) to control the moving averages of states. While default values of 0.5 proved robust, optimal tuning remains task-dependent.
- Theoretical Symmetry: Practical Transformers often use asymmetric weights, while Hopfield energy functions typically assume symmetry. Reconciling these two realities remains an open theoretical challenge.
Conclusion
Modern Hopfield Attention offers a computationally efficient, parameter-free method to improve Transformer performance. By leveraging the hidden states of Modern Hopfield Networks, we can prevent rank collapse and allow deep models to maintain high-fidelity representations across their entire depth.
Summary updated