Universal In-Context Approximation By Prompting Fully Recurrent Models

Aleksandar Petrov, Tom A. Lamb, Alasdair Paren, Philip H. S. Torr, Adel Bibi

TL;DR

This paper proves that fully recurrent neural networks—including RNNs, LSTMs, and modern State Space Models (SSMs) like Mamba—are universal in-context approximators, meaning they can be prompted to compute any function without changing their weights. To demonstrate this, the authors introduce LSRL, a novel programming language that compiles directly into recurrent model weights, while revealing that multiplicative gating is essential for numerical stability in practice.

What Problem Does This Paper Address?

In-context learning (ICL) allows large foundation models to solve new tasks purely via prompting, without any fine-tuning or weight updates. While recent theoretical work proved that Transformers are "universal in-context approximators" (they can approximate any function given the right prompt), this capability was heavily attributed to their attention mechanism.

This left a major open question: Can fully recurrent architectures (like classic RNNs, LSTMs, and increasingly popular State Space Models) also act as universal in-context approximators? This paper fills that gap by analyzing the theoretical limits of prompting in recurrent models, which is crucial for understanding the safety, security, and alignment of these non-Transformer architectures.

Methods

The authors approach this problem constructively by designing a new domain-specific programming language called Linear State Recurrent Language (LSRL). LSRL allows researchers to write algorithms using basic primitives (linear layers, ReLUs, linear state updates, concatenation, and element-wise multiplication) that compile directly into the exact weight matrices of recurrent neural networks.

Using LSRL, they construct specific programs capable of:

  1. Approximating continuous functions ($C^{vec}$): The model stores the user's query in its hidden state and sequentially scans prompt tokens that describe a discretized grid of the target function.
  2. Representing token-to-token mappings ($C^{tok}$): The model acts as a dictionary lookup, matching a query sequence against key-value pairs provided in the prompt.

They also introduce a mathematical compiler trick to show that any program compiled to a linear RNN can be converted into a classic non-linear RNN by splitting hidden states into positive and negative components to bypass the ReLU activation function.

Figure 1: Compilation of an LSRL program to a Linear RNN. An example of a simple LSRL program that takes a sequence of 0s and 1s as an input and outputs 1 if there have been more 1s than 0s and 0 otherwise. The LSRL compiler follows the rules in App. A to simplify the computation DAG into a path graph. The resulting path graph can be represented as a Linear RNN with one layer.

Datasets

As a purely theoretical and constructive paper, no standard machine learning training datasets (like ImageNet or WikiText) were used. Instead, the authors evaluated their compiled models using synthetic benchmarks:

  • 25 distinct target continuous functions to evaluate vector approximation.
  • 25 discrete dictionary maps to evaluate token sequence matching.

These synthetic setups were primarily used to evaluate how well the compiled models handled query-prompt sequences under varying levels of parameter noise.

Key Insights and Ideas

  • Attention is Not Required for Universal ICL: Recurrent models can store a query in their fixed-size hidden state and sequentially scan prompt tokens to perform complex function approximation, completely bypassing the need for an attention mechanism.
  • Shorter Prompts Than Transformers: Surprisingly, for continuous function approximation, the theoretical prompt length required for Linear RNNs ($O(\epsilon^{-din})$) scales significantly better with the desired precision ($\epsilon$) than the best-known bounds for Transformers ($O(\epsilon^{-10-14din-4din^2})$).
  • The Importance of Gating: While pure Linear RNNs can theoretically approximate any function, doing so requires highly unstable conditional logic (f_ifelse). Architectures with multiplicative gating (LSTMs, GRUs, Mamba, Hawk/Griffin) allow for a much more stable implementation of conditional logic, making them far better candidates for practical in-context learning.

Key Results

  • Universal Proof: The paper formally proves that RNNs, LSTMs, GRUs, Linear RNNs, and gated SSMs (Hawk/Griffin/Mamba) are all universal in-context approximators.
  • Numerical Stability Analysis: Through synthetic testing, the authors demonstrated that standard ReLU-based conditional operators degrade rapidly under parameter noise, often failing completely. In contrast, implementations leveraging multiplicative gating maintained high robustness and precision even when Gaussian noise was added to the model weights.

Limitations and Future Work

  • Existence vs. Learnability: The paper provides a constructive existence proof—it shows that universal in-context approximating weights exist, but it does not guarantee that standard training algorithms (like gradient descent) can actually discover these weights in practice.
  • Unconstrained State Matrices: The proofs assume no structural restrictions on the state transition matrix $A$. However, many modern SSMs restrict $A$ to be diagonal to ensure fast training and inference. While the authors note their compiled matrices are highly sparse and often diagonal, formally extending the proof to strictly diagonal constraints remains future work.

Summary updated

BibTeX
@inproceedings{petrov2024universal,
  author = {Aleksandar Petrov and Tom A. Lamb and Alasdair Paren and Philip H. S. Torr and Adel Bibi},
  title = {{Universal In-Context Approximation By Prompting Fully Recurrent Models}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=GproaSYZk5},
}
Figure