Paper recreation · interactive demo

Titans: Learning to Memorize at Test Time

Behrouz, Zhong, Mirrokni (Google) — arXiv:2501.00663

The memory in Titans is a small MLP that keeps training during the forward pass. Each token gives it a recall task: map the token's key to its value. How badly it fails, the surprise, is the gradient it steps along. Nothing on this page is trained beforehand. The only weight updates are the ones the memory makes on itself while it reads.

\[ \begin{aligned} \ell_t &= \lVert M_{t-1}(k_t) - v_t \rVert^2 && k_t = x_t W_K,\; v_t = x_t W_V \\ S_t &= \eta_t\, S_{t-1} - \theta_t\, \nabla \ell_t && \text{past surprise + momentary surprise} \\ M_t &= (1 - \alpha_t)\, M_{t-1} + S_t && \alpha_t \text{ forgets} \\ y_t &= M_t(q_t) && q_t = x_t W_Q \end{aligned} \]

A memory that learns while it reads

A freshly initialised memory reads a random sequence one chunk at a time. Each chunk is read with the memory as it was before that chunk, then the memory takes one gradient step on it. Turn \(\theta\) to 0 and nothing is learned. Raise \(\alpha\) and old tokens fade, so recent positions are recalled better than old ones.

Surprise while reading: the inner loss on each chunk, measured before the memory updates on it. 4 sequences, averaged.

Recall error by position, after reading the whole sequence. Dashed: the untouched initial memory. With \(\alpha > 0\) the newest tokens are recalled best.

Chunk size matters, and smaller is not better

chunk_size=1 is the literal per-token recurrence from the paper, and it memorizes worse than chunked updates. Gradients from single tokens partly cancel each other out. A chunk's mean gradient points in a cleaner direction. Bars show recall error after reading divided by error before, with the \(\theta\), \(\eta\), \(\alpha\) and depth from above at T=128 (lower is better).

Capacity, not length

Reading more tokens stops helping. The memory is an MLP of fixed size, and \(\alpha\) keeps forgetting, so recall error after 16, 64 or 256 tokens settles on about the same plateau. Settings come from the first panel; chunk_size 4.

Does it buy long-range recall? (result from train.py)

A needle token is hidden outside an 8-token attention window in a 64-token sequence, and the model must emit it at the end. Both models have the same width and depth. The memory is the only path to the needle. This result comes from the PyTorch run and is not recomputed here.

MAG (attention + memory)
1.000
window attention only
0.117
chance (8 classes)
0.125

What this demo simplifies

In the repo, \(\eta\), \(\theta\) and \(\alpha\) come from each token through learned gate heads, and the outer weights (\(W_K, W_V, W_Q\), the gates, the memory's initial weights) are trained by backprop through the inner updates. This page has no outer training. \(W_K\) and \(W_V\) are random and frozen, and the gates are fixed scalars from the sliders. The defaults match the repo's gate biases: \(\theta = \operatorname{softplus}(-1.05) \approx 0.30\), \(\alpha = \sigma(-4) \approx 0.018\), \(\eta = 0.5\).

Everything else is the repo's NeuralMemory inner loop, ported line for line: an MLP of width 32 (hidden ×2, SiLU), loss averaged over features and tokens, \(\theta\) scaled by chunk length, momentum \(S\), decay on every weight and bias, and reading before updating. The architectures that combine the memory with attention (MAC, MAG, MAL) are not run live. The needle result above comes from train.py.