Skip to content

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.

Advanced ~18 min10 steps

What runs in CodeSplash

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

Read first

01

Retrieve, then generate

Read

A 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:

p(y∣x)=∑zp(z∣x) p(y∣x,z)p(y \mid x) = \sum_{z} p(z \mid x)\, p(y \mid x, z)

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.

02

Training the retriever: REALM and Atlas

Read

REALM 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.

03

Reading many passages: FiD and RETRO

Read

Fusion-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.

04

Retrieval at the output: kNN-LM

Read

A 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:

p(w)=λ pkNN(w)+(1−λ) pLM(w)p(w) = \lambda\, p_{\mathrm{kNN}}(w) + (1-\lambda)\, p_{\mathrm{LM}}(w)

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.

05

Deciding when to retrieve: Self-RAG and GraphRAG

Read

Self-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.

06

Ask documents in CodeSplash

Read

Ask 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.

07

Evaluate the parts separately

Predict

Predict first

A RAG system gives a wrong answer. To fix it, what do you need to have measured?

Answer accuracy alone is enoughRetrieval quality, faithfulness to the passages and citation validity, each measured separatelyOnly the number of citations
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.

08

Where it stands in CodeSplash

Read
EntryIn 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
09

Check: retrieval-augmented models

Checkpoint

Check 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.

10

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