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

Transformers learn to implement preconditioned gradient descent for in-context learning

Kwangjun Ahn, Xiang Cheng, Hadi Daneshmand, Suvrit Sra

Abstract

Large Language Models (LLMs) demonstrate a remarkable capacity for In-Context Learning (ICL), solving novel tasks from a few examples without weight updates. This paper provides a theoretical characterization of this phenomenon by analyzing the training dynamics of linear Transformers on linear regression tasks. We prove that the global minimizers of the training objective implement preconditioned gradient descent, where the model automatically learns to account for data covariance and noise. Our results show that multi-layer Transformers naturally converge to weights that execute iterative optimization steps, bridging the gap between architectural expressivity and learned algorithmic behavior.

Introduction

The ability of Transformers to perform In-Context Learning (ICL) has sparked significant interest in understanding whether these models act as implicit optimizers. While previous work has shown that Transformers can be constructed to simulate gradient descent (GD), it remained unclear if standard training procedures actually lead to such algorithmic solutions. This study addresses this gap by examining the loss landscape of linear Transformers trained on synthetic linear regression tasks, seeking to identify the specific algorithms learned at the global minima.

Methodology

The authors employ a mathematically rigorous framework to analyze the learning process:

  • Architecture: The study focuses on linear Transformers (attention without softmax), which allows for exact theoretical analysis of the recurrence in the attention mechanism.
  • Task: The models are trained on in-context linear regression, where a prompt consists of $(x, y)$ pairs followed by a query $x_{n+1}$.
  • Data Distribution: Synthetic datasets are used, with covariates $x$ drawn from Gaussian distributions with covariance $\Sigma$. This setup allows for precise control over the "conditioning" of the problem.
  • Analysis: The researchers characterize the global minimizers and critical points of the training loss to determine the functional form of the learned predictor.

Theoretical Insights

The theoretical analysis reveals that Transformers do not merely learn "vanilla" gradient descent but a more sophisticated preconditioned gradient descent algorithm.

Automatic Preconditioning

A single-layer Transformer's global minimum corresponds exactly to one step of preconditioned GD. The model learns an internal representation of the data's covariance, allowing it to "whiten" the input and accelerate convergence compared to standard GD. Furthermore, the learned preconditioning matrix accounts for "data inadequacy"—regularizing the solution when the number of examples in the prompt is small.

Multi-layer Iterative Optimization

In multi-layer architectures, each layer effectively performs one iteration of the optimization algorithm. The authors identify critical points in an $L$-layer model that correspond to $L$ steps of preconditioned GD. When constraints on parameter flexibility are relaxed, the model can even learn "curvature correction" (akin to GD++), further improving its efficiency.

Empirical Results

To validate the theory, the authors trained 3-layer linear Transformers on synthetic regression tasks. The experimental results strongly support the theoretical predictions.

Convergence of a 3-layer linear Transformer to the theoretical preconditioned gradient descent solution.

As shown in the figure above, the training loss (d) decreases steadily, while the learned weight matrices $A_0, A_1, A_2$ (a-c) converge toward the predicted preconditioning structures. Specifically, the distance between the transformed weights and the identity matrix approaches zero, indicating that the model has successfully learned to precondition the gradient steps at each layer.

Discussion and Limitations

This work provides a foundational step in understanding the algorithmic nature of ICL. However, several challenges remain:

  • Linearity: The current proofs primarily apply to linear attention. While preliminary results suggest similar behavior for ReLU-based attention, the full softmax attention used in production models like GPT-4 requires further study.
  • Task Complexity: The analysis is restricted to linear regression. Real-world ICL involves complex, non-linear reasoning that may involve more diverse algorithmic primitives.
  • Optimization Paths: While the experiments show convergence to the predicted algorithmic solutions, proving that all local minima or training trajectories lead to these points remains an open theoretical question.

Despite these limitations, the discovery that Transformers naturally learn to implement preconditioned gradient descent offers a powerful lens through which to view the "magic" of in-context learning.

Summary updated

BibTeX
@inproceedings{ahn2023transformers,
  author = {Kwangjun Ahn and Xiang Cheng and Hadi Daneshmand and Suvrit Sra},
  title = {{Transformers learn to implement preconditioned gradient descent for in-context learning}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=LziniAXEI9},
}
Figure