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

Cast predict scores to float before converting to numpy #2783

Merged
merged 2 commits into from
Jun 26, 2024
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
2 changes: 1 addition & 1 deletion sentence_transformers/cross_encoder/CrossEncoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def predict(
if convert_to_tensor:
pred_scores = torch.stack(pred_scores)
elif convert_to_numpy:
pred_scores = np.asarray([score.cpu().detach().numpy() for score in pred_scores])
pred_scores = np.asarray([score.cpu().detach().float().numpy() for score in pred_scores])

if input_was_string:
pred_scores = pred_scores[0]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_cross_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path
from typing import Generator, List, Tuple

import numpy as np
import pytest
import torch
from torch.utils.data import DataLoader
Expand Down Expand Up @@ -171,3 +172,12 @@ def test_safe_serialization(safe_serialization: bool) -> None:
model.save(cache_folder, safe_serialization=safe_serialization)
model_files = list(Path(cache_folder).glob("**/pytorch_model.bin"))
assert 1 == len(model_files)


def test_bfloat16() -> None:
model = CrossEncoder("cross-encoder/stsb-distilroberta-base", automodel_args={"torch_dtype": torch.bfloat16})
score = model.predict([["Hello there!", "Hello, World!"]])
assert isinstance(score, np.ndarray)

ranking = model.rank("Hello there!", ["Hello, World!", "Heya!"])
assert isinstance(ranking, list)
Loading