Files
mem0ai--mem0/tests/rerankers/test_huggingface_reranker_normalize.py
wehub-resource-sync 555e282cc4
pi-agent-plugin checks / lint (push) Has been cancelled
pi-agent-plugin checks / test (20) (push) Has been cancelled
pi-agent-plugin checks / test (22) (push) Has been cancelled
pi-agent-plugin checks / build (push) Has been cancelled
TypeScript SDK CI / check_changes (push) Has been cancelled
TypeScript SDK CI / changelog_check (push) Has been cancelled
ci / changelog_check (push) Has been cancelled
ci / check_changes (push) Has been cancelled
ci / build_mem0 (3.10) (push) Has been cancelled
ci / build_mem0 (3.11) (push) Has been cancelled
ci / build_mem0 (3.12) (push) Has been cancelled
CLI Node CI / lint (push) Has been cancelled
CLI Node CI / test (20) (push) Has been cancelled
CLI Node CI / test (22) (push) Has been cancelled
CLI Node CI / build (push) Has been cancelled
CLI Python CI / lint (push) Has been cancelled
CLI Python CI / test (3.10) (push) Has been cancelled
CLI Python CI / test (3.11) (push) Has been cancelled
CLI Python CI / test (3.12) (push) Has been cancelled
CLI Python CI / build (push) Has been cancelled
openclaw checks / lint (push) Has been cancelled
openclaw checks / test (20) (push) Has been cancelled
openclaw checks / test (22) (push) Has been cancelled
openclaw checks / build (push) Has been cancelled
opencode-plugin checks / build (push) Has been cancelled
TypeScript SDK CI / build_ts_sdk (20) (push) Has been cancelled
TypeScript SDK CI / build_ts_sdk (22) (push) Has been cancelled
TypeScript SDK CI / integration_ts_sdk (20) (push) Has been cancelled
TypeScript SDK CI / integration_ts_sdk (22) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:03:45 +08:00

52 lines
1.9 KiB
Python

"""Unit tests for HuggingFaceReranker score normalization.
These exercise the pure ``_normalize_scores`` helper directly, so they do not
require ``transformers`` / ``torch`` to be installed.
"""
import math
import pytest
from mem0.reranker.huggingface_reranker import HuggingFaceReranker
def _sigmoid(x):
return 1.0 / (1.0 + math.exp(-x))
class TestHuggingFaceNormalizeScores:
def test_logits_mapped_via_sigmoid(self):
scores = HuggingFaceReranker._normalize_scores([2.0, 8.0, 5.0])
assert scores == pytest.approx([_sigmoid(2.0), _sigmoid(8.0), _sigmoid(5.0)])
def test_output_bounded_between_zero_and_one(self):
for s in HuggingFaceReranker._normalize_scores([-12.0, -1.0, 0.0, 3.0, 15.0]):
assert 0.0 <= s <= 1.0
def test_sigmoid_preserves_ranking_order(self):
raw = [1.0, -4.0, 9.0, 2.5]
normalized = HuggingFaceReranker._normalize_scores(raw)
# argsort of raw and normalized must match — sigmoid is monotonic.
assert sorted(range(len(raw)), key=lambda i: raw[i]) == sorted(
range(len(normalized)), key=lambda i: normalized[i]
)
def test_single_score_not_collapsed_to_zero(self):
# Regression: a lone document used to normalize to ~0.0 under min-max.
# A positive logit must now yield a clearly-relevant score (> 0.5).
(score,) = HuggingFaceReranker._normalize_scores([4.2])
assert score == pytest.approx(_sigmoid(4.2))
assert score > 0.5
def test_tied_scores_not_collapsed_to_zero(self):
# Regression: tied candidates all collapsed to ~0.0 under min-max.
scores = HuggingFaceReranker._normalize_scores([3.0, 3.0, 3.0])
assert scores == pytest.approx([_sigmoid(3.0)] * 3)
def test_zero_logit_maps_to_half(self):
assert HuggingFaceReranker._normalize_scores([0.0]) == pytest.approx([0.5])
def test_empty_scores(self):
assert HuggingFaceReranker._normalize_scores([]) == []