One-Layer Transformers are Provably Optimal for In-context Reasoning and Distributional Association Learning in Next-Token Prediction Tasks

Quan Nguyen, Thanh Nguyen-Tang · DOI

Abstract

Large Language Models (LLMs) demonstrate a remarkable ability to prioritize in-context information over global statistics, a phenomenon known as in-context learning. This paper provides a theoretical analysis of one-layer decoder-only transformers, proving they are Bayes-optimal for in-context recall tasks. We demonstrate that these models can converge at a linear rate to optimal solutions using normalized gradient descent. Crucially, we show that with appropriate inductive biases, transformers can generalize to out-of-distribution (OOD) data, such as tokens never encountered as targets during training.

Introduction

The success of LLMs hinges on their ability to balance global statistical patterns with specific in-context reasoning. For instance, in a next-token prediction task, a model must decide whether to follow a common global pattern or a specific association established earlier in the prompt. While this behavior is observed empirically, the underlying theoretical mechanisms—including training dynamics and generalization capabilities—have remained unclear. This research addresses these questions by analyzing the full training trajectory and the ability of transformers to handle "Out-of-Distribution" (OOD) samples, specifically predicting tokens that were never used as targets during the training phase.

Methods

We analyze a one-layer decoder-only transformer employing three attention mechanisms: linear, ReLU, and softmax. To facilitate a rigorous mathematical analysis, we utilize an associative memory parameterization that structures weight matrices ($W, V, F$) to mimic trigger-based recall. We study the dynamics of Normalized Gradient Descent (NGD), proving a linear convergence rate to the Bayes risk. For softmax attention, we employ a two-phase analysis covering an initial stabilization of attention scores followed by exponential loss decay.

The models are evaluated on a synthetic In-context Recall Task (ICR). In this task, sentences contain "trigger" tokens followed by "output" tokens. The model's objective is to predict the correct output token based on the most recent trigger. We test both noiseless and noisy environments (where a generic noise token appears with probability $\alpha$). To test OOD generalization, we introduce "neutral" tokens during testing that were never seen in an output position during training, determining if the model learned the underlying logic of recall rather than simple memorization.

Key Results

Our theoretical and empirical findings confirm that one-layer transformers are provably Bayes optimal, achieving the lowest possible theoretical error for ICR tasks. A significant insight is the functional specialization within the architecture: attention heads manage in-context reasoning, while feed-forward layers account for global statistics and noise.

We find that inductive bias is paramount for generalization. While standard transformers can represent the optimal solution, gradient descent often fails to find it without specific parameterization. As shown in the comparison below, "reparameterized" models consistently achieve 0-loss in noiseless settings and Bayes-loss in noisy settings, while also generalizing to unseen output tokens ($y$). In contrast, "original" models often fail to generalize OOD, preferring to memorize the training distribution.

Comparison of model performance and generalization capabilities across noiseless and noisy settings.

Discussion and Future Work

This work establishes a foundational "unit" of understanding for transformer dynamics. We prove that for this class of problems, the loss decreases at a linear rate $O(N \exp(-\eta t))$. However, limitations remain: the analysis is restricted to one-layer models and synthetic data. Real-world language involves deeper architectures and complex hierarchical dependencies. Future research will explore how standard, non-parameterized transformers can be encouraged to achieve similar OOD generalization through architectural tweaks or novel training techniques.

Summary updated

BibTeX
@misc{nguyen2025one,
  author = {Quan Nguyen and Thanh Nguyen-Tang},
  title = {{One-Layer Transformers are Provably Optimal for In-context Reasoning and Distributional Association Learning in Next-Token Prediction Tasks}},
  year = {2025},
  eprint = {2505.15009},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2505.15009},
}
Figure