Can Looped Transformers Learn to Implement Multi-step Gradient Descent for In-context Learning?

Khashayar Gatmiry, Nikunj Saunshi, Sashank J. Reddi, Stefanie Jegelka, Sanjiv Kumar

Introduction

Transformers have demonstrated a remarkable capacity for In-Context Learning (ICL), the ability to solve novel tasks provided within a prompt without explicit parameter updates. A prominent hypothesis suggests that the Transformer's forward pass implicitly executes optimization algorithms, such as gradient descent, on the provided in-context data. While prior research has established the expressivity of Transformers—proving they are capable of representing such algorithms—the question of learnability remained open. Specifically, it was unclear whether the non-convex optimization landscape of a Transformer would allow standard training procedures to converge to these algorithmic solutions.

This paper provides the first theoretical evidence that looped Transformers—architectures where a single layer is applied iteratively—not only have the capacity to simulate multi-step gradient descent but actually learn to do so during training. By analyzing the optimization landscape of linear looped Transformers for in-context regression, the authors demonstrate that gradient flow converges to a global minimizer implementing a data-adaptive iterative solver.

Methodology

The study focuses on Linear Looped Transformers. Unlike standard architectures where each layer possesses unique weights, a looped model reuses the same parameters $(P, Q)$ across $L$ iterations. This weight-sharing serves as a strong inductive bias, encouraging the model to learn a fixed-point iterative algorithm.

Task Setup

The authors evaluate the model on in-context linear regression. Given a prompt consisting of $n$ pairs of $(x_i, y_i)$ and a query $x_q$, the model must predict the corresponding $y_q$. The data is generated synthetically using Gaussian distributions:

  • Inputs $x$ are sampled from $N(0, \Sigma^*)$.
  • Ground-truth weights $w^*$ are sampled from $N(0, (\Sigma^*)^{-1})$.
  • Labels are generated as $y = w^{*T}x$.

Theoretical Analysis

To understand the learning process, the authors analyze the population loss landscape, representing the average loss over the data distribution. They address the inherent non-convexity of the looped architecture by deriving a gradient dominance condition. This mathematical property ensures that as long as the loss is non-zero, the gradient remains sufficiently large to guide the optimization toward a global minimum, effectively bypassing potential local minima or plateaus.

Key Insights: Preconditioned Gradient Descent

The theoretical analysis reveals that the learned weights do not merely implement "vanilla" gradient descent. Instead, the model learns to precondition the gradient.

In standard gradient descent, the update step is often slowed by the condition number of the data covariance. The looped Transformer learns a preconditioner matrix that approximates the inverse of the population covariance $(\Sigma^*)^{-1}$. This adaptation allows the model to perform more efficient updates, effectively whitening the data internally to accelerate convergence toward the task solution.

Results and Generalization

The authors prove several critical properties of the trained model:

  1. Global Optimality: Gradient flow is guaranteed to converge to a global minimizer that implements $L$ steps of preconditioned gradient descent.
  2. Convergence Rate: The training process converges at a rigorous rate, provided the number of in-context samples $n$ is sufficiently large relative to the dimensionality.
  3. Algorithmic Generalization: Because the model learns a general procedure rather than a static mapping, it generalizes to new data distributions.
  4. Test-Time Unrolling: A significant finding is that a model trained with a fixed number of loops (e.g., $L=5$) can be "unrolled" for additional iterations at test time (e.g., $L=20$), resulting in increased prediction accuracy. This confirms that the model has learned a stable iterative algorithm.

Limitations and Future Directions

While this work marks a significant milestone in understanding ICL, several constraints remain:

  • Linearity: The proof is currently limited to linear self-attention and linear regression. The inclusion of non-linearities like Softmax or MLPs introduces significant complexity to the loss landscape.
  • Gaussian Assumptions: The results rely on Gaussian data distributions; extending the theory to natural, non-Gaussian data is a necessary next step.
  • Fixed Step Sizes: The current architecture assumes a specific looping structure. Future research could explore how Transformers might learn to adaptively adjust step sizes or internal parameters across iterations.

Conclusion

This research bridges the gap between the expressivity and learnability of algorithms within Transformers. By proving that looped models naturally converge to iterative solvers like preconditioned gradient descent, the authors provide a theoretical foundation for the "Transformer-as-optimizer" perspective, offering a clearer picture of how these models solve complex tasks in-context.

Summary updated

BibTeX
@inproceedings{gatmiry2024can,
  author = {Khashayar Gatmiry and Nikunj Saunshi and Sashank J. Reddi and Stefanie Jegelka and Sanjiv Kumar},
  title = {{Can Looped Transformers Learn to Implement Multi-step Gradient Descent for In-context Learning?}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=o8AaRKbP9K},
}
Figure