Pre-trained neural language models can learn a substantial amount of in-depth knowledge from data without any access to an external memory, as a parameterized implicit knowledge base.
However, the downsides are that they cannot easily expand or revise their memory, cannot straightforwardly provide insight into their predictions and may produce hallucinations.
RAG architecture

Endow pretrained parametric-memory generation models with a non-parametric memory through a general-purpose fine-tuning approch: retrieval-augmented generation (RAG).
The models leverage two components:
- a retriever \(p_{\eta}(z|x)\) with parameters \(\eta\) that returns (top-\(K\) truncated) distributions over text passages given a query \(x\),
- a generator \(p_{\theta}(y_i|x, z, y_{<i})\) parameterized by \(\theta\) that auto-regressively generates output token \(y_i\) given previous tokens, the query \(x\) and retrieved passages \(z\).
RAG-Sequence Model: use the same retrieved document to generate the complete sequence.
1 | class RAGSequence(nn.Module): |
RAG-Token Model: marginalize over the retrieved documents at each generation step.
1 | def rag_token_nll( |
Retriever: Dense Passage Retriever (DPR).
\(p_\eta(z|x)\) is based on DPR, following a bi-encoder architecture:
where \(d(z)=\text{BERT}_d(z)\) and \(q(x)=\text{BERT}_q(x)\) are the dense vector representations of the passage \(z\) and query \(x\), respectively. Calculating top-k of \(p_\eta(\cdot|x)\) is the maximum inner product search (MIPS) problem, which can be solved in sub-linear time (using approximation algorithms like clustering, HNSW or hashing ).
1 | class MeanTextEncoder(nn.Module): |
Generator: BART. The generator \(p_\theta(y_i|x, z, y_{<i})\) is based on BART, a denoising autoencoder for pretraining sequence-to-sequence models. It uses a standard Transformer-based encoder-decoder architecture with a bidirectional encoder and an autoregressive decoder.
1 | # Context = [question tokens] + [document tokens] |
Comments
Sign in with GitHub to join the conversation.