Skip to content

Commit 1729aab

Browse files
authored
new: preserve embeddings in a type set by their model (#492)
* new: preserve embeddings in a type set by their model * fix: remove type coercion * fix: remove redundant type * fix: fix random data type in tests
1 parent 42fca3b commit 1729aab

7 files changed

Lines changed: 12 additions & 19 deletions

File tree

‎fastembed/image/onnx_embedding.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import Any, Iterable, Optional, Sequence, Type, Union
22

3-
import numpy as np
43

54
from fastembed.common.types import NumpyArray
65
from fastembed.common import ImageInput, OnnxProvider
@@ -195,7 +194,7 @@ def _preprocess_onnx_input(
195194
return onnx_input
196195

197196
def _post_process_onnx_output(self, output: OnnxOutputContext) -> Iterable[NumpyArray]:
198-
return normalize(output.model_output).astype(np.float32)
197+
return normalize(output.model_output)
199198

200199

201200
class OnnxImageEmbeddingWorker(ImageEmbeddingWorker[NumpyArray]):

‎fastembed/late_interaction/colbert.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _post_process_onnx_output(
4646
self, output: OnnxOutputContext, is_doc: bool = True
4747
) -> Iterable[NumpyArray]:
4848
if not is_doc:
49-
return output.model_output.astype(np.float32)
49+
return output.model_output
5050

5151
if output.input_ids is None or output.attention_mask is None:
5252
raise ValueError(
@@ -58,11 +58,11 @@ def _post_process_onnx_output(
5858
if token_id in self.skip_list or token_id == self.pad_token_id:
5959
output.attention_mask[i, j] = 0
6060

61-
output.model_output *= np.expand_dims(output.attention_mask, 2).astype(np.float32)
61+
output.model_output *= np.expand_dims(output.attention_mask, 2)
6262
norm = np.linalg.norm(output.model_output, ord=2, axis=2, keepdims=True)
6363
norm_clamped = np.maximum(norm, 1e-12)
6464
output.model_output /= norm_clamped
65-
return output.model_output.astype(np.float32)
65+
return output.model_output
6666

6767
def _preprocess_onnx_input(
6868
self, onnx_input: dict[str, NumpyArray], is_doc: bool = True, **kwargs: Any

‎fastembed/late_interaction_multimodal/colpali.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def _post_process_onnx_image_output(
142142
assert self.model_description.dim is not None, "Model dim is not defined"
143143
return output.model_output.reshape(
144144
output.model_output.shape[0], -1, self.model_description.dim
145-
).astype(np.float32)
145+
)
146146

147147
def _post_process_onnx_text_output(
148148
self,
@@ -157,7 +157,7 @@ def _post_process_onnx_text_output(
157157
Returns:
158158
Iterable[NumpyArray]: Post-processed output as NumPy arrays.
159159
"""
160-
return output.model_output.astype(np.float32)
160+
return output.model_output
161161

162162
def tokenize(self, documents: list[str], **kwargs: Any) -> list[Encoding]:
163163
texts_query: list[str] = []

‎fastembed/text/onnx_embedding.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import Any, Iterable, Optional, Sequence, Type, Union
22

3-
import numpy as np
43
from fastembed.common.types import NumpyArray, OnnxProvider
54
from fastembed.common.onnx_model import OnnxOutputContext
65
from fastembed.common.utils import define_cache_dir, normalize
@@ -313,7 +312,7 @@ def _post_process_onnx_output(self, output: OnnxOutputContext) -> Iterable[Numpy
313312
processed_embeddings = embeddings
314313
else:
315314
raise ValueError(f"Unsupported embedding shape: {embeddings.shape}")
316-
return normalize(processed_embeddings).astype(np.float32)
315+
return normalize(processed_embeddings)
317316

318317
def load_onnx_model(self) -> None:
319318
self._load_onnx_model(

‎fastembed/text/pooled_embedding.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def _post_process_onnx_output(self, output: OnnxOutputContext) -> Iterable[Numpy
116116

117117
embeddings = output.model_output
118118
attn_mask = output.attention_mask
119-
return self.mean_pooling(embeddings, attn_mask).astype(np.float32)
119+
return self.mean_pooling(embeddings, attn_mask)
120120

121121

122122
class PooledEmbeddingWorker(OnnxTextEmbeddingWorker):

‎fastembed/text/pooled_normalized_embedding.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import Any, Iterable, Type
22

3-
import numpy as np
43

54
from fastembed.common.types import NumpyArray
65
from fastembed.common.onnx_model import OnnxOutputContext
@@ -145,7 +144,7 @@ def _post_process_onnx_output(self, output: OnnxOutputContext) -> Iterable[Numpy
145144

146145
embeddings = output.model_output
147146
attn_mask = output.attention_mask
148-
return normalize(self.mean_pooling(embeddings, attn_mask)).astype(np.float32)
147+
return normalize(self.mean_pooling(embeddings, attn_mask))
149148

150149

151150
class PooledNormalizedEmbeddingWorker(OnnxTextEmbeddingWorker):

‎tests/test_custom_models.py‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,11 @@ def test_mock_add_custom_models():
9191
expected_output = {
9292
f"{PoolingType.MEAN.lower()}-normalized": normalize(
9393
mean_pooling(dummy_token_embedding, dummy_attention_mask)
94-
).astype(np.float32),
95-
f"{PoolingType.MEAN.lower()}": mean_pooling(dummy_token_embedding, dummy_attention_mask),
96-
f"{PoolingType.CLS.lower()}-normalized": normalize(dummy_token_embedding[:, 0]).astype(
97-
np.float32
9894
),
95+
f"{PoolingType.MEAN.lower()}": mean_pooling(dummy_token_embedding, dummy_attention_mask),
96+
f"{PoolingType.CLS.lower()}-normalized": normalize(dummy_token_embedding[:, 0]),
9997
f"{PoolingType.CLS.lower()}": dummy_token_embedding[:, 0],
100-
f"{PoolingType.DISABLED.lower()}-normalized": normalize(dummy_pooled_embedding).astype(
101-
np.float32
102-
),
98+
f"{PoolingType.DISABLED.lower()}-normalized": normalize(dummy_pooled_embedding),
10399
f"{PoolingType.DISABLED.lower()}": dummy_pooled_embedding,
104100
}
105101

0 commit comments

Comments
 (0)