Transformers are Universal In-context Learners

Takashi Furuya, Maarten V. de Hoop, Gabriel Peyré

Transformers have revolutionized machine learning, largely due to their remarkable ability to perform "in-context learning" (ICL)—solving novel tasks simply by conditioning on a few examples provided in a prompt. Despite their empirical success, the theoretical underpinnings of why Transformers excel at ICL have remained elusive.

While classical universal approximation theorems establish that standard multi-layer perceptrons (MLPs) can approximate any continuous function, Transformers operate differently. They do not merely map an input $x$ to an output $y$; they map a token $x$ to an output $y$ conditioned on a set of other tokens (the context). Previous theoretical attempts to prove Transformer universality often relied on unrealistic assumptions, such as requiring the internal embedding dimension to grow as the number of context tokens or the desired precision increased.

This paper provides a rigorous mathematical proof that deep Transformers are indeed universal approximators for in-context mappings. Crucially, it demonstrates that a single Transformer with a fixed embedding dimension can approximate any continuous context-dependent function to arbitrary precision, regardless of whether it processes ten tokens, ten thousand, or an infinite distribution.

A New Mathematical Framework: Measure Theory

To achieve this, the researchers shift the analytical perspective from discrete matrices to measure theory. Instead of viewing a context as a finite list of $n$ tokens, they treat it as a probability distribution (a measure) of tokens. This "mean-field" approach provides a uniform mathematical framework capable of handling any number of tokens.

Key technical components of their proof include:

  • Wasserstein Distance: Used to define "smoothness" and continuity. If two contexts are close in Wasserstein distance, the Transformer's output remains similar.
  • Space-Time Lifting: To address masked (causal) attention used in autoregressive models like GPTs, the authors "lift" the tokens into a space-time representation, maintaining mathematical consistency where tokens can only attend to previous states.
  • Stone-Weierstrass Theorem: The core of the proof relies on showing that the "algebra" of functions generated by Transformer layers is "dense" in the space of continuous functions. This implies that adding more layers allows the network to converge on any target function.

Key Theoretical Insights and Results

The paper establishes several foundational results for both unmasked and masked architectures:

  • Fixed Dimension Universality: The most significant insight is that wider models are not strictly necessary for better precision or longer contexts. Depth (adding more layers) is the primary driver of expressivity. The embedding dimension remains fixed regardless of the desired precision, and the number of attention heads only needs to be proportional to the token dimension.
  • Theorem 1 (Unmasked Attention): For any continuous in-context mapping and any error margin $\epsilon$, there exists a deep Transformer utilizing unmasked attention (akin to Vision Transformers) that can approximate that mapping.
  • Theorem 2 (Masked Attention): A parallel result holds for causal Transformers (like LLMs), provided the contexts exhibit certain "Lipschitz" regularity—meaning the context does not change too abruptly over time. The authors identify a property called "causal identifiability," proving that masked Transformers naturally learn functions where the output at time $t$ depends exclusively on the context state up to that time.
  • In-Context Regression: The framework successfully encompasses in-context ridge regression—a standard benchmark for ICL—as a specific type of mapping covered by their universal approximation theory.

Limitations and Future Directions

As a purely theoretical work, the paper does not rely on empirical datasets, and it acknowledges several limitations that pave the way for future research:

  • Non-Quantitative Bounds: While the proof guarantees the existence of a Transformer for any given precision, it does not provide explicit bounds on the number of layers or parameters required to achieve a specific error $\epsilon$.
  • Architectural Nuances: The current proof constructs attention heads that essentially output scalars. Bridging this gap to real-world vector-valued heads could yield tighter theoretical results. Additionally, the framework does not explicitly account for modern positional encoding techniques like Rotary Positional Embeddings (RoPE).
  • Optimization vs. Approximation: The theorems prove that Transformers can represent these complex functions, but they do not guarantee that standard optimization algorithms like Gradient Descent will successfully find these optimal weights during training.

Summary updated

BibTeX
@inproceedings{furuya2025transformers,
  author = {Takashi Furuya and Maarten V. de Hoop and Gabriel Peyré},
  title = {{Transformers are Universal In-context Learners}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=6S4WQD1LZR},
}
Figure