TL;DR
Researchers at Google have developed the Memorizing Transformer, a model that can "read and memorize" new data at inference time without needing to update its weights. By using a massive, non-differentiable external memory and approximate $k$-nearest-neighbor ($k$NN) lookups, the model can attend to up to 262,000 tokens. This allows it to recall specific definitions, functions, and facts from the distant past of a document or codebase, effectively giving the model a long-term memory.
What Problem Does This Paper Address?
Standard Transformers are remarkably good at understanding language, but they suffer from a "short-term memory" problem. The self-attention mechanism, which allows the model to relate different parts of a sequence, has a computational cost that grows quadratically with the sequence length. Consequently, most models are limited to a context window of a few thousand tokens.
This is a major hurdle for tasks involving long-form content. If you are reading a 500-page novel, a complex software repository, or a mathematical proof, important information (like a character's name, a function definition, or a lemma) might have appeared tens of thousands of tokens ago. To "learn" this new information, traditional models usually require expensive retraining or fine-tuning. The authors wanted a model that could acquire and use new knowledge immediately, simply by "reading" it.
Methods
The core innovation is the $k$NN-augmented attention layer.
Here is how the architecture works in practice:
- External Memory: The model maintains a massive cache of (key, value) pairs representing previously processed subsequences.
- Non-Differentiable Storage: Crucially, gradients are not backpropagated into this external memory. This "frozen" nature allows the memory to scale to hundreds of thousands of tokens because the model doesn't need to recompute old keys and values every time the parameters change.
- Approximate $k$NN Lookup: Instead of attending to every single token in the history (which would be too slow), the model performs a fast $k$-nearest-neighbor search to find the top-$k$ (e.g., $k=32$) most relevant memories for a given query.
- Gated Combination: A learned "gate" allows the model to balance information from its immediate local context (the last 512 tokens) and the retrieved long-term memories.
- Handling "Staleness": Because the model's parameters change during training, older keys in the memory might become "stale." The authors use key/query normalization to keep the representations stable and comparable over time.
Datasets
The authors evaluated the model on five diverse long-form datasets:
- PG-19: A collection of English-language books published before 1919.
- C4 (4K+): A "colossal" web crawl, filtered for long-form articles.
- arXiv Math: Technical papers in mathematics typeset in LaTeX.
- Github: A large corpus of source code repositories (C, C++, Java, Python, etc.).
- Isabelle: Formal mathematical proofs. This is particularly challenging as proofs rely heavily on previously defined lemmas.
Key Insights and Ideas
- Scaling Parameters vs. Scaling Memory:
One of the most striking findings is that a smaller model (200M parameters) with an 8K token memory can match the performance (perplexity) of a vanilla Transformer with 5x more parameters (1B). This suggests that for long-context tasks, memory is a more efficient way to improve performance than simply making the model bigger.
- Retrieval as Rapid Learning: The paper frames attention as a form of information retrieval. By storing function definitions or math lemmas as (key, value) pairs, the model effectively "memorizes" them and can look them up later, mimicking the way a human might flip back a few pages in a book.
- Generalization to Larger Memories: Models trained with a small memory (e.g., 8K tokens) actually show continued performance gains when given a much larger memory (up to 262K) at test time.
- The "Aha" Moment in Math/Code: Qualitative analysis showed the model specifically using the memory to look up rare words, citation names, and—most impressively—the exact bodies of functions and mathematical definitions defined much earlier in the text.
Key Results
- Perplexity Gains: Across all five datasets, adding external memory significantly reduced perplexity (a measure of how well the model predicts the next token). On the arXiv Math dataset, increasing memory from "None" to 65K tokens dropped perplexity from 3.29 to 2.26.
- Efficiency: The $k$NN lookup adds minimal overhead. Adding an 8K memory to a 200M parameter model only increased the step time from 0.2s to 0.25s.
- Fine-tuning Success: The authors demonstrated that you don't have to train from scratch. You can take a pre-trained "vanilla" Transformer and fine-tune it to use external memory in a fraction of the original training time, closing 85% of the performance gap within just 20K steps.
Limitations and Future Work
- Distributional Shift: While normalization helps, the "staleness" of old memories remains a challenge during long training runs as the model's internal representations evolve.
- Training Instability: The authors noted that training very large memories from scratch can sometimes be unstable, leading them to prefer a "pre-train small, fine-tune large" strategy.
- Future Directions: The authors suggest that this architecture could eventually be used to leverage entire "knowledge bases" or massive code repositories at inference time, potentially leading to models that can act as expert assistants with access to vast amounts of specific, technical documentation.
Summary updated