Abstract
Transformer-based language models are trained on large datasets to predict the next token given an input sequence. Despite this simple training objective, they have led to revolutionary advances in natural language processing. Underlying this success is the self-attention mechanism. In this work, we ask: What does a single self-attention layer learn from next-token prediction? We show that training self-attention with gradient descent learns an automaton which generates the next token in two distinct steps: (1) Hard retrieval: Given input sequence, self-attention precisely selects the high-priority input tokens associated with the last input token. (2) Soft composition: It then creates a convex combination of the high-priority tokens from which the next token can be sampled. Under suitable conditions, we rigorously characterize these mechanics through a directed graph over tokens extracted from the training data. We prove that gradient descent implicitly discovers the strongly-connected components (SCC) of this graph and self-attention learns to retrieve the tokens that belong to the highest-priority SCC available in the context window. Our theory relies on decomposing the model weights into a directional component and a finite component that correspond to hard retrieval and soft composition steps respectively. This also formalizes a related implicit bias formula conjectured in [Tarzanagh et al. 2023]. We hope that these findings shed light on how self-attention processes sequential data and pave the path toward demystifying more complex architectures.
Introduction
While Transformers have revolutionized artificial intelligence, our theoretical understanding of why they work so well remains limited. We know they are trained to predict the next token in a sequence, but how does this simple objective lead to the sophisticated contextual representations observed in models like GPT-4? This paper addresses the "black box" of the self-attention mechanism, bridging the gap between empirical success and mathematical rigor. By asking what specific relationships in training data a self-attention layer captures, the authors reveal how the geometry of gradient descent dictates final model weights.
Methods: Token-Priority Graphs and SVMs
The researchers focus on a single-layer self-attention model with a linear classification head. To analyze it, they introduce a novel framework based on Token-Priority Graphs (TPGs).
- Graph Construction: The training data is represented as a directed graph where nodes are tokens. An edge from token $A$ to token $B$ exists if, in the training data, $A$ is the "target" (next token) and $B$ is a preceding token in the sequence. This mapping allows the model to identify which tokens are relevant to a given context.
- Graph-SVM: The study proves that training this model is equivalent to solving a specific type of Support Vector Machine (SVM) problem on this graph. The model's optimization path is implicitly biased toward discovering the Strongly Connected Components (SCCs) of the data graph.
- Weight Decomposition: The learned attention weights ($W$) decompose into two parts: a directional component ($W_{hard}$) that grows to infinity, acting as a filter for high-priority tokens, and a finite component ($W_{soft}$) that determines the weighting for final prediction.
Key Insights: The Two-Step Automaton
The central finding is that self-attention acts as a two-step automaton. It doesn't merely "look" at the entire sequence; it learns a data-driven hierarchy.
- Hard Retrieval: The model identifies and selects only the "highest priority" tokens from the input sequence. This priority is not arbitrary but is a structural property of the directed graph formed by the training data.
- Soft Composition: Once high-priority tokens are retrieved, the model creates a convex combination (a blend) of these tokens to form the final prediction.
This mechanism formalizes the conjecture that attention layers are essentially margin-maximizers. By training to predict the next token, the model pushes the score of the "correct" token as far as possible above the "incorrect" ones, mirroring the behavior of an SVM.
Key Results
The theoretical framework was validated using synthetic datasets (acyclic and general cyclic) to ensure mathematical rigor. Key findings include:
- Global Convergence: For models using cross-entropy loss, gradient descent always converges to the global directional solution of the Graph-SVM.
- Empirical Validation: In experiments, the correlation between weights learned by gradient descent and the theoretical SVM solution exceeded 0.98 after 4,000 iterations.
- Separation of Concerns: The theory successfully explains the distinct roles of "priority" (suppressing irrelevant tokens) and "composition" (blending relevant ones).
- Loss Function Sensitivity: While cross-entropy leads to global solutions, other loss functions like squared loss may cause the model to converge to "local" versions of these graphs, highlighting how optimization choices affect learning.
Limitations and Future Work
While this work provides a rigorous foundation, it is limited to a single-layer self-attention model with a linear head. Real-world Transformers utilize dozens of layers and non-linear MLP blocks, where information composition becomes significantly more complex. Additionally, the proofs assume "realizability"—that the correct next token is always present in the input sequence.
Future research will aim to extend this graphical view to multi-layer, multi-head architectures to determine if this automaton behavior scales and how it interacts with the deeper hierarchies of modern large language models.
Summary updated