Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes implementation of similarity() #1748

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions mteb/evaluation/evaluators/RetrievalEvaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ def search(
self.corpus_embeddings[request_qid].append(sub_corpus_embeddings)

# Compute similarites using self defined similarity otherwise default to cosine-similarity
similarity_scores = cos_sim(query_embeddings, sub_corpus_embeddings)
if hasattr(self.model, "similarity"):
similarity_scores = self.model.similarity(
float(self.model.similarity(e1, e2))
for e1, e2 in zip(query_embeddings, sub_corpus_embeddings)
query_embeddings, sub_corpus_embeddings
)
else:
similarity_scores = cos_sim(query_embeddings, sub_corpus_embeddings)
is_nan = torch.isnan(similarity_scores)
if is_nan.sum() > 0:
logger.warning(
Expand Down Expand Up @@ -376,6 +376,9 @@ def __init__(self, model, **kwargs):
self.save_corpus_embeddings = kwargs.get("save_corpus_embeddings", False)
self.corpus_embeddings = {}

if hasattr(self.model, "similarity") and callable(self.model.similarity):
Samoed marked this conversation as resolved.
Show resolved Hide resolved
self.similarity = self.model.similarity

def encode_corpus(
self,
corpus: list[dict[str, str]],
Expand Down
2 changes: 1 addition & 1 deletion mteb/models/sentence_transformer_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(
if isinstance(self.model, CrossEncoder):
self.predict = self._predict

if hasattr(self.model, "similarity"):
Samoed marked this conversation as resolved.
Show resolved Hide resolved
if hasattr(self.model, "similarity") and callable(self.model.similarity):
self.similarity = self.model.similarity

def encode(
Expand Down
Loading