Abstract
Attention-based Transformers have demonstrated strong adaptability across a wide range of tasks and have become the backbone of modern Large Language Models (LLMs). However, their underlying mechanisms remain open for further exploration. The energy-based perspective has long provided a valuable principle for understanding neural computation. In this paper, we revisit the principle of energy as a lens to understand attention-based Transformer models. We present a unified energy-based framework which is composed of three key components: the local energy $E_i$, the global energy $F$, and the employed optimization algorithms. We show that different attention forms including unnormalized linear attention, gated linear attention and standard softmax attention can be induced by choosing their corresponding recipes within this framework. Building on this framework, we propose energy-based modifications of attention structures. Inspired by classical gradient descent (GD) algorithms, we extend the original attention formulation based on standard GD to the momentum-based GD, Nesterov Accelerated Gradient (NAG), and Newton's method, each inducing a corresponding new attention structure. Our experiments provide preliminary support for the potential of the energy-based framework for designing attention mechanisms.
Introduction
While Transformers are the undisputed foundation of modern AI, our theoretical understanding of why their specific architecture works so well remains incomplete. A popular emerging theory is unrolled optimization: the idea that each layer of a neural network is actually performing one step of an iterative algorithm to minimize an implicit objective function. This paper seeks to bridge the gap by providing a unified framework that can explain existing attention mechanisms (like Linear and Softmax attention) and serve as a blueprint for engineering entirely new, more efficient attention structures.
A Unified Energy-Based Framework
The core of this research is the definition of a framework composed of three "recipe" ingredients:
- Local Energy ($E_i$): Models how individual tokens interact.
- Global Energy ($F$): Defines how these local interactions are aggregated into a system-wide objective.
- Optimization Algorithm: The rule used to update token representations to minimize the global energy.
By varying these ingredients, the authors demonstrate that standard attention mechanisms are specific instances of this optimization process. For example, standard Softmax Attention is equivalent to performing one step of 1st-order Gradient Descent (GD) on a "Helmholtz free energy" function.
Advanced Optimizers as Attention Mechanisms
The core innovation lies in swapping the "Optimization Algorithm" ingredient. By replacing standard gradient descent with more advanced algorithms, the authors derive novel attention structures:
- MomenMHA: Uses Momentum-based GD, which implicitly introduces a complex web of skip connections across layers. These connections follow a geometric series based on the distance between layers.
- NagMHA: Uses Nesterov Accelerated Gradient (NAG). This adds a "lookahead" mechanism, resulting in even denser information flow than standard momentum.
- MHA2nd1st & LightMHA2nd1st: Inspired by Newton’s Method (2nd-order optimization). These versions use a covariance matrix to "precondition" the updates, allowing tokens to adaptively adjust their movement along different dimensions of the embedding space.
Key Insights and Results
The paper formalizes the intuition that the residual connection in a Transformer acts as the "current state," while the attention output acts as the "gradient update." One of the most striking findings is that applying momentum to the energy minimization process mathematically results in a series of skip connections, as seen in Figure 1.
Experimental results on the MiniPile dataset and GLUE Benchmark provide strong support for this framework:
- Faster Convergence: In pre-training, NagMHA consistently reached lower validation loss significantly faster than the standard Softmax baseline across all model sizes (30M to 160M parameters).
- Superior Accuracy: On the GLUE benchmark, momentum-based variants generally outperformed standard Transformers. MomenMHA achieved the highest average score (74.59 vs. 74.18 for the baseline).
- The Power of Lookahead: The success of the Nesterov version suggests that "looking ahead" to a predicted future state before calculating the attention gradient is highly effective for language modeling.
Conclusion and Future Work
This energy-based perspective offers a principled way to design new Transformer architectures. While 2nd-order methods like MHA2nd1st introduce some computational overhead, their ability to precondition features shows promise for scaling to larger models. Future work may explore these mechanisms in "Loop Transformers"—architectures that reuse the same weights across multiple iterations—where advanced optimizers are a natural fit for weight-shared recurrent structures.
Summary updated