Skip to content
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
5 changes: 3 additions & 2 deletions model2vec/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,9 @@ def _encode_batch(self, sentences: Sequence[str], normalize: bool) -> np.ndarray
out[i] = emb.mean(axis=0)

if normalize:
norm = np.linalg.norm(out, axis=1, keepdims=True) + 1e-32
np.divide(out, norm, out=out)
out32 = out.astype(np.float32)
norm = np.linalg.norm(out32, axis=1, keepdims=True) + 1e-32
return (out32 / norm).astype(out.dtype)

return out

Expand Down
12 changes: 12 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ def test_encode_single_sentence_empty(
assert np.all(encoded == 0)


def test_encode_single_sentence_empty_float16(
mock_vectors: np.ndarray, mock_tokenizer: Tokenizer, mock_config: dict[str, str]
) -> None:
"""Test encoding of a single empty sentence with float16 embeddings."""
model = StaticModel(vectors=mock_vectors.astype(np.float16), tokenizer=mock_tokenizer, config=mock_config)
model.normalize = True
encoded = model.encode("")
assert not np.isnan(encoded).any()
assert np.all(encoded == 0)
assert encoded.dtype == model.embedding.dtype


def test_encode_multiple_sentences(
mock_vectors: np.ndarray, mock_tokenizer: Tokenizer, mock_config: dict[str, str]
) -> None:
Expand Down
Loading