TL;DR
Language models store facts in their weights, but those facts are hard to inspect, hard to update and easy to get wrong. Lewis et al. add a second memory: a dense index of 21 million Wikipedia passages, searched by a retriever. For each question, the model retrieves a few passages, and a generator writes the answer conditioned on them. Training is end to end, so the retriever learns which passages help the generator.
On open-domain question answering the resulting RAG-Sequence model scores 44.5 exact match on Natural Questions, above the DPR reader's 41.5 and an 11-billion-parameter closed-book T5 at 34.5.
Where you'll see it: almost every "chat with your documents" system, which pairs a search index with a language model in the same pattern.
The problem
By 2020 a large pretrained model could answer trivia questions from its parameters alone. Those answers were limited in three ways that the paper names. The model cannot easily expand or revise what it knows, because knowledge is smeared across billions of weights. It cannot easily show where an answer came from, so provenance is missing. And it sometimes produces fluent wrong answers, which the field calls hallucination.
Earlier systems had a memory you could inspect. Open-domain question answering systems such as DrQA and DPR retrieved passages and then extracted an answer span from one of them. Those systems were good at extraction but could not write free-form answers, and they could not combine clues spread across several passages. The gap the paper targets is the combination: a generator that can write, grounded in a memory that can be searched and changed.
The paper is also a test of whether such a hybrid can be built as a general recipe. It covers several tasks with different output shapes, from short answers and long generated text to a three-way classification label, and asks one architecture to handle all of them.
Key idea
Treat the retrieved passage as a hidden variable. The question is the input, the answer is the output, and the passage that produced the answer is never labelled. Instead of choosing one passage, the model averages over a set of likely passages, weighting each by how well it matches the question and how well it supports the answer.
Two models follow from where that average is taken. RAG-Sequence uses one passage for the whole answer. RAG-Token lets each output token draw on a different passage. Both use the same two parts: a retriever that gives a probability to each passage, and a generator that gives a probability to each output token given the question, the passage and the words written so far.
flowchart LR Q[Question x] --> R[Retriever p eta] R --> P[Top k passages z] P --> G[Generator p theta] Q --> G G --> Y[Answer y] I[(Wikipedia index, 21M passages)] --> R
The method
Latent documents and two marginals
Let be the input, the target sequence, and a retrieved passage. The retriever defines , and the generator defines . The paper approximates the sum over all passages by a sum over the top retrieved passages.
For RAG-Sequence the same passage is used for every token, so the passage is marginalised outside the product over tokens:
For RAG-Token the marginal is taken inside the product, so each token can use a different passage:
The difference matters. Inside the product, the sum over passages can change at each token. Outside it, one passage has to support the whole answer. A worked example below shows that the two formulas give different numbers for the same probabilities.
The retriever
The retriever is DPR, a bi-encoder (see bi-encoder). A BERT-base document encoder embeds each passage once, and a BERT-base query encoder embeds the question. The score is a dot product, . Because the document vectors do not depend on the question, they can be precomputed and stored in an index. Choosing the top passages then becomes maximum inner product search, which can be approximated quickly with a tool such as FAISS (see maximum inner product search).
The paper uses the December 2018 Wikipedia dump, split into disjoint 100-word passages, for 21 million passages in total. Each passage is stored as one dense vector. The appendix says the index takes about 100 GB of CPU memory in the original code, reduced to 36 GB with FAISS compression in the later HuggingFace port.
The generator
The generator is BART-large, a pretrained encoder-decoder transformer (see encoder-decoder architecture and Transformer) with about 400 million parameters. The question and the retrieved passage are concatenated and fed to the encoder. BART was pretrained to reconstruct text from corrupted versions, which the authors found gives strong generation results.
The authors call the BART weights the parametric memory and the index the non-parametric memory. The name captures the split: facts the generator has learned live in the weights, and facts in the index live outside them and can be swapped.
Training
The retriever and generator are trained together on pairs by minimising the negative marginal log-likelihood . This is a maximum likelihood objective with a latent variable (the passage), and the optimiser is Adam. Gradients flow back into the query encoder and the generator. Nothing tells the retriever which passage to find; it learns from whether the answer becomes more likely when the passage is included.
The document encoder and the index are kept fixed. Re-encoding all 21 million passages after each update would be costly, and the authors report that strong results do not need it. Only the query encoder and the generator are fine-tuned. This is an instance of fine-tuning a pretrained model on a new task with a small learning rate.
Decoding
RAG-Token is an ordinary autoregressive model once the marginal is written as a per-token transition probability, so beam search applies directly. RAG-Sequence has no per-token form, so the authors run beam search separately for each passage. For "thorough decoding" they then add a forward pass for every passage in which a candidate answer did not appear in the beam, and sum. For open-domain QA the appendix reports using thorough decoding with 50 retrieved passages for RAG-Sequence, since answers are short; for MS-MARCO and Jeopardy it uses fast decoding, because thorough decoding did not improve results.
Results
Open-domain question answering
The main open-domain table reports exact match per dataset. This lesson focuses on the Natural Questions (NQ) column:
| Model | NQ exact match |
|---|---|
| T5-11B (closed book) | 34.5 |
| T5-11B + SSM (closed book) | 36.6 |
| REALM (open book) | 40.4 |
| DPR (open book) | 41.5 |
| RAG-Token | 44.1 |
| RAG-Sequence | 44.5 |
The closed-book models store everything in their weights. The open-book models retrieve passages. The gap between DPR's 41.5 and RAG-Sequence's 44.5 is the gain from training the generator on top of retrieved passages, with the retriever initialised from DPR.
The paper also reports that RAG can produce correct answers when none of the retrieved passages contains the answer verbatim, at 11.8 percent accuracy on NQ. An extractive model would score zero on those questions by construction.
Other tasks
On the MS-MARCO natural language generation task, RAG-Sequence beats a BART baseline by 2.6 BLEU and 2.6 ROUGE-L points, according to the paper's text. On Jeopardy question generation, RAG-Token and RAG-Sequence both beat BART on Q-BLEU-1. On FEVER fact verification, RAG is within 4.3 points of the best three-way system, which uses intermediate retrieval supervision that RAG does not have, and within 2.7 points of a two-way model given gold evidence.
Ablations and updating knowledge
Freezing the retriever (training only the generator) lowers NQ exact match from 44.0 to 41.2 for RAG-Sequence and from 43.5 to 37.8 for RAG-Token, according to the ablation table. Replacing the dense retriever with BM25 gives 29.7 on NQ for RAG-Token and 31.8 for RAG-Sequence, well below the dense retriever.
To test updating, the authors built an index from a December 2016 Wikipedia dump and compared it with the 2018 index on 82 world leaders whose office changed between the two dates. The NQ model answered 70 percent correctly with the 2016 index for 2016 leaders and 68 percent with the 2018 index for 2018 leaders. With mismatched indices, accuracy fell to 12 and 4 percent. Changing the index changed the answers, with no retraining.
Why it mattered
The paper turned a loose idea into a recipe. A search index, a dense retriever, a pretrained seq2seq generator and a marginal likelihood objective became one trainable system, and the authors released code. Later retrieval-augmented systems mostly keep this skeleton: retrieve, condition, generate, and train with the retrieved passages treated as hidden.
The paper also made two claims that became standard. First, a model's knowledge can be updated by changing the index, not the weights. Second, a model can be made more checkable because each answer is tied to passages it could be asked to show. Both claims are argued for in the paper, but they are not guarantees: retrieval can return wrong passages, and a generator can still ignore or misread them.
Reading guide
Read the paper in this order and use these lessons as preparation.
- Section 1, Introduction. Needs retrieval-augmented generation for the idea of an external memory, and latent variable models for why the passage is not observed.
- Section 2.1, Models. Needs latent variable models and maximum likelihood estimation. The two marginal formulas are the core of the paper, so read the derivation twice.
- Section 2.2, Retriever (DPR). Needs bi-encoders and maximum inner product search.
- Section 2.3, Generator (BART). Needs encoder-decoder architectures and transformers. BERT helps for the query and document encoders.
- Section 2.4, Training. Needs fine-tuning and Adam.
- Section 2.5, Decoding. Needs beam search.
- Sections 3 and 4, Experiments and Results. Read the dataset descriptions for the task you care about. Only the results need no new prerequisites.
- Section 5, Related Work. Helpful if you want the lineage to REALM and earlier retrieval-based question answering.
Check yourself
QuestionRecall: in one sentence, what is the non-parametric memory in RAG and what is the parametric memory?Show answer
The non-parametric memory is the dense index of Wikipedia passages, which can be changed without retraining. The parametric memory is the BART generator's weights, which store what the model has learned during pretraining and fine-tuning.
QuestionComputation (illustrative numbers, not from the paper): two passages have retriever probabilities 0.7 and 0.3. For a two-token answer, passage 1 gives per-token probabilities 0.5 and 0.2, and passage 2 gives 0.1 and 0.9. What is the RAG-Sequence probability, and what is the RAG-Token probability?Show answer
RAG-Sequence: sum over passages of the retriever weight times the product of token probabilities. That is 0.7 times (0.5 times 0.2) plus 0.3 times (0.1 times 0.9), which is 0.07 plus 0.027, giving 0.097. RAG-Token: for each token, average over passages first. Token one gives 0.7 times 0.5 plus 0.3 times 0.1, which is 0.38. Token two gives 0.7 times 0.2 plus 0.3 times 0.9, which is 0.41. The product is 0.38 times 0.41, which is about 0.156. The token model is larger here because it can use passage 1 for one token and passage 2 for the other.
QuestionWhy: why does the paper keep the document encoder and the index fixed during training instead of fine-tuning them end to end?Show answer
Changing the document encoder changes every passage vector, so the whole index would need to be rebuilt after each update. Re-encoding 21 million passages is expensive. The authors report that fixing the document encoder does not hurt results much, so only the query encoder and generator are trained. The retriever still learns to pick useful passages by changing the query side.
QuestionApplied: a team uses a RAG system and wants a fact updated after the index was built. Which part do they change, and what can go wrong?Show answer
They add or replace the passages in the index, re-encoding those passages with the same document encoder. They do not retrain the generator. What can go wrong is that the retriever must still rank the new passage highly for relevant queries, and the generator must still read it. The paper's index-swap test shows the effect works when the index matches the question's time frame; a mismatched index dropped accuracy sharply.
Sources
- Lewis, P. et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. arXiv:2005.11401
- Karpukhin, V. et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. arXiv:2004.04906
- Lewis, M. et al. (2019). BART: Denoising Sequence-to-Sequence Pre-training. arXiv:1910.13461