Retrieval-augmented models: RAG, REALM, RETRO, FiD, Atlas, kNN-LM and more
Let a language model read retrieved text instead of relying on memory. Compare the ways the research models combine passages with generation, and see how CodeSplash's Ask documents uses the idea and how to evaluate each part separately.
What runs in CodeSplash
- LNG049RETRO Explained here
- LNG050REALM Explained here
- LNG051Retrieval-Augmented Generation (RAG) Partly runs in CodeSplash (AI)
- LNG052Fusion-in-Decoder (FiD) Explained here
- LNG053Atlas retrieval-augmented language model Explained here
- LNG058k-nearest-neighbor language model (kNN-LM) Explained here
- LNG059Self-RAG Explained here
- LNG060GraphRAG graph-based retrieval and summarization Explained here
- Semantic search (BM25, sentence embeddings, hybrid) Not verified yet
You will be able to
- Write RAG's marginalization over retrieved passages, per sequence and per token
- Contrast where retrieval enters in REALM, RETRO, FiD, Atlas and kNN-LM
- Describe Self-RAG's retrieval decisions and GraphRAG's community summaries
- Evaluate retrieval, faithfulness and citations of a RAG system separately
Retrieve, then generate
ReadA language model's memory is fixed at training time and can't cite its sources. Retrieval-augmented generationRetrieval-augmented language modelA language model that retrieves passages for each input and generates from them, instead of relying only on what its weights remember. fetches passages for each input and generates from them. The original RAG treats the retrieved passage z as a hidden variable and sums over the top few:
RAG-Sequence uses one passage for the whole answer; RAG-Token may switch passage at every token. With two passages and a two-token answer, the reference program gets 0.204 per sequence and 0.290 per token: switching lets each token lean on the passage that supports it best.
Training the retriever: REALM and Atlas
ReadREALM pretrains a masked language model that retrieves documents first, and trains the retriever through the same marginal likelihood: documents that help predict the masked words get pulled closer. In the reference program, two documents combine into target probability 0.62, loss 0.478. Because the retriever changes, its document index must be rebuilt periodically.
Atlas trains the retriever to agree with the reader: passages the reader finds most useful become the retriever's targets. The reference program's cross-entropy between the reader's preferences and the retriever's scores is 0.784. Atlas reached strong question answering with few examples and a much smaller model.
Reading many passages: FiD and RETRO
ReadFusion-in-DecoderFusion-in-DecoderEncodes each retrieved passage separately with the question, then lets one decoder attend over all of them to write the answer. encodes each passage separately with the question, then lets the decoder attend over all of them together. Encoding cost grows linearly with the number of passages, but the decoder's memory doesn't: with 20 passages of 128 tokens, the reference program counts 2,560 memory tokens and 81,920 cross-attention scores per head for a 32-token answer.
RETRO retrieves neighbors for every chunk of the input from a database of trillions of tokens, using frozen BERT vectors, and cross-attends to them while generating. Its point: a smaller model with a big database can match a larger model without one. No public weights were released.
Retrieval at the output: kNN-LM
ReadA kNN-LMkNN-LMMixes a language model's next-token distribution with one built from the next tokens of the nearest stored contexts in a datastore. keeps a datastore of (context vector, next token) pairs from text. At each step it finds the nearest stored contexts and turns their next tokens into a distribution, then mixes that with the model's own:
In the reference program the neighbors put 0.922 on one token; mixed with λ = 0.6, it ends at 0.713. Adding text to the datastore adapts the model without training.
Deciding when to retrieve: Self-RAG and GraphRAG
ReadSelf-RAG fine-tunes a model to emit reflection tokens: whether to retrieve, whether a passage is relevant, whether the answer is supported. Candidates are ranked by a weighted mix of those judgments; the reference program scores two candidates 0.81 and 0.58 and keeps the first.
GraphRAG uses a language model to extract entities and relations from documents, groups the graph into communities, and writes a summary per community. Questions about themes across a whole collection are then answered from summaries rather than single passages. Building the index takes many language-model calls.
Ask documents in CodeSplash
ReadAsk documents is CodeSplash's retrieval-augmented feature. Search runs on your Mac with Semantic search's BM25, dense or hybrid retrievers; the answer is written by the language model you configure, from the passages found, with a citation for every sentence. Documents go to a cloud provider only as retrieved passages, after you confirm it for that collection.
The research models here are explained only. Ask documents implements the retrieve-then-generate pattern with existing parts; it isn't the trained RAG model.
Evaluate the parts separately
PredictPredict first
A RAG system gives a wrong answer. To fix it, what do you need to have measured?
Reveal
Each part separately: did retrieval find the right passages (recall@k on judged queries), did the answer stay faithful to them (a judge or a person), and do the citations point at passages that say what's claimed? A wrong answer from good passages and one from bad passages need different fixes.
Where it stands in CodeSplash
Read| Entry | In CodeSplash |
|---|---|
| RAG (LNG051) | Partly runs in CodeSplash (AI): Ask documents answers from retrieved passages and checks every citation; the trained RAG model is explained here |
| RETRO, REALM, FiD, Atlas (LNG049, LNG050, LNG052, LNG053) | Explained here |
| kNN-LM, Self-RAG, GraphRAG (LNG058–LNG060) | Explained here |
Check: retrieval-augmented models
CheckpointCheck yourself
What is the difference between RAG-Sequence and RAG-Token?
Show the answer
Answer: a
Both marginalize over the retrieved passages; RAG-Token does it per token, so one answer can combine facts from several passages.
Check yourself
Ask documents cites a passage for every sentence. What does a valid citation prove?
Show the answer
Answer: b
Citations make claims checkable against sources. They don't make the sources right, and they don't show what retrieval missed.
Check yourself
Why does REALM rebuild its document index during training?
Show the answer
Answer: b
Training the retriever end to end means document embeddings computed earlier no longer match the current encoder.
Check yourself
In REALM, how does the retriever learn which documents are useful?
Show the answer
Answer: b
Treating the document as a latent variable lets the prediction loss flow back into the retriever.
Check yourself
What was RETRO's main claim?
Show the answer
Answer: a
RETRO moves knowledge from parameters into a searchable database, retrieving neighbors for every chunk.
Check yourself
RETRO embeds its database chunks with a frozen BERT. What does freezing allow?
Show the answer
Answer: b
With a frozen embedder the trillion-token index never goes stale, unlike REALM's trainable retriever.
Check yourself
Fusion-in-Decoder encodes each passage separately. Why?
Show the answer
Answer: b
Joint encoding of all passages would be quadratic in their total length. The decoder still attends over everything, which is where its memory goes.
Check yourself
You double the number of passages FiD reads. What happens to the decoder's cross-attention memory?
Show the answer
Answer: b
Memory tokens are passages × tokens per passage, and every decoder step attends over all of them.
Check yourself
How does Atlas train its retriever?
Show the answer
Answer: b
The reader's signal tells the retriever which passages to rank high, so the two improve together.
Check yourself
What did Atlas show about retrieval and model size?
Show the answer
Answer: a
Knowledge in the retrieved text substitutes for knowledge stored in weights.
Check yourself
How does a kNN-LM turn retrieved neighbors into a prediction?
Show the answer
Answer: b
The datastore maps contexts to next tokens; weighting neighbors by distance gives p_kNN, which is interpolated with p_LM.
Check yourself
How can a kNN-LM adapt to a new domain without training?
Show the answer
Answer: a
New (context, next token) pairs change what neighbors are found, shifting predictions toward the new domain.
Check yourself
What do Self-RAG's reflection tokens let the model decide?
Show the answer
Answer: b
The model is fine-tuned to critique its own retrieval and generation, so retrieval happens only when useful.
Check yourself
Self-RAG labels its own answer "supported". What should an evaluation still do?
Show the answer
Answer: b
Self-assessment is a generated claim like any other. Independent checks measure faithfulness.
Check yourself
Which question is GraphRAG built for, where passage retrieval struggles?
Show the answer
Answer: b
Community summaries answer collection-wide questions that no single passage contains.
Check yourself
What is GraphRAG's main cost?
Show the answer
Answer: b
Every document is read by a model to build the graph, and every community is summarized, before a single question is answered.
Recap
Recap- RAG marginalizes over retrieved passages, per sequence or per token.
- REALM and Atlas train the retriever from the reader's signal; RETRO uses a frozen huge database; FiD fuses passages in the decoder; kNN-LM mixes neighbors' next tokens into the output.
- Self-RAG decides when to retrieve; GraphRAG summarizes communities of an entity graph.
- Ask documents uses the retrieve-then-generate pattern with citations. Evaluate retrieval, faithfulness and citations separately.
Concepts in this lesson