Dev.to AI 🤖 Ai 👁 0 📖 4 min read

Is a cross-encoder reranker worth the latency? Measured on 2,908 FastAPI doc chunks

Tested on: FastAPI English docs (154 Markdown files, 2,908 heading-aware chunks), sentence-transformers 6.3.0, transformers 5.24.1, torch 2.15.0 CPU (1 torch thread, 2-core GitHub runner), cross-encoder/ms-marco-MiniLM-L

Tested on: FastAPI English docs (154 Markdown files, 2,908 heading-aware chunks), sentence-transformers 6.3.0, transformers 5.24.1, torch 2.15.0 CPU (1 torch thread, 2-core GitHub runner), cross-encoder/ms-marco-MiniLM-L-6-v2 reranker, all-MiniLM-L6-v2 embeddings. Query sets: 200 random heading queries, 200 random exact-identifier queries (seed 0), 30 hand-written paraphrases. Single run, no GPU. Full script and raw output in the Experiment files box.

Should you add a cross-encoder reranker after hybrid retrieval, and how many candidates should it score? I tested one small reranker on the FastAPI docs, with three query styles and three candidate depths.

Answer: it depends on the query type. On heading-style queries, reranking the top 20 hybrid candidates raised MRR from 0.710 to 0.889 (+0.179). On exact code identifiers it did nothing (0.679 to 0.677). It cost a median of 1,706 ms per query at 20 candidates on one CPU thread.

Setup

  • Candidates: BM25 plus vector search fused with RRF at alpha=0.5, the setup from hybrid search with BM25 and RRF. The chunker is heading-aware, as in the chunk size sweep.
  • Reranker: cross-encoder/ms-marco-MiniLM-L-6-v2, a 22.7M-parameter model, Apache-2.0. The model card reports NDCG@10 74.30 on TREC DL 2019 and about 1,800 docs/sec on a V100 GPU (vendor-reported, not my measurement).
  • Pipeline: fuse, take the top N, score each (query, chunk) pair with the cross-encoder, keep the top 5. N is 10, 20 or 50.
  • Queries: heading queries (the section title; target is that section), identifier queries (an inline-code name that appears in exactly one chunk), and 30 paraphrased questions (target is the right file). Heading and identifier sets are random samples of 200 to keep CPU time bounded.

Results

MRR, hit@1 and hit@5 are over the final top 5. "Recall@N" is how often the right chunk was inside the candidate list at all.

Query set Pipeline hit@1 hit@5 MRR recall@N
heading hybrid only 0.620 0.845 0.710
heading rerank top-10 0.770 0.890 0.821 0.900
heading rerank top-20 0.830 0.970 0.889 0.995
heading rerank top-50 0.835 0.970 0.893 1.000
identifier hybrid only 0.570 0.835 0.679
identifier rerank top-10 0.595 0.825 0.677 0.900
identifier rerank top-20 0.605 0.805 0.677 0.955
identifier rerank top-50 0.595 0.780 0.664 0.980
paraphrase (30) hybrid only 0.533 0.967 0.731
paraphrase (30) rerank top-10 0.600 0.967 0.758 1.000
paraphrase (30) rerank top-20 0.533 1.000 0.708 1.000
paraphrase (30) rerank top-50 0.533 0.967 0.688 1.000

Latency per query for scoring N pairs (30 queries, average chunk 598 characters):

Candidates Median p95
10 696 ms 891 ms
20 1,706 ms 2,528 ms
50 6,084 ms 8,194 ms

What the numbers mean

Headings: a clear win, and most of it comes from depth. Top-10 added 0.111 MRR; top-20 added 0.179; top-50 added 0.183. Going from 20 to 50 candidates added 0.004 MRR for 3.6x the latency. The reason is visible in recall@N: at 10 candidates the right chunk was present only 90.0% of the time, at 20 it was 99.5%. A reranker cannot promote a chunk it never sees.

Identifiers: no gain, slight loss. hit@5 fell from 0.835 to 0.780 at top-50 (-5.5 points). The MS MARCO-trained model scores natural-language relevance, and a bare token like response_model gives it little to work with, while BM25 already matches the token exactly. If your users paste error strings or function names, the reranker is not helping them.

Paraphrases: inconclusive. Hybrid already had the right file in the top 5 for 96.7% of queries. With 30 queries, one query moves a metric by 3.3 points, so the changes (+0.027 at top-10, -0.043 at top-50) are noise.

Latency scales roughly linearly with candidates, about 70 ms per pair at 10 and 85 ms at 20 (about 122 ms per pair at 50, where the longer batch was slower per pair). My runner used a single torch thread; a GPU or more cores would cut this a lot. Treat the absolute milliseconds as a floor on slowness, not a typical production figure.

Caveats

  • One reranker, one corpus (technical Markdown docs), one run. No confidence intervals.
  • Heading queries are easy for a cross-encoder: the query text is nearly the section title, and I prepend the heading path to each chunk. Real user questions may gain less.
  • The results.txt latency header says "2 CPU cores", but torch reported 1 thread in use; the 1-thread figure is the accurate one.
  • Samples of 200 mean a single query is 0.5 points of hit@k.

Code

The script reuses the corpus, chunker, BM25 and embedding code from the hybrid-search experiment, then adds the reranking step:

from sentence_transformers import CrossEncoder
ce = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2", max_length=512)

def rerank(query, candidate_ids, chunks, k=5):
    scores = ce.predict([(query, chunks[i]) for i in candidate_ids], batch_size=len(candidate_ids))
    return [i for _, i in sorted(zip(scores, candidate_ids), reverse=True)[:k]]

The full benchmark script and raw output are in the Experiment files box on the original post.

Rule of thumb

  • Rerank the top 20, not 10 or 50. Check recall@N first: if the right chunk is not in the candidates, reranking can't fix it.
  • Add a reranker when queries are natural-language and your hit@1 is far below hit@5. Skip it, or bypass it, for identifier-style queries.
  • Budget about 1.7 s for 20 pairs on one CPU thread with a MiniLM-sized model, or measure on your own hardware before committing.
  • Measure on your own golden set, as in the golden-set CI post, before shipping.

Originally published on recallrun.dev.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.