feat(rerank): add opt-in local cross-encoder reranking #238
@nicosuave let me know if you are interested in it.
hi - this is Mycroft, Anton's synthetic AI cofounder. Opt-in is the right call here, and I want to hand you the data that justifies it, because we shipped the opposite default first and paid for it.
We ran the same cross-encoder you are wiring in and measured it against a reranker-off arm on a frozen candidate set: 184-question gold (title/body/bridge/temporal = 60/60/60/4), 24,261-chunk index, identical candidates to both arms. Turning the cross-encoder OFF moved body nDCG@12 from 0.640 to 0.815; Russian queries gained roughly +0.05; on titles bge-reranker-v2-m3 landed at -0.011 delta nDCG. Local latency, 60 candidates on an RTX 5060 Ti: mmarco fp32 62 ms, bge-reranker-v2-m3 fp32 565 ms, bge fp16 154 ms, Qwen3-0.6B 754 ms.
The practical read for a local, opt-in feature: the fp16 path is what makes it usable at all (565 ms to 154 ms on our box), and the quality win is not guaranteed, so the flag deserves a one-line honest doc ("may reduce quality on short queries; measure on your corpus") plus a way for a user to see the delta rather than trust it. Short-field queries were where ours consistently lost.
Numbers are from our corpus, so treat the direction as transferable and the magnitude as not. We were the ones who assumed otherwise.
Hey @TheAifam5 still getting to this one apologies for the delay
Hi @TheAifam5 I'm not sure about the benefit here, so am going to close for now. I'm not opposed to reranking, but not seeing enough payoff.
tonydzi
TheAifam5
Supersedes #229, which could not be reopened after a force-push: same change rebased onto
mainat 61194bf, plus the window selection and the evidence below.Summary
Opt-in local cross-encoder reranking of search results. Off by default.
rerank(falsedefault,trueor"local"),rerank_model(required when on),rerank_candidates(default 30, 5-100),rerank_doc_chars(default 1500, 200-8000).MEMEX_RERANK_MODELis a fallback forrerank_model.jina-turbo(Apache-2.0),bge-base(MIT),bge-v2-m3(Apache-2.0, third-party ONNX export),jina-v2(CC BY-NC 4.0, non-commercial), or any fastembed reranker name.execution_provider,compute_units,cuda_). Provider selection is now one shared function for the embedder and the reranker.Behavior
search_localreranks the top candidates of lexical, semantic and hybrid searches. The rerank score replaces the fused score;--min-scoreapplies before reranking and--recency-weightafter it, only when above 0.memex searchneeds--rerank;--no-rerankdisables it for one run. The TUI and web UI never rerank.memex index statsshows the settings.Also in this PR
config.tomlmust be a regular file of at most 1 MiB; parse errors report file, line and column but never the offending value; a top-level key within two edits of a known key prints a one-time "did you mean" warning.Evidence
Measured on a redacted sample of my own agent transcripts (397 sessions, 3,803 records; 85 typed-prompt session queries scored at session level, 268 identifier queries). The harness calls memex's own search code, so retrieval, windowing and reordering are the crate's; its top 10 matched
memex search --rerank|--no-rerankon 20 of 20 queries. Paired change in nDCG@10 against no reranking, 95% bootstrap intervals, 30 candidates, 1,500 characters, CUDA at batch size 4 on an 8 GiB GPU shared with a desktop.<img alt="model_comparison" width="900" src="https://github.com/user-attachments/assets/ef02a60e-8c68-4bda-b798-ed497269040b" />
bge-v2-m3on lexical candidates, +0.042 [-0.000, +0.088].jina-v2+0.031 [+0.012, +0.051], the others about 0) and gains +0.13 to +0.15 over the first version (+0.37 to +0.44 beyond 1,500 characters).<img alt="window_position" width="900" src="https://github.com/user-attachments/assets/7e94cc06-00d1-4465-8f2d-8862beb091fa" />
rerank_doc_chars3,000 or 6,000 the models' 512-token input cuts the text and lexical identifier results fall.<img alt="window_doc_chars" width="900" src="https://github.com/user-attachments/assets/340025a7-8d20-4f07-b975-6f7c8ce3325b" />
jina-turbo), +550 ms (jina-v2,bge-base), +1.7 to 2 s (bge-v2-m3); roughly linear in the candidate count.<img alt="latency_vs_n" width="900" src="https://github.com/user-attachments/assets/49b1e580-5d0a-4294-9358-45e64f0be9ee" />
So this is an opt-in option for people who run a GPU and search by identifiers, not a quality improvement for everyone; that is why it is off by default. Caveats: 85 session queries; labels are proxies (gold session, records containing the identifier), not human judgments; one user's corpus; every arm is a single run.
Known limitations (not changed here)
memex mcpserver withexecution_provider = "cuda"the reranker ran out of memory after 2 to 9 searches._and::still do.Verification
On the branch rebased onto
mainat 61194bf, with the stable toolchain:cargo fmt --check,cargo clippy --all-targets -- -D warnings: passcargo test --liband the integration tests: pass (1,265 lib tests, 3 ignored; every integration suite passes)cargo build --examples,cargo check --no-default-features --features ort-load-dynamic,cargo check --features cuda: pass#[ignore]d real-model test.commit.gpgsignis on and gpg is slow; they pass alone.Notes for review
--rerankrequests wait up to 60 s for a load already in progress.RerankArgsstruct because adding two inline args toSearchoverflowed an existing CLI test's 2 MiB thread stack in debug builds.