VerifiedarXiv:2005.1140127 min
Retrieval · LLMs

RAG: Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks

Let a language model look up Wikipedia passages before it answers.

RAG connects a neural retriever over 21 million Wikipedia passages to a BART generator. It adds up the answer's probability over the passages it retrieved, and it trains both parts from question-answer pairs alone.

Explaining the paperRetrieval-Augmented Generation for Knowledge-Intensive NLP TasksLewis, Perez, Piktus, et al. · Facebook AI Research · NeurIPS 2020 · arXiv:2005.11401 ↗

Nobody tells the retriever which passage was right. It learns from how much each passage raised the probability of the correct answer, and the facts stay in an index that can be swapped without retraining.

A language model trained on text stores facts in its weights. Ask a model with no access to anything else who wrote The Old Man and the Sea and the answer has to come out of those weights. There is no way to see which part of the network supplied it, no way to correct it short of more training, and when the fact was never learned well the model still produces a fluent answer, sometimes an invented one.

Retrieval-Augmented Generation (RAG), from Facebook AI Research, UCL and NYU in 2020, gives the model a second store of facts that it reads at answer time. Before writing, a retriever searches 21 million Wikipedia passages and returns the few most relevant. A generator then writes the answer once per retrieved passage, and the model adds those answers up, weighted by how relevant the retriever judged each passage. Both parts are fine-tuned together, and the training data holds only questions and answers.

The page goes through the pieces in order: the two memories, the pipeline and its shapes, how retrieval is scored, why the model sums over passages instead of picking one, the two ways to place that sum (RAG-Sequence and RAG-Token), how the retriever learns without being told which passage was right, decoding, the results, and the experiment that swaps the index. One running example carries through: the question "Who wrote The Old Man and the Sea?" with five retrieved passages.

Two kinds of memory

The paper calls knowledge held in a network's weights parametric memory. Large pre-trained models hold a lot of it. In the closed-book setting, a model answers questions from its weights alone, and the largest one the paper compares against is T5-11B, with 11 billion parameters. Parametric memory has three problems the paper names in its introduction: it cannot easily be expanded or revised, it gives no insight into which knowledge produced a prediction, and it can produce hallucinations, fluent statements that are false.

Non-parametric memory is knowledge held as data outside the weights, here a collection of text passages the model searches. Facts in it can be read, cited, added and deleted like rows in a database. The paper's target is knowledge-intensive tasks, which it defines as tasks a human could not reasonably be expected to do without access to an external knowledge source: open-domain question answering, fact verification, trivia-style question writing.

Retrieval-plus-reading systems existed before RAG. REALM and ORQA had combined a masked language model with a differentiable retriever, but only for extractive question answering, where the answer must be a span of characters copied from a retrieved passage. RAG attaches the retriever to a sequence-to-sequence generator, a model that reads an input text and writes an output text token by token. A generator can write an answer that appears in no passage, write a whole sentence, or output a class label, so one recipe covers question answering, question generation and fact verification.

Retriever, index, generator

Three pieces make up the system. The question xx goes into the retriever, which scores passages zz from the document index and returns the top KK. The generator reads the question together with one retrieved passage at a time and scores or writes the output yy. The paper writes the two learned parts as probability distributions: the retriever is pη(z∣x)p_\eta(z \mid x), the probability that passage zz is the one to read, with parameters η\eta; the generator is pθ(yi∣x,z,y1:i−1)p_\theta(y_i \mid x, z, y_{1:i-1}), the probability of the next output token given the question, the passage, and the tokens written so far, with parameters θ\theta.

Figure 1 · the RAG pipeline
A question x is embedded by the query encoder; a Maximum Inner Product Search over a frozen 21M-passage index lights up the top-K passages; those passages plus the question are read by the BART generator, whose per-document predictions are combined into one answer y. The retriever pη(z|x) and generator pθ(y|x,z) are the only learned pieces.

The passage zz never appears in the training data. Training pairs are (question, answer), and nothing records which passage the answer came from. A quantity the model uses but never observes is a latent variable. RAG handles it the standard way for latent variables: instead of guessing a single value, it sums over the values it considers, weighted by their probability.

The components, with the sizes from the paper's Appendix G:

The paper counts 626M trainable parameters (110M + 110M + 406M, including the document encoder it does not train). T5-11B has about 18 times as many.

To condition BART on a passage, the paper says only that it concatenates the passage and the question. The authors' HuggingFace port spells out the string: passage title, " / ", passage text, " // ", question, truncated to 300 tokens. For our question, the generator input built from the second passage is one string:

# one generator input (a single string, wrapped here to fit)
"The Old Man and the Sea / The Old Man and the Sea is a novella written by
the American author Ernest Hemingway in 1951 ... // who wrote the old man
and the sea"

BART runs on all five strings as a batch, so one question costs five generator passes. BART was pre-trained as a denoising autoencoder, reconstructing text that had been corrupted by masking, deletion and shuffling, which gives it a general ability to write fluent text conditioned on an input.

Retrieval as an inner product

The retriever has to rank 21 million passages for every question, fast, and the question and the right passage may share few words ("who wrote" against "a novella written by the American author"). RAG uses Dense Passage Retrieval (DPR), which maps text to embeddings, vectors placed so that texts with related meaning get vectors with a large inner product. DPR is a bi-encoder: one encoder for questions, q(x)\mathbf{q}(x), and a separate one for passages, d(z)\mathbf{d}(z), each the 768-dimensional output at BERT's [CLS] position. The score of a passage is the inner product of the two vectors, and the retriever's distribution is a softmax of those scores:

pη(z∣x)  ∝  exp⁡ ⁣(d(z)⊤q(x)),d(z)=BERTd(z),q(x)=BERTq(x)p_\eta(z \mid x) \;\propto\; \exp\!\big(\mathbf{d}(z)^\top \mathbf{q}(x)\big), \qquad \mathbf{d}(z) = \mathrm{BERT}_d(z), \quad \mathbf{q}(x) = \mathrm{BERT}_q(x)pη(z∣x)  ∝  exp⁡ ⁣(d(z)⊤q(x)),d(z)=BERTd(z),q(x)=BERTq(x)\begin{gathered} p_\eta(z \mid x) \;\propto\; \exp\!\big(\mathbf{d}(z)^\top \mathbf{q}(x)\big), \\ \mathbf{d}(z) = \mathrm{BERT}_d(z), \quad \mathbf{q}(x) = \mathrm{BERT}_q(x) \end{gathered}
(1)

The more expressive alternative is a cross-encoder, a single network that reads the question and passage together and outputs a score (the DPR question-answering system uses one to re-rank its top passages). A cross-encoder has to be run on every (question, passage) pair at query time, which means 21 million BERT passes per question. With a bi-encoder the passage side does not depend on the question, so all 21 million d(z)\mathbf{d}(z) are computed once, stored, and each question costs one BERT pass plus a search. The DPR paper chose the inner product for exactly this reason: the similarity function has to decompose so that passage representations can be precomputed.

Finding the passages with the largest d(z)⊤q(x)\mathbf{d}(z)^\top \mathbf{q}(x) is a Maximum Inner Product Search (MIPS). Scoring every stored vector exactly is 21M × 768 ≈ 16.1 billion multiply-adds per question. RAG instead builds a FAISS index with a Hierarchical Navigable Small World (HNSW) graph, which links each vector to near neighbours and answers a query by walking the graph, visiting a small fraction of the vectors. The search is approximate, and its cost grows sub-linearly with the number of passages. The paper keeps the index on CPU; it needs about 100 GB of RAM for all of Wikipedia, or 36 GB after FAISS compression (the LEANN explainer covers shrinking such an index further). In Figure 2, pick a question and a KK and watch which passages are retrieved and how the weight splits among them.

Figure 2 · retrieval as nearest neighbors
K = 5
Every passage is a point in the dense vector space; the question is its own vector. The top-K passages are the ones most aligned with the query, ringed and connected with brightness set by the softmax weight pη(z|x), whose values are listed along the bottom. Pick a question and a K. The drawing uses distance: for vectors of equal length, the largest inner product is the nearest point, which is why MIPS looks like nearest-neighbor search.

The ∝\propto in (1) hides a normalizing constant, and the sum in that constant runs over the top KK retrieved passages only, never over all 21 million:

pη(z∣x)  =  exp⁡ ⁣(d(z)⊤q(x))∑z′∈top-kexp⁡ ⁣(d(z′)⊤q(x)),z∈top-kp_\eta(z \mid x) \;=\; \frac{\exp\!\big(\mathbf{d}(z)^\top \mathbf{q}(x)\big)}{\sum_{z' \in \text{top-}k}\exp\!\big(\mathbf{d}(z')^\top \mathbf{q}(x)\big)}, \qquad z \in \text{top-}k

In the code this is a log_softmax over the KK retrieved scores. At retrieval time DPR ranks passages by the raw inner product. RAG adds the softmax over the retrieved set because the sum over passages in the next section needs weights that add up to one. Moving KK in Figure 2 renormalizes the weights over whichever passages are included.

The retriever does not start from scratch. RAG initializes both encoders from a trained DPR model and builds the index with its document encoder. That DPR model was trained with retrieval supervision: for Natural Questions and TriviaQA questions it saw passages known to contain the answer, and it learned to score them above a small set of negatives (the other questions' passages in the batch plus one high-scoring BM25 passage that lacks the answer). So the retriever starts out good at finding answer-bearing Wikipedia passages, and RAG's fine-tuning adapts it to each task with no further retrieval labels.

Summing over passages

Suppose the retriever returned five passages for "Who wrote The Old Man and the Sea?". Use these illustrative numbers for the rest of the page: rank 1 is the article on the 1958 film adaptation, which names Spencer Tracy but not the author; rank 2 is the novella's own article, which names Ernest Hemingway; rank 3 is a passage of Hemingway's biography mentioning the 1953 Pulitzer Prize for the book; ranks 4 and 5 are about fishing. The retrieval scores after the softmax are

pη(z∣x)=(0.482,  0.323,  0.097,  0.059,  0.040)p_\eta(z \mid x) = (0.482,\; 0.323,\; 0.097,\; 0.059,\; 0.040)

and running BART once per passage gives the gold answer "Ernest Hemingway" these probabilities:

pθ(y∣x,z)=(0.08,  0.74,  0.41,  0.02,  0.01)p_\theta(y \mid x, z) = (0.08,\; 0.74,\; 0.41,\; 0.02,\; 0.01)

The obvious design reads only the top passage. Here that gives the gold answer probability 0.08, because the highest-scoring passage is the near-miss. RAG instead treats the passage as a latent variable and sums it out: it adds up each passage's generator probability for the answer, weighted by that passage's retriever probability. This is marginalizing over zz, and in the simplest variant, RAG-Sequence, one passage is assumed to be behind the whole answer:

pRAG-Seq(y∣x)  ≈  ∑z∈top-kpη(z∣x) pθ(y∣x,z)  =  ∑z∈top-kpη(z∣x)∏iNpθ(yi∣x,z,y1:i−1)p_{\text{RAG-Seq}}(y \mid x) \;\approx\; \sum_{z \in \text{top-}k} p_\eta(z \mid x)\, p_\theta(y \mid x, z) \;=\; \sum_{z \in \text{top-}k} p_\eta(z \mid x) \prod_{i}^{N} p_\theta(y_i \mid x, z, y_{1:i-1})pRAG-Seq(y∣x)≈∑z∈top-kpη(z∣x) pθ(y∣x,z)=∑z∈top-kpη(z∣x)∏iNpθ(yi∣x,z,y1:i−1)\begin{aligned} p_{\text{RAG-Seq}}(y \mid x) &\approx \sum_{z \in \text{top-}k} p_\eta(z \mid x)\, p_\theta(y \mid x, z) \\ &= \sum_{z \in \text{top-}k} p_\eta(z \mid x) \prod_{i}^{N} p_\theta(y_i \mid x, z, y_{1:i-1}) \end{aligned}
(2)

The ≈\approx is the top-KK truncation: the exact marginal would sum over every passage in the index. The right-hand form writes pθ(y∣x,z)p_\theta(y \mid x, z) as the product of the per-token probabilities, which is how a sequence model scores a whole output. With the numbers above:

p(y∣x)=0.482(0.08)+0.323(0.74)+0.097(0.41)+0.059(0.02)+0.040(0.01)=0.319p(y \mid x) = 0.482(0.08) + 0.323(0.74) + 0.097(0.41) + 0.059(0.02) + 0.040(0.01) = 0.319p(y∣x)=0.482(0.08)+0.323(0.74)+0.097(0.41)+0.059(0.02)+0.040(0.01)=0.319\begin{aligned} p(y \mid x) &= 0.482(0.08) + 0.323(0.74) + 0.097(0.41) \\ &\quad + 0.059(0.02) + 0.040(0.01) \\ &= 0.319 \end{aligned}

That is four times the top-1 probability, and most of it, 0.323×0.74=0.2390.323 \times 0.74 = 0.239, comes from the novella's article at rank 2. The sum also matters for training: every retriever weight appears in it, so the probability of the gold answer depends smoothly on every score, and a gradient can flow back into the retriever. A hard top-1 choice has no such gradient. Figure 3 shows the sum as a weighted vote over candidate answers, for a question whose gold answer is also Hemingway.

Figure 3 · the marginal as a weighted vote
start
Each retrieved document carries an amber retriever weight pη and a teal generator distribution pθ over three candidate answers; the bottom row is the marginal p(y|x) = Σ pη·pθ. The slider moves the weights from uniform to the posterior p(z | x, y = Hemingway), the direction training pushes them (derived in the training section), and the marginal sharpens onto the gold answer. No label says which document was correct.

The sum runs over KK passages, and KK is a choice. With K=1K=1 the model is the top-1 reader again. Each added passage pulls the marginal toward its own generator probability, by an amount set by its softmax weight after renormalization. A passage that contains the answer pulls it up. An off-topic passage pulls it down, and more strongly when the retriever's scores are close together, because then the softmax gives the off-topic passage a larger share. In Figure 4, with ten ranked passages, the gold passage at rank 2 and a near-miss at rank 1, slide KK and then change the score gap between ranks.

Figure 4 · how many passages to marginalize over
K = 3 (best)
0.30 / rank
Ten ranked passages, each with a retrieval score that falls by a fixed gap per rank and an answer probability pθ(ans|z): rank 1 a near-miss (0.10), rank 2 gold (0.82), ranks 3 and 4 partial, the rest off-topic. The amber bars are the softmax over the included top-K, and the readout is the marginal. At the default gap of 0.30 the marginal is 0.10 at K=1, peaks at 0.42 at K=3, and settles near 0.30 by K=10. At gap 0 the weights are equal and K=10 falls to 0.19; at gap 1.50 the curve is flat past the peak but stuck near 0.24, because the near-miss at rank 1 holds most of the weight.

The paper trains with K∈{5,10}K \in \{5, 10\} and reports no significant difference between the two. At test time KK is set on development data. For open-domain QA the reported test numbers use 15 passages for RAG-Token and 50 for RAG-Sequence; on Natural Questions, accuracy rises monotonically with more passages for RAG-Sequence and peaks at 10 for RAG-Token (the paper's Figure 3). The paper does not analyse why the two differ. One reading consistent with Figure 4: a trained retriever gives low-ranked passages small weights, so each extra passage dilutes the marginal only a little while adding a chance that it carries the answer.

RAG-Sequence and RAG-Token

Equation (2) sums over passages once, for the whole answer. Some outputs combine facts from different passages. A Jeopardy clue for Hemingway might name two of his novels, and no single retrieved passage need mention both. The second model, RAG-Token, lets a different passage account for each token by moving the sum over passages inside the product over tokens:

pRAG-Tok(y∣x)  ≈  ∏iN  ∑z∈top-kpη(z∣x) pθ(yi∣x,z,y1:i−1)p_{\text{RAG-Tok}}(y \mid x) \;\approx\; \prod_{i}^{N} \; \sum_{z \in \text{top-}k} p_\eta(z \mid x)\, p_\theta(y_i \mid x, z, y_{1:i-1})
(3)

The retriever weights pη(z∣x)p_\eta(z \mid x) are the same in (2) and (3): the question is encoded once and the passages are retrieved once. Only the position of the sum differs. In (2), each passage scores the whole answer and the scores are averaged. In (3), at each position the next-token probabilities of all passages are averaged into one mixture, the token is scored under that mixture, and the per-token scores are multiplied.

Take two passages and a two-token answer. Doc 1 supports the second token (probability 0.9, and 0.1 for the first); doc 2 supports the first token (0.9, and 0.1 for the second); the retriever weights both 0.5. RAG-Sequence gives 0.5(0.1)(0.9)+0.5(0.9)(0.1)=0.090.5(0.1)(0.9) + 0.5(0.9)(0.1) = 0.09, because under each passage one of the two tokens is unlikely. RAG-Token gives (0.05+0.45)(0.45+0.05)=0.25(0.05 + 0.45)(0.45 + 0.05) = 0.25, because each token is scored under the mixture, where one passage supports it. If instead doc 1 supports both tokens and doc 2 neither, RAG-Sequence gives 0.41 and RAG-Token 0.25: summing whole-answer scores favours an answer one passage explains end to end. Figure 5 has these cases as presets.

Figure 5 · where the sum sits
0.50
0.90
Two documents, a two-token answer, and one table of generator probabilities, scored two ways. Pick a support pattern, then drag the retriever weight on doc 1 and the strength of support. At a weight of 0 or 1 only one document counts, and at a strength of 0.50 every document gives every token the same probability; in both cases the two formulas agree. At the default split pattern RAG-Token scores the answer 2.8 times higher.

Both forms reduce to the same thing when the output is one token. With N=1N = 1 the product has a single factor and the sum can go on either side of it, so the paper treats classification as generating a one-token answer: for FEVER, the labels supports, refutes and not-enough-info are each mapped to a single output token, and the two models are identical. In code, the only difference is the order of a sum over tokens and a logsumexp over passages:

# one question, K = 5 passages (shapes as in the HuggingFace port)
q      = query_encoder(x)                  # (768,)  BERT-base [CLS] vector
ids, D = index.search(q, k=5)              # D: (5, 768) stored doc vectors
s      = D @ q                             # (5,)  inner products d(z).q(x)
log_pe = log_softmax(s)                    # (5,)  over the 5 retrieved only
ctx    = [title[i] + " / " + text[i] + " // " + x for i in ids]
logits = bart(ctx, y)                      # (5, N, vocab): one pass per passage
log_pt = gather(log_softmax(logits), y)    # (5, N): log p_theta(y_i | x, z, y_<i)

# RAG-Sequence: product over tokens first, then the sum over passages
log_p_seq = logsumexp(log_pe + log_pt.sum(1))
# RAG-Token: sum over passages at every token, then the product over tokens
log_p_tok = logsumexp(log_pe[:, None] + log_pt, dim=0).sum()

The paper shows RAG-Token combining passages on a real generation. Given the answer entity "Hemingway" with five retrieved passages, the model wrote the Jeopardy clue "The Sun Also Rises" is a novel by this author of "A Farewell to Arms". For each token, the paper plots which passage most likely produced it, the posterior over the passage given that token:

p(z∣x,yi,y1:i−1)  =  pη(z∣x) pθ(yi∣x,z,y1:i−1)∑z′pη(z′∣x) pθ(yi∣x,z′,y1:i−1)p(z \mid x, y_i, y_{1:i-1}) \;=\; \frac{p_\eta(z \mid x)\, p_\theta(y_i \mid x, z, y_{1:i-1})}{\sum_{z'} p_\eta(z' \mid x)\, p_\theta(y_i \mid x, z', y_{1:i-1})}

The prior pη(z∣x)p_\eta(z \mid x) is fixed for the whole clue; only the second factor changes from token to token. So the posterior moves away from the prior exactly where the passages disagree about the next token. The paper reports that passage 2, which mentions The Sun Also Rises, dominates the posterior when "Sun" is generated, and passage 1 dominates when A Farewell to Arms is generated. After the first token of each title the posterior flattens. Figure 6 rebuilds this with an illustrative table of per-token probabilities; the paper publishes the plot but not its numbers.

Figure 6 · which document explains which word
token 2/15
The clue for Hemingway, one heatmap column per token, one row per retrieved document; the amber bars are the fixed prior pη(z|x). In RAG-Token mode each column is the per-token posterior: doc 2 takes 0.86 of the posterior at "Sun" and doc 1 takes 0.72 at the opening "A", and on "Also Rises" and "to Arms" every document gives the token nearly the same probability, so the column returns to the prior. In RAG-Sequence mode one posterior covers the whole clue. Click a column, scrub, or press Play; the bottom line gives the whole clue's probability under each model from the same table.

The flattening is the paper's evidence that the two memories divide the work. Once "The Sun" is written, every passage gives "Also" about the same high probability, because the title is stored in BART's weights. The authors checked this with a BART model trained without retrieval: fed the partial clue "The Sun, it completed "The Sun Also Rises" is a novel by this author of "The Sun Also Rises", and fed the clue up to "A it completed "A Farewell to Arms". The paper reads this as the retrieved passage guiding the generation and drawing out knowledge already stored in the generator's parameters. In the RAG-Sequence mode of Figure 6, doc 2 takes 0.75 of the single posterior because it explains the whole clue best, and the whole clue is 1.75 times more probable under RAG-Token than under RAG-Sequence.

Training without retrieval labels

The training set is pairs (xj,yj)(x_j, y_j) of inputs and target outputs. RAG minimizes the negative log of the marginal probability of each target, summed over the pairs, with Adam:

L  =  ∑j−log⁡p(yj∣xj)\mathcal{L} \;=\; \sum_{j} -\log p(y_j \mid x_j)
(4)

where p(y∣x)p(y \mid x) is (2) or (3). For the running example the loss is −log⁡0.319=1.14-\log 0.319 = 1.14 nats (natural-log units); a top-1 model would have −log⁡0.08=2.53-\log 0.08 = 2.53. Nothing in (4) mentions which passage was correct, yet minimizing it changes the retriever in a specific direction. To see which, write the retrieval scores as sz=d(z)⊤q(x)s_z = \mathbf{d}(z)^\top \mathbf{q}(x) and put the softmax of (1) into (2). The log marginal is then a difference of two log-sums:

log⁡p(y∣x)  =  log⁡∑zesz pθ(y∣x,z)  −  log⁡∑zesz\log p(y \mid x) \;=\; \log \sum_{z} e^{s_z}\, p_\theta(y \mid x, z) \;-\; \log \sum_{z} e^{s_z}

The derivative of a log-sum-of-exponentials with respect to one score is that term's share of the sum. The first term's shares are the retriever weights reweighted by how well each passage predicts yy, which is the posterior over passages given the answer, p(z∣x,y)p(z \mid x, y); the second term's shares are the retriever weights themselves:

∂log⁡p(y∣x)∂sz  =  p(z∣x,y)−pη(z∣x),p(z∣x,y)=pη(z∣x) pθ(y∣x,z)p(y∣x)\frac{\partial \log p(y \mid x)}{\partial s_z} \;=\; p(z \mid x, y) - p_\eta(z \mid x), \qquad p(z \mid x, y) = \frac{p_\eta(z \mid x)\, p_\theta(y \mid x, z)}{p(y \mid x)}∂log⁡p(y∣x)∂sz=p(z∣x,y)−pη(z∣x),p(z∣x,y)=pη(z∣x) pθ(y∣x,z)p(y∣x)\begin{aligned} \frac{\partial \log p(y \mid x)}{\partial s_z} &= p(z \mid x, y) - p_\eta(z \mid x), \\ p(z \mid x, y) &= \frac{p_\eta(z \mid x)\, p_\theta(y \mid x, z)}{p(y \mid x)} \end{aligned}
(5)

Gradient ascent on the log-likelihood raises the score of a passage whose posterior exceeds its prior, which happens exactly when pθ(y∣x,z)>p(y∣x)p_\theta(y \mid x, z) > p(y \mid x): the passage predicted the gold answer better than the current marginal does. For the running example the posterior is (0.121,0.749,0.125,0.004,0.001)(0.121, 0.749, 0.125, 0.004, 0.001), so the gradients on the five scores are:

∂log⁡p∂s=(−0.361,  +0.426,  +0.028,  −0.055,  −0.038)\frac{\partial \log p}{\partial s} = (-0.361,\; +0.426,\; +0.028,\; -0.055,\; -0.038)

The film article (answer probability 0.08, below the marginal 0.319) is pushed down, the novella article pushed up. The biography passage, at 0.41, is just above the marginal and gets a small push up. The gradients sum to zero because the posterior and the prior each sum to one. For RAG-Token the log-likelihood is a sum over tokens, so the gradient on szs_z is the sum over tokens of the per-token posterior from the Hemingway example minus the prior. Figure 7 runs these gradient steps on five free scores.

Figure 7 · the gradient that teaches retrieval
step 0/80
Five candidate documents with fixed gold-answer probabilities pθ(gold|x,z) (teal values), retrieval scores starting near-uniform, and 80 gradient-ascent steps on the log marginal likelihood using equation (5). The arrows are each score's current gradient. The softmax pη(z|x) moves to doc A (0.94 at the last step) and p(gold answer) climbs to 0.81. Doc B, at 0.45, is pulled up while the marginal is below 0.45 and pushed down once the marginal passes it.

In the real model the scores are not free parameters. Each is an inner product with the same query vector, and the document vectors are fixed, because RAG freezes the document encoder and the index. The gradient reaches the query encoder through q(x)\mathbf{q}(x), and by the chain rule (∂sz/∂q=d(z)\partial s_z / \partial \mathbf{q} = \mathbf{d}(z)) it is

∇qlog⁡p(y∣x)  =  ∑z∈top-k[p(z∣x,y)−pη(z∣x)] d(z)\nabla_{\mathbf{q}} \log p(y \mid x) \;=\; \sum_{z \in \text{top-}k} \big[p(z \mid x, y) - p_\eta(z \mid x)\big]\, \mathbf{d}(z)
(6)

The query moves toward the posterior-weighted average of the retrieved document vectors and away from the prior-weighted average. Two consequences follow from the scores sharing one vector. Moving the query toward a helpful passage also raises the score of every passage whose vector points the same way, so a neighbour can enter the top KK. And a passage outside the top KK gets no gradient at all, however useful it would have been, because it is not in the sum. Figure 8 shows both.

Figure 8 · the query vector moves, the documents stay
step 0/60
Six fixed document vectors A to F, each labelled with u = pθ(gold|x,z), and a trainable query vector. The ringed three are the top-3 by inner product. Press Play: steps of equation (6) rotate the query from the near-miss A toward the helpful B, C (u = 0.45, near B) enters the top-3 and D leaves, and p(y|x) rises from 0.17 to 0.60. F has the highest u = 0.90 but points away from the query, is never retrieved, and never affects the update.

The document encoder is frozen to save computation. If BERTd\mathrm{BERT}_d were trained, every update would make the stored vectors stale, and keeping the index valid would mean re-encoding 21 million passages and rebuilding the index periodically, which is what REALM does during its pre-training. The paper reports that this step is not necessary for strong performance and trains only BERTq\mathrm{BERT}_q and BART. In code, a training step continues the forward pass shown earlier:

# one training step on a (question, answer) pair; nothing says which passage
loss = -log_p_seq             # or -log_p_tok: one term of equation (4)
loss.backward()
#   BART's 406M weights get a gradient through log_pt
#   the query encoder gets one through s = D @ q
#   D was read out of the index as plain numbers: the document encoder gets none
opt.step()                    # Adam on the query encoder and BART

The paper checks that the learned retrieval matters by freezing the query encoder too. On the Natural Questions development set, RAG-Sequence drops from 44.0 to 41.2 exact match and RAG-Token from 43.5 to 37.8. Replacing the dense retriever with BM25, a classic word-overlap ranking function whose scores the paper uses as the retrieval logits, drops them to 31.8 and 29.7. Training ran in Fairseq with mixed precision on eight 32 GB V100 GPUs.

The label-free signal can also fail. In preliminary experiments on story generation, the retriever "collapsed" to returning the same passages for every input, after which the generator learned to ignore them and the model performed like plain BART (Appendix H). The authors suggest two causes: story generation needs less factual knowledge, and long targets could give less informative gradients to the retriever. When no retrieved passage predicts the target better than the others, the posterior in (5) stays equal to the prior and the scores receive no consistent push.

Decoding

At test time the model has to produce an output, approximately arg⁡max⁡yp(y∣x)\arg\max_y p(y \mid x), and the two variants need different procedures. RAG-Token fits a standard decoder: its per-token mixture is an ordinary next-token distribution,

pθ′(yi∣x,y1:i−1)  =  ∑z∈top-kpη(z∣x) pθ(yi∣x,z,y1:i−1)p'_\theta(y_i \mid x, y_{1:i-1}) \;=\; \sum_{z \in \text{top-}k} p_\eta(z \mid x)\, p_\theta(y_i \mid x, z, y_{1:i-1})
(7)

so it plugs into beam search unchanged. RAG-Sequence's probability does not factor into per-token terms, because the sum over passages sits outside the product. The paper runs a separate beam search for each passage, collects every hypothesis from every beam into a candidate set, and then needs each candidate's probability under every passage. Thorough Decoding runs an extra forward pass for each (candidate, passage) pair whose candidate did not appear in that passage's beam. Fast Decoding skips those passes and treats pθ(y∣x,zi)p_\theta(y \mid x, z_i) as 0 when yy was not in passage ziz_i's beam. The paper used Thorough Decoding with greedy decoding for open-domain QA, where answers are short, and Fast Decoding with beam size 4 for MS-MARCO and Jeopardy, where Thorough Decoding did not improve the scores.

Results

Open-domain QA gives the model only a question, and the score is exact match (EM): the percentage of questions whose generated answer string equals a gold answer. The paper's Table 1, with the closed-book models (parametric memory only) and the retrieve-and-extract systems:

Model (test EM)NQTQATQA-WikiWQCT
T5-11B (closed book)34.5n/a50.137.4n/a
T5-11B+SSM (closed book)36.6n/a60.544.7n/a
REALM40.4n/an/a40.746.8
DPR41.557.9n/a41.150.6
RAG-Token44.155.266.145.550.0
RAG-Sequence44.556.868.045.252.2

RAG sets the state of the art on Natural Questions (44.5 for RAG-Sequence against DPR's 41.5), WebQuestions (45.5 for RAG-Token) and CuratedTrec (52.2 for RAG-Sequence). TriviaQA has two test sets. On the TQA-Wiki set used by T5, RAG-Sequence scores 68.0 against 60.5 for T5-11B+SSM. On the standard open-domain test set, DPR's 57.9 beats RAG-Sequence's 56.8. The DPR system compared here uses a BERT cross-encoder to re-rank passages and an extractive reader; RAG uses neither. T5-large, the T5 model closest to RAG's size at 770M parameters, scores 28.9 on NQ.

Because RAG generates the answer, a passage can help without containing the answer string, and the model can be right when no passage contains it. On Natural Questions, in cases where the correct answer appears in none of the retrieved passages, RAG is still right 11.8% of the time. An extractive reader, which can only copy a span, scores 0% on those cases by construction.

On generation tasks the baseline is BART fine-tuned without retrieval. For abstractive question answering on MS-MARCO (the NLG v2.1 task, used here without its supplied gold passages), RAG-Sequence beats BART by 2.6 points on both BLEU-1 (44.2 against 41.6) and ROUGE-L (40.8 against 38.2), metrics that count word and subsequence overlap with the reference answer. For Jeopardy question generation, where the input is an answer entity and the output a clue, RAG-Token scores 22.2 on Q-BLEU-1 (a BLEU variant that up-weights matching entities), RAG-Sequence 21.4 and BART 19.7. Human raters compared 452 pairs of BART and RAG-Token clues: they judged RAG more factual in 42.7% of pairs and BART in 7.1%, and RAG more specific in 37.4% against 16.8%. RAG's clues are also more varied: 53.8% of RAG-Sequence's trigrams are distinct and 46.8% of RAG-Token's, against 32.4% of BART's, with no diversity-promoting decoding.

FEVER asks whether Wikipedia supports or refutes a claim, or has not enough information. RAG reaches 72.5% label accuracy on the three-way task, 4.3 points below the state of the art (76.8), and 89.5% on the two-way task, 2.7 below 92.2. Those systems are pipelines trained with supervision on which evidence to retrieve, and the two-way one is given the gold evidence sentence; RAG sees only the claim and retrieves its own evidence. Its top retrieved passage comes from a gold evidence article 71% of the time, and a gold article is in the top 10 in 90% of cases.

Swapping the index, and limits

Facts in the index can be changed without training. The paper built a second index from the December 2016 Wikipedia dump (the one used by DrQA), listed 82 world leaders who changed between December 2016 and December 2018, and asked the Natural Questions RAG model "Who is {position}?" (for example, "Who is the President of Peru?") with each index. In Figure 9, pick a position and an index; the table under the flow has the paper's measured accuracies.

Figure 9 · hot-swapping the index
The same question and the same weights with a different Wikipedia index. Switching between the 2016 and 2018 snapshots changes the retrieved passage and so the answer. The per-country passages are illustrative; the paper reports only the aggregate accuracies in the table: 70% with the 2016 index on 2016 leaders, 68% with the 2018 index on 2018 leaders, and 12% (2018 index, 2016 leaders) and 4% (2016 index, 2018 leaders) when mismatched. The shuffled option is our own negative control, not a paper experiment: unrelated passages in, no grounded answer out.

If the model answered from its weights, swapping the index would leave the answers unchanged, and the 2016-index model would do as well on 2018 leaders as the 2018-index model. Accuracy instead falls from 68% to 4%, so the answer comes from the retrieved passage. A purely parametric model such as T5 or BART needs further training to change these answers. The same property makes RAG's answers checkable: the retrieved passages can be shown next to the output.

The paper's results come with limits:

The paper's proposed next step is to pre-train the retriever and generator jointly from scratch, for example with a denoising objective like BART's. The recipe it did establish has four parts: a bi-encoder retriever over a fixed dense index, a pre-trained seq2seq generator run once per retrieved passage, a sum over the top KK passages either per answer or per token, and training on (input, output) pairs alone, where the gradient (5) moves the query encoder toward passages that raised the probability of the target.

Provenance Verified against primary literatureHow we verify
DPR, Karpukhin et al. (2020)The bi-encoder retriever and the index: two BERT-base encoders, 768-dim [CLS] vectors, 21,015,324 100-word passages, relevance = inner product. Pre-trained with retrieval supervision on Natural Questions and TriviaQA.
BART, Lewis et al. (2019)The generator: a denoising seq2seq Transformer, 406M parameters (Sec 2.3 of the RAG paper rounds to 400M).
REALM / ORQA (2019-2020)Earlier masked-LM models with a differentiable retriever, used only for extractive QA. REALM re-indexes periodically during pre-training; RAG never re-indexes.
HuggingFace port (official code)modeling_rag.py: doc_scores = bmm(question vector, retrieved doc embeddings), log_softmax over n_docs (default 5), logsumexp over docs; retrieval_rag.py joins title + " / " + text + " // " + question, max 300 tokens.
Gradient of the marginald log p(y|x) / d s_z = p(z|x,y) - p_eta(z|x): derived here from equations (2) and (4); the paper states the objective, not this gradient.
correctionTwo corrections. (1) Section 4.1 says RAG sets a new state of the art "on all four open-domain QA tasks", and qualifies TriviaQA only in a parenthesis. On the standard open-domain TriviaQA test set DPR scores 57.9 EM and RAG-Sequence 56.8 (Table 1), so RAG leads on NQ, WebQuestions and CuratedTrec, and on TriviaQA only on the T5-comparable Wiki test set (68.0 vs 60.5). The abstract's "three open domain QA tasks" is the accurate count. (2) Appendix G describes the index as "21M 728 dimensional vectors, consisting of 15.3B values". DPR's vectors are BERT-base [CLS] outputs, d = 768 (DPR Sec 3.1; retrieval_vector_size = 768 in the code), so the index holds about 16.1 billion values; 15.3B is 21M x 728 and inherits the typo.

Questions you might still have

?

If nobody labels which passage is correct, how does the retriever learn?
From the gradient of the answer's marginal likelihood. For each retrieved passage the gradient on its score is its posterior weight given the gold answer minus its prior weight from the retriever, so passages whose generator run gave the gold answer more probability than the current average are pushed up and the others down. Only the query encoder moves; the document vectors are fixed.

?

Why does RAG sum over several retrieved passages instead of reading the top one?
Nothing says which retrieved passage holds the answer, and the top hit is often a near-miss. The sum lets a passage at rank 2 or 3 carry the answer, and it gives the retriever a gradient, because every passage's weight appears in the probability of the gold answer. A hard top-1 choice would give neither.

?

What is the difference between RAG-Sequence and RAG-Token?
Where the sum over passages sits. RAG-Sequence multiplies the token probabilities under each passage and then sums over passages, so one passage has to explain the whole answer. RAG-Token sums over passages at every token and then multiplies, so different tokens can come from different passages. RAG-Sequence scored best on three of the four QA datasets, RAG-Token on Jeopardy question generation.

?

Does RAG need retraining when the facts change?
Not for facts that live in the index. The paper swapped its December 2018 Wikipedia index for a December 2016 one with no retraining and the answers about 82 world leaders followed the index: 70% correct with the 2016 index on 2016 leaders, 68% with the 2018 index on 2018 leaders, and 12% and 4% when mismatched.

?

Is RAG the same thing as putting search results in a chatbot prompt?
The shape is the same: retrieve passages, then generate conditioned on them. The paper's version differs in that the retriever and generator are fine-tuned together through the marginal likelihood, the model runs the generator once per passage and sums, and the index is a dense vector index over Wikipedia rather than a web search engine.

?

How big is the model compared with T5?
626M trainable parameters (two 110M BERT-base encoders plus 406M for BART), against 11 billion for T5-11B. On Natural Questions RAG-Sequence scores 44.5 EM; T5-11B scores 34.5 and T5-large, the closest in size at 770M, scores 28.9. The facts live in 21 million stored passage vectors that are not trained.

Footnotes & further reading

  1. The paper: Lewis, Perez, Piktus, Petroni, Karpukhin, Goyal, Küttler, Lewis, Yih, Rocktäschel, Riedel, Kiela, Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (Facebook AI Research / UCL / NYU, NeurIPS 2020). The official code is the HuggingFace Transformers port; the model itself is in modeling_rag.py (the get_nll and marginalize methods implement equations (2) and (3)) and retrieval_rag.py.
  2. The retriever: Karpukhin et al., Dense Passage Retrieval for Open-Domain Question Answering (DPR): the bi-encoder, the inner-product score, the 21,015,324-passage Wikipedia split, and the in-batch plus BM25-negative training that RAG starts from.
  3. The generator: Lewis et al., BART: Denoising Sequence-to-Sequence Pre-training. BART-large has 406M parameters; the RAG paper's Section 2.3 rounds to 400M.
  4. The predecessors: Guu et al., REALM, and Lee et al., Latent Retrieval (ORQA), which paired a learned retriever with a masked language model for extractive QA.
  5. The search: Johnson et al., Billion-scale similarity search (FAISS), and Malkov & Yashunin, HNSW, the graph index RAG uses for fast MIPS.
  6. The closed-book baseline: Roberts, Raffel and Shazeer, How Much Knowledge Can You Pack Into the Parameters of a Language Model?, the source of the T5 and T5+SSM numbers (see the T5 explainer for the model).
  7. The benchmarks: open Natural Questions, TriviaQA, WebQuestions and CuratedTrec for QA; MS-MARCO for abstractive QA; and FEVER for fact verification.