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 problem
Question: in PyTorch, what happens to modules kept in a plain Python list instead of nn.Sequential?
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.
Goals
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.
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 |
The question set
| type | single | explain | numeric | code | multihop | unanswerable |
|---|---|---|---|---|---|---|
dev, test each |
120 | 40 | 20 | 20 | 20 | 20 |
train, 32 dev, 32 test, so a system tuned on train never saw the passage of a dev answer.Spans, not chunk ids: relevance is recomputed for any chunking.
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
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.
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.
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.
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 |
Accuracy rests on correct; the other two say whether a reader can check the answer against its sources.
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.
is_trainOne 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
base-repeat: same model, same contexts, greedy decoding; only the neighbors in each batch of 8 differ.
Greedy decoding is deterministic only for a fixed batch, precision and kernel. Batch alike, or count re-batching as noise.
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 |
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
dev questions with gold spans, made and checked by models.cov@T compares chunkings.load_array: a shuffle argument invented without passages, is_train found with them.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.