NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Mechanics of Next Token Prediction with Self-Attention

Yingcong Li, Yixiao Huang, Muhammed Emrullah Ildiz, Ankit Singh Rawat, Samet Oymak · DOI

Mechanics of Next Token Prediction with Self-Attention

Large Language Models (LLMs) have revolutionized artificial intelligence through the simple objective of next-token prediction. While empirically successful, the underlying mechanics of how self-attention optimizes for this task have remained largely a "black box." This research demystifies the learning process of a single self-attention layer, proving that gradient descent transforms the mechanism into a two-stage automaton: a "hard retrieval" phase that isolates high-priority tokens, followed by a "soft composition" phase that blends them into a final prediction.

The Two-Stage Automaton

The core finding of this study is that self-attention does not merely perform statistical guessing. Instead, it learns to execute a specific logic based on the training data's structure. As training progresses, the attention weights grow in magnitude, causing the softmax function to saturate. This leads to a functional decomposition:

  1. Hard Retrieval: The model identifies and isolates a subset of "high-priority" tokens from the input sequence, effectively ignoring lower-priority "distractor" tokens.
  2. Soft Composition: The model then performs a probabilistic blending of these high-priority candidates to generate the final output distribution.
The two-stage mechanism of self-attention: hard retrieval of high-priority tokens followed by soft composition for prediction.

Theoretical Framework: Token-Priority Graphs

To characterize this behavior, the authors introduce Token-Priority Graphs (TPGs). In a TPG, nodes represent tokens, and directed edges represent potential transitions found in the training data. The researchers demonstrate that training a self-attention layer with log-loss is mathematically equivalent to solving a specific Support Vector Machine (SVM) problem defined on this graph.

Through this lens, the "priority" of a token is shown to be contextual, conditioned on the last token in the input sequence. Gradient descent implicitly discovers the graph-theoretic structure of the data, such as Strongly Connected Components (SCCs)—groups of tokens that share equal priority because they are mutually reachable in the training sequences.

Visualization of Token-Priority Graphs ($G^{(1)}$ and $G^{(2)}$) and their corresponding next-token label mappings, illustrating how data structure dictates retrieval logic.

Key Results and Insights

  • Implicit Bias of Gradient Descent: The study proves that under standard conditions, gradient descent globally converges to the directional SVM solution. In high-dimensional simulations, the correlation between weights learned via gradient descent and the theoretical SVM solution exceeded 0.98.
  • Saturating Logic: The model successfully learns to suppress irrelevant tokens to near-zero probability. This "saturating" behavior is what allows a continuous neural network to act like a discrete logic automaton.
  • Contextual Priority: The priority level of any given token $x_i$ is determined by its relationship to the final token $x_T$ in the context window, rather than a global frequency count.

Limitations and Future Directions

While providing a rigorous foundation for understanding attention, the study focuses on a single-layer model with a linear classification head. Real-world Transformers utilize deep, multi-layered architectures where "multi-hop" reasoning occurs across layers. Furthermore, the interaction between self-attention and the Feed-Forward Network (MLP) layers—which comprise a significant portion of a Transformer's parameters—remains an open area for investigation. Future work will aim to extend this graph-theoretic framework to multi-layer architectures and more complex, non-linear data distributions.

Summary updated

BibTeX
@inproceedings{li2024mechanics,
  author = {Yingcong Li and Yixiao Huang and Muhammed Emrullah Ildiz and Ankit Singh Rawat and Samet Oymak},
  title = {{Mechanics of Next Token Prediction with Self-Attention}},
  booktitle = {International Conference on Artificial Intelligence and Statistics},
  year = {2024},
  url = {https://openreview.net/forum?id=Hf6vScHuRq},
}
Figure