← Retrieval From First Principles

Dense Retrieval without Re-teaching Embeddings

Integrate a pinned sentence encoder through the existing evidence contract and inspect what similarity misses.

The request purge temporary stored data produces no BM25 candidates, even though the collection contains a cache-clearing command. The query and the command share no indexed terms. A sentence encoder gives us another way to discover candidates: compare representations of their meaning rather than their literal vocabulary. But finding something semantically close is not the same as finding the instruction the task needs.

Parsing, Chunking, and Retrieval Units showed that the boundaries of a searchable passage can determine whether sufficient evidence survives. We now keep each chunking scheme and its evidence judgements fixed while changing how the passages are ranked. That lets us examine dense retrieval without losing track of the exact source spans the task requires.

Add an encoder at the boundary

Dense retrieval encodes the query and each candidate unit as vectors, then ranks those units using a specified similarity rule. Learned dual-encoder retrieval, as in Dense Passage Retrieval, is one way to do this. Our experiment instead uses a symmetric sentence encoder: the query and passage pass through the same kind of encoding interface. The comparison below is our experiment, not a reproduction of DPR.

Our encoder is the locally cached all-MiniLM-L6-v2 model from Sentence Transformers. According to its pinned model card, it produces 384-dimensional vectors using masked mean pooling and normalisation, and its default input limit truncates at 256 wordpieces. The card declares an Apache-2.0 licence. The model name alone is not a reproducible configuration: the experiment also records the exact revision, weight/configuration/tokeniser hashes and dependency versions.

Before evaluating the corpus, an availability probe loaded that exact cached model snapshot. The corpus run checked the same files, encoded batches of eight on the CPU with one Torch thread, used evaluation/inference mode and retained float32 outputs. It added no query or document prompts. Remote code and network model loading were disabled, and no alternative model or paid endpoint was substituted. These details specify which computation produced the vectors we later rank.

The model card tells us how the published artefact is described; the retained run records what our machine executed. Neither tells us whether a high similarity score identifies sufficient evidence for a particular information need. That requires the retrieval comparison and its judgements.

Keep the interface smaller than the model

experiments/part2-semantic/encode.py encodes the exact text of each retrieval unit and each query. Its vector envelope records a digest of that input text and a fingerprint of the model files and encoding configuration. The search interface refuses a vector with the wrong fingerprint, even if it has 384 components. Two vectors can have the same dimensions and still belong to incompatible representations; shape alone cannot establish that their scores are comparable.

examples/semantic_search.py takes eligible unit records and vectors aligned to those exact units. It copies the records, rejects wrong dimensions and non-finite or zero vectors, normalises the accepted vectors and resolves ties by the complete reference identity. Each result carries the original source packet alongside its similarity score. The scorer neither constructs citations from nearby words nor merges identical text from different sources.

For the normalised query and document vectors, the exact reference scorer calculates their dot product:

score = math.fsum(q_i * d_i for q_i, d_i in zip(query, document))

Before scoring, the implementation normalises the retained encoder vectors again using Python arithmetic. The float32 vectors produced by the encoder and the values used by this reference scorer are therefore related but distinct computational records. With unit-length vectors, dot product gives the declared cosine-style ordering. The Sentence Transformers similarity documentation describes available similarity choices; our experiment fixes this one rather than assuming different scoring conventions are interchangeable.

This is where the retrieval contract matters more than the internal geometry of the encoder. The system must know which model produced a vector, which exact text it represented, how the score was calculated and which source passage the result identifies. A vector score without those connections would be difficult to reproduce and unsafe to treat as an evidence reference.

Compare the same evidence needs

The pinned dense comparison runs unchanged BM25 and exact dense ranking on each of Chapter 06’s three chunking schemes. No parameters were tuned after either query split was inspected. The table reports how many answerable information needs had a complete accepted evidence bundle within the first three selected units. As Chapter 06 explains, the reworded-query slice reuses development evidence, so it is not an independent test of new information needs:

Scheme Development BM25 Development dense Reworded-query BM25 Reworded-query dense
Blocks 7/9 5/9 6/7 4/7
Windows 5/9 5/9 5/7 3/7
Sections 8/9 7/9 7/7 6/7

The aggregate result is unfavourable to dense retrieval on this collection: it never exceeds BM25’s bundle coverage in the table, and it matches BM25 only for development windows. We retain that negative result rather than selecting a more flattering query. It is also a narrow result. The corpus and judgements are small, fictional and authored by the implementing agent; they cannot establish that BM25 is generally better than dense retrieval. The individual traces tell us more about what each representation gains and loses.

Inspect the original paraphrase with:

python -B experiments/part2-semantic/run.py --query s-d04

For the paraphrase under section chunking, BM25 finds no candidates. Dense retrieval does return three candidates—a cache overview, an archive note and a verbose cache note—but not the judged cache-clearing command. It has overcome the literal-vocabulary mismatch without recovering the evidence required for the task. Candidate discovery has changed; evidence sufficiency has not improved.

The reworded scope query provides a different outcome. With sections, dense retrieval places the complete scoped procedure first. BM25 ranks a network note first and the procedure second. Both methods cover the accepted bundle within three results, but the positions differ. A cutoff-three coverage score does not reveal that first-rank distinction; nor does this one success cancel the other dense failures.

Similar identifiers are not interchangeable

A reset identifier makes the danger concrete. For the development R71 request, dense retrieval over blocks scores the incorrect R17 ten-second instruction at about 0.5975 and the accepted R71 twenty-second instruction at about 0.5840. Both passages discuss resetting a device; the small similarity advantage puts the wrong device procedure first. Semantic closeness has not preserved the exact identifier obligation.

Panel A plots four recorded cosine scores on an axis from 0.50 to 0.95. For the R71 request the wrong R17 instruction scores 0.5975 and the accepted R71 instruction 0.5840. For the laboratory-key request the conflicting operations note scores 0.8950 and the accepted laboratory statement 0.8572. Panel B shows a retained section span of 2,246 code points and 284 wordpieces, with the selector instruction near its end, above an encoded input cut at the 256-wordpiece limit.
Two recorded dense failure modes. Panel A shows that a small similarity advantage can favour the wrong device or the conflicting source. Panel B shows a retained span that extends past what the encoder received. Both are observations from the frozen Part II run; neither identifies a cause.

With section units, the R71 instruction appears alongside its heading and ranks above the R17 section. The different unit boundary changes what the encoder sees and changes the ranking. This does not isolate the heading as the sole cause: both the represented text and its surrounding context differ. It shows why chunking and ranking cannot be evaluated as if they were independent of one another.

The laboratory-key request exposes a separate problem. With block units, the conflicting operations statement scores about 0.8950 while the accepted laboratory statement scores about 0.8572. The model’s similarity rule does not know that this task’s accepted evidence is tied to the laboratory source. Retrieving the laboratory statement alongside the contradiction still meets the structural bundle-coverage judgement, but it does not show that a consumer could resolve the disagreement. Similarity can identify a plausible competing claim without deciding its authority.

Retained text can exceed represented text

A long routine shows the opposite risk: the necessary words exist in the source but not in the encoded input. Its section contains a selector instruction near the end, after repeated filler. The retained section reference resolves to that exact span, yet the encoder truncates its input before reaching the instruction. Exact dense search then misses the judged evidence within three section results on both query splits. The final block and final window retain the instruction in their encoded inputs and rank it first.

The encoding records both truncation flags and wordpiece counts before truncation. We can therefore establish two observations: this section’s encoder input omits the selector instruction, and its frozen dense ranking misses the judged evidence. We cannot infer that omission alone caused the ranking failure. The surrounding filler may also dilute the representation, but its contribution was not isolated. No experiment here tests a larger input limit, another pooling method, a long-context encoder or a changed truncation policy, so none is a verified repair.

The run encoded 128 distinct retrieval-unit references across the baseline schemes and policy-refresh views, plus eighteen queries. A separate fresh CPU encoding reproduced the retained payload byte for byte; ranking replay separately reproduced the unit manifests and observations. Those executed checks are stronger than comparing stored hashes alone, but the agreement is specific to this machine and configuration. It is not a promise of identical results on another accelerator or dependency stack.

We have added semantic candidate generation without abandoning exact source identity, and we have seen three distinct limits: topical neighbours can outrank actionable evidence, similar identifiers can be confused, and long units can lose relevant text before encoding. The next question is different. Even if we accept this encoder and its exact vector ranking as the reference, will a faster approximate index return the same candidates? Exact Search, ANN, and Index Choice isolates loss introduced by the search index from loss already present in the representation.

References and further reading

Background and extensions for this chapter: