Two-Stage RAG Reranking: Cross-Encoders & MMR Diversity Selection
How Two-Stage Retrieval (Hybrid Top-50 → Cross-Encoder Top-5) dramatically boosts precision, how Maximal Marginal Relevance (MMR) prunes redundant near-duplicate chunks, and fixing Lost-in-the-Middle context ordering.
Two-Stage RAG retrieves 50 fast candidates first, then re-scores them with an accurate Cross-Encoder Reranker and deduplicates near-identical chunks via MMR (Maximal Marginal Relevance).
Fast bi-encoder retrieval often ranks a slightly off-topic chunk #1 or returns 5 near-duplicate paragraphs; reranking + MMR puts the sharpest, most diverse evidence in the prompt.
Adding a 40 ms Cross-Encoder Reranker + MMR Diversity filter is often the single biggest accuracy jump in RAG — frequently lifting answer accuracy by +10% to +20%!
Reranking lets you pass 5 sharp, non-redundant chunks instead of 20 noisy chunks — cutting LLM input token costs by 4× while improving answer faithfulness!
Without MMR, overlapping chunks from the same paragraph crowd out all 5 slots of your Top-5 context window, leaving multi-part user questions half-answered.
Classic RAG architecture question: 'Why can't we use a Cross-Encoder for Stage-1 search over 10M docs, and how does MMR prevent redundant chunks?'
In the MMR formula score(d) = λ · sim(d, Q) − (1 − λ) · max_{s ∈ S} sim(d, s), what happens when you set λ = 1.0?
- Design the 2-stage production RAG funnel: high-recall Hybrid retrieval (Top-50) → high-precision Cross-Encoder reranking (Top-5)
- Implement Maximal Marginal Relevance (MMR) greedy selection: argmax [λ·sim(d, Q) − (1−λ)·max sim(d, S)]
- Tune MMR's λ parameter from pure relevance (λ=1) to balanced diversity (λ≈0.5–0.7)
- Apply LongContextReorder to mitigate the LLM Lost-in-the-Middle U-shaped attention curve
Think of hiring for a 5-person team: Stage 1 (Hybrid Search) scans 10,000 resumes in seconds to pick the Top-50 finalists. Stage 2 (Cross-Encoder) conducts deep 1-on-1 interviews with those 50 finalists. Finally, MMR ensures you don't hire 5 identical frontend engineers — you hire 1 frontend, 1 backend, 1 ML, 1 infra, and 1 designer!
1. Two-Stage Retrieval Funnel: Bi-Encoder + Cross-Encoder
By running the expensive Cross-Encoder only on the Top-50 candidates from Stage 1, you get full cross-attention accuracy in just ~40 milliseconds!
Why Cross-Encoders Beat Bi-Encoders on Subtle Queries
A Bi-Encoder compresses a 400-word chunk into 1 vector before ever seeing the query. A Cross-Encoder reads [CLS] Q [SEP] D together, catching subtle negations, exact date constraints, and multi-hop keyword matches that single-vector dot products miss!
2. Maximal Marginal Relevance (MMR): Eliminating Redundant Clones
Drag the λ slider in the mini-lab below to see the exact point where Doc C (Diverse New Fact) overtakes Doc B (Near-Duplicate Clone) for Slot #2!
Greedy MMR Algorithm (Top-K Diverse Selection)
- Pick #1: ; initialize .
- Repeat until :
MMR λ Relevance-vs-Diversity Tuner (Pick Slot #2)
3. Beating 'Lost in the Middle' When Packing the LLM Prompt
Once you have your diverse Top-5 gold chunks, don't just dump them in descending order — respect the LLM's U-shaped attention curve!
Edge-Heavy Context Packing
Research (Liu et al., 2023) proved LLMs pay strongest attention to tokens at the very beginning and very end of the retrieved context block, and weakest attention to the middle. Ordering Top-5 chunks as [#1, #3, #5, #4, #2] places your two strongest pieces of evidence right at the two attention peaks!
Worked Example: One Step of MMR Selection (λ = 0.6)
Slot #1 already chose D1. Remaining candidates: D2 (rel=0.90, sim_to_D1=0.95) and D3 (rel=0.82, sim_to_D1=0.25).
.
In the Lost-in-the-Middle phenomenon, where in the retrieved context block do LLMs exhibit the LOWEST recall accuracy?
Why should MMR run on the small Top-50 candidate pool (not the 10-million-doc corpus)?
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
ColBERT stores per-token document embeddings offline and computes fast MaxSim sum of maximum token similarities at query time — approaching Cross-Encoder accuracy at near Bi-Encoder speed!
Two-Stage Cross-Encoder Rerank + MMR Diversity Funnel
Watch a buried gold chunk jump from #4 to #1 under Cross-Encoder attention, then watch MMR prune near-duplicates.
Multi-Facet Research & Legal RAG
Cross-Encoder + MMR ensures multi-part questions receive diverse citations across different sections and documents.
import torch
import torch.nn.functional as F
# Maximal Marginal Relevance (MMR): Balance Query Relevance vs. Result Diversity
def mmr_select(query_emb: torch.Tensor, doc_embs: torch.Tensor, k: int = 3, lam: float = 0.7) -> list[int]:
q = F.normalize(query_emb, dim=-1)
D = F.normalize(doc_embs, dim=-1)
rel = (D @ q).tolist()
sim_mat = (D @ D.T).tolist()
selected, remaining = [], list(range(len(D)))
for _ in range(min(k, len(D))):
best_i = max(
remaining,
key=lambda i: lam * rel[i] - (1 - lam) * (max(sim_mat[i][j] for j in selected) if selected else 0.0),
)
selected.append(best_i)
remaining.remove(best_i)
return selected
torch.manual_seed(0)
q = torch.randn(16)
docs = torch.stack([q + 0.01*torch.randn(16), q + 0.01*torch.randn(16), q + 0.5*torch.randn(16), torch.randn(16)])
print("MMR diverse top-3 indices (avoids near-duplicate #1):", mmr_select(q, docs, k=3, lam=0.6))
[0, 1] relevance and cosine doc-doc similarity!λ·sim(d, Q) − (1−λ)·max sim(d, S)), lowering λ from 0.8 down to 0.3 has what effect?