GO TO A SECTION

↑ ↓ to navigate · Enter to openExplore all sections →
FDEInterviews logo
ICML 2022

Improving language models by retrieving from trillions of tokens

RETRO retrieves from a 2-trillion-token database for every 64-token chunk and rivals models about 25 times its size. Some of its most striking scores come from test text already in the database.

Breakdown by FDE Interviews · 12 min read · Updated

About the original · 2021
AUTHORS
Borgeaud, Mensch, Hoffmann, Cai et al.
FROM
DeepMind
PUBLISHED
Dec 2021 · ICML 2022
CITED BY
2,034Semantic Scholar · Nov 2026
IN THIS BREAKDOWN · 10 SECTIONS

You can read 23% of this breakdown.

01

The paper in one minute

RETRO (Retrieval-Enhanced Transformer) adds a lookup step to an autoregressive language model. The input is split into chunks of 64 tokens. For each chunk, the model retrieves its k nearest neighbors from a database, where each entry is a 64-token chunk plus the 64 tokens that followed it in the original document. Similarity is the distance between frozen BERT embeddings of the chunks, searched approximately with ScaNN, which the authors say takes about 10 ms over a 2-trillion-token database.

The retrieved text goes through a small transformer encoder, and the main model attends to it with chunked cross-attention: tokens in chunk u+1 can attend to the neighbors retrieved for chunk u, which keeps the model autoregressive. Cross-attention layers are inserted every third block. Because BERT is frozen, all neighbors for the training data can be computed in advance.

Training uses DeepMind's multilingual MassiveText data; during training the model retrieves from a 600-billion-token subset, and at evaluation from 1.75 trillion tokens. Models range from 172M to 7.5B parameters, about 8% to 30% more than matching baselines.

The gains are large and consistent. Retrieval improves every dataset in the main scaling comparison at every model size, by roughly what a 10 times larger model would give, and keeps improving as the database grows and as more neighbors are retrieved (up to about 40). On the Pile, the 7.5B RETRO beats Jurassic-1 (178B) and Gopher (280B) on most subsets. An existing transformer can be "retrofitted" by training only the new cross-attention and encoder weights on 3% of the original data.

The authors also take test leakage seriously. Because the model can copy from its database, they measure how much evaluation text overlaps database chunks and show results as a function of that overlap. Retrieval helps even on chunks with little overlap, but much of the most dramatic result, on Wikitext103, comes from overlap.

THE IDEA, VISUALLY
CHUNK-LEVEL RETRIEVAL (SECTION 2) chunk 1 chunk 2 chunk 3 chunk 4 ... 64 tokens each, 32 per sequence frozen BERT key averaged over the chunk ScaNN search ~10 ms, 2T tokens k neighbors, each [N, F]: 64-token chunk + its continuation encoder 2 layers chunked cross-attention chunk 3 attends to chunk 2's neighbors One key per 64-token chunk: 56 bytes stored per database token against 4,096 for token-level kNN-LM (our calculation). Neighbors from the same document are filtered in training. Gains resemble a 10x larger model from 172M to 7.5B params. arXiv 2112.04426, DeepMind WIKITEXT103 TEST PERPLEXITY BY DATABASE (TABLE 4) baseline, no retrieval 22.96 (4.52 bits) kNN-LM, Wikipedia 4B 19.54 (4.29 bits) RETRO, Wikipedia 4B 18.97 (4.25 bits) RETRO, MassiveText 1% 20.33 (4.35 bits) RETRO, MassiveText 10% 14.95 (3.90 bits) RETRO, C4 174B 10.23 (3.35 bits) RETRO, MassiveText 1.8T 3.92 (1.97 bits) Lower is better. The full-database figure is 'partly due to partial overlap with Wikitext103 not caught by our deduplication'. RETRO still beats baselines on chunks that share at most 8 tokens with the database (Figure 6).
Left: RETRO's retrieval path. Each 64-token chunk of the input is embedded with a frozen BERT; its nearest neighbors in the database, each a 64-token chunk plus its 64-token continuation, are encoded and attended to by the next chunk through chunked cross-attention. Right: Wikitext103 test perplexity by retrieval database (Table 4). Most of the drop to 3.92 with the full database reflects test text that deduplication missed, as the table's note says.
Research notes: the problem, approach and results
~10x

parameter increase that retrieval's gain resembles, roughly constant from 172M to 7.5B parameters (Figure 1)

22.96 -> 3.92

Wikitext103 test perplexity, baseline vs RETRO with the full database; 'partly due to' overlap deduplication missed (Table 4)

73x

fewer bytes stored per database token than token-level kNN-LM, because one key covers a 64-token chunk (our calculation)

THE PROBLEM

Language models get better by growing, which raises training and serving cost, and their knowledge is fixed at training time. Can a smaller model get the same quality by looking things up in a very large text database while it predicts?

THE IDEA

Split the input into 64-token chunks. For each chunk, retrieve similar chunks and their continuations from a 2-trillion-token database using frozen BERT embeddings and approximate nearest-neighbor search, and let the model attend to them through a chunked cross-attention layer built into the transformer.

THE RESULT

A 7.5B-parameter RETRO model matched or beat 178B and 280B models on most Pile subsets, and retrieval gave gains similar to a 10 times larger model across sizes from 172M to 7.5B. Gains grew with database size. Much of the dramatic Wikitext103 result came from evaluation text present in the database.

02

Why it matters to an FDE

Retrieval can stand in for model size. The central result is that a small model with a large, relevant database can match much larger models on knowledge-heavy text. For customers, this is the argument for retrieval-augmented systems over ever-larger models: knowledge lives in a database that can be updated, filtered and audited.

Index at the chunk level. RETRO stores one key per 64-token chunk, not per token. That is why it could index trillions of tokens; token-level kNN-LM would need about 4 KB per token. The same arithmetic applies to customer vector indexes: chunk size sets the index size, the retrieval granularity and the cost.

Check evaluation sets against the index. A retrieval model can copy evaluation text if it is in the database. RETRO's Wikitext103 perplexity drops from 22.96 to 3.92 with the full database, and the authors attribute this partly to overlap that their deduplication missed. When a customer's RAG system scores very well on a test set, check whether the test documents, or near copies, are in the index.

Measure with a leakage filter. The paper's method is reusable: for each evaluation chunk, find the longest run of tokens it shares with its retrieved neighbors, and report results separately for chunks with low overlap. That separates "found the answer" from "found the test".

Know what RETRO is not. It is retrieval built into pretraining, with a custom architecture. Most production RAG puts retrieved text into the prompt of an off-the-shelf model. RETRO's lessons transfer (scaling the database, chunk granularity, leakage), the architecture usually does not.

THE FULL BREAKDOWN

Understand the mechanism. Try it on a real problem.

Unlock this breakdown and 290 more papers.

Premium opens every paper in full, plus every answer, concept, course and guide on the site. One payment for six months of access.

6 months · One payment · No auto-renewal

Get Premium · ₹2,000 / $25
  • ◇1 architecture diagram
  • </>1 runnable code experiment
  • ▦2 results tables
  • ?5 interview questions with answers
  1. 03Key takeaways

    Architecture, database, scaling, retrofitting, question answering, leakage, two misreadings

    Free account
  2. 04How it works

    Database layout, retrieval, the encoder and cross-attention, training data, and the leakage metric

    Premium
  3. 05The numbers that matter

    Wikitext103 by database, storage, Pile comparisons, QA scores, retrofitting and model sizes

    Premium
  4. 06Try it yourself

    Check index arithmetic, read Table 4 in bits per token, run the overlap filter, check ratios

    Premium
  5. 07Where it breaks

    Leakage in headline numbers, custom architecture, private data, QA behind FiD, filter gaps

    Premium
  6. 08Using it on a deployment

    Database scale, chunk granularity, leakage checks for RAG evaluations, updatable knowledge

    Premium
  7. 09Interview angles

    Five questions this paper prepares you for, with answer hints

    Premium
Back to the paper library →