sentence-transformers-haystack 1.0.0__tar.gz → 2.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/CHANGELOG.md +14 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/PKG-INFO +1 -1
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/pyproject.toml +2 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/embedders/sentence_transformers/embedding_backend/backend.py +28 -1
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/embedders/sentence_transformers/embedding_backend/sparse_backend.py +6 -1
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/embedders/sentence_transformers/sentence_transformers_doc_image_embedder.py +3 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/embedders/sentence_transformers/sentence_transformers_document_embedder.py +3 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/embedders/sentence_transformers/sentence_transformers_sparse_document_embedder.py +3 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/embedders/sentence_transformers/sentence_transformers_sparse_text_embedder.py +3 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/embedders/sentence_transformers/sentence_transformers_text_embedder.py +12 -11
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/rankers/sentence_transformers/sentence_transformers_diversity.py +11 -1
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/rankers/sentence_transformers/sentence_transformers_similarity.py +11 -1
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_sentence_transformers_diversity.py +21 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_sentence_transformers_embedding_backend.py +25 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_sentence_transformers_similarity.py +21 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_sentence_transformers_sparse_embedding_backend.py +14 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_sentence_transformers_text_embedder.py +8 -24
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/.gitignore +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/LICENSE.txt +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/README.md +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/pydoc/config_docusaurus.yml +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/embedders/py.typed +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/embedders/sentence_transformers/__init__.py +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/embedders/sentence_transformers/embedding_backend/__init__.py +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/rankers/py.typed +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/src/haystack_integrations/components/rankers/sentence_transformers/__init__.py +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/__init__.py +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/conftest.py +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_files/images/apple.jpg +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_files/images/haystack-logo.png +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_files/images/image_metadata.png +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_files/pdf/sample_pdf_1.pdf +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_hybrid_rag_e2e.py +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_sentence_transformers_doc_image_embedder.py +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_sentence_transformers_document_embedder.py +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_sentence_transformers_sparse_document_embedder.py +0 -0
- {sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/test_sentence_transformers_sparse_text_embedder.py +0 -0
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [integrations/sentence_transformers-v2.0.0] - 2026-10-23
|
|
4
|
+
|
|
5
|
+
### 🚜 Refactor
|
|
6
|
+
|
|
7
|
+
- [**breaking**] SentenceTransformersTextEmbedder - always require quantization_ranges for int8/uint8 precision (#4154)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## [integrations/sentence_transformers-v1.0.0] - 2026-10-16
|
|
11
|
+
|
|
12
|
+
### 🧹 Chores
|
|
13
|
+
|
|
14
|
+
- [**breaking**] Drop Python 3.10 support (#4105)
|
|
15
|
+
|
|
16
|
+
|
|
3
17
|
## [integrations/sentence_transformers-v0.2.0] - 2026-10-01
|
|
4
18
|
|
|
5
19
|
### 🐛 Bug Fixes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: sentence-transformers-haystack
|
|
3
|
-
Version: 1.0
|
|
3
|
+
Version: 2.1.0
|
|
4
4
|
Summary: Haystack integration for sentence_transformers
|
|
5
5
|
Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/sentence_transformers#readme
|
|
6
6
|
Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
|
{sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/pyproject.toml
RENAMED
|
@@ -57,6 +57,7 @@ dependencies = [
|
|
|
57
57
|
"pytest-rerunfailures",
|
|
58
58
|
"mypy",
|
|
59
59
|
"pip",
|
|
60
|
+
"onnxruntime",
|
|
60
61
|
]
|
|
61
62
|
|
|
62
63
|
[tool.hatch.envs.test.scripts]
|
|
@@ -78,6 +79,7 @@ disallow_incomplete_defs = true
|
|
|
78
79
|
module = [
|
|
79
80
|
# these libraries do not ship type stubs
|
|
80
81
|
"PIL.*",
|
|
82
|
+
"onnxruntime",
|
|
81
83
|
]
|
|
82
84
|
ignore_missing_imports = true
|
|
83
85
|
|
|
@@ -6,11 +6,36 @@ import json
|
|
|
6
6
|
from typing import Any, ClassVar, Literal
|
|
7
7
|
|
|
8
8
|
import numpy as np
|
|
9
|
+
from haystack.lazy_imports import LazyImport
|
|
9
10
|
from haystack.utils.auth import Secret
|
|
10
11
|
from PIL.Image import Image
|
|
11
12
|
|
|
12
13
|
from sentence_transformers import SentenceTransformer, quantize_embeddings
|
|
13
14
|
|
|
15
|
+
with LazyImport(message="Run 'pip install \"sentence-transformers[onnx]\"'") as onnxruntime_import:
|
|
16
|
+
import onnxruntime as ort
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _resolve_onnx_session_options(model_kwargs: dict[str, Any] | None) -> dict[str, Any] | None:
|
|
20
|
+
"""
|
|
21
|
+
Build an `onnxruntime.SessionOptions` object from a `session_options` dict in `model_kwargs`.
|
|
22
|
+
|
|
23
|
+
Each key of the dict is set as an attribute of the `SessionOptions`, e.g. `{"intra_op_num_threads": 1}`.
|
|
24
|
+
|
|
25
|
+
:param model_kwargs: The model kwargs passed to the Sentence Transformers model.
|
|
26
|
+
:returns: A copy of `model_kwargs` with `session_options` replaced by the built `SessionOptions` object, or
|
|
27
|
+
`model_kwargs` itself when it has no `session_options` dict.
|
|
28
|
+
:raises ImportError: If `model_kwargs` has a `session_options` dict and `onnxruntime` is not installed.
|
|
29
|
+
"""
|
|
30
|
+
if not model_kwargs or not isinstance(model_kwargs.get("session_options"), dict):
|
|
31
|
+
return model_kwargs
|
|
32
|
+
|
|
33
|
+
onnxruntime_import.check()
|
|
34
|
+
session_options = ort.SessionOptions()
|
|
35
|
+
for name, value in model_kwargs["session_options"].items():
|
|
36
|
+
setattr(session_options, name, value)
|
|
37
|
+
return {**model_kwargs, "session_options": session_options}
|
|
38
|
+
|
|
14
39
|
|
|
15
40
|
class _SentenceTransformersEmbeddingBackendFactory:
|
|
16
41
|
"""
|
|
@@ -99,7 +124,9 @@ class _SentenceTransformersEmbeddingBackend:
|
|
|
99
124
|
revision=revision,
|
|
100
125
|
local_files_only=local_files_only,
|
|
101
126
|
truncate_dim=truncate_dim,
|
|
102
|
-
model_kwargs=model_kwargs
|
|
127
|
+
model_kwargs=_resolve_onnx_session_options(model_kwargs=model_kwargs)
|
|
128
|
+
if backend == "onnx"
|
|
129
|
+
else model_kwargs,
|
|
103
130
|
# `tokenizer_kwargs` was renamed to `processor_kwargs` in sentence-transformers 5.4.0
|
|
104
131
|
processor_kwargs=tokenizer_kwargs,
|
|
105
132
|
config_kwargs=config_kwargs,
|
|
@@ -8,6 +8,9 @@ from typing import Any, ClassVar, Literal
|
|
|
8
8
|
from haystack.dataclasses.sparse_embedding import SparseEmbedding
|
|
9
9
|
from haystack.utils.auth import Secret
|
|
10
10
|
|
|
11
|
+
from haystack_integrations.components.embedders.sentence_transformers.embedding_backend.backend import (
|
|
12
|
+
_resolve_onnx_session_options,
|
|
13
|
+
)
|
|
11
14
|
from sentence_transformers import SparseEncoder
|
|
12
15
|
|
|
13
16
|
|
|
@@ -93,7 +96,9 @@ class _SentenceTransformersSparseEncoderEmbeddingBackend:
|
|
|
93
96
|
trust_remote_code=trust_remote_code,
|
|
94
97
|
revision=revision,
|
|
95
98
|
local_files_only=local_files_only,
|
|
96
|
-
model_kwargs=model_kwargs
|
|
99
|
+
model_kwargs=_resolve_onnx_session_options(model_kwargs=model_kwargs)
|
|
100
|
+
if backend == "onnx"
|
|
101
|
+
else model_kwargs,
|
|
97
102
|
# `tokenizer_kwargs` was renamed to `processor_kwargs` in sentence-transformers 5.4.0
|
|
98
103
|
processor_kwargs=tokenizer_kwargs,
|
|
99
104
|
config_kwargs=config_kwargs,
|
|
@@ -113,6 +113,9 @@ class SentenceTransformersDocumentImageEmbedder:
|
|
|
113
113
|
:param model_kwargs:
|
|
114
114
|
Additional keyword arguments for `AutoModelForSequenceClassification.from_pretrained`
|
|
115
115
|
when loading the model. Refer to specific model documentation for available kwargs.
|
|
116
|
+
With `backend="onnx"`, a `session_options` dict is turned into an `onnxruntime.SessionOptions` with
|
|
117
|
+
those attributes set, for example `{"session_options": {"intra_op_num_threads": 1}}` to limit
|
|
118
|
+
the threads each inference call uses.
|
|
116
119
|
:param tokenizer_kwargs:
|
|
117
120
|
Additional keyword arguments forwarded as `processor_kwargs` to the Sentence Transformers model
|
|
118
121
|
when loading it. Refer to the
|
|
@@ -106,6 +106,9 @@ class SentenceTransformersDocumentEmbedder:
|
|
|
106
106
|
:param model_kwargs:
|
|
107
107
|
Additional keyword arguments for `AutoModelForSequenceClassification.from_pretrained`
|
|
108
108
|
when loading the model. Refer to specific model documentation for available kwargs.
|
|
109
|
+
With `backend="onnx"`, a `session_options` dict is turned into an `onnxruntime.SessionOptions` with
|
|
110
|
+
those attributes set, for example `{"session_options": {"intra_op_num_threads": 1}}` to limit
|
|
111
|
+
the threads each inference call uses.
|
|
109
112
|
:param tokenizer_kwargs:
|
|
110
113
|
Additional keyword arguments forwarded as `processor_kwargs` to the Sentence Transformers model
|
|
111
114
|
when loading it. Refer to the
|
|
@@ -94,6 +94,9 @@ class SentenceTransformersSparseDocumentEmbedder:
|
|
|
94
94
|
:param model_kwargs:
|
|
95
95
|
Additional keyword arguments for `AutoModelForSequenceClassification.from_pretrained`
|
|
96
96
|
when loading the model. Refer to specific model documentation for available kwargs.
|
|
97
|
+
With `backend="onnx"`, a `session_options` dict is turned into an `onnxruntime.SessionOptions` with
|
|
98
|
+
those attributes set, for example `{"session_options": {"intra_op_num_threads": 1}}` to limit
|
|
99
|
+
the threads each inference call uses.
|
|
97
100
|
:param tokenizer_kwargs:
|
|
98
101
|
Additional keyword arguments forwarded as `processor_kwargs` to the Sentence Transformers model
|
|
99
102
|
when loading it. Refer to the
|
|
@@ -75,6 +75,9 @@ class SentenceTransformersSparseTextEmbedder:
|
|
|
75
75
|
:param model_kwargs:
|
|
76
76
|
Additional keyword arguments for `AutoModelForSequenceClassification.from_pretrained`
|
|
77
77
|
when loading the model. Refer to specific model documentation for available kwargs.
|
|
78
|
+
With `backend="onnx"`, a `session_options` dict is turned into an `onnxruntime.SessionOptions` with
|
|
79
|
+
those attributes set, for example `{"session_options": {"intra_op_num_threads": 1}}` to limit
|
|
80
|
+
the threads each inference call uses.
|
|
78
81
|
:param tokenizer_kwargs:
|
|
79
82
|
Additional keyword arguments forwarded as `processor_kwargs` to the Sentence Transformers model
|
|
80
83
|
when loading it. Refer to the
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
from typing import Any, Literal
|
|
6
6
|
|
|
7
|
-
from haystack import component, default_from_dict, default_to_dict
|
|
7
|
+
from haystack import component, default_from_dict, default_to_dict
|
|
8
8
|
from haystack.utils import ComponentDevice, Secret
|
|
9
9
|
from haystack.utils.hf import deserialize_hf_model_kwargs, serialize_hf_model_kwargs
|
|
10
10
|
|
|
@@ -13,8 +13,6 @@ from haystack_integrations.components.embedders.sentence_transformers.embedding_
|
|
|
13
13
|
_SentenceTransformersEmbeddingBackendFactory,
|
|
14
14
|
)
|
|
15
15
|
|
|
16
|
-
logger = logging.getLogger(__name__)
|
|
17
|
-
|
|
18
16
|
|
|
19
17
|
@component
|
|
20
18
|
class SentenceTransformersTextEmbedder:
|
|
@@ -96,6 +94,9 @@ class SentenceTransformersTextEmbedder:
|
|
|
96
94
|
:param model_kwargs:
|
|
97
95
|
Additional keyword arguments for `AutoModelForSequenceClassification.from_pretrained`
|
|
98
96
|
when loading the model. Refer to specific model documentation for available kwargs.
|
|
97
|
+
With `backend="onnx"`, a `session_options` dict is turned into an `onnxruntime.SessionOptions` with
|
|
98
|
+
those attributes set, for example `{"session_options": {"intra_op_num_threads": 1}}` to limit
|
|
99
|
+
the threads each inference call uses.
|
|
99
100
|
:param tokenizer_kwargs:
|
|
100
101
|
Additional keyword arguments forwarded as `processor_kwargs` to the Sentence Transformers model
|
|
101
102
|
when loading it. Refer to the
|
|
@@ -122,9 +123,10 @@ class SentenceTransformersTextEmbedder:
|
|
|
122
123
|
:param quantization_ranges:
|
|
123
124
|
Calibration ranges to use when `precision` is "int8" or "uint8", with shape `(2, embedding_dim)`:
|
|
124
125
|
minimum values in the first row and maximum values in the second.
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
Required for "int8" and "uint8" because ranges cannot be inferred from a single text.
|
|
127
|
+
Compute ranges from a representative sample of embeddings.
|
|
128
|
+
:raises ValueError:
|
|
129
|
+
`precision` is "int8" or "uint8" and `quantization_ranges` is not provided.
|
|
128
130
|
"""
|
|
129
131
|
|
|
130
132
|
self.model = model
|
|
@@ -148,12 +150,11 @@ class SentenceTransformersTextEmbedder:
|
|
|
148
150
|
self.backend = backend
|
|
149
151
|
self.quantization_ranges = quantization_ranges
|
|
150
152
|
if precision in ("int8", "uint8") and quantization_ranges is None:
|
|
151
|
-
|
|
152
|
-
"
|
|
153
|
-
"
|
|
154
|
-
"Pass `quantization_ranges` computed from a representative sample of embeddings.",
|
|
155
|
-
precision=precision,
|
|
153
|
+
msg = (
|
|
154
|
+
f"Precision '{precision}' requires `quantization_ranges` computed from "
|
|
155
|
+
"a representative sample of embeddings."
|
|
156
156
|
)
|
|
157
|
+
raise ValueError(msg)
|
|
157
158
|
|
|
158
159
|
def _get_telemetry_data(self) -> dict[str, Any]:
|
|
159
160
|
"""
|
|
@@ -11,6 +11,9 @@ from haystack.utils import ComponentDevice, Secret
|
|
|
11
11
|
from haystack.utils.hf import deserialize_hf_model_kwargs, serialize_hf_model_kwargs
|
|
12
12
|
from haystack.utils.misc import _deduplicate_documents
|
|
13
13
|
|
|
14
|
+
from haystack_integrations.components.embedders.sentence_transformers.embedding_backend.backend import (
|
|
15
|
+
_resolve_onnx_session_options,
|
|
16
|
+
)
|
|
14
17
|
from sentence_transformers import SentenceTransformer
|
|
15
18
|
|
|
16
19
|
|
|
@@ -162,6 +165,9 @@ class SentenceTransformersDiversityRanker:
|
|
|
162
165
|
:param model_kwargs:
|
|
163
166
|
Additional keyword arguments for `AutoModelForSequenceClassification.from_pretrained`
|
|
164
167
|
when loading the model. Refer to specific model documentation for available kwargs.
|
|
168
|
+
With `backend="onnx"`, a `session_options` dict is turned into an `onnxruntime.SessionOptions` with
|
|
169
|
+
those attributes set, for example `{"session_options": {"intra_op_num_threads": 1}}` to limit
|
|
170
|
+
the threads each inference call uses.
|
|
165
171
|
:param tokenizer_kwargs:
|
|
166
172
|
Additional keyword arguments forwarded as `processor_kwargs` to the Sentence Transformers model
|
|
167
173
|
when loading it. Refer to the
|
|
@@ -206,7 +212,11 @@ class SentenceTransformersDiversityRanker:
|
|
|
206
212
|
model_name_or_path=self.model_name_or_path,
|
|
207
213
|
device=self.device.to_torch_str(),
|
|
208
214
|
token=self.token.resolve_value() if self.token else None,
|
|
209
|
-
model_kwargs=
|
|
215
|
+
model_kwargs=(
|
|
216
|
+
_resolve_onnx_session_options(model_kwargs=self.model_kwargs)
|
|
217
|
+
if self.backend == "onnx"
|
|
218
|
+
else self.model_kwargs
|
|
219
|
+
),
|
|
210
220
|
# `tokenizer_kwargs` was renamed to `processor_kwargs` in sentence-transformers 5.4.0
|
|
211
221
|
processor_kwargs=self.tokenizer_kwargs,
|
|
212
222
|
config_kwargs=self.config_kwargs,
|
|
@@ -12,6 +12,9 @@ from haystack.utils.hf import deserialize_hf_model_kwargs, serialize_hf_model_kw
|
|
|
12
12
|
from haystack.utils.misc import _deduplicate_documents
|
|
13
13
|
from torch.nn import Identity, Sigmoid
|
|
14
14
|
|
|
15
|
+
from haystack_integrations.components.embedders.sentence_transformers.embedding_backend.backend import (
|
|
16
|
+
_resolve_onnx_session_options,
|
|
17
|
+
)
|
|
15
18
|
from sentence_transformers import CrossEncoder
|
|
16
19
|
|
|
17
20
|
|
|
@@ -97,6 +100,9 @@ class SentenceTransformersSimilarityRanker:
|
|
|
97
100
|
:param model_kwargs:
|
|
98
101
|
Additional keyword arguments for `AutoModelForSequenceClassification.from_pretrained`
|
|
99
102
|
when loading the model. Refer to specific model documentation for available kwargs.
|
|
103
|
+
With `backend="onnx"`, a `session_options` dict is turned into an `onnxruntime.SessionOptions` with
|
|
104
|
+
those attributes set, for example `{"session_options": {"intra_op_num_threads": 1}}` to limit
|
|
105
|
+
the threads each inference call uses.
|
|
100
106
|
:param tokenizer_kwargs:
|
|
101
107
|
Additional keyword arguments forwarded as `processor_kwargs` to the Sentence Transformers model
|
|
102
108
|
when loading it. Refer to the
|
|
@@ -155,7 +161,11 @@ class SentenceTransformersSimilarityRanker:
|
|
|
155
161
|
device=self.device.to_torch_str(),
|
|
156
162
|
token=self.token.resolve_value() if self.token else None,
|
|
157
163
|
trust_remote_code=self.trust_remote_code,
|
|
158
|
-
model_kwargs=
|
|
164
|
+
model_kwargs=(
|
|
165
|
+
_resolve_onnx_session_options(model_kwargs=self.model_kwargs)
|
|
166
|
+
if self.backend == "onnx"
|
|
167
|
+
else self.model_kwargs
|
|
168
|
+
),
|
|
159
169
|
# `tokenizer_kwargs` was renamed to `processor_kwargs` in sentence-transformers 5.4.0
|
|
160
170
|
processor_kwargs=self.tokenizer_kwargs,
|
|
161
171
|
config_kwargs=self.config_kwargs,
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
from unittest.mock import MagicMock, call, patch
|
|
6
6
|
|
|
7
|
+
import onnxruntime as ort
|
|
7
8
|
import pytest
|
|
8
9
|
import torch
|
|
9
10
|
from haystack import Document, Pipeline
|
|
@@ -741,6 +742,26 @@ class TestSentenceTransformersDiversityRanker:
|
|
|
741
742
|
backend="onnx",
|
|
742
743
|
)
|
|
743
744
|
|
|
745
|
+
@patch(
|
|
746
|
+
"haystack_integrations.components.rankers.sentence_transformers.sentence_transformers_diversity.SentenceTransformer"
|
|
747
|
+
)
|
|
748
|
+
def test_from_dict_warm_up_onnx_session_options(self, mocked_sentence_transformer):
|
|
749
|
+
ranker = SentenceTransformersDiversityRanker.from_dict(
|
|
750
|
+
{
|
|
751
|
+
"type": "haystack_integrations.components.rankers.sentence_transformers.sentence_transformers_diversity.SentenceTransformersDiversityRanker",
|
|
752
|
+
"init_parameters": {
|
|
753
|
+
"backend": "onnx",
|
|
754
|
+
"model_kwargs": {"session_options": {"intra_op_num_threads": 1}},
|
|
755
|
+
},
|
|
756
|
+
}
|
|
757
|
+
)
|
|
758
|
+
ranker.warm_up()
|
|
759
|
+
|
|
760
|
+
session_options = mocked_sentence_transformer.call_args.kwargs["model_kwargs"]["session_options"]
|
|
761
|
+
assert isinstance(session_options, ort.SessionOptions)
|
|
762
|
+
assert session_options.intra_op_num_threads == 1
|
|
763
|
+
assert ranker.to_dict()["init_parameters"]["model_kwargs"]["session_options"] == {"intra_op_num_threads": 1}
|
|
764
|
+
|
|
744
765
|
@patch(
|
|
745
766
|
"haystack_integrations.components.rankers.sentence_transformers.sentence_transformers_diversity.SentenceTransformer"
|
|
746
767
|
)
|
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
from unittest.mock import patch
|
|
6
6
|
|
|
7
7
|
import numpy as np
|
|
8
|
+
import onnxruntime as ort
|
|
8
9
|
from haystack.utils.auth import Secret
|
|
9
10
|
|
|
10
11
|
from haystack_integrations.components.embedders.sentence_transformers.embedding_backend.backend import (
|
|
12
|
+
_resolve_onnx_session_options,
|
|
11
13
|
_SentenceTransformersEmbeddingBackendFactory,
|
|
12
14
|
)
|
|
13
15
|
|
|
@@ -58,6 +60,29 @@ def test_model_initialization(mock_sentence_transformer):
|
|
|
58
60
|
)
|
|
59
61
|
|
|
60
62
|
|
|
63
|
+
def test_resolve_onnx_session_options():
|
|
64
|
+
model_kwargs = {"file_name": "onnx/model.onnx", "session_options": {"intra_op_num_threads": 1}}
|
|
65
|
+
|
|
66
|
+
resolved = _resolve_onnx_session_options(model_kwargs=model_kwargs)
|
|
67
|
+
|
|
68
|
+
assert isinstance(resolved["session_options"], ort.SessionOptions)
|
|
69
|
+
assert resolved["session_options"].intra_op_num_threads == 1
|
|
70
|
+
assert resolved["file_name"] == "onnx/model.onnx"
|
|
71
|
+
# the component's own model_kwargs stay serializable
|
|
72
|
+
assert model_kwargs["session_options"] == {"intra_op_num_threads": 1}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@patch("haystack_integrations.components.embedders.sentence_transformers.embedding_backend.backend.SentenceTransformer")
|
|
76
|
+
def test_model_initialization_onnx_session_options(mock_sentence_transformer):
|
|
77
|
+
_SentenceTransformersEmbeddingBackendFactory.get_embedding_backend(
|
|
78
|
+
model="onnx_session_model", model_kwargs={"session_options": {"intra_op_num_threads": 2}}, backend="onnx"
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
session_options = mock_sentence_transformer.call_args.kwargs["model_kwargs"]["session_options"]
|
|
82
|
+
assert isinstance(session_options, ort.SessionOptions)
|
|
83
|
+
assert session_options.intra_op_num_threads == 2
|
|
84
|
+
|
|
85
|
+
|
|
61
86
|
@patch("haystack_integrations.components.embedders.sentence_transformers.embedding_backend.backend.SentenceTransformer")
|
|
62
87
|
def test_embedding_function_with_kwargs(mock_sentence_transformer):
|
|
63
88
|
embedding_backend = _SentenceTransformersEmbeddingBackendFactory.get_embedding_backend(model="model")
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
from unittest.mock import MagicMock, patch
|
|
6
6
|
|
|
7
7
|
import numpy as np
|
|
8
|
+
import onnxruntime as ort
|
|
8
9
|
import pytest
|
|
9
10
|
import torch
|
|
10
11
|
from haystack import Document
|
|
@@ -68,6 +69,26 @@ class TestSentenceTransformersSimilarityRanker:
|
|
|
68
69
|
backend="onnx",
|
|
69
70
|
)
|
|
70
71
|
|
|
72
|
+
@patch(
|
|
73
|
+
"haystack_integrations.components.rankers.sentence_transformers.sentence_transformers_similarity.CrossEncoder"
|
|
74
|
+
)
|
|
75
|
+
def test_from_dict_warm_up_onnx_session_options(self, mocked_cross_encoder):
|
|
76
|
+
ranker = SentenceTransformersSimilarityRanker.from_dict(
|
|
77
|
+
{
|
|
78
|
+
"type": "haystack_integrations.components.rankers.sentence_transformers.sentence_transformers_similarity.SentenceTransformersSimilarityRanker",
|
|
79
|
+
"init_parameters": {
|
|
80
|
+
"backend": "onnx",
|
|
81
|
+
"model_kwargs": {"session_options": {"intra_op_num_threads": 1}},
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
)
|
|
85
|
+
ranker.warm_up()
|
|
86
|
+
|
|
87
|
+
session_options = mocked_cross_encoder.call_args.kwargs["model_kwargs"]["session_options"]
|
|
88
|
+
assert isinstance(session_options, ort.SessionOptions)
|
|
89
|
+
assert session_options.intra_op_num_threads == 1
|
|
90
|
+
assert ranker.to_dict()["init_parameters"]["model_kwargs"]["session_options"] == {"intra_op_num_threads": 1}
|
|
91
|
+
|
|
71
92
|
@patch(
|
|
72
93
|
"haystack_integrations.components.rankers.sentence_transformers.sentence_transformers_similarity.CrossEncoder"
|
|
73
94
|
)
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
from unittest.mock import patch
|
|
6
6
|
|
|
7
|
+
import onnxruntime as ort
|
|
7
8
|
import pytest
|
|
8
9
|
import torch
|
|
9
10
|
from haystack.dataclasses.sparse_embedding import SparseEmbedding
|
|
@@ -62,6 +63,19 @@ def test_sparse_model_initialization(mock_sparse_encoder):
|
|
|
62
63
|
)
|
|
63
64
|
|
|
64
65
|
|
|
66
|
+
@patch(
|
|
67
|
+
"haystack_integrations.components.embedders.sentence_transformers.embedding_backend.sparse_backend.SparseEncoder"
|
|
68
|
+
)
|
|
69
|
+
def test_sparse_model_initialization_onnx_session_options(mock_sparse_encoder):
|
|
70
|
+
_SentenceTransformersSparseEmbeddingBackendFactory.get_embedding_backend(
|
|
71
|
+
model="onnx_session_model", model_kwargs={"session_options": {"intra_op_num_threads": 2}}, backend="onnx"
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
session_options = mock_sparse_encoder.call_args.kwargs["model_kwargs"]["session_options"]
|
|
75
|
+
assert isinstance(session_options, ort.SessionOptions)
|
|
76
|
+
assert session_options.intra_op_num_threads == 2
|
|
77
|
+
|
|
78
|
+
|
|
65
79
|
@patch(
|
|
66
80
|
"haystack_integrations.components.embedders.sentence_transformers.embedding_backend.sparse_backend.SparseEncoder"
|
|
67
81
|
)
|
|
@@ -64,10 +64,10 @@ class TestSentenceTransformersTextEmbedder:
|
|
|
64
64
|
assert embedder.precision == "int8"
|
|
65
65
|
assert embedder.quantization_ranges == [[-1.0, -1.0], [1.0, 1.0]]
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
@pytest.mark.parametrize("precision", ["int8", "uint8"])
|
|
68
|
+
def test_init_quantized_precision_without_ranges_raises(self, precision):
|
|
69
|
+
with pytest.raises(ValueError, match="requires `quantization_ranges`"):
|
|
70
|
+
SentenceTransformersTextEmbedder(model="model", precision=precision)
|
|
71
71
|
|
|
72
72
|
def test_to_dict(self):
|
|
73
73
|
component = SentenceTransformersTextEmbedder(model="model", device=ComponentDevice.from_str("cpu"))
|
|
@@ -114,6 +114,7 @@ class TestSentenceTransformersTextEmbedder:
|
|
|
114
114
|
tokenizer_kwargs={"model_max_length": 512},
|
|
115
115
|
config_kwargs={"use_memory_efficient_attention": False},
|
|
116
116
|
precision="int8",
|
|
117
|
+
quantization_ranges=[[-1.0] * 256, [1.0] * 256],
|
|
117
118
|
encode_kwargs={"task": "clustering"},
|
|
118
119
|
)
|
|
119
120
|
data = component.to_dict()
|
|
@@ -138,7 +139,7 @@ class TestSentenceTransformersTextEmbedder:
|
|
|
138
139
|
"precision": "int8",
|
|
139
140
|
"encode_kwargs": {"task": "clustering"},
|
|
140
141
|
"backend": "torch",
|
|
141
|
-
"quantization_ranges":
|
|
142
|
+
"quantization_ranges": [[-1.0] * 256, [1.0] * 256],
|
|
142
143
|
},
|
|
143
144
|
}
|
|
144
145
|
|
|
@@ -223,6 +224,7 @@ class TestSentenceTransformersTextEmbedder:
|
|
|
223
224
|
"local_files_only": False,
|
|
224
225
|
"truncate_dim": 256,
|
|
225
226
|
"precision": "int8",
|
|
227
|
+
"quantization_ranges": [[-1.0] * 256, [1.0] * 256],
|
|
226
228
|
},
|
|
227
229
|
}
|
|
228
230
|
component = SentenceTransformersTextEmbedder.from_dict(data)
|
|
@@ -239,6 +241,7 @@ class TestSentenceTransformersTextEmbedder:
|
|
|
239
241
|
assert component.local_files_only is False
|
|
240
242
|
assert component.truncate_dim == 256
|
|
241
243
|
assert component.precision == "int8"
|
|
244
|
+
assert component.quantization_ranges == [[-1.0] * 256, [1.0] * 256]
|
|
242
245
|
|
|
243
246
|
@patch(
|
|
244
247
|
"haystack_integrations.components.embedders.sentence_transformers.sentence_transformers_text_embedder._SentenceTransformersEmbeddingBackendFactory"
|
|
@@ -432,24 +435,6 @@ class TestSentenceTransformersTextEmbedder:
|
|
|
432
435
|
# without truncation, the embedding has 128 length
|
|
433
436
|
assert len(embedding_trunc) == 64
|
|
434
437
|
|
|
435
|
-
@pytest.mark.integration
|
|
436
|
-
def test_run_quantization(self, del_hf_env_vars_if_empty):
|
|
437
|
-
"""
|
|
438
|
-
sentence-transformers-testing/stsb-bert-tiny-safetensors maps sentences & paragraphs to a 128 dimensional dense
|
|
439
|
-
vector space
|
|
440
|
-
"""
|
|
441
|
-
checkpoint = "sentence-transformers-testing/stsb-bert-tiny-safetensors"
|
|
442
|
-
text = "a nice text to embed"
|
|
443
|
-
|
|
444
|
-
embedder_def = SentenceTransformersTextEmbedder(
|
|
445
|
-
model=checkpoint, precision="int8", device=ComponentDevice.from_str("cpu")
|
|
446
|
-
)
|
|
447
|
-
result_def = embedder_def.run(text=text)
|
|
448
|
-
embedding_def = result_def["embedding"]
|
|
449
|
-
|
|
450
|
-
assert len(embedding_def) == 128
|
|
451
|
-
assert all(isinstance(el, int) for el in embedding_def)
|
|
452
|
-
|
|
453
438
|
@pytest.mark.integration
|
|
454
439
|
def test_run_quantization_with_ranges(self, del_hf_env_vars_if_empty):
|
|
455
440
|
"""
|
|
@@ -466,5 +451,4 @@ class TestSentenceTransformersTextEmbedder:
|
|
|
466
451
|
|
|
467
452
|
assert len(embedding) == 128
|
|
468
453
|
assert all(isinstance(el, int) for el in embedding)
|
|
469
|
-
# without explicit ranges, a single text produces a degenerate all-equal embedding
|
|
470
454
|
assert len(set(embedding)) > 1
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/__init__.py
RENAMED
|
File without changes
|
{sentence_transformers_haystack-1.0.0 → sentence_transformers_haystack-2.1.0}/tests/conftest.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|