Retrieval-Augmented Generation

Dive into Deep Learning · §19.5

Retrieval-Augmented Generation
a question set over the book · stages and their measurements · a checked judge · floor, baseline and ceiling

The Answering Passage Was First, and the Answer Still Missed Its Fact

The problem

Question: in PyTorch, what happens to modules kept in a plain Python list instead of nn.Sequential?

  • Passage [1], ranked first: “… Modules in a plain list stay unregistered, without any warning.”
  • Answer: four sentences on registration, citing [2] and [3]. Nothing about the warning.
  • Judge: wrong; faithful; supported.

The baseline answers 0.91 of 240 questions. Passage skipped? Misled by its neighbors? For other questions, ranked too low? One accuracy number cannot tell.

By the End of This Deck You Can

Goals

  • build a question set with gold passages and no leakage between splits;
  • measure each stage: recall, coverage at a token budget, accuracy, faithfulness, citations;
  • check a model judge before trusting its accuracy;
  • bound what a better retriever can add, between a floor without passages and an oracle ceiling.

Running example: this book in 5,324 section chunks; 240 dev questions about 32 documents, among them the chapter’s load_array question; Qwen3.5-9B answers, Qwen3.8-27B judges.

A Retriever Finds Passages; This Deck Measures What Is Done with Them

Where this fits

movement section
vectors Word Embeddings
Pretrained Encoders
Text Embeddings from Contrastive Training
finding Sparse, Dense and Approximate Retrieval
using Retrieval-Augmented Generation
Failures and Fixes in RAG
  • Uses: BM25 + dense search fused by reciprocal ranks, a reranker, paired bootstrap intervals.
  • Skips: changing stages, sorting failures and testing fixes (next section); retrieval inside agent loops.

A Question Set over Your Own Corpus Comes with Gold Passages

The question set

type single explain numeric code multihop unanswerable
dev, test each 120 40 20 20 20 20
  • Item: question, reference, key facts, gold spans (character ranges), alternative spans, flags.
  • Split by document: 166 train, 32 dev, 32 test, so a system tuned on train never saw the passage of a dev answer.
  • A chunk is relevant if it overlaps half a span, or half of itself.

Spans, not chunk ids: relevance is recomputed for any chunking.

Models Drafted, Checked and Verified Every Question, with Rules Between

How the set was made

step by
draft two questions per passage DeepSeek-V4.1-Flash
verbatim evidence, no “the passage above”, no copied words, no near duplicates rules
stands alone? supported? complete? unique? Qwen3.8-27B
verify every dev and test item Claude Opus 5.5

Verifier: 620 calls, about $7; the whole set about $15.

Blind spots the models share pass every check. The verifier may have read the book. The checker is also the judge.

Check: Verifier and Checker Agree on 84% of Items. Is That Agreement?

Check

Both pass about 9 items in 10. Cohen’s kappa corrects observed agreement p_o for chance agreement p_e at the raters’ own pass rates:

\kappa = \frac{p_o - p_e}{1 - p_e}

Answer. p_e \approx 0.9^2 + 0.1^2 = 0.82, so \kappa \approx (0.84 - 0.82)/0.18 \approx 0.1 (measured: 0.12). Barely above chance: the few items each rejects are mostly different ones.

Extreme case, “is the answer unique?”: 96% agreement, κ = 0.00.

Five Stages, Each with Its Own Measurement

The pipeline

stage decides measured by
query question or rewrite the stages after it
retrieval BM25 + dense, fused Recall@k
reranking order of the top candidates Recall@k
assembly chunks, order, token budget coverage cov@T
generation cited answer or abstention accuracy, faithfulness, citations

cov@T: does the context, filled in rank order up to T tokens, hold half of a gold span? Equal text, not equal chunk counts.

One Fixed Prompt; Every Stage Change Alters Only the Context or the Query

Grounded generation

System: answer about Dive into Deep Learning using only the numbered passages; cite after each statement, e.g. [2]; else reply exactly: I cannot answer this from the provided passages.

User: Passages: [1] heading path, text … Question: … Answer in at most four sentences.

  • Qwen3.5-9B: thinking off, greedy, bf16, batches of 8, left padding.
  • Closed book: the same, without passages.
  • Unanswerable question: a reply containing the abstention sentence is correct; any other reply is a false answer (string match, no judge).

Correct, Faithful and Supported Are Three Different Verdicts

What the judge decides

the answer correct faithful supported
right, from memory, not from the passages yes no no
repeats an incomplete passage, citing it no yes yes
draws on [1], cites only [2] either yes no
  • correct: every key fact, nothing contradicting the reference.
  • faithful: every statement backed by the passages in the context.
  • supported: the cited passages suffice on their own.

Accuracy rests on correct; the other two say whether a reader can check the answer against its sources.

On Qwen3-4B’s Answers the Judge Is a Third Labeler; on Ours κ Says Little

A judge, checked first

Qwen3.8-27B, thinking on, 4-bit weights, one copy per 24 GB GPU; answers labeled by two frontier models. κ on correct:

Qwen3-4B answers Qwen3.5-9B answers (ours)
judge vs Claude Opus 5.5 0.89 −0.04
judge vs Gemini 4 Argon 0.77 0.52
the two labelers 0.81 0.47
judge vs their consensus 0.91 [0.80, 1.00] 0.00 [0.00, 1.00]
agreement with the consensus 0.97 0.97
wrong by both labelers: judge passes 0 of 21 3 of 3

Ours are right 9 times in 10: with 3 wrong answers, chance explains nearly all agreement, so κ is uninformative. The three each omit a key fact.

On our answers the judge leans lenient: accuracy, if anything, too high. Labels by models, not people.

Without Passages the Generator Invents; with Them It Finds is_train

One answer

Question: how does the book’s legacy load_array decide whether its DataLoader shuffles? Reference: it passes shuffle=is_train.

context answer, abridged judge
none “relies on the shuffle argument passed to the DataLoader constructor” wrong
five chunks, the right one first “passes this is_train value directly to the DataLoader constructor as the shuffle parameter … [1]” correct, faithful, supported

Retrieval turns a plausible invention into the book’s answer, and the answer cites the passage it came from.

Check: The Same 240 Prompts in Other Batches. How Many Answers Change?

Check

base-repeat: same model, same contexts, greedy decoding; only the neighbors in each batch of 8 differ.

  • 0 of 240 contexts differ; 95 of 240 answers differ.
  • Accuracy: -0.017, interval [-0.04, +0.01]: within noise.
  • bf16: another padding changes the order of the sums; the logits move by a few rounding steps and flip near-ties.
  • The judge’s server batches too: 5 of 54 answers changed a verdict.

Greedy decoding is deterministic only for a fixed batch, precision and kernel. Batch alike, or count re-batching as noise.

Retrieval Lifts Accuracy from 0.32 to 0.91; an Oracle Adds Nothing Measurable

Floor, baseline, ceiling

240 dev questions closed book baseline oracle
accuracy 0.32 0.91 0.93
citation precision 0.72 1.00 (by construction)
abstains, unanswerable 10 of 20 19 of 20 18 of 20
  • Baseline − closed: +0.60 [+0.53, +0.66]; book-specific: 0.15 → 0.91.
  • Oracle − baseline, all questions: +0.01 [-0.03, +0.06]. Retrieval has no room left to show a gain.

Check: 213 of 220 Hits. What Can a Perfect Retriever Add?

Check

Accuracy on answerable questions: \textrm{accuracy} = h\, a_1 + (1 - h)\, a_0

h = 213/220: share of answerable questions whose context holds a relevant chunk (a hit); accuracy a_1 = 0.93 on hits, a_0 = 0.29 on the seven misses.

Answer. If a_1 stays put, (1 - h)(a_1 - a_0) \approx 0.032 \times 0.64 \approx 0.02. The oracle measures +0.02 [-0.03, +0.07], as predicted. 15 of 20 errors had a relevant passage in the context.

Recap

Recap

  • 240 dev questions with gold spans, made and checked by models.
  • Five stages, each with its own measurement; cov@T compares chunkings.
  • load_array: a shuffle argument invented without passages, is_train found with them.
  • Judge vs each labeler on Qwen3-4B’s answers: κ 0.89 and 0.77; the labelers vs each other: 0.81. On ours, 3 wrong answers: κ uninformative.
  • Re-batching changed 40% of answers in bf16; accuracy moved within noise.
  • Closed 0.32, baseline 0.91, oracle 0.93: the generator limits accuracy.

Next: Which Stage Lost Each Wrong Answer?

Next

The baseline leaves 21 dev answers wrong, 15 of the 20 answerable ones with a relevant passage in the context. The next section changes one stage at a time, sorts the failures by stage and tests a fix chosen in advance.