Repository navigation
fix: reject multi-label cross encoders in SASEvaluator - #13251
Conversation
|
@AryanRG is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
8b1af31 to
d3ad109
Compare
|
Confirmed the fix locally. On main, the repro with a three-label CrossEncoder mock returns |
|
Hi @AryanRG! What's the status on this PR? It's in draft since about 4 weeks. Are you planning to keep on working on this or should we close it? |
|
@bogdankostic yes, I’d like to finish this — sorry for leaving it in draft. Rechecked the fix against current main; focused tests, type/style checks and pre-commit pass. It’s ready for review now. |
bogdankostic
left a comment
There was a problem hiding this comment.
Thanks for working on this @AryanRG! I think the check should live in warm_up() instead of run(). I left a few inline comments on this and on the test.
| # SAS needs one similarity score per pair, so multi-label models (for example NLI) are not supported | ||
| num_labels = self._similarity_model.num_labels | ||
| if num_labels != 1: | ||
| msg = ( | ||
| f"SASEvaluator requires a Cross Encoder with exactly one output score, but '{self._model}' " | ||
| f"has {num_labels} labels. Use a single-score Cross Encoder or a Bi-Encoder " | ||
| "(SentenceTransformer) model." | ||
| ) | ||
| raise ValueError(msg) |
There was a problem hiding this comment.
Let's move this check into warm_up(), so that an unsupported model fails once at load time instead of on every non-empty run() call.
if cross_encoder_used:
cross_encoder = CrossEncoder(self._model, device=device, token=token)
if cross_encoder.num_labels != 1:
raise ValueError(msg)
self._similarity_model = cross_encoder| # The mocked model has a length of 0, so run would try to load the real model without this | ||
| monkeypatch.setattr(SASEvaluator, "warm_up", lambda self: None) |
There was a problem hiding this comment.
With the check in warm_up(), this test can follow test_warm_up_passes_token_to_hf: patch CrossEncoder and AutoConfig and call evaluator.warm_up(). Then we don't need this workaround here.
| evaluator = SASEvaluator(model="cross-encoder/nli-deberta-v3-xsmall") | ||
| model = MagicMock(spec=CrossEncoder) | ||
| model.num_labels = 3 | ||
| model.predict.return_value = np.array([[2.0, -1.0, 0.0]]) |
There was a problem hiding this comment.
This return value is never used, since the test asserts that predict isn't called. Let's remove it.
| model.predict.return_value = np.array([[2.0, -1.0, 0.0]]) |
| model.predict.return_value = np.array([[2.0, -1.0, 0.0]]) | ||
| evaluator._similarity_model = model | ||
|
|
||
| with pytest.raises(ValueError, match="3"): |
There was a problem hiding this comment.
match="3" also matches the v3 in the model name, so this passes regardless of the reported label count.
| with pytest.raises(ValueError, match="3"): | |
| with pytest.raises(ValueError, match="has 3 labels"): |
|
@bogdankostic moved validation into |
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
bogdankostic
left a comment
There was a problem hiding this comment.
Thanks for addressing the comments @AryanRG!
Congrats on your first contribution 🎉
Related Issues
Separate unsupported-model case found while independently validating #13244. That PR now owns the merged one-label normalization change; this PR only rejects multi-label Cross Encoders.
Proposed Changes:
A three-label CrossEncoder returns nested individual scores even though SASEvaluator declares a list of floats. The aggregate then averages class columns rather than scalar answer similarities.
Reject CrossEncoder models whose num_labels is not 1 in warm_up(), before caching the loaded model. Existing input validation, empty-input behavior, lazy warm-up, #13244's Identity/Sigmoid normalization, and bi-encoder behavior remain unchanged. The PR adds an original regression, model and exception docstrings, a concise documentation note, and a fixes release note.
How did you test it?
Rechecked the revised warm-up validation on November 11 at
f691f1e4f8eband against current mainbbe4efe50c71:All checks ran through Hatch using retained Python 3.12 runtimes under the workspace resource guards. Full-project tests, downloaded pretrained-model integration tests, docs build and additional platform/Python combinations were not rerun. Hosted workflows may require maintainer approval; approval-gated or empty checks are not passing-test evidence.
Notes for the reviewer
#13244 merged after this PR opened and touched the same CrossEncoder branch. This branch is now rebased directly onto that merge. Its parameterized one-label mock explicitly sets num_labels = 1 so both Identity and Sigmoid normalization tests exercise the intended supported model. The #13244 production change, tests, imports, and release note remain base-only and do not appear in this PR's four-file diff.
A label-selection API would be a separate design. This change only rejects a shape that SASEvaluator cannot interpret as one semantic answer-similarity score.
This contribution was fully generated with AI assistance, including the implementation, regression test, documentation, and release note.
Checklist