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

One Step of Gradient Descent is Provably the Optimal In-Context Learner with One Layer of Linear Self-Attention

Arvind V. Mahankali, Tatsunori Hashimoto, Tengyu Ma

Introduction

Large Language Models (LLMs) possess the remarkable ability to perform In-Context Learning (ICL)—solving new tasks simply by observing a few examples in a prompt, without requiring any weight updates. Recent empirical studies have suggested that when small transformers are trained on simple linear regression problems, they implicitly "learn" to implement standard optimization algorithms like Ordinary Least Squares (OLS) or Gradient Descent (GD).

Specifically, previous work (e.g., von Oswald et al., 2022) observed that a one-layer transformer with linear self-attention behaves empirically like a single step of Gradient Descent. However, the theoretical foundation for this phenomenon remained a mystery. Is one step of GD truly the mathematically optimal operation for this specific architecture, or is it merely a byproduct of the training dynamics? This paper rigorously proves that under specific conditions, the global minimizer of the pre-training loss for such a transformer is exactly one step of gradient descent.

Theoretical Framework and Methodology

To isolate the behavior of the attention mechanism, the authors analyze a simplified transformer architecture:

  • Single Layer: The model contains only one self-attention layer.
  • Linear Self-Attention: The softmax function is removed from the attention mechanism, rendering the operations purely linear.
  • No MLP: The Feed-Forward Network (MLP) layer is excluded.

The researchers treat the transformer's weights as variables and mathematically search for the global minimizer of the pre-training loss. The training task is synthetic linear regression: given a sequence of context pairs $(x_i, y_i)$ and a query $x_{n+1}$, the model must predict $y_{n+1}$. By relying entirely on synthetically generated data, the authors maintain precise control over the underlying distributions, testing isotropic (standard Gaussian) covariates, non-isotropic (skewed) covariates, and nonlinear target functions.

Key Findings: The Mathematics of In-Context Learning

Architecture as an Algorithmic Constraint

The mathematical heavy lifting of the paper demonstrates that the gradients of the transformer's loss function align perfectly with the structure of a gradient descent update. The reason the transformer implements one step of GD rather than a more complex algorithm (like Ridge Regression or OLS) is fundamentally architectural. A single layer of linear self-attention lacks the "expressive budget" required to compute a matrix inverse. Consequently, one step of GD serves as the mathematically optimal "best effort" within the model's structural constraints.

Theorem 1: The Core Proof

For standard Gaussian (isotropic) inputs, the authors prove that the weights minimizing the transformer's loss result in a model that outputs $\hat{y} = \eta \sum (y_i x_i^T) x_{query}$. This formula perfectly mirrors a single step of Gradient Descent starting from an initialization of zero. Furthermore, the paper derives a specific, closed-form expression for the optimal learning rate ($\eta$) that the transformer learns, which is dictated by the noise level and the dimensionality of the data.

Theorem 2: Adaptation to Skewed Data

When the input data distribution is altered to be non-isotropic (skewed by a non-identity covariance matrix $\Sigma$), the optimal transformer adapts its algorithm. Rather than performing vanilla GD, it implements pre-conditioned Gradient Descent. The model automatically utilizes its learned weights to "un-skew" the data using the inverse covariance matrix ($\Sigma^{-1}$), effectively executing a more sophisticated optimization step.

Robustness to Nonlinearity

A surprising insight emerges when the relationship between $x$ and $y$ is made nonlinear (e.g., labels generated by complex functions like deep neural networks). Even when trained on this complex data, the one-layer transformer still implements one step of linear gradient descent. Because the architecture is strictly bounded by linear attention, it cannot capture the nonlinearities; instead, it defaults to finding the best linear approximation of the gradient.

Empirical Validation

To bridge theory and practice, the authors trained actual one-layer transformers and compared their behavior against the theoretical predictions. The empirical results matched the mathematical proofs with high precision. As the scaling of the input covariates increased, the transformer's error relative to a strict "GD Baseline" remained remarkably low, even as its error relative to the "Ground Truth" labels grew. This divergence confirms that the model was faithfully executing the GD algorithm rather than magically deducing the underlying ground truth.

Limitations and Future Directions

While this paper provides a rigorous foundation for understanding ICL in simplified models, it acknowledges several limitations:

  • Architectural Simplicity: Real-world LLMs feature dozens of layers and utilize Softmax attention. While the linear, single-layer simplification is necessary for mathematical tractability, it does not capture the full complexity of production models. It remains to be proven how multiple layers might compose to implement multiple steps of GD or advanced algorithms like Newton's Method.
  • Synthetic Data: The proofs rely heavily on Gaussian distributions and specific symmetries. Natural language data is highly structured and non-random, meaning these findings represent a theoretical baseline rather than a complete explanation of LLM behavior in the wild.

Ultimately, this work demystifies the "first step" of how transformers "think" in-context. Extending this rigorous mathematical analysis to multi-head attention and multi-layer architectures stands as the next critical frontier in the theory of In-Context Learning.

Summary updated

BibTeX
@inproceedings{mahankali2024one,
  author = {Arvind V. Mahankali and Tatsunori Hashimoto and Tengyu Ma},
  title = {{One Step of Gradient Descent is Provably the Optimal In-Context Learner with One Layer of Linear Self-Attention}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=8p3fu56lKc},
}
Figure