Learning What to Remember: Test-Time Training via Context Distillation

Zixuan Wang, Xingyu Dang, Rui-Jie Zhu, Zixin Wen, Hengyu Fu, Wenhao Chai, Jason D. LeeNeurIPS 2026

TL;DR

When humans read a sprawling novel or debug a massive codebase, we don’t memorize every single word—we selectively hold onto details that feel like they will matter later. Test-Time Context Distillation (TTCD) teaches language models to do the exact same thing during inference, compressing distant history directly into the model's weights based on its future utility. Even better, you don't have to train a model from scratch to use it; it can be retrofitted onto existing pre-trained Transformers to unlock long-context reasoning at a fraction of full attention's cost.

What Problem Does This Paper Address?

Standard Transformers have a photographic memory—their key-value (KV) cache stores every past token, which makes exact retrieval easy, but causes compute and memory costs to balloon quadratically as sequences grow.

To escape this quadratic trap, researchers have turned to modern recurrent architectures and Test-Time Training (TTT). In TTT, a subset of the model’s parameters—called fast weights—are updated on the fly during inference, acting as a fixed-size, evolving memory state.

Because this memory has a bounded capacity, the model faces a tough editorial decision at every step: what to write, what to keep, and what to forget? Current TTT methods stumble here in two ways:

  1. Reconstruction objectives try to compress all past tokens uniformly, wasting precious memory capacity on irrelevant trivia.
  2. Next-token prediction objectives suffer from recency bias, hyper-focusing on local signals right next to the current token rather than attributing credit to distant history.

The authors ask: How do we design an online objective that explicitly teaches fast weights to store only the past information that will be useful for future predictions?

Methods

Enter Test-Time Context Distillation (TTCD), and its practical "drop-in" variant, In-Place TTCD (IP-TTCD).

Figure 1: Overview of TTCD. A shared and frozen backbone is run with two context lengths: a short-window student that observes only recent tokens and a long-window teacher that observes a longer history. TTCD treats the discrepancy between the teacher representation and the student’s projected hidden state as a context-distillation loss, and uses this loss to update the down-projection fast weight at test time. The predictive contribution of the self-teacher relative to a short-context student is distilled into fast weights, which persists after the evidence leaves the local window. The learned information can be retrieved by later related queries, benefiting future predictions.

Instead of inventing a brand-new architecture, IP-TTCD builds on standard Transformer blocks (borrowing the "in-place" setup from prior work, where the down-projection matrix $\mathbf{W}_{\text{down}}$ inside the SwiGLU MLP layer doubles as the adaptable fast weight).

What’s novel is how those weights get updated as a sequence streams by in chunks:

  • Two Views, One Backbone: For each chunk of tokens, the model runs two causal sliding-window views over the shared, frozen backbone: a short-window student (seeing only recent local tokens) and a long-window self-teacher (seeing a longer prefix of past history).
  • Counterfactual Supervision: At any given position, if the teacher produces a different MLP output than the student, that discrepancy represents the exact predictive signal contributed by the distant history that the student missed. TTCD uses the squared $L_2$ distance between the student's output and the stop-gradient teacher's output as an online self-supervised loss.
  • Fast Parallelism & Hardware Fusion: Updating weights sequentially chunk-by-chunk normally kills GPU parallelism. The authors introduce an off-policy approximation—computing chunk activations using the base weights $\mathbf{W}_{\text{down}}^{(0)}$ and accumulating updates via a fast causal parallel scan, enhanced by lightweight causal convolutions and a linear projection. For inference, they also fuse the teacher and student sliding-window attentions into a single custom GPU kernel.

Datasets

No new benchmarks were introduced; instead, the authors tested TTCD across synthetic, pre-training, and continual learning setups:

  • Synthetic Probing: MAD benchmark (testing recall, denoising, compression, memorization, and selective copying).
  • Pre-training & Perplexity: Long-Data-Collections (32K context length) for training from scratch, and Books for sliding-window perplexity evaluation.
  • Continual Pre-training: The 64K-context book subset from ProLong to extend existing models (SmolLM2-360M/1.7B, Qwen3-0.6B/1.7B, and LLaMA-3.1-8B).
  • Downstream & Long-Context Retrieval: RULER (13 retrieval/NIAH tasks up to 64K), BABILong (QA across long contexts), and short-context sanity checks (HellaSwag, ARC-Easy/Challenge, MMLU).

Key Insights and Ideas

  • Subtraction isolates utility: By subtracting the short-window representation from the long-window representation, TTCD naturally filters out local, easily predictable patterns. The fast weights are forced to memorize only remote context that alters future representations.
  • Memory writes and reads align naturally: Mathematically, the gradient update writes the teacher–student discrepancy into $\mathbf{W}_{\text{down}}$ keyed by the student’s MLP activation. Later, even after the original evidence falls out of the sliding window, a related query triggers a similar student activation—replaying that stored long-context insight right when needed.
  • Drop-in architectural continual learning: Because IP-TTCD modifies existing MLP weights via a low-rank residual update, you can bolt it onto off-the-shelf pre-trained Transformers (like LLaMA-3.1-8B) during continual pre-training, teaching frozen architectures how to adapt at test time.
  • Large chunks don't hurt: Increasing the update chunk size from 512 all the way to 2,048 tokens maintains (and slightly improves) perplexity, meaning you get massive parallel efficiency without sacrificing memory quality.

Key Results

Across the board, IP-TTCD outperforms sub-quadratic and TTT rivals (DeltaNet, Gated DeltaNet, Sliding-Window Attention, and IP-TTT), with the gap widening as sequences get longer:

Figure 2: Sliding window perplexity on the Books [17] at increasing context lengths. We show that IP-TTCD consistently achieves lower perplexity than all the TTT-related baselines.
  • Synthetic Memory: On MAD, IP-TTCD hits the highest overall score (82.1 vs. IP-TTT's 78.4 and Gated DeltaNet's 74.4), leading on memory-bottlenecked tasks like Compress (50.0) and Memorize (88.9).
  • From-Scratch Pre-training (760M): Achieves the lowest sliding-window perplexity up to 32K tokens. On RULER Needle-in-a-Haystack (NIAH) at 32K, IP-TTCD more than doubles IP-TTT’s accuracy (21.96% vs. 9.29%) and leads on BABILong at 32K (6.73% vs. 4.46%), while keeping the highest short-context commonsense average (39.90%).
  • Continual Pre-training: Retrofitting IP-TTCD boosts RULER performance over standard continual pre-training (CPT) and IP-TTT—for instance, lifting SmolLM2-1.7B at 64K from 13.38% (CPT) to 19.17%, and LLaMA-3.1-8B at 64K from 25.30% (CPT) to 27.65%.
  • Inference Overhead: With custom fused dual-window kernels on an H100 (FlashAttention-3 stack), IP-TTCD's prefill overhead relative to IP-TTT is just 1.3%–6.6%.

Limitations and Future Work

  • Compute Overhead: Because TTCD computes a second student MLP path and dual-window attention, training is still 1.1$\times$–1.2$\times$ slower than standard Transformer baselines (and naive unfused prefill is up to 1.3$\times$ slower at small windows).
  • Scale of From-Scratch Runs: While continual pre-training was validated up to 8B parameters, pre-training from scratch was capped at 760M parameters due to compute limits. Scaling from-scratch TTCD to multi-billion-parameter frontier models remains an open next step.

Summary updated

Abstract

Effective long-context modeling is not merely about retaining more of the past, but about preserving the information that may prove relevant later. Test-time training (TTT) is an appealing approach that performs online parameter updates for long-context modeling, yet existing TTT methods only optimize either reconstruction or online adaptation objectives without considering the future utility of retained information. In this work, we propose Test-Time Context Distillation (TTCD), a TTT framework that explicitly optimizes the allocation of limited memory capacity for future use. Specifically, TTCD uses a long-window teacher to supervise the fast weights of a short-window student, where the hidden-state discrepancy between them offers a dense, self-supervised signal guiding the model to memorize the contextual information crucial for future tasks. We focus on an in-place variant: In-Place TTCD (IP-TTCD), which uses the existing MLP parameters as the fast weights. Experiments on long-context language modeling tasks show IP-TTCD consistently outperforms DeltaNet, Gated DeltaNet, sliding-window attention, and TTT when pre-trained from scratch. Furthermore, IP-TTCD allows pre-trained transformer models to adapt their parameters during inference through continual pre-training, gaining long-context capabilities with only a lightweight architectural augmentation.
BibTeX
@inproceedings{wang2026learning,
  author = {Zixuan Wang and Xingyu Dang and Rui-Jie Zhu and Zixin Wen and Hengyu Fu and Wenhao Chai and Jason D. Lee},
  title = {{Learning What to Remember: Test-Time Training via Context Distillation}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=JSruMJtXkx},
}
Figure