Skip to main content
QUICK REVIEW

[Paper Review] Memorizing Transformers

Yuhuai Wu, Markus N. Rabe|arXiv (Cornell University)|Mar 16, 2022
Topic ModelingComputer Science39 citations
TL;DR

This paper augments decoder-only Transformers with a large, non-differentiable external memory accessed via approximate kNN to memorize and retrieve past inputs, improving language modeling across diverse long-context datasets.

ABSTRACT

Language models typically need to be trained or finetuned in order to acquire new knowledge, which involves updating their weights. We instead envision language models that can simply read and memorize new data at inference time, thus acquiring new knowledge immediately. In this work, we extend language models with the ability to memorize the internal representations of past inputs. We demonstrate that an approximate kNN lookup into a non-differentiable memory of recent (key, value) pairs improves language modeling across various benchmarks and tasks, including generic webtext (C4), math papers (arXiv), books (PG-19), code (Github), as well as formal theorems (Isabelle). We show that the performance steadily improves when we increase the size of memory up to 262K tokens. On benchmarks including code and mathematics, we find that the model is capable of making use of newly defined functions and theorems during test time.

Motivation & Objective

  • Motivate extending language models to acquire new knowledge by memorizing past inputs at inference time rather than updating weights.
  • Propose a simple, scalable kNN-augmented attention mechanism that retrieves exact memories from an external store.
  • Demonstrate that memory size improves perplexity across multiple long-context domains and model scales.

Proposed method

  • Add a non-differentiable external memory of (key, value) pairs that is updated with model-generated keys/values from previous steps.
  • In one attention layer, perform an approximate kNN lookup into memory for each query, and combine memory-attention with local dense attention via a learned gate.
  • Use Transformer-XL style caching for ongoing context and a sliding causal mask to maintain local context.
  • Normalize keys and queries to mitigate staleness due to distributional shift between training steps and memory contents.
  • Employ approximate kNN implementations to balance retrieval quality and computational efficiency, enabling memory sizes up to tens of thousands of tokens.

Experimental results

Research questions

  • RQ1Does a large external memory accessed via kNN enhance perplexity on long-form text and code datasets?
  • RQ2How does memory size affect performance, and is there a point of diminishing returns?
  • RQ3Can pre-trained models effectively utilize external memory through fine-tuning, or is training from scratch required?
  • RQ4What patterns do models exhibit when retrieving from memory (e.g., function names, definitions, lemmas)?

Key findings

  • External memory consistently improves perplexity across long-form datasets and architectures (e.g., long web text, books, code, and formal proofs).
  • Increasing memory size yields continued performance gains up to very large memory scales, with notable benefits for code and mathematics tasks.
  • A gating mechanism biases heads to prefer external memory, enabling effective long-range retrieval without differentiable memory gradients.
  • Models can leverage memory to recall definitions, lemmas, and function names, demonstrating retrieval-like behavior for formal proofs and code.
  • Fine-tuning pretrained models to utilize memory can rapidly close the gap to memorized training regimes, indicating practical applicability.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.