Abstract
Autoregressively trained transformers have brought a profound revolution to the world, especially with their in-context learning (ICL) ability to address downstream tasks. Recently, several studies suggest that transformers learn a mesa-optimizer during autoregressive (AR) pretraining to implement ICL. Namely, the forward pass of the trained transformer is equivalent to optimizing an inner objective function in-context. However, whether the practical non-convex training dynamics will converge to the ideal mesa-optimizer is still unclear. Towards filling this gap, we investigate the non-convex dynamics of a one-layer linear causal self-attention model autoregressively trained by gradient flow, where the sequences are generated by an AR process $x_{t+1} = W x_t$. First, under a certain condition of data distribution, we prove that an autoregressively trained transformer learns $W$ by implementing one step of gradient descent to minimize an ordinary least squares (OLS) problem in-context. It then applies the learned $\widehat{W}$ for next-token prediction, thereby verifying the mesa-optimization hypothesis. Next, under the same data conditions, we explore the capability limitations of the obtained mesa-optimizer. We show that a stronger assumption related to the moments of data is the sufficient and necessary condition that the learned mesa-optimizer recovers the distribution. Besides, we conduct exploratory analyses beyond the first data condition and prove that generally, the trained transformer will not perform vanilla gradient descent for the OLS problem. Finally, our simulation results verify the theoretical results.
Introduction
Transformers exhibit a near-magical ability known as In-Context Learning (ICL): they can adapt to new tasks provided in a prompt without any explicit parameter updates. A leading theoretical explanation for this phenomenon is the mesa-optimization hypothesis, which suggests that during autoregressive (AR) pretraining, the transformer learns to implement an internal optimization algorithm (such as gradient descent) within its forward pass.
While previous research has shown that transformers could represent such optimizers, a significant gap remained: does the actual non-convex training process—starting from random weights and using standard gradient-based methods—actually lead to these solutions? This paper addresses this gap by analyzing the training dynamics of a one-layer linear causal self-attention model.
Theoretical Framework
We analyze a mathematically tractable model: a one-layer linear causal self-attention transformer. The model is trained on sequences generated by a first-order AR process, $x_{t+1} = Wx_t$, where $W$ is a transition matrix. The training process uses gradient flow (gradient descent with an infinitesimal step size) to minimize the prediction loss. Unlike prior studies that often assume specific structures for the model's weight matrices, this work analyzes the joint evolution of the Query, Key, Value, and Projection matrices from random initialization.
The Emergence of Mesa-Optimization
Our primary result, Theorem 4.1, rigorously proves that under specific data conditions (Assumption 4.1), the transformer's weights converge to a state where the forward pass is mathematically equivalent to performing one step of gradient descent to solve an Ordinary Least Squares (OLS) problem in-context. The model learns to estimate the transition matrix $W$ by optimizing an internal objective and then uses this estimate for next-token prediction. This confirms that mesa-optimization is a natural outcome of the AR training process.
Capability Limitations and the Role of Sparsity
Despite the emergence of an optimizer, we uncover a striking "capability limitation." When the training data follows a standard Gaussian distribution, the learned mesa-optimizer fails to perfectly recover the true signal. Specifically, Proposition 4.1 shows that the prediction ratio $E[\hat{y}/y]$ converges to $1/5$, meaning the model only recovers a fraction of the underlying data distribution.
Perfect recovery is only achieved when the data is highly sparse (e.g., one-hot vectors). Theorem 4.2 establishes that sparsity is both a necessary and sufficient condition for the transformer to perfectly learn the AR process. This suggests that the "algorithm" learned by simple one-layer transformers is highly sensitive to the statistical properties of the training data.
Key Results and Simulation
The theoretical predictions regarding weight convergence are verified through extensive simulations. We observe the product of the weights ($ab$) converging to specific constants predicted by our theory, which determine the effectiveness of the learned mesa-optimizer.
For Gaussian data, this constant leads to the $1/5$ failure mode, while for sparse data, it aligns with perfect recovery. Furthermore, we prove that without specific structural constraints, the transformer generally implements a "preconditioned" version of gradient descent rather than the vanilla version often assumed in simpler theories.
Conclusion
This work bridges the gap between the representational capacity of transformers and their actual training dynamics. While we confirm the emergence of mesa-optimizers in autoregressively trained models, we also highlight significant limitations in their ability to generalize across different data distributions. Future research will explore how multi-layer architectures and non-linear attention mechanisms might overcome these capability bottlenecks.
Summary updated