Paper commentary
Learning to (Learn at Test Time): RNNs with Expressive Hidden States
- Problem Self-attention keeps a cache that grows with context, while existing RNN layers squeeze the history into a fixed-size state whose expressive power limits long-context use.
- Method Make the RNN hidden state a trainable model f with weights W_t, and make each token's update one gradient step on a self-supervised loss, computed in mini-batches and in a dual form.

Figure 1 . All sequence modeling layers can be expressed as a hidden state that transitions according to an update rule. Our key idea is to make the hidden state itself a modelf with weightsW , and the update rule a gradient step on the self-supervised lossℓ. Therefore, updating the hidden state on a test sequence is equivalent to training the modelf at test time. This process, known as Test-Time Training (TTT), is programmed into our TTT layers.
Original paper · CC BY 4.0 · Yu Sun, Xinhao Li, Karan Dalal, Jiarui Xu, Arjun Vikram, Genghan Zhang, Yann Dubois, Xinlei Chen, Xiaolong Wang, Sanmi Koyejo, Tatsunori Hashimoto, Carlos Guestrin · Learning to (Learn at Test Time): RNNs with Expressive Hidden States · Original paper figure · Extracted the figure area from page 1 of the original PDF.Why fixed-size RNN states struggle with long context
Self-attention stores every key-value pair in a cache that grows with the context, so the work for each new token grows as well. Existing RNN layers instead compress the whole history into a state with a fixed number of entries, which keeps the cost per token constant but caps how much past information the state can hold. Mamba is the RNN the authors use as the main recurrent baseline, and it scales similarly to a strong Transformer on the scaling plots.
The test that exposes the problem is whether later tokens become easier to predict as the context grows, since they condition on more information. The Transformer's average perplexity at each token index keeps falling across its 32k context, whereas the same metric for Mamba levels off after 16k.
A hidden state that is a trained model
The hidden state is made equal to the weights W_t of a model f, which can be a linear model, a small network, or another learner. Each new token triggers one gradient descent step on a self-supervised loss, and the output is the model applied to that token with the updated weights. The update and output rules are written as follows.
The update is a gradient step with learning rate η, and the output rule is the prediction on x_t made with the updated weights. Because the gradient is taken at every token, each input sequence receives its own trajectory of weights, even at test time, which is why the layers are called Test-Time Training layers. The loss first used is a reconstruction of x_t from a corrupted input x̃_t, and W_0 is set to zero in this first form.
In the first form the self-supervised task is pure reconstruction, which denoising autoencoders also use as a training signal, so the model has to learn correlations between the dimensions of x_t. Figure 4 shows that one gradient step lowers the TTT loss on the token it was computed from, and the loss on later tokens keeps falling as the test sequence advances in the first three TTT layers of a 125M network with T = 2048.
The same figure shows the loss values averaged over test sequences and a sliding window of 10 timesteps.
What the inner loop learns and which models it uses
The self-supervised task is learned in the outer loop: a training view θ_K x_t replaces the corrupted input, and a label view θ_V x_t replaces the target, so the inner loss becomes the following. \[ \ell(W;x_t)=\left\|f(\theta_K x_t;W)-\theta_V x_t\right\|^2 \] Only W is updated in the inner loop, while θ_K, θ_V and θ_Q are trained with the rest of the network, so the views act as hyper-parameters of the loss.
The output uses a test view θ_Q x_t, so the output rule becomes z_t = f(θ_Q x_t; W_t), since the training view has fewer dimensions than x_t. Using a separate test view lets the output carry information that the state does not need to store, which the authors present as extra flexibility of the self-supervised task.
TTT-Linear uses f(x)=Wx with a square W, and TTT-MLP uses a two-layer MLP whose hidden dimension is four times the input dimension, followed by a GELU activation. In both cases f is wrapped with a Layer Normalization and a residual connection, so f(x) = x + LN(f_res(x)) with f_res being the linear model or the MLP. The inner-loop learning rate is an outer-loop function of the token, η(x) = η_base σ(θ_lr · x), with η_base set to 1 for TTT-Linear and 0.1 for TTT-MLP.
The initial weights W_0 are learned in the outer loop and shared by all sequences, which the authors find improves training stability. Mamba's backbone, which places temporal convolutions before the recurrent layer, is used by TTT-Linear and TTT-MLP unless noted otherwise, because the authors found it also improves their perplexity.
Making the inner loop parallel and fast
The naive update cannot be parallelized, because each W_t depends on W_{t-1} both in the subtraction and inside the gradient. Mini-batch gradient descent sits between the two: gradients within a mini-batch use the weights from the end of the previous mini-batch, computed as follows.
\[ G_t = \nabla \ell(W_{t'}; x_t), \qquad t' = t - \mathrm{mod}(t, b) \] Here b is the TTT batch size, so b gradients can be computed at once, and b = 16 is used in all experiments. Smaller b gives lower perplexity because more gradient steps are taken, and b = 16 gives the 11.09 perplexity that the final TTT-Linear result reaches.
For a linear f, computing each gradient separately requires d×d outer products, which are memory and I/O heavy and do not map onto the matrix units of an A100 GPU.
The primal form costs O(b×d²) per mini-batch, and the dual form costs the same O(b×d²) for W_b plus an extra O(b²×d) for the outputs. The authors trade a little theoretical cost for hardware utilization, since d is a few hundred and b is only 16, and they report that the JAX dual-form training is more than 5× faster than the primal form.
How TTT relates to linear attention and self-attention
With a linear f, batch gradient descent with η = 1/2, W_0 = 0, and the output rule using the test view, the TTT layer returns the same output as linear attention in the simplest form. Plugging W_t into the output rule gives z_t as a sum over s from 1 to t of (θ_V x_s)(θ_K x_s)^T(θ_Q x_t), which is the linear attention formula.
The authors note that the original linear attention includes a normalizer and a feature expansion, which prior work found can hurt performance, so the equivalent TTT layer omits both. Figure 8 shows how the choice of model and update rule (online, batch or mini-batch gradient descent) induces each TTT layer, and marks TTT-Linear and TTT-MLP as the two instantiations proposed here.
With the Nadaraya-Watson estimator as a nonparametric learner, the prediction is a kernel-weighted average of the stored labels, f(x; x_1,...,x_t) = (Σ κ(x, x_s) y_s) / (Σ κ(x, x_s)), where y_s = θ_V x_s. The kernel is κ(x, x′; θ_K, θ_Q) ∝ exp((θ_K x)^T θ_Q x′), so the TTT layer built from this learner produces the same outputs as self-attention.
In this case the hidden state is the list of processed inputs, the update appends x_t to the list, and the output scans the list with κ, so the layer does not produce a different implementation from attention.
From linear attention to TTT-Linear
Table 1 starts from linear attention at 15.91 perplexity and an improved version at 15.23, with all models at 125M parameters trained under the recipe of the experiments section. The TTT equivalence row also reaches 15.23, and adding a learnable W_0 moves it to 15.27, which is a slight increase of 0.04. Components are added row by row, so each row changes only the components of the rows above it.
Adding LN and a residual connection to the inner model lowers perplexity from 15.27 to 14.05, a change of −1.22. Switching from batch GD to mini-batch TTT, with the batch size changed from b = T = 2048 to b = 16, lowers perplexity from 14.05 to 12.35, a change of −1.70.
Learning the inner-loop learning rate lowers perplexity from 12.35 to 11.99, a change of −0.36. Adding the Mamba backbone lowers it further to 11.09, a change of −0.90, which is the final TTT-Linear result in the scaling experiments. The rows show cumulative additions on a single 125M setting, so they do not isolate each component's effect in a separate run.
Scaling trends on the Pile and Books
On the Pile at 2k context, TTT-Linear (M), Mamba and Transformer have comparable perplexity, and the lines mostly overlap. TTT-MLP (M) is slightly worse under large FLOP budgets, because TTT-MLP has better perplexity than TTT-Linear at every model size but its extra FLOPs offset that advantage.
At 8k context on the Pile, TTT-Linear (M) and TTT-MLP (M) perform significantly better than Mamba, which the authors contrast with the 2k result. TTT-MLP (T), with the Transformer backbone, performs slightly better than Mamba around 1.3B at 8k.
On Books at 2k context, Mamba performs slightly better than TTT-Linear, whereas the two lines roughly overlapped on the Pile at 2k. At 32k context on Books, TTT-Linear (M) and TTT-MLP (M) perform better than Mamba, and TTT-MLP (T) performs slightly better than Mamba as well.
The authors hypothesize that the temporal convolutions of the Mamba backbone help more when the sequence layer has a less expressive hidden state, which is why the linear model benefits from them more than the MLP. TTT-MLP (T) is only slightly worse than TTT-MLP (M) at 1.3B on Books, and the authors suggest the Transformer backbone may suit larger models and longer context, which they did not test beyond their evaluations.
The models are 125M, 350M, 760M and 1.3B parameters, while Mamba uses 130M, 370M, 790M and 1.4B, and all are trained with the Chinchilla recipe from the Mamba paper. The tokenizer is the Llama tokenizer for all models, which differs from the GPT-2 and GPT-NeoX tokenizers used in the Mamba paper. TF finetune starts from the model trained at Books 2k and uses 20% more tokens to finetune at the designated long context, and it is included from 4k context on.
Using more tokens at long context
Figure 2 shows scaling trends on the Pile with 8k context on the left, and perplexity per token index from 128 to 32k on the right, where all methods have matched training FLOPs as Mamba 1.4B. On the right, the authors observe that TTT-Linear and TTT-MLP keep reducing perplexity as more tokens are conditioned on, while Mamba stops improving after 16k.
Under matched FLOPs, TTT-MLP performs worse than TTT-Linear at short context and better at long context. The authors attribute this to the MLP hidden state being more expressive, so its extra capacity is used at long context but is redundant at short context.
Training and inference speed
On a v5e-256 TPU pod at 2k context, the Transformer baseline takes 0.30s per training iteration, while TTT-Linear takes 0.27s per iteration. The authors report that TTT-Linear is already 10% faster there without any systems optimization. Mamba runs only on GPUs, so the authors wrote inference kernels for TTT on an NVIDIA A100 GPU, while the training kernel was not written.
Figure 12 reports latency for prefill with batch size 16 at context lengths 2k to 32k, and for decode with batch size 512 at context lengths 512 to 8k. Time per token grows linearly for the Transformer as context increases, while it stays roughly constant for TTT-Linear, TTT-MLP and Mamba. The authors note that the forward latency of these networks rises slightly with context, and suggest GPU throttling at large context as one possible cause.
The Transformer baseline runs much faster here than in the Mamba paper because the authors serve it with vLLM instead of the HuggingFace Transformer. Prefill is the same operation as the forward pass during training, so the dual form is used for it, while decode is sequential and uses the primal form.
Limitations
TTT-MLP still faces challenges in memory I/O, even though it shows larger potential in long context.
The scaling trends show no clean linear fit in Figure 10 or Figure 11, including for Transformers, so the authors connect the points rather than fit a scaling law.
Hybrid architectures that combine self-attention and TTT layers were not tested, because the baselines are not hybrid.
Paper info
- Authors
- Yu Sun, Xinhao Li, Karan Dalal, Jiarui Xu, Arjun Vikram, Genghan Zhang, Yann Dubois, Xinlei Chen, Xiaolong Wang, Sanmi Koyejo, Tatsunori Hashimoto, Carlos Guestrin
- Venue
- arXiv · Preprint
- First published
- 2024-7-5
- Added
- 2026-10-30
- Updated
- 2025-8-31
- Version
- v4
- arXiv
- 2407.04620
- Category
- cs.LG · cs.AI · cs.CL
- Original paper terms
- CC BY 4.0
- What we show
- Paper info, commentary and permitted figures from the original paper
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Abstract
Self-attention performs well in long context but has quadratic complexity. Existing RNN layers have linear complexity, but their performance in long context is limited by the expressive power of their hidden states. We present a practical framework for instantiating sequence modeling layers with linear complexity and expressive hidden states. The key idea is to make the hidden state a machine learning model itself, and the update rule a step of self-supervised learning. Since the hidden state is updated by training even on test sequences, our layers are called Test-Time Training (TTT) layers. We consider two instantiations: TTT-Linear and TTT-MLP, whose hidden state is a linear model and a two-layer MLP respectively. We evaluate our instantiations at the scale of 125M to 1.3B parameters, comparing with a strong Transformer and Mamba, a modern RNN. Similar to Transformer, TTT-Linear and TTT-MLP can keep reducing perplexity by conditioning on more tokens, while Mamba cannot after 16k context. TTT-MLP still faces challenges in memory I/O, but shows larger potential in long context, pointing to a promising direction for future research.
Quote from the paper
Yu Sun, Xinhao Li, Karan Dalal, Jiarui Xu, Arjun Vikram, Genghan Zhang, Yann Dubois, Xinlei Chen, Xiaolong Wang, Sanmi Koyejo, Tatsunori Hashimoto, Carlos Guestrin (2024). Learning to (Learn at Test Time): RNNs with Expressive Hidden States. arXiv:2407.04620v4. https://arxiv.org/abs/2407.04620v4
BibTeX
@misc{sun2024learning,
title = {{Learning to (Learn at Test Time): RNNs with Expressive Hidden States}},
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},
year = {2024},
eprint = {2407.04620},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2407.04620v4}
}