Transformers Learn In-Context by Gradient Descent
Johannes von OswaldEyvind NiklassonEttore RandazzoJoão SacramentoAlexander MordvintsevAndrey ZhmoginovMax Vladymyrov
Proves that transformer forward passes mechanistically implement gradient descent during in-context learning by establishing a direct equivalence between linear self-attention layers and gradient-based optimization steps.
Modern artificial intelligence heavily relies on Transformer architectures, largely due to their ability to adapt to new information provided in prompt sequences without modifying model parameters—a capability known as in-context learning. Despite widespread deployment, the internal mechanisms governing how Transformers achieve this adaptation have remained poorly understood. Understanding these mechanisms is critical for explaining model behavior, improving computational efficiency, and architecting better architectures. The article addresses this gap by evaluating whether in-context learning operates as an internal optimization process, demonstrating that Transformers implicitly perform gradient descent within their forward pass.
To evaluate this relationship, the article introduces a mathematical construction proving that a single linear self-attention layer can execute a step of standard gradient descent on a regression loss. The authors then test this empirically by training various Transformer models—ranging from single linear layers to deep, multi-layer networks with multi-layer perceptrons—on linear and non-linear regression tasks across thousands of synthetic datasets. They measure cosine similarity, prediction errors, parameter interpolation, and generalization performance against explicit gradient descent baselines under both standard and out-of-distribution conditions.
The investigation yields several key findings. First, trained single-layer linear self-attention models converge directly to the theoretical gradient descent weight construction, matching the predictions and behavior of one-step gradient descent on both normal and out-of-distribution validation data. Second, deeper multi-layer Transformers consistently outperform standard gradient descent; they discover an accelerated optimization algorithm, termed GD++, which applies an iterative data transformation that reduces the condition number of the data covariance matrix to speed up learning. Third, incorporating multi-layer perceptrons allows Transformers to solve non-linear regression tasks by performing gradient descent on deep data representations, behaving equivalently to kernelized regression. Fourth, when input and target values are provided as separate sequence tokens, initial softmax self-attention layers learn a copying mechanism that formats tokens into paired representations, enabling subsequent layers to execute gradient-based updates.
These findings establish that training a Transformer across tasks operates as meta-learning, instantiating an internal optimization algorithm inside the network's forward computations. This mechanistic understanding demystifies prompt-based learning and shows that standard architectural components, such as multi-layer perceptrons and attention heads, serve complementary roles in feature extraction, data organization, and optimization. Furthermore, the analysis reveals that standard features like softmax attention and layer normalization can slightly degrade linear regression precision relative to linear attention, though two-head configurations mitigate these discrepancies.
Based on these results, researchers and engineers should explore targeted architectural refinements, such as embedding declarative optimization nodes into self-attention blocks to reduce layer depth requirements. Organizations should also evaluate linear attention mechanisms for tasks requiring fast internal linear adaptation. Further research is necessary before deploying these concepts to large production systems, particularly to evaluate whether the gradient descent framing scales to massive language models, complex token structures, noisy training distributions, and classification objectives.
Confidence in these findings is high for the evaluated domain of regression tasks and controlled small-scale models. However, readers should note the boundary conditions: the empirical validation focuses on synthetic linear and non-linear regression, and full-scale autoregressive language models involve higher distributional complexity that may rely on additional emergent mechanisms.
- Paper: MetaICL: Learning to Learn In Context, Sewon Min et al. (2022). MetaICL establishes the meta-learning foundation for framing in-context learning as an optimization and task-adaptation process across prompt demonstrations.
- Paper: Exact solutions to the nonlinear dynamics of learning in deep linear neural networks, Andrew M. Saxe et al. (2014). Provides foundational analytical derivations for how linear network weights evolve under gradient descent, directly informing mathematical constructions of internal optimization dynamics.
- Paper: In-context Convergence of Transformers, Yu Huang et al. (2024). Extends the study of linear regression in-context learning by providing rigorous finite-time convergence guarantees and training dynamics for standard softmax attention layers.
- Paper: How Transformers Learn Causal Structure with Gradient Descent, Eshaan Nichani et al. (2024). Generalizes in-context optimization and induction-head mechanics to show how gradient descent training leads transformers to recover latent causal graph structures.
- Paper: Test-time regression: a unifying framework for designing sequence models with associative memory, Ke Alexander Wang et al. (2025). Builds upon the equivalence between attention layers and gradient-based regression by formulating a unifying test-time regression framework across modern sequence models.
- Paper: Test-Time Training with KV Binding Is Secretly Linear Attention, Junchen Liu et al. (2026). Critically re-examines the dual perspective between test-time gradient optimization and attention by demonstrating the mathematical equivalence between test-time training and linear attention.
- Paper: Nested Learning: The Illusion of Deep Learning Architectures, Ali Behrouz et al. (2025). Expands the mesa-optimization view of attention into a broader theoretical paradigm that formalizes deep neural architectures as multi-level nested optimization problems.
- Paper: Auto-Regressive Next-Token Predictors are Universal Learners, Eran Malach (2024). Investigates the theoretical limits of autoregressive next-token optimization and in-context computation, showing how sequential step-by-step prediction serves as a universal learner.
