Linear Transformers are Versatile In-Context Learners

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

Abstract

Recent research has demonstrated that transformers, particularly linear attention models, implicitly execute gradient-descent-like algorithms on data provided in-context during their forward inference step. However, their capability in handling more complex problems remains unexplored. In this paper, we prove that any linear transformer maintains an implicit linear model and can be interpreted as performing a variant of preconditioned gradient descent. We also investigate the use of linear transformers in a challenging scenario where the training data is corrupted with different levels of noise. Remarkably, we demonstrate that for this problem linear transformers discover an intricate and highly effective optimization algorithm, surpassing or matching in performance many reasonable baselines. We reverse-engineer this algorithm and show that it is a novel approach incorporating momentum and adaptive rescaling based on noise levels. Our findings show that even linear transformers possess the surprising ability to discover sophisticated optimization strategies.

Introduction

In-Context Learning (ICL) is the mechanism that allows Large Language Models to solve new tasks using only a few examples provided in the prompt, without any parameter updates. While empirically successful, the underlying mechanism remains a subject of intense study. Recent theories propose that transformers might be performing "mesa-optimization"—executing an internal optimization algorithm during their forward pass. This paper investigates whether linear transformers can handle complex, noisy regression problems where noise levels vary between examples, and reverse-engineers the "algorithms" they discover.

Methodology

The researchers focused on Linear Transformers, stripping away non-essential components like MLPs and LayerNorm to isolate the attention mechanism's behavior. They analyzed two variants: a "FULL" model with complete parameter matrices and a "DIAG" model restricted to diagonal matrices. These models were trained on synthetic noisy linear regression tasks where the noise variance ($\sigma$) was either drawn from a uniform distribution or a discrete categorical set. By reparameterizing the diagonal model, the authors could map the transformer's internal state updates to recognizable optimization steps.

The Transformer as a Mesa-Optimizer

The study reveals that linear transformers do not just perform simple gradient descent; they implement a more sophisticated second-order optimization algorithm.

  • The Newton Connection: Each layer maintains an internal weight vector, and the updates are mathematically equivalent to a variant of preconditioned gradient descent, similar to Newton's method. This explains the rapid convergence observed compared to standard first-order methods.
  • Adaptive Rescaling and Ridge Regression: In noisy environments, the model utilizes specific weights to perform "adaptive rescaling." It effectively implements Ridge Regression internally, shrinking its predictions when it detects high noise levels.
  • Discovery of Momentum: The internal updates naturally incorporate momentum-like terms, allowing the model to smooth its optimization path by leveraging information from previous gradients.

Key Results and Performance

The experimental results demonstrate the remarkable efficiency of these discovered algorithms. Linear transformers (both FULL and DIAG) significantly outperformed the standard GD++ baseline on mixed-noise tasks.

Linear transformers (Diag and Full) outperform standard gradient descent (GD++) and match or exceed sophisticated baselines like Adaptive Ridge Regression (AdaRR) as the number of layers increases.

As shown in Figure 1, with four or more layers, the transformers actually outperformed Adaptive Ridge Regression (AdaRR), a standard statistical baseline that uses a closed-form solution to estimate noise. Remarkably, the transformers' performance matched or even exceeded a "Tuned" Ridge Regression baseline that had its hyperparameters manually optimized for the specific noise distribution.

Loss curves across different noise variances $\sigma$ show that linear transformers adapt their internal optimization strategy to the specific noise level of the input sequence.

Figure 2 illustrates the adaptive nature of the transformer. Unlike static baselines, the transformer's loss curve remains low across a wide range of noise variances, indicating that it successfully identifies and adjusts to the noise level in-context. This suggests that the "algorithm discovery" is driven by the transformer's ability to structure information flow, as even the simpler DIAG model performs competitively.

Conclusion

This work demonstrates that linear transformers are capable of discovering and implementing complex optimization strategies, including momentum and adaptive rescaling, to solve noisy in-context tasks. While the study is restricted to linear settings, it provides a blueprint for understanding how more complex LLMs might function as internal optimizers. These findings suggest a future where transformers could be used for automated algorithm discovery, potentially finding more efficient optimization paths than those manually engineered by humans.

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