Learning to (Learn at Test Time): RNNs with Expressive Hidden States

Yu Sun, Xinhao Li, Karan Dalal, Jiarui Xu, Arjun Vikram, Genghan Zhang, Yann Dubois, Xinlei Chen, Xiaolong Wang, Sanmi Koyejo, Tatsunori Hashimoto, Carlos Guestrin

Sequence modeling is currently dominated by Transformers, but their quadratic computational complexity severely limits their ability to process extremely long contexts. Recurrent Neural Networks (RNNs) offer a linear-time alternative, yet they historically struggle to maintain performance over long sequences.

This paper introduces Test-Time Training (TTT) layers, a novel class of sequence modeling layers where the hidden state is itself a machine learning model. By updating this "state-model" via self-supervised learning during inference, TTT layers achieve the linear complexity of RNNs while matching the long-context performance of Transformers.

The "Goldilocks" Problem in Sequence Modeling

Modern AI architectures face a fundamental trade-off when processing sequences:

  • Transformers are incredibly expressive and handle long contexts beautifully. However, their computational cost grows quadratically ($O(n^2)$) with sequence length because they must store every past token in a "KV cache."
  • RNNs (like Mamba or LSTMs) are much faster ($O(n)$) because they compress the entire history into a fixed-size hidden state.

Unfortunately, this fixed state becomes a bottleneck. As the sequence grows, the RNN eventually "forgets" or fails to capture complex relationships. The authors identify this as a compression challenge: how can a model fit millions of tokens of context into a compact state without losing the critical connections that self-attention naturally captures?

The Solution: Test-Time Training (TTT) Layers

To solve the compression challenge, the authors propose replacing the standard hidden state vector $s_t$ with a set of weights $W_t$ belonging to an internal model $f$ (such as a linear model or a small Multi-Layer Perceptron).

This architecture operates on two distinct learning loops:

  1. The Inner Loop (Test-Time Training): As the model processes a sequence during inference, it performs a step of gradient descent to update $W_t$. The "training data" for this update is the sequence itself, utilizing a self-supervised task (e.g., reconstructing the current token from a corrupted version of itself).
  2. The Outer Loop (Standard Training): The parameters that govern how the model learns at test time—such as the learning rate and the projection matrices for the reconstruction task—are learned during the initial pre-training phase on a large dataset.

Training a model during inference sounds computationally prohibitive. To resolve this, the authors developed a "dual form" mathematical equivalent. This dual form allows the TTT updates to be computed using highly optimized matrix multiplications (matmuls), making the process over 5x faster than a naive implementation.

The paper evaluates two specific instantiations of this framework: TTT-Linear (where the hidden state is a linear model) and TTT-MLP (where the hidden state is a two-layer neural network).

Key Insights: Compression and Unification

The core intuition behind TTT is that self-supervised learning is an optimal compression heuristic. Just as a Large Language Model compresses the internet into its static weights during pre-training, a TTT layer compresses a long conversation into its dynamic hidden weights during inference.

Furthermore, the authors use this framework to theoretically unify the field of sequence modeling:

  • Linear Attention is proven to be a special case of a TTT layer utilizing a linear model and a specific update rule.
  • Self-Attention can be viewed as a TTT layer that uses a non-parametric learner (specifically, the Nadaraya-Watson estimator).

Empirical Results

The models were trained and evaluated at scales ranging from 125M to 1.3B parameters using The Pile (for 2k and 8k short-context evaluations) and Books3 (for long-context evaluations up to 32k tokens).

  • Breaking the RNN Plateau: Traditional modern RNNs like Mamba show a performance "plateau"; after about 16k tokens, adding more context does not improve their next-token prediction. In contrast, TTT layers—like Transformers—continue to reduce perplexity as the context gets longer.
  • Long-Context Superiority: At a 32k context window, both TTT-Linear and TTT-MLP significantly outperform Mamba. TTT-MLP shows the most promise for scaling to even longer sequences.
  • Efficiency: TTT-Linear is already 10% faster than a Transformer baseline at a 2k context length on TPUs, even without heavy optimization. While TTT-MLP is currently slower in wall-clock time due to memory I/O overhead, it is highly "FLOP-efficient," achieving better performance with less raw computation.

Limitations and the Road Ahead

While TTT layers present a massive step forward for linear-time sequence modeling, there are hurdles to overcome. TTT-MLP is currently bottlenecked by memory I/O (moving data between the processor and memory). Unlocking its full speed will require custom GPU kernels and hardware-aware optimizations.

Additionally, while the 1.3B parameter scale proves the concept, testing TTT layers at the 70B+ parameter scale remains a critical next step. Looking further ahead, the authors speculate that for truly massive contexts—spanning millions or billions of tokens—the hidden state could be scaled up to even more complex architectures, such as small Convolutional Neural Networks (CNNs).

Summary updated

BibTeX
@inproceedings{sun2025learning,
  author = {Yu Sun and Xinhao Li and Karan Dalal and Jiarui Xu and Arjun Vikram and Genghan Zhang and Yann Dubois and Xinlei Chen and Xiaolong Wang and Sanmi Koyejo and Tatsunori Hashimoto and Carlos Guestrin},
  title = {{Learning to (Learn at Test Time): RNNs with Expressive Hidden States}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=eifW0W0xgt},
}
Figure