Long Context  · March 2022

Memorizing Transformers

intermediate

long-context

Extend a transformer's effective context to millions of tokens by adding a non-differentiable kNN memory that retrieves relevant past key/value pairs from outside the standard attention window.

§ 1 · Premise

The 8K ceiling and the routes around it

A 2022-era Transformer decoder at 8K context and 1024-dim heads carries 281921024=162 \cdot 8192 \cdot 1024 = 16M scalar entries per layer in its KV cache — manageable at 32\sim 32 MB per layer fp16 but quadratic in attention compute: T2dh=8.61010T^2 d_h = 8.6 \cdot 10^{10} multiply-adds per layer per head. Doubling TT quadruples it. By the time Wu et al. wrote Memorizing Transformers (arXiv 2203.08913) in early 2022, two routes around the ceiling had been published:

Both modify the model. Wu et al.’s wager is that the simpler primitive is retrieval: keep the regular self-attention mechanism untouched, but at one chosen layer also let each query look up the kk nearest neighbours of its own vector inside a vast external store of past keys/values produced by that same layer. Retrieved (K,V)(K, V) pairs are spliced into the local attention through a learned gate. The store can be billions of entries; it is not backpropagated through. The contribution is the demonstration that a single mid-stack non-differentiable kNN attention substantially improves language-model perplexity at fixed compute, generalises across domains, and scales sub-linearly with memory size.

§ 2 · Derivation

Attention extended by a kNN-retrieved set

Starting point — single-layer attention. Let qt,kt,vtRdh\mathbf{q}_t, \mathbf{k}_t, \mathbf{v}_t \in \mathbb{R}^{d_h} be the query, key, value at position tt for one attention head. Standard causal attention over a local window of length WW is

otlocal=softmax ⁣(qtK[tW:t]dh)V[tW:t].\mathbf{o}^{\text{local}}_t = \mathrm{softmax}\!\left(\frac{\mathbf{q}_t K_{[t-W:t]}^\top}{\sqrt{d_h}}\right) V_{[t-W:t]}.

In Wu et al.’s setup WW matches the Transformer-XL recurrent context size (1024 or 2048 tokens, §3). Anything older than WW has been evicted.

Step 1 — the external memory. At one chosen layer \ell^\star, write every (k,v)(\mathbf{k}, \mathbf{v}) pair produced by that layer into an external memory M\mathcal{M}. The store is indexed by the keys: the index supports approximate-nearest-neighbour (ANN) search using inner product as the similarity. Wu et al. implement this with ScaNN (Guo et al. 2020), which gives sub-linear lookup time even at M=109|\mathcal{M}| = 10^9 entries (§3.1). Memory is per attention head per layer but only at the single layer \ell^\star — not the whole stack.

Step 2 — retrieval at inference and training. At every query position tt in layer \ell^\star, run a top-kk search on the external memory:

Nk(qt)=ANN-TopK ⁣(qt,  {k(m)}mM),Nk=k.\mathcal{N}_k(\mathbf{q}_t) = \mathrm{ANN\text{-}TopK}\!\left(\mathbf{q}_t,\; \{\mathbf{k}^{(m)}\}_{m \in \mathcal{M}}\right), \quad |\mathcal{N}_k| = k.

Wu et al. use k=32k = 32 or k=64k = 64 (Table 1). The returned set {(k(m),v(m))}mNk(qt)\{(\mathbf{k}^{(m)}, \mathbf{v}^{(m)})\}_{m \in \mathcal{N}_k(\mathbf{q}_t)} is treated as constants — no gradient flows back into M\mathcal{M}. From these, compute an external attention output exactly as standard attention:

otext=softmax ⁣(qtKNk(qt)dh)VNk(qt).\mathbf{o}^{\text{ext}}_t = \mathrm{softmax}\!\left(\frac{\mathbf{q}_t K_{\mathcal{N}_k(\mathbf{q}_t)}^\top}{\sqrt{d_h}}\right) V_{\mathcal{N}_k(\mathbf{q}_t)}.

The external softmax is independent of the local softmax — two separate normalizations.

Step 3 — the learned gate. The two outputs otlocal\mathbf{o}^{\text{local}}_t and otext\mathbf{o}^{\text{ext}}_t are combined via a per-head learnable scalar gate gh(0,1)g_h \in (0, 1) (Eq. 1, §3.2):

ot=(1σ(gh))otlocal+σ(gh)otext,\mathbf{o}_t = (1 - \sigma(g_h)) \cdot \mathbf{o}^{\text{local}}_t + \sigma(g_h) \cdot \mathbf{o}^{\text{ext}}_t,

where σ\sigma is the sigmoid and ghg_h is a single scalar trained alongside everything else. Wu et al. choose the gate-then-mix form (rather than concatenating KlocalK^{\text{local}} and KextK^{\text{ext}} into one softmax) for two reasons:

  1. Scale incompatibility. External keys come from arbitrary past contexts and may have very different norms from local keys; sharing a softmax would let one set dominate the other in a way that’s hard to control.
  2. Training-time stability. The gate starts near σ(gh)0.05\sigma(g_h) \approx 0.05 so external attention is off-by-default; the model learns to open the gate only when retrieval helps. Sharing a softmax would couple the gradient signal of “did retrieval help?” to “did the right key happen to be retrieved?” — Wu et al. report training instabilities under the shared form (§3.2 footnote).

Step 4 — non-differentiability and what it costs. The discrete top-kk step in Nk(qt)\mathcal{N}_k(\mathbf{q}_t) is non-differentiable. Gradients flow:

The trade is explicit: cheap memory (no autograd buffers), unbounded in size, at the price of losing the ability to learn the indexing function itself.

Step 5 — memory layer placement. Why only one layer? Two reasons. Memory cost is O(M2dh)O(|\mathcal{M}| \cdot 2 d_h) scalars; replicating across all LL layers multiplies that by LL for negligible benefit (the layer-wise ablation in Table 5 shows the 9th layer of 12 captures most of the gain). And the model only needs to learn one gate; multiple memory layers would compete for the same “open the gate when retrieval is useful” signal during early training.

Cost accounting. Per query in the memory layer, with local window WW and retrieval budget kk:

FLOPs(W+k)dh  +  TANN(qt,M).\mathrm{FLOPs} \approx (W + k) \cdot d_h \;+\; T_{\mathrm{ANN}}(\mathbf{q}_t, |\mathcal{M}|).

With W=1024,k=32W = 1024, k = 32 the dense attention overhead is 3%\sim 3\% on top of the local-window attention. The ANN lookup cost depends on the index but scales as O(M)O(\sqrt{|\mathcal{M}|}) or better for ScaNN. Memory footprint: M=65|\mathcal{M}| = 65K entries at dh=128d_h = 128 in fp32 is 64\sim 64 MB per head per layer — cheap; M=262|\mathcal{M}| = 262K is 256\sim 256 MB, etc.

Parameter count. One learnable scalar gate per head per memory layer; negligible vs. the base model.

Memory bank of N (key, value) pairs shown as 2D points. The query (blue cross at the centre, draggable via the X/Y sliders) retrieves the K nearest neighbours (filled dots), which are then merged into the current attention pass.Memory bank (N = 80 stored K, V pairs)queryRetrieved 8 / 80 keys for this query — attention runs only over these plus the local window.Effective reach = window + retrieved memory tokens, regardless of memory size N.
The memory bank holds K, V pairs from past attention computations as a non-differentiable external store. For each query, a kNN index returns the top-K nearest historical keys — which the current layer then merges into its standard attention. Total cost per token is constant in memory size N (kNN is O(log N) with FAISS-style indexes), so the effective receptive field grows without growing the per-step compute.

§ 3 · Reference implementation

kNN-augmented attention layer in pseudocode

# State:
#   memory_index: an ANN store keyed by past K^(l*), holding (K, V) at the memory layer
#   gate: learnable scalar per head, init so sigmoid(gate) ~= 0.05

def memorizing_attn(q, k_local, v_local, memory_index, gate, k_retrieve=32):
    # q, k_local, v_local: [B, T, H, d_h]
    # Local attention over the in-window window
    o_local = scaled_dot_product_attention(q, k_local, v_local)        # [B, T, H, d_h]

    # External retrieval: top-k nearest keys for each query (no gradient through index)
    with torch.no_grad():
        retrieved_k, retrieved_v = memory_index.ann_search(q, k_retrieve)
        # retrieved_*: [B, T, H, k_retrieve, d_h], treated as constants

    # External softmax over only the retrieved set, independent normalization
    scores = einsum("bthd,bthkd->bthk", q, retrieved_k) / sqrt(d_h)
    weights = softmax(scores, dim=-1)
    o_ext = einsum("bthk,bthkd->bthd", weights, retrieved_v)

    g = sigmoid(gate)[None, None, :, None]                              # [1, 1, H, 1]
    o = (1 - g) * o_local + g * o_ext

    # Add this chunk's local (K, V) to the memory for future queries
    memory_index.add(k_local.detach(), v_local.detach())
    return o

The sketch elides three production concerns: (1) the ANN index rebuild cadence — Wu et al. periodically re-fit the ScaNN partitioning every NN tokens written; (2) per-document memory isolation (each document’s memory is private to avoid cross-document leakage at training time, §3.3); (3) the rotary or relative-position handling for retrieved keys, which lacks a natural “distance” to the current query — Wu et al. simply omit position encoding for the retrieved set.

§ 4 · Empirical evidence

What is and isn’t known

Introducing paper (Wu et al. 2022).

  1. Perplexity gains across domains. On four long-document datasets — arXiv math, GitHub code, PG-19 books, C4 web — a 200M-parameter Memorizing Transformer with M=65,536|\mathcal{M}| = 65{,}536 entries lowers test perplexity by 1.5–2.7 nats vs. a matched non-memory baseline (Table 1). The largest gains come on arXiv math, where citation and notation reuse make retrieval particularly useful.
  2. Memory-size scaling. The same architecture’s perplexity continues to drop as M|\mathcal{M}| grows from 1,5361{,}536 to 262,144262{,}144 entries (Figure 1) — i.e., the model genuinely uses the bigger memory and is not just absorbing a fixed amount of recent context.
  3. Parameter-count tradeoff. At fixed compute, adding kNN memory matches the gain from roughly 5×5\times more parameters; e.g., a 200M memory model rivals an 800M no-memory baseline on arXiv (Table 2).
  4. Layer-placement ablation. The 9th layer of 12 gives the largest gain; adding memory to every layer adds at most 0.1 nat on top (Table 5).
  5. Generalization across model sizes. The technique was retested at 1B parameters with a 1\sim 1 nat perplexity gain that holds at scale (§4.2).

Independent follow-up.

Sensitivity studies — what is not publicly known. The introducing paper sweeps memory size and layer placement but not (a) the gate’s initialization or the gate-vs.-shared-softmax tradeoff in detail, (b) the choice of inner product vs. cosine vs. learned similarity inside the ANN, or (c) how retrieval quality interacts with RoPE / ALiBi position encodings (the paper uses T5-style relative bias). I don’t know of an independent reproduction at scales >1> 1B parameters; the published results stop there.

Production adoption. None recorded in this knowledge base. Memorizing Transformers’ practical legacy is the broader retrieval-augmented family (RAG-style retrieval into the prompt, KV-cache compression with retrieval gates) rather than the specific “kNN-from-attention-keys at one layer” mechanism, which was largely superseded by RAG architectures that retrieve documents into the input rather than (K,V)(K, V) pairs into the attention.

Cite

BibTeX entry for the original paper
@article{arxiv2203_08913,
  title  = {Memorizing Transformers},
  author = {Yuhuai Wu, Markus N. Rabe, DeLesley Hutchins, Christian Szegedy},
  year   = {2022},
  eprint = {2203.08913},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/2203.08913}
}

Or cite the paper directly: arXiv:2203.08913.

Export

BibTeX
@article{arxiv_2203_08913,
  title         = {Memorizing Transformers},
  author        = {Yuhuai Wu and Markus N. Rabe and DeLesley Hutchins and Christian Szegedy},
  year          = {2022},
  eprint        = {2203.08913},
  archivePrefix = {arXiv},
  url           = {https://arxiv.org/abs/2203.08913}
}
CSL JSON
{
  "id": "arxiv_2203_08913",
  "type": "article-journal",
  "title": "Memorizing Transformers",
  "author": [
    {
      "literal": "Yuhuai Wu"
    },
    {
      "literal": "Markus N. Rabe"
    },
    {
      "literal": "DeLesley Hutchins"
    },
    {
      "literal": "Christian Szegedy"
    }
  ],
  "issued": {
    "date-parts": [
      [
        2022
      ]
    ]
  },
  "URL": "https://arxiv.org/abs/2203.08913",
  "number": "2203.08913",
  "source": "arXiv"
}
RIS
TY  - JOUR
TI  - Memorizing Transformers
AU  - Yuhuai Wu
AU  - Markus N. Rabe
AU  - DeLesley Hutchins
AU  - Christian Szegedy
PY  - 2022
JO  - arXiv
AN  - arXiv:2203.08913
UR  - https://arxiv.org/abs/2203.08913
ER  -