emeans-haystack 0.1.2__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.
- emeans_haystack-0.1.2/.gitignore +11 -0
- emeans_haystack-0.1.2/CHANGELOG.md +21 -0
- emeans_haystack-0.1.2/LICENSE.txt +202 -0
- emeans_haystack-0.1.2/PKG-INFO +162 -0
- emeans_haystack-0.1.2/README.md +134 -0
- emeans_haystack-0.1.2/pydoc/config_docusaurus.yml +15 -0
- emeans_haystack-0.1.2/pyproject.toml +200 -0
- emeans_haystack-0.1.2/src/haystack_integrations/components/preprocessors/emeans/__init__.py +8 -0
- emeans_haystack-0.1.2/src/haystack_integrations/components/preprocessors/emeans/_backend.py +1183 -0
- emeans_haystack-0.1.2/src/haystack_integrations/components/preprocessors/emeans/_model.py +337 -0
- emeans_haystack-0.1.2/src/haystack_integrations/components/preprocessors/emeans/_utils.py +181 -0
- emeans_haystack-0.1.2/src/haystack_integrations/components/preprocessors/emeans/clusterer.py +223 -0
- emeans_haystack-0.1.2/src/haystack_integrations/components/preprocessors/emeans/fitter.py +298 -0
- emeans_haystack-0.1.2/src/haystack_integrations/components/preprocessors/py.typed +0 -0
- emeans_haystack-0.1.2/src/haystack_integrations/components/rankers/emeans/__init__.py +7 -0
- emeans_haystack-0.1.2/src/haystack_integrations/components/rankers/emeans/ranker.py +166 -0
- emeans_haystack-0.1.2/src/haystack_integrations/components/rankers/py.typed +0 -0
- emeans_haystack-0.1.2/tests/__init__.py +3 -0
- emeans_haystack-0.1.2/tests/conftest.py +47 -0
- emeans_haystack-0.1.2/tests/test_backend.py +444 -0
- emeans_haystack-0.1.2/tests/test_backend_gpu_mock.py +1486 -0
- emeans_haystack-0.1.2/tests/test_clusterer.py +525 -0
- emeans_haystack-0.1.2/tests/test_conformance.py +311 -0
- emeans_haystack-0.1.2/tests/test_fitter.py +424 -0
- emeans_haystack-0.1.2/tests/test_gpu.py +339 -0
- emeans_haystack-0.1.2/tests/test_import_no_torch.py +144 -0
- emeans_haystack-0.1.2/tests/test_model.py +391 -0
- emeans_haystack-0.1.2/tests/test_quickstart.py +93 -0
- emeans_haystack-0.1.2/tests/test_quickstart_integration.py +62 -0
- emeans_haystack-0.1.2/tests/test_ranker.py +333 -0
- emeans_haystack-0.1.2/tests/test_readme.py +83 -0
- emeans_haystack-0.1.2/tests/test_release_metadata.py +40 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.2
|
|
4
|
+
|
|
5
|
+
- The README now states the supported emeans-rocm range (`>=0.2.5,<0.3`) and what happens outside it: a release outside the range is not used, GPU fits run on the stock faiss-cpu e-means engine (on AMD in native FP32), a warning (once per process) names the installed version and the range, and `stats["emeans_source"]` is `"faiss-cpu <version>"`.
|
|
6
|
+
- Benchmark records now carry `unused_threshold` (benchmarks only, not the package).
|
|
7
|
+
- The README benchmark links are now absolute GitHub URLs, so they work on the PyPI page.
|
|
8
|
+
- First release on PyPI: `pip install emeans-haystack`. The README no longer asks to install from a clone.
|
|
9
|
+
- No code or API change: the public API and the `.npz` model format are unchanged.
|
|
10
|
+
|
|
11
|
+
## 0.1.1
|
|
12
|
+
|
|
13
|
+
- The package now passes an explicit `unused_threshold` (0.1, or 0.0 when d <= 2) to every e-means engine: stock faiss-cpu, emeans-rocm, the GPU probe and the assigners. emeans-rocm 0.2.6 changed its default to a cost-based threshold; the explicit value keeps the mass-based revival of 0.2.5 and stock faiss.
|
|
14
|
+
- `stats["unused_threshold"]` reports the value used.
|
|
15
|
+
- emeans-rocm 0.2.6 is tested; the supported range stays `>=0.2.5,<0.3`. The local GPU envs pin `emeans-rocm==0.2.6`.
|
|
16
|
+
- Benchmarks were rerun on emeans-rocm 0.2.6 (see `benchmarks/RESULTS.md`). Models saved with an earlier version load and assign unchanged, because assignment from saved centroids does not depend on the engine version. A refit on emeans-rocm 0.2.6 gives assignments (`cluster_id`s) that differ from a 0.2.5 fit about as much as a different seed does (on Cohere 100k, same-seed ARI between versions 0.27-0.40, between 0.2.5 seeds 0.25-0.39), because 0.2.6 places revived centroids differently at d > 2. Objectives stay close: within 1.342 %, lower on 0.2.6 in 71 of 96 main-cell records, largest at Cohere 100k x 65536 (1.5 rows per centroid).
|
|
17
|
+
- The public API and the `.npz` model format are unchanged.
|
|
18
|
+
|
|
19
|
+
## 0.1.0
|
|
20
|
+
|
|
21
|
+
First release.
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026-present ogaude <ogaude.code@proton.me>
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: emeans-haystack
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Haystack components that cluster document embeddings with e-means (Meta FAIR)
|
|
5
|
+
Project-URL: Documentation, https://github.com/ogaude/emeans-haystack/tree/main/integrations/emeans#readme
|
|
6
|
+
Project-URL: Issues, https://github.com/ogaude/emeans-haystack/issues
|
|
7
|
+
Project-URL: Source, https://github.com/ogaude/emeans-haystack/tree/main/integrations/emeans
|
|
8
|
+
Author-email: ogaude <ogaude.code@proton.me>
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE.txt
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
19
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Requires-Dist: faiss-cpu>=1.16.0
|
|
22
|
+
Requires-Dist: haystack-ai>=3.5
|
|
23
|
+
Requires-Dist: numpy>=2.0
|
|
24
|
+
Requires-Dist: packaging>=22
|
|
25
|
+
Provides-Extra: gpu
|
|
26
|
+
Requires-Dist: torch; extra == 'gpu'
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# emeans-haystack
|
|
30
|
+
|
|
31
|
+
[](https://pypi.org/project/emeans-haystack)
|
|
32
|
+
[](https://pypi.org/project/emeans-haystack)
|
|
33
|
+
|
|
34
|
+
- [Integration page](https://haystack.deepset.ai/integrations/emeans)
|
|
35
|
+
- [Changelog](https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/emeans/CHANGELOG.md)
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install emeans-haystack # CPU: brings haystack-ai, faiss-cpu and numpy
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Requirements: Python 3.11 or newer, `haystack-ai>=3.5`, `faiss-cpu>=1.16.0` (the first release that contains
|
|
46
|
+
`faiss.contrib.e_means`), `numpy>=2.0`.
|
|
47
|
+
|
|
48
|
+
**GPU, NVIDIA:**
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install "emeans-haystack[gpu]" # adds PyTorch from PyPI (CUDA build)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**GPU, AMD (ROCm):** install the ROCm build of PyTorch first, from AMD's or PyTorch's ROCm index, then install
|
|
55
|
+
`emeans-haystack` **without** `[gpu]`. The `[gpu]` extra pulls the PyPI build of PyTorch, which would replace your
|
|
56
|
+
ROCm build and leave you on CPU.
|
|
57
|
+
|
|
58
|
+
On AMD, upstream e-means runs only in native FP32 through plain PyTorch. For the fast Triton kernels and BF16/FP16 on
|
|
59
|
+
RX 9000 (gfx120x) GPUs, run `pip install emeans-rocm` after installing ROCm PyTorch; there is no extra for it.
|
|
60
|
+
[emeans-rocm](https://pypi.org/project/emeans-rocm/) (import name `emeans_rocm`, source at
|
|
61
|
+
https://github.com/ogaude/e-means-rocm) is the author's package of Meta's e-means code under its MIT license. This package supports emeans-rocm releases `>=0.2.5,<0.3`; the install line stays unpinned. A release outside that range is not used: GPU fits run on the stock faiss-cpu e-means engine (on AMD in native FP32, `stats["backend"]` `"gpu-pytorch"`), a warning (once per process) names the installed version and the range, and `stats["emeans_source"]` is `"faiss-cpu <version>"`. A source checkout of emeans-rocm on the path (not the installed wheel) has no checkable version and is used as is. When it
|
|
62
|
+
is installed, the GPU backends use it on every GPU, AMD and NVIDIA. Its NVIDIA path has not been re-run on hardware,
|
|
63
|
+
so NVIDIA users who want the verified path leave it uninstalled. On AMD, if its Triton backend fails the start-up
|
|
64
|
+
probe (for example, no Triton in your ROCm PyTorch), fits run in native FP32, and a warning (once per process) and
|
|
65
|
+
`stats["fallback_reason"]` give the probe error. If every one of its GPU backends fails the probe on your GPU, the
|
|
66
|
+
stock faiss-cpu e-means engine is tried on the GPU in native FP32 before falling back to the CPU; a warning and
|
|
67
|
+
`stats["fallback_reason"]` name the backends that failed. CPU fits always use the faiss-cpu e-means. The fitter's
|
|
68
|
+
`stats["emeans_source"]` names the package and version that ran, for example `"emeans-rocm 0.2.6"` or
|
|
69
|
+
`"faiss-cpu 1.16.0"`, and `stats["backend"]` which backend ran.
|
|
70
|
+
|
|
71
|
+
The package passes `unused_threshold=0.1` (`0.0` when d <= 2) to every e-means engine, so it keeps mass-based
|
|
72
|
+
revival of unused clusters, the rule of stock faiss and emeans-rocm 0.2.5, also on emeans-rocm 0.2.6, whose default is
|
|
73
|
+
cost-based revival; `stats["unused_threshold"]` reports the value used. emeans-rocm 0.2.6 places revived centroids
|
|
74
|
+
differently (along the donor cluster's long axis), so GPU fits at d > 2 are not the same as on 0.2.5; CPU fits and the
|
|
75
|
+
stock faiss-cpu engine are unchanged. Models saved with an earlier version load and assign unchanged, because
|
|
76
|
+
assignment from saved centroids does not depend on the engine version. A refit on emeans-rocm 0.2.6 gives assignments
|
|
77
|
+
(`cluster_id`s) that differ from those of a 0.2.5 fit about as much as a different seed does, while the objectives stay
|
|
78
|
+
close (figures under Benchmarks). The benchmark figures below are for emeans-rocm 0.2.6.
|
|
79
|
+
|
|
80
|
+
Importing the package never imports PyTorch. PyTorch is loaded only when a GPU backend is considered.
|
|
81
|
+
|
|
82
|
+
## Quick Start
|
|
83
|
+
|
|
84
|
+
A CPU run of fit, cluster and rank on random embeddings; it downloads no model.
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
import numpy as np
|
|
88
|
+
from haystack import Document
|
|
89
|
+
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
|
90
|
+
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
|
91
|
+
|
|
92
|
+
from haystack_integrations.components.preprocessors.emeans import EmeansDocumentClusterer, EmeansFitter
|
|
93
|
+
from haystack_integrations.components.rankers.emeans import EmeansClusterRanker
|
|
94
|
+
|
|
95
|
+
rng = np.random.default_rng(0)
|
|
96
|
+
docs = [Document(content=f"document {i}", embedding=rng.normal(size=64).tolist()) for i in range(500)]
|
|
97
|
+
|
|
98
|
+
# Fit once and save the model.
|
|
99
|
+
fitted = EmeansFitter(target_cluster_size=50, model_path="clusters.npz", overwrite=True).run(documents=docs)
|
|
100
|
+
stats = fitted["stats"]
|
|
101
|
+
|
|
102
|
+
# Assign a stable cluster_id to every document.
|
|
103
|
+
clustered = EmeansDocumentClusterer().run(documents=docs, model_path=fitted["model_path"])["documents"]
|
|
104
|
+
|
|
105
|
+
# Retrieve, then keep the best document of each cluster.
|
|
106
|
+
store = InMemoryDocumentStore()
|
|
107
|
+
store.write_documents(clustered)
|
|
108
|
+
query_embedding = rng.normal(size=64).tolist()
|
|
109
|
+
retriever = InMemoryEmbeddingRetriever(document_store=store, top_k=50)
|
|
110
|
+
retrieved = retriever.run(query_embedding=query_embedding)["documents"]
|
|
111
|
+
diverse = EmeansClusterRanker(top_k=5).run(documents=retrieved)["documents"]
|
|
112
|
+
|
|
113
|
+
print(stats["k"], stats["backend"])
|
|
114
|
+
for doc in diverse:
|
|
115
|
+
print(doc.score, doc.meta["cluster_id"])
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
In real use a Haystack document embedder, for example `SentenceTransformersDocumentEmbedder` from
|
|
119
|
+
`sentence-transformers-haystack`, sits in front. The full pipelines are in the repository's
|
|
120
|
+
[examples](https://github.com/ogaude/emeans-haystack/tree/main/examples): 01 indexing, 02 diverse retrieval, 03 new
|
|
121
|
+
documents, 04 GPU.
|
|
122
|
+
|
|
123
|
+
## Benchmarks
|
|
124
|
+
|
|
125
|
+
- e-means needs fewer passes than Lloyd's k-means (`faiss.Kmeans`, scikit-learn `KMeans`) to reach the same objective in all 5 sweep comparisons (k = 4096 and 16384); its passes are not faster, and at its default 5 passes Lloyd's k-means keeps the better objective in 3 settings (below). On 1M Cohere embeddings (768-d) with k = 16384 (pass-sweep medians), CPU e-means after 5 passes (objective 0.18569) beats `faiss.Kmeans` after 25 passes (0.18746); at the 25-pass points a pass costs about the same (114 vs 111 s/pass), so 575 s vs 2765 s.
|
|
126
|
+
- On an AMD RX 9060 XT with `emeans-rocm` 0.2.6, GPU e-means was faster than CPU e-means cold in all 16 settings and warm in all 12 that have a CPU warm time. FP32 against FP32 at 1M x 65536 on Cohere (cold, end-to-end, CPU 2232 s): native FP32 31.1x, Triton FP32 27.5x; Triton BF16 on the GPU against FP32 on the CPU: 187.0x (11.9 s). The smallest gain is 4.1x at random 100k x 1024. Against emeans-rocm 0.2.5 on the same cells, the median cold fit time ratio is 0.99 (warm 0.85). A refit on 0.2.6 gives assignments that differ from a 0.2.5 fit about as much as a different seed does: on Cohere 100k the same-seed ARI between versions is 0.27-0.40, against 0.25-0.39 between 0.2.5 seeds and 1.0 for a BF16 0.2.5 rerun. The objectives stay close: within 1.342 % of 0.2.5 and lower on 0.2.6 in 71 of 96 main-cell records, the largest difference at Cohere 100k x 65536 (1.5 rows per centroid). Models saved before load and assign unchanged ([comparison](https://github.com/ogaude/emeans-haystack/blob/main/benchmarks/RESULTS.md)).
|
|
127
|
+
- Without a GPU, on real data (Cohere), CPU e-means finishes sooner than Lloyd's k-means with an objective within 0.5 % in 6 of 7 comparable settings (11 of 14 over both datasets). Lloyd's k-means has the better objective in 3 settings: 100k x 65536 on both datasets (degenerate: 1.5 rows per centroid; e-means +22.22 % on Cohere, +36.81 % on random) and random 100k x 16384 (e-means +7.90 %).
|
|
128
|
+
- On real data, FP16/BF16 objectives are between -0.070 % and +0.055 % of FP32, but at about 100k rows they change document assignments almost as much as reseeding does: use FP32 when stable `cluster_id`s matter.
|
|
129
|
+
- Full tables, protocol, every cut made for time or memory and the settings not run: [benchmarks/RESULTS.md](https://github.com/ogaude/emeans-haystack/blob/main/benchmarks/RESULTS.md). Random data (Gaussian noise, no cluster structure) is used for speed; its objectives appear here only as the exceptions noted above.
|
|
130
|
+
|
|
131
|
+
## Contributing
|
|
132
|
+
|
|
133
|
+
Refer to the general [Contribution Guidelines](https://github.com/deepset-ai/haystack-core-integrations/blob/main/CONTRIBUTING.md).
|
|
134
|
+
|
|
135
|
+
GPU tests are marked `integration` and `gpu`, so `hatch run test:unit` and CI never run them. They are local
|
|
136
|
+
only: run them with `hatch run gpu:gpu-tests` (upstream e-means) or `hatch run gpu-fork:gpu-tests` (installs
|
|
137
|
+
`emeans-rocm` 0.2.6 from PyPI). They skip when PyTorch sees no GPU.
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
`emeans-haystack` is distributed under the Apache-2.0 license (see `LICENSE.txt`).
|
|
142
|
+
|
|
143
|
+
Third-party notices:
|
|
144
|
+
|
|
145
|
+
- e-means is developed by Meta FAIR and ships in faiss (`faiss.contrib.e_means`) under the MIT license
|
|
146
|
+
(Copyright (c) Facebook, Inc. and its affiliates). This package depends on faiss-cpu and does not include its code.
|
|
147
|
+
- emeans-rocm (optional, not a dependency) packages Meta's e-means code and is distributed under the MIT license.
|
|
148
|
+
|
|
149
|
+
## Citation
|
|
150
|
+
|
|
151
|
+
If you use e-means, cite the paper:
|
|
152
|
+
|
|
153
|
+
```bibtex
|
|
154
|
+
@misc{bessa2026emeans,
|
|
155
|
+
title = {{e-means}: outperforming {k-means} (in one-tenth the epochs)},
|
|
156
|
+
author = {Bessa, Swann and Mazar{\'e}, Pierre-Emmanuel and Szilvasy, Gergely and
|
|
157
|
+
Touvron, Hugo and Picard, David and Douze, Matthijs and J{\'e}gou, Herv{\'e}},
|
|
158
|
+
year = {2026},
|
|
159
|
+
note = {Preprint},
|
|
160
|
+
url = {https://arxiv.org/abs/2611.09501}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# emeans-haystack
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/emeans-haystack)
|
|
4
|
+
[](https://pypi.org/project/emeans-haystack)
|
|
5
|
+
|
|
6
|
+
- [Integration page](https://haystack.deepset.ai/integrations/emeans)
|
|
7
|
+
- [Changelog](https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/emeans/CHANGELOG.md)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install emeans-haystack # CPU: brings haystack-ai, faiss-cpu and numpy
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Requirements: Python 3.11 or newer, `haystack-ai>=3.5`, `faiss-cpu>=1.16.0` (the first release that contains
|
|
18
|
+
`faiss.contrib.e_means`), `numpy>=2.0`.
|
|
19
|
+
|
|
20
|
+
**GPU, NVIDIA:**
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install "emeans-haystack[gpu]" # adds PyTorch from PyPI (CUDA build)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**GPU, AMD (ROCm):** install the ROCm build of PyTorch first, from AMD's or PyTorch's ROCm index, then install
|
|
27
|
+
`emeans-haystack` **without** `[gpu]`. The `[gpu]` extra pulls the PyPI build of PyTorch, which would replace your
|
|
28
|
+
ROCm build and leave you on CPU.
|
|
29
|
+
|
|
30
|
+
On AMD, upstream e-means runs only in native FP32 through plain PyTorch. For the fast Triton kernels and BF16/FP16 on
|
|
31
|
+
RX 9000 (gfx120x) GPUs, run `pip install emeans-rocm` after installing ROCm PyTorch; there is no extra for it.
|
|
32
|
+
[emeans-rocm](https://pypi.org/project/emeans-rocm/) (import name `emeans_rocm`, source at
|
|
33
|
+
https://github.com/ogaude/e-means-rocm) is the author's package of Meta's e-means code under its MIT license. This package supports emeans-rocm releases `>=0.2.5,<0.3`; the install line stays unpinned. A release outside that range is not used: GPU fits run on the stock faiss-cpu e-means engine (on AMD in native FP32, `stats["backend"]` `"gpu-pytorch"`), a warning (once per process) names the installed version and the range, and `stats["emeans_source"]` is `"faiss-cpu <version>"`. A source checkout of emeans-rocm on the path (not the installed wheel) has no checkable version and is used as is. When it
|
|
34
|
+
is installed, the GPU backends use it on every GPU, AMD and NVIDIA. Its NVIDIA path has not been re-run on hardware,
|
|
35
|
+
so NVIDIA users who want the verified path leave it uninstalled. On AMD, if its Triton backend fails the start-up
|
|
36
|
+
probe (for example, no Triton in your ROCm PyTorch), fits run in native FP32, and a warning (once per process) and
|
|
37
|
+
`stats["fallback_reason"]` give the probe error. If every one of its GPU backends fails the probe on your GPU, the
|
|
38
|
+
stock faiss-cpu e-means engine is tried on the GPU in native FP32 before falling back to the CPU; a warning and
|
|
39
|
+
`stats["fallback_reason"]` name the backends that failed. CPU fits always use the faiss-cpu e-means. The fitter's
|
|
40
|
+
`stats["emeans_source"]` names the package and version that ran, for example `"emeans-rocm 0.2.6"` or
|
|
41
|
+
`"faiss-cpu 1.16.0"`, and `stats["backend"]` which backend ran.
|
|
42
|
+
|
|
43
|
+
The package passes `unused_threshold=0.1` (`0.0` when d <= 2) to every e-means engine, so it keeps mass-based
|
|
44
|
+
revival of unused clusters, the rule of stock faiss and emeans-rocm 0.2.5, also on emeans-rocm 0.2.6, whose default is
|
|
45
|
+
cost-based revival; `stats["unused_threshold"]` reports the value used. emeans-rocm 0.2.6 places revived centroids
|
|
46
|
+
differently (along the donor cluster's long axis), so GPU fits at d > 2 are not the same as on 0.2.5; CPU fits and the
|
|
47
|
+
stock faiss-cpu engine are unchanged. Models saved with an earlier version load and assign unchanged, because
|
|
48
|
+
assignment from saved centroids does not depend on the engine version. A refit on emeans-rocm 0.2.6 gives assignments
|
|
49
|
+
(`cluster_id`s) that differ from those of a 0.2.5 fit about as much as a different seed does, while the objectives stay
|
|
50
|
+
close (figures under Benchmarks). The benchmark figures below are for emeans-rocm 0.2.6.
|
|
51
|
+
|
|
52
|
+
Importing the package never imports PyTorch. PyTorch is loaded only when a GPU backend is considered.
|
|
53
|
+
|
|
54
|
+
## Quick Start
|
|
55
|
+
|
|
56
|
+
A CPU run of fit, cluster and rank on random embeddings; it downloads no model.
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import numpy as np
|
|
60
|
+
from haystack import Document
|
|
61
|
+
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
|
|
62
|
+
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
|
63
|
+
|
|
64
|
+
from haystack_integrations.components.preprocessors.emeans import EmeansDocumentClusterer, EmeansFitter
|
|
65
|
+
from haystack_integrations.components.rankers.emeans import EmeansClusterRanker
|
|
66
|
+
|
|
67
|
+
rng = np.random.default_rng(0)
|
|
68
|
+
docs = [Document(content=f"document {i}", embedding=rng.normal(size=64).tolist()) for i in range(500)]
|
|
69
|
+
|
|
70
|
+
# Fit once and save the model.
|
|
71
|
+
fitted = EmeansFitter(target_cluster_size=50, model_path="clusters.npz", overwrite=True).run(documents=docs)
|
|
72
|
+
stats = fitted["stats"]
|
|
73
|
+
|
|
74
|
+
# Assign a stable cluster_id to every document.
|
|
75
|
+
clustered = EmeansDocumentClusterer().run(documents=docs, model_path=fitted["model_path"])["documents"]
|
|
76
|
+
|
|
77
|
+
# Retrieve, then keep the best document of each cluster.
|
|
78
|
+
store = InMemoryDocumentStore()
|
|
79
|
+
store.write_documents(clustered)
|
|
80
|
+
query_embedding = rng.normal(size=64).tolist()
|
|
81
|
+
retriever = InMemoryEmbeddingRetriever(document_store=store, top_k=50)
|
|
82
|
+
retrieved = retriever.run(query_embedding=query_embedding)["documents"]
|
|
83
|
+
diverse = EmeansClusterRanker(top_k=5).run(documents=retrieved)["documents"]
|
|
84
|
+
|
|
85
|
+
print(stats["k"], stats["backend"])
|
|
86
|
+
for doc in diverse:
|
|
87
|
+
print(doc.score, doc.meta["cluster_id"])
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
In real use a Haystack document embedder, for example `SentenceTransformersDocumentEmbedder` from
|
|
91
|
+
`sentence-transformers-haystack`, sits in front. The full pipelines are in the repository's
|
|
92
|
+
[examples](https://github.com/ogaude/emeans-haystack/tree/main/examples): 01 indexing, 02 diverse retrieval, 03 new
|
|
93
|
+
documents, 04 GPU.
|
|
94
|
+
|
|
95
|
+
## Benchmarks
|
|
96
|
+
|
|
97
|
+
- e-means needs fewer passes than Lloyd's k-means (`faiss.Kmeans`, scikit-learn `KMeans`) to reach the same objective in all 5 sweep comparisons (k = 4096 and 16384); its passes are not faster, and at its default 5 passes Lloyd's k-means keeps the better objective in 3 settings (below). On 1M Cohere embeddings (768-d) with k = 16384 (pass-sweep medians), CPU e-means after 5 passes (objective 0.18569) beats `faiss.Kmeans` after 25 passes (0.18746); at the 25-pass points a pass costs about the same (114 vs 111 s/pass), so 575 s vs 2765 s.
|
|
98
|
+
- On an AMD RX 9060 XT with `emeans-rocm` 0.2.6, GPU e-means was faster than CPU e-means cold in all 16 settings and warm in all 12 that have a CPU warm time. FP32 against FP32 at 1M x 65536 on Cohere (cold, end-to-end, CPU 2232 s): native FP32 31.1x, Triton FP32 27.5x; Triton BF16 on the GPU against FP32 on the CPU: 187.0x (11.9 s). The smallest gain is 4.1x at random 100k x 1024. Against emeans-rocm 0.2.5 on the same cells, the median cold fit time ratio is 0.99 (warm 0.85). A refit on 0.2.6 gives assignments that differ from a 0.2.5 fit about as much as a different seed does: on Cohere 100k the same-seed ARI between versions is 0.27-0.40, against 0.25-0.39 between 0.2.5 seeds and 1.0 for a BF16 0.2.5 rerun. The objectives stay close: within 1.342 % of 0.2.5 and lower on 0.2.6 in 71 of 96 main-cell records, the largest difference at Cohere 100k x 65536 (1.5 rows per centroid). Models saved before load and assign unchanged ([comparison](https://github.com/ogaude/emeans-haystack/blob/main/benchmarks/RESULTS.md)).
|
|
99
|
+
- Without a GPU, on real data (Cohere), CPU e-means finishes sooner than Lloyd's k-means with an objective within 0.5 % in 6 of 7 comparable settings (11 of 14 over both datasets). Lloyd's k-means has the better objective in 3 settings: 100k x 65536 on both datasets (degenerate: 1.5 rows per centroid; e-means +22.22 % on Cohere, +36.81 % on random) and random 100k x 16384 (e-means +7.90 %).
|
|
100
|
+
- On real data, FP16/BF16 objectives are between -0.070 % and +0.055 % of FP32, but at about 100k rows they change document assignments almost as much as reseeding does: use FP32 when stable `cluster_id`s matter.
|
|
101
|
+
- Full tables, protocol, every cut made for time or memory and the settings not run: [benchmarks/RESULTS.md](https://github.com/ogaude/emeans-haystack/blob/main/benchmarks/RESULTS.md). Random data (Gaussian noise, no cluster structure) is used for speed; its objectives appear here only as the exceptions noted above.
|
|
102
|
+
|
|
103
|
+
## Contributing
|
|
104
|
+
|
|
105
|
+
Refer to the general [Contribution Guidelines](https://github.com/deepset-ai/haystack-core-integrations/blob/main/CONTRIBUTING.md).
|
|
106
|
+
|
|
107
|
+
GPU tests are marked `integration` and `gpu`, so `hatch run test:unit` and CI never run them. They are local
|
|
108
|
+
only: run them with `hatch run gpu:gpu-tests` (upstream e-means) or `hatch run gpu-fork:gpu-tests` (installs
|
|
109
|
+
`emeans-rocm` 0.2.6 from PyPI). They skip when PyTorch sees no GPU.
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
`emeans-haystack` is distributed under the Apache-2.0 license (see `LICENSE.txt`).
|
|
114
|
+
|
|
115
|
+
Third-party notices:
|
|
116
|
+
|
|
117
|
+
- e-means is developed by Meta FAIR and ships in faiss (`faiss.contrib.e_means`) under the MIT license
|
|
118
|
+
(Copyright (c) Facebook, Inc. and its affiliates). This package depends on faiss-cpu and does not include its code.
|
|
119
|
+
- emeans-rocm (optional, not a dependency) packages Meta's e-means code and is distributed under the MIT license.
|
|
120
|
+
|
|
121
|
+
## Citation
|
|
122
|
+
|
|
123
|
+
If you use e-means, cite the paper:
|
|
124
|
+
|
|
125
|
+
```bibtex
|
|
126
|
+
@misc{bessa2026emeans,
|
|
127
|
+
title = {{e-means}: outperforming {k-means} (in one-tenth the epochs)},
|
|
128
|
+
author = {Bessa, Swann and Mazar{\'e}, Pierre-Emmanuel and Szilvasy, Gergely and
|
|
129
|
+
Touvron, Hugo and Picard, David and Douze, Matthijs and J{\'e}gou, Herv{\'e}},
|
|
130
|
+
year = {2026},
|
|
131
|
+
note = {Preprint},
|
|
132
|
+
url = {https://arxiv.org/abs/2611.09501}
|
|
133
|
+
}
|
|
134
|
+
```
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
loaders:
|
|
2
|
+
- modules:
|
|
3
|
+
- haystack_integrations.components.preprocessors.emeans.fitter
|
|
4
|
+
- haystack_integrations.components.preprocessors.emeans.clusterer
|
|
5
|
+
- haystack_integrations.components.rankers.emeans.ranker
|
|
6
|
+
search_path: [../src]
|
|
7
|
+
processors:
|
|
8
|
+
- type: filter
|
|
9
|
+
documented_only: true
|
|
10
|
+
skip_empty_modules: true
|
|
11
|
+
renderer:
|
|
12
|
+
description: e-means clustering integration for Haystack
|
|
13
|
+
id: integrations-emeans
|
|
14
|
+
filename: emeans.md
|
|
15
|
+
title: e-means
|