Linear Transformers are Versatile In-Context Learners

Max Vladymyrov, Johannes von Oswald, Mark Sandler, Rong Ge

Large Language Models exhibit a fascinating emergent property called In-Context Learning (ICL): they can learn to solve new tasks simply from examples provided in their input prompt, without any updates to their underlying weights. Recent research has attempted to reverse-engineer this phenomenon using simplified models, specifically linear transformers trained on linear regression tasks. Previous work showed that these models implicitly execute a form of gradient descent (dubbed "GD++") during their forward pass. However, it remained unknown if transformers could handle more complex, realistic optimization problems.

This paper reveals that linear transformers are capable of discovering highly sophisticated optimization algorithms during in-context learning, going well beyond basic gradient descent. By mathematically proving that each transformer layer maintains an implicit linear regression model, the authors show that these networks can adaptively estimate noise levels and outperform standard closed-form statistical baselines on complex, noisy regression tasks.

The Mixed Noise Variance Problem

To test the limits of ICL, the authors tackle the mixed noise variance problem. Instead of assuming all data has the same noise level, the model is fed sequences where the noise variance changes from prompt to prompt. To solve this optimally, the transformer cannot just blindly apply gradient descent; it must implicitly estimate the noise level of the current context and adjust its optimization strategy on the fly.

The paper relies on synthetic datasets generated from a noisy linear regression model. Each sequence contains data points $(x_i, y_i)$ where $y_i$ is generated using a ground-truth weight vector plus Gaussian noise. To test the "mixed noise" capability, the noise variance for each sequence is sampled from either a Uniform distribution or a Categorical distribution.

Experimental Setup

The authors approach the problem through a mix of theoretical proofs and empirical experiments. They mathematically deconstruct the forward pass of a linear transformer, proving that the output of every layer can be explained as a linear combination of the input with latent weight vectors.

They train simplified, decoder-only linear transformers on regression tasks, testing three variants:

  • FULL: Standard full parameter matrices.
  • DIAG: Parameter matrices restricted strictly to diagonal matrices.
  • GD++: An even more restricted diagonal variant known to mimic basic preconditioned gradient descent.

These models are compared against exact, closed-form mathematical solutions for Ridge Regression, including an Adaptive Ridge Regression (ADARR) that explicitly estimates noise variance, and a heavily tuned version (TUNEDRR).

Key Insights: How Transformers Optimize

  • Preconditioned GD with Momentum: The layer-by-layer updates in a linear transformer mathematically resemble a complex variant of preconditioned gradient descent that includes momentum-like behaviors.
  • GD++ is a Second-Order Optimizer: The previously discovered "GD++" algorithm was thought to be a simple gradient descent variant. The authors prove it actually achieves convergence rates typical of powerful second-order algorithms (like Newton's method).
  • The Power of Diagonal Matrices: Massive, dense matrices are not strictly necessary for complex ICL. Restricting the attention matrices to be purely diagonal (DIAG) still allows the model to solve the mixed noise problem perfectly. The diagonal parameters act as specific "valves" controlling the flow of information between the data and the labels.
  • Adaptive Rescaling (Implicit Early Stopping): Linear transformers solve the mixed noise problem by learning to adapt their step sizes. If the model implicitly detects high noise in the prompt, it scales down its updates—effectively inventing a form of early stopping to prevent overfitting to the noise.

Key Results

Remarkably, linear transformers (both FULL and DIAG) with 4 or more layers actually outperform the closed-form Adaptive Ridge Regression (ADARR) baseline on high-noise tasks ($\sigma_{max} \ge 4$). With 5 or more layers, the transformers match or exceed the performance of TUNEDRR, a baseline given an unfair advantage via explicit hyperparameter tuning for specific noise levels.

Performance across layers for varying maximum noise levels.

Conversely, the restricted GD++ model performs poorly on mixed noise tasks. It fails to adapt to varying noise levels, instead acting as if it is just guessing a single, constant average noise variance for all data.

Evaluation loss across the noise variance spectrum.

As seen in the variance spectrum analysis, GD++ exhibits a sharp "V-shape" loss curve, indicating it optimizes for a single variance and fails elsewhere. Meanwhile, the FULL and DIAG models adaptively flatten the curve, successfully handling the mixed noise spectrum.

Limitations and Future Work

The study is strictly limited to linear transformers (no softmax attention) and simplified decoder-only architectures without MLPs or LayerNorm. Furthermore, the analysis focuses entirely on linear regression and does not prove these exact mechanisms apply to complex, non-linear function classes or natural language.

Future work should explore whether these discovered algorithms are mathematically optimal, how they scale to larger datasets, and how these insights translate to standard, non-linear transformer architectures used in modern LLMs.

Summary updated

BibTeX
@inproceedings{vladymyrov2024linear,
  author = {Max Vladymyrov and Johannes von Oswald and Mark Sandler and Rong Ge},
  title = {{Linear Transformers are Versatile In-Context Learners}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=p1ft33Mu3J},
}
Figure