konakona

From Linear Attention to Test-Time Training

Linear attention reduces the quadratic cost of standard attention to linear, which is great. But can we do better?

This blog post is based on the CVPR 2026 paper ViT3^3: Unlocking Test-Time Training in Vision.

In this post, we explore the connection between attention and sequence modeling, and how it leads to a new paradigm called test-time training (TTT). First, let’s start with a brief review of standard softmax attention and linear attention.

Softmax Attention

Let Q,KRN×dkQ, K \in \mathbb{R}^{N \times d_k} be the query and key matrices, and let VRN×dvV \in \mathbb{R}^{N \times d_v} be the value matrix. Standard scaled dot-product attention is

O=softmax(QKTdk)V,O = \operatorname{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V,

where the softmax is applied row-wise. Computing all pairwise query-key scores is O(N2dk)O(N^2 d_k), and mixing the values is O(N2dv)O(N^2 d_v). We usually summarize this as O(N2d)O(N^2 d): the quadratic dependence on NN makes long contexts expensive.

Linear Attention

Linear attention replaces the softmax similarity with a factorized kernel. It uses a feature map ϕ:RdkRr\phi: \mathbb{R}^{d_k} \to \mathbb{R}^{r}, applied row-wise to QQ and KK, to define

κ(qi,kj)=ϕ(qi)Tϕ(kj),i,j.\kappa(q_i, k_j) = \phi(q_i)^T \phi(k_j), \quad \forall i,j.

In some variants, κ\kappa approximates the exponential softmax kernel; in others, it is a different kernel chosen for efficiency.

Substituting this into the attention formula gives:

oi=j=1Nϕ(qi)Tϕ(kj)vjj=1Nϕ(qi)Tϕ(kj),=ϕ(qi)T(j=1Nϕ(kj)vjT)ϕ(qi)T(j=1Nϕ(kj)),=ϕ(qi)TSϕ(qi)Tz,\begin{aligned} o_i &= \frac{\sum_{j=1}^{N} \phi(q_i)^T \phi(k_j) v_j}{\sum_{j=1}^{N} \phi(q_i)^T \phi(k_j)}, \\ &= \frac{\phi(q_i)^T \left(\sum_{j=1}^{N} \phi(k_j) v_j^T\right)}{\phi(q_i)^T \left(\sum_{j=1}^{N} \phi(k_j)\right)}, \\ &= \frac{\phi(q_i)^T S}{\phi(q_i)^T z}, \end{aligned}

where

S=j=1Nϕ(kj)vjT,z=j=1Nϕ(kj).S = \sum_{j=1}^{N} \phi(k_j) v_j^T, \qquad z = \sum_{j=1}^{N} \phi(k_j).

Here, SS is a fixed-size summary of the key-value pairs, while zz acts as a normalization term.

Here, SRr×dvS \in \mathbb{R}^{r \times d_v}, zRrz \in \mathbb{R}^{r}, and oiRdvo_i \in \mathbb{R}^{d_v}. Once SS and zz have been computed, evaluating one output costs O(rdv)O(r d_v). Computing all outputs therefore costs O(Nrdv)O(N r d_v). In the common case r=dv=dr = d_v = d, this becomes O(Nd2)O(Nd^2): linear in the sequence length NN.

This kernel-based formulation is described in Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention.

Causal Linear Attention

In autoregressive generation, only a prefix of the sequence is visible, and the sequence grows over time. Linear attention can maintain the corresponding summary incrementally:

St=j=1tϕ(kj)vjT,zt=j=1tϕ(kj).\begin{aligned} S_t &= \sum_{j=1}^{t}\phi(k_j)v_j^T, \\ z_t &= \sum_{j=1}^{t}\phi(k_j). \end{aligned}

Constructing the summary for a prefix of length tt costs O(td2)O(t d^2) when r=dv=dr = d_v = d. Each new token then updates the state as follows:

St+1=St+ϕ(kt+1)vt+1T,zt+1=zt+ϕ(kt+1).\begin{aligned} S_{t+1} &= S_t + \phi(k_{t+1})v_{t+1}^T, \\ z_{t+1} &= z_t + \phi(k_{t+1}). \end{aligned}

Each update costs O(d2)O(d^2), so processing a sequence of length NN remains O(Nd2)O(Nd^2) overall.

A Different View of Attention

We can reformulate the two types of attention in the following way. When KK and VV are fixed, softmax attention computes

O=σ(QKT)V,O = \sigma(QK^T)V,

where QQ can be viewed as an input to the linear layer KTK^T, followed by a softmax activation, and then a linear layer VV without activation. This is a two-layer MLP with a nonlinearity in between.

Softmax attention as a two-layer MLP: Q is multiplied by K transpose, normalized with softmax, and multiplied by V to produce O.

For linear attention, we can consider the simplest unnormalized form, and deliberately omitting the feature map. By associativity, we have

O=(QKT)V=Q(KTV).O = (QK^T)V = Q(K^TV).

Similarly, we can consider QQ as an input to a linear layer KTVK^T V (whose weights can be precomputed), without any intervening activation. This is a single-layer MLP.

Linear attention as a single-layer MLP: Q is multiplied by the precomputed K transpose V summary to produce O.

The pattern is clear: build a model with KK and VV as parameters, then apply the model to QQ to get the output. This is an important reformulation of the attention mechanism.

The MLP in the middle can be viewed as a sequence-modeling function. Under this perspective, the key difference between softmax and linear attention is that softmax attention keeps the individual keys and values available to each query, whereas linear attention first reduces them to a fixed-size summary. This compression makes linear attention more efficient, but can limit the information available to each query.

Sequence modelling can therefore be viewed as a compression problem: build a compact representation of the observed sequence, then use it to predict future tokens. The central trade-off is between the fidelity of that representation and the cost of constructing and evaluating it. Naturally, one might ask: can we learn a compression function more expressive than the hand-designed linear attention while still keeping the cost linear?

One possible answer is to use a neural network. Self-supervised learning can encode a large training set in model weights while capturing useful structures and relationships in the data. This idea leads to the concept of Test-Time Training (TTT).

The connection between attention and an inner learner was introduced in Learning to (Learn at Test Time) and was further developed in Learning to (Learn at Test Time): RNNs with Expressive Hidden States.

The Test-Time Training Paradigm

TTT treats the key-value pairs as a small dataset. At inference time, an inner model is optimized to map KK to VV using a loss L(V^,V)\mathcal{L}(\hat{V}, V), thereby compressing the current sequence into its parameters. The queries QQ are then fed to this adapted model to produce OO. Because this optimization happens during inference, it is called test-time training.

The test-time training paradigm: keys pass through an inner model to predict values, the loss updates the model weights, and the updated model maps queries to outputs.

Formally, let f(;W)f(\cdot; W) be the inner model with temporary parameters WW. A full-sequence inner-model update can be written as

W=WηWi=1NL(f(ki;W),vi),O=f(Q;W).W' = W - \eta \nabla_W \sum_{i=1}^{N} \mathcal{L}\left(f(k_i; W), v_i\right), \qquad O = f(Q; W').

The update aims to compress the current sequence into WW'. In a causal setting, the same idea updates the state from each growing prefix:

Wt=Wt1ηWL(f(kt;Wt1),vt),ot=f(qt;Wt).W_t = W_{t-1} - \eta \nabla_W \mathcal{L}\left(f(k_t; W_{t-1}), v_t\right), \qquad o_t = f(q_t; W_t).

The Outer Loop

During ordinary training, the outer loop optimizes the task loss, but in order to do so, it must also “optimize” the inner loop. It learns the projections that produce QQ, KK, and VV, sometimes along with the inner-model initialization W0W_0 — these all affect the inner-loop optimization.

At deployment, outer-loop parameters are frozen, while the inner model is adapted to each new sequence at test time. The outer loop is therefore, in some sense, a meta-learning objective: it learns parameters that make the inner-loop adaptation more effective.

In principle, the inner model can be any differentiable neural-network architecture, which creates a large design space. ViT3^3: Unlocking Test-Time Training in Vision studies this design space for vision.

Practical Limitation

TTT avoids materializing all N2N^2 query-key interactions, but linear scaling in NN does not make it free. A linear inner model still typically costs O(Nd2)O(Nd^2) and, because it is adapted at runtime, also requires a backward pass, which significantly increases the computational burden and puts pressure on memory throughput. The inner model must therefore be small enough to be practical, and the outer loop must learn an initialization that makes the inner model effective with only a few gradient steps.

Contents