Abstract
While Transformers have demonstrated remarkable success in In-Context Learning (ICL), the underlying mechanisms by which they adapt to new tasks during inference remain a subject of intense theoretical study. This paper investigates how Transformers learn to predict the next token in sequences governed by first-order autoregressive (AR) processes ($s_{t+1} = Ws_t$). We characterize the algorithms implemented by one-layer linear Transformers, demonstrating that they perform ICL either by simulating gradient descent in the forward pass or by utilizing positional encodings to perform geometric reconstructions of the transition matrix.
Introduction
In-Context Learning (ICL) allows Large Language Models (LLMs) to solve novel tasks provided in a prompt without any parameter updates. While previous work has shown that Transformers can perform linear regression in-context, most LLMs are fundamentally autoregressive—they predict the next token based on the sequence of previous ones. This study bridges the gap by exploring how Transformers handle sequences where the underlying rule—a context matrix $W$—varies between sequences. By analyzing the global minima of the training loss for one-layer linear Transformers, we uncover the specific mathematical strategies these models employ to "learn" the transition matrix $W$ on the fly.
The Autoregressive Learning Task
The authors focus on a first-order AR process where each sequence is generated by a unique matrix $W$. The model's task is to predict $s_{t+1}$ given the history $s_1, \dots, s_t$. Two primary data representations are analyzed:
- Augmented Tokens: Each token $e_t$ is formatted as $(0, s_t, s_{t-1})$, providing the model with explicit information about the previous state and the current state simultaneously. This format provides the model with explicit "error" information.
- Non-Augmented Tokens: Tokens consist solely of the states $e_t = s_t$, forcing the model to rely on its internal structure and Positional Encodings (PE) to infer the temporal relationship between states.
Mechanisms of In-Context Learning
The research identifies two distinct algorithmic approaches depending on the tokenization scheme used during training.
1. The Forward Pass as an Optimizer
In the augmented setting, the Transformer effectively implements an optimization algorithm. The authors prove that a single layer can represent one step of gradient descent on an internal objective function designed to estimate $W$. By processing the error information embedded in the augmented tokens, the model's weights $(A, B, P)$ act as operators that refine the estimate of the transition matrix during the forward pass.
2. The Geometric Interpretation and Positional Encodings
In the non-augmented setting, where explicit error information is missing, the model utilizes Positional Encodings as geometric tools. For sequences involving rotations (where $W$ is an orthogonal matrix), the model learns to implement trigonometric identities. For instance, it can utilize the relationship $2 \cos \theta R_\theta - I_2 = R_{2\theta}$ to predict subsequent states. This suggests that Positional Encodings are not merely "tags" for sequence order but are integral to the mathematical transformations the model performs.
Multi-Head Dynamics and Orthogonality
When extending the analysis to multi-head attention, a "divide and conquer" strategy emerges. The authors found that different attention heads learn to be mutually orthogonal. Each head specializes in attending to specific coordinates or components of the transformation $W$. This specialization allows the model to reconstruct complex, high-dimensional matrices by aggregating the partial results from individual heads, effectively parallelizing the task of reconstructing the context matrix.
Empirical Validation: From Synthetic Data to Natural Language
To ensure the AR process is a valid proxy for natural language, the authors tested their assumptions on the text of Moby Dick. By converting the text into embeddings using a pre-trained GPT-2 model, they demonstrated that an AR process fits real language sequences significantly better than shuffled text. This suggests that the "next-token-as-linear-transform" assumption captures a meaningful aspect of how LLMs process language.
Furthermore, while a single layer can implement basic gradient descent, experiments with deeper models (incorporating softmax and MLPs) show superior performance. This indicates that depth allows Transformers to implement more sophisticated, higher-order optimization algorithms than those achievable by a single linear layer. Visualizations of trained models confirm these theories, showing smooth, periodic patterns in learned positional encodings and the predicted orthogonality in attention heads.
Conclusion and Limitations
This work provides a rigorous theoretical foundation for understanding ICL in autoregressive contexts, showing that Transformers can act as forward-pass optimizers or geometric calculators. However, several challenges remain. The current theory relies heavily on linear attention and assumes commutativity among context matrices. Additionally, while first-order AR processes are a useful proxy, real-world language involves higher-order dependencies. Future research is needed to extend these proofs to non-commuting matrices and standard softmax attention mechanisms.
Summary updated