agentmemory icon indicating copy to clipboard operation
agentmemory copied to clipboard

Reranker is a no-op: text-classification softmax over single-logit cross-encoder always returns 1.0

Open arumata opened this issue 6 months ago • 1 comments

Summary

RERANK_ENABLED=true is a silent no-op. The cross-encoder rerank step added in #93 always assigns the same score (1.0) to every candidate, so it never reorders results — it only adds latency and an ~80 MB model download.

Root cause

Xenova/ms-marco-MiniLM-L-6-v2 is a cross-encoder with a single relevance-logit head. src/state/reranker.ts runs it through a text-classification pipeline:

// src/state/reranker.ts:18
pipeline = await createPipeline("text-classification", "Xenova/ms-marco-MiniLM-L-6-v2", { quantized: true });
...
// src/state/reranker.ts:56
const score = Array.isArray(output) ? output[0]?.score ?? 0 : 0;

The text-classification pipeline applies softmax over the model's logits. With a single-logit head, softmax([x]) === [1.0] for any input. So output[0].score is always 1.0, and every candidate gets an identical rerank score.

Worse: the function then overwrites the real retrieval score with this constant —

return scores.map((s, i) => ({ ...s.result, combinedScore: s.rerankScore /* = 1.0 */, rerankPosition: i + 1 }));

so the BM25+vector+graph (RRF) signal is discarded. The output order only survives because Array.prototype.sort is stable.

Reproduction

Calling the model the way the code does vs. reading the raw logit:

text-classification (current code):
  pair A: [{"label":"LABEL_0","score":1}]
  pair B: [{"label":"LABEL_0","score":1}]      // identical for every input

raw cross-encoder logit (correct usage):
  pair A = -11.162   pair B = -10.661           // discriminative

End to end, with RERANK_ENABLED=true + a local embedding provider, smart-search returns byte-for-byte the same ranking as RERANK_ENABLED unset, and every result reports score: 1.000.

Impact

  • RERANK_ENABLED=true does nothing useful — pure cost (model load + per-query inference over the top-20).
  • The retrieval combinedScore is clobbered to 1.0, so any downstream consumer reading that score gets garbage.

Suggested fix

Load the model with AutoModelForSequenceClassification and read the raw logit (encode the pair via the tokenizer's text_pair), instead of the softmax text-classification pipeline. Higher logit = more relevant.

⚠️ Heads-up: the fix is necessary but not sufficient

I patched it locally to read the raw logit (scores became discriminative, e.g. -9.56 … -11.43). But once the reranker actually runs, ms-marco-MiniLM-L-6-v2 — trained on MS MARCO web-search passages — is strongly lexical and degrades abstractive memory recall: on a small adversarial set (query paraphrases with zero keyword overlap), enabling the fixed reranker dropped correct hits out of the top results and made recall worse than no reranker.

Two things to reconsider alongside the softmax fix:

  1. Don't replace combinedScore outright. Blend the rerank score with the retrieval score (or only promote, never demote below retrieval) so a weak cross-encoder can't wreck a good RRF ordering.
  2. Model choice. A stronger reranker (e.g. bge-reranker) may suit semantic memory recall better than the MS MARCO model.

Environment

  • agentmemory v0.9.24 (main @ fd9e3bd), src/state/reranker.ts unchanged since #93.
  • Reranker: Xenova/ms-marco-MiniLM-L-6-v2, @xenova/transformers ^2.17.2.

arumata avatar May 29 '26 18:05 arumata

Confirming this on @agentmemory/[email protected] and adding the numbers, plus one extra data point on the proposed workaround.

Setup: Xenova/ms-marco-MiniLM-L-6-v2 at dtype: "q8", @huggingface/[email protected], in the published Docker image. Same process, same loaded model, four (query, document) pairs — two relevant, two deliberately irrelevant.

Via the text-classification pipeline (what src/state/reranker.ts does):

rerank "Check Docker container health status via SSH" -> [{"label":"LABEL_0","score":1}]
rerank "The weather in Paris is nice today"           -> [{"label":"LABEL_0","score":1}]

Every candidate scores exactly 1. Confirmed.

The underlying model is fine, though. Reading the logits tensor directly (AutoModelForSequenceClassification, bypassing the pipeline) gives a clearly separated, correctly-ordered signal:

query document logit
Docker container health check Check Docker container health status via SSH -4.498
Docker container health check The weather in Paris is nice today -8.332
数据库连接失败 排查 postgres 连接超时与连接池耗尽 -3.950
数据库连接失败 今天中午吃什么比较好 -4.834

Relevant documents score higher in both languages, so the cross-encoder is doing its job — the single-logit softmax in the pipeline is what collapses that spread to a constant.

On the "use raw logits" workaround: passing { function_to_apply: "none" } to the pipeline call still returns score: 1 on this version, so that option does not appear to be honored for this pipeline here. A fix has to read the logits tensor (or apply the activation itself), not just change the pipeline option.

Impact beyond ordering: src/state/hybrid-search.ts returns combinedScore: s.rerankScore, and src/functions/search.ts:798 / smart-search.ts:204 surface that as the response score field — so with RERANK_ENABLED=true every search result reports score: 1.0. The order is unaffected (a stable sort over equal keys preserves the RRF ranking), but the score is lost.

Suggested fix direction: sort by the raw logit (optionally mapped through a sigmoid if a 0–1 combinedScore is required downstream) and stop overwriting combinedScore, which currently discards the BM25+vector+graph signal.

cbsincool avatar Oct 09 '26 08:10 cbsincool