emeans 0.1.1__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-0.1.1/.gitignore +1 -0
- emeans-0.1.1/Cargo.lock +288 -0
- emeans-0.1.1/Cargo.toml +38 -0
- emeans-0.1.1/LICENSE +25 -0
- emeans-0.1.1/PKG-INFO +37 -0
- emeans-0.1.1/README.md +95 -0
- emeans-0.1.1/build.rs +26 -0
- emeans-0.1.1/examples/bench.rs +38 -0
- emeans-0.1.1/pyproject.toml +27 -0
- emeans-0.1.1/python/Cargo.toml +20 -0
- emeans-0.1.1/python/LICENSE +25 -0
- emeans-0.1.1/python/README.md +21 -0
- emeans-0.1.1/python/emeans/__init__.py +136 -0
- emeans-0.1.1/python/src/lib.rs +274 -0
- emeans-0.1.1/python/tests/test_emeans.py +82 -0
- emeans-0.1.1/src/emeans.rs +942 -0
- emeans-0.1.1/src/error.rs +31 -0
- emeans-0.1.1/src/knn.rs +496 -0
- emeans-0.1.1/src/lib.rs +35 -0
- emeans-0.1.1/src/recipe.rs +171 -0
- emeans-0.1.1/src/rng.rs +111 -0
- emeans-0.1.1/tests/train.rs +199 -0
emeans-0.1.1/.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/target
|
emeans-0.1.1/Cargo.lock
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
|
2
|
+
# It is not intended for manual editing.
|
|
3
|
+
version = 4
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "autocfg"
|
|
7
|
+
version = "1.5.1"
|
|
8
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
9
|
+
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
|
10
|
+
|
|
11
|
+
[[package]]
|
|
12
|
+
name = "crossbeam-deque"
|
|
13
|
+
version = "0.8.8"
|
|
14
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
15
|
+
checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a"
|
|
16
|
+
dependencies = [
|
|
17
|
+
"crossbeam-epoch",
|
|
18
|
+
"crossbeam-utils",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[[package]]
|
|
22
|
+
name = "crossbeam-epoch"
|
|
23
|
+
version = "0.9.21"
|
|
24
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
25
|
+
checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d"
|
|
26
|
+
dependencies = [
|
|
27
|
+
"crossbeam-utils",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[[package]]
|
|
31
|
+
name = "crossbeam-utils"
|
|
32
|
+
version = "0.8.23"
|
|
33
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
34
|
+
checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6"
|
|
35
|
+
|
|
36
|
+
[[package]]
|
|
37
|
+
name = "either"
|
|
38
|
+
version = "1.19.0"
|
|
39
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
40
|
+
checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be"
|
|
41
|
+
|
|
42
|
+
[[package]]
|
|
43
|
+
name = "emeans"
|
|
44
|
+
version = "0.1.1"
|
|
45
|
+
dependencies = [
|
|
46
|
+
"matrixmultiply",
|
|
47
|
+
"rayon",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
[[package]]
|
|
51
|
+
name = "emeans-python"
|
|
52
|
+
version = "0.1.1"
|
|
53
|
+
dependencies = [
|
|
54
|
+
"emeans",
|
|
55
|
+
"numpy",
|
|
56
|
+
"pyo3",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
[[package]]
|
|
60
|
+
name = "heck"
|
|
61
|
+
version = "0.5.0"
|
|
62
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
63
|
+
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
|
64
|
+
|
|
65
|
+
[[package]]
|
|
66
|
+
name = "libc"
|
|
67
|
+
version = "0.2.191"
|
|
68
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
69
|
+
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
|
70
|
+
|
|
71
|
+
[[package]]
|
|
72
|
+
name = "matrixmultiply"
|
|
73
|
+
version = "0.3.11"
|
|
74
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
75
|
+
checksum = "3f607c237553f086e7043417a51df26b2eb899d3caff94e6a67592ff992fedc7"
|
|
76
|
+
dependencies = [
|
|
77
|
+
"autocfg",
|
|
78
|
+
"rawpointer",
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
[[package]]
|
|
82
|
+
name = "ndarray"
|
|
83
|
+
version = "0.17.2"
|
|
84
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
85
|
+
checksum = "520080814a7a6b4a6e9070823bb24b4531daac8c4627e08ba5de8c5ef2f2752d"
|
|
86
|
+
dependencies = [
|
|
87
|
+
"matrixmultiply",
|
|
88
|
+
"num-complex",
|
|
89
|
+
"num-integer",
|
|
90
|
+
"num-traits",
|
|
91
|
+
"portable-atomic",
|
|
92
|
+
"portable-atomic-util",
|
|
93
|
+
"rawpointer",
|
|
94
|
+
]
|
|
95
|
+
|
|
96
|
+
[[package]]
|
|
97
|
+
name = "num-complex"
|
|
98
|
+
version = "0.4.6"
|
|
99
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
100
|
+
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
|
|
101
|
+
dependencies = [
|
|
102
|
+
"num-traits",
|
|
103
|
+
]
|
|
104
|
+
|
|
105
|
+
[[package]]
|
|
106
|
+
name = "num-integer"
|
|
107
|
+
version = "0.1.47"
|
|
108
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
109
|
+
checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b"
|
|
110
|
+
dependencies = [
|
|
111
|
+
"num-traits",
|
|
112
|
+
]
|
|
113
|
+
|
|
114
|
+
[[package]]
|
|
115
|
+
name = "num-traits"
|
|
116
|
+
version = "0.2.19"
|
|
117
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
118
|
+
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
|
119
|
+
dependencies = [
|
|
120
|
+
"autocfg",
|
|
121
|
+
]
|
|
122
|
+
|
|
123
|
+
[[package]]
|
|
124
|
+
name = "numpy"
|
|
125
|
+
version = "0.30.0"
|
|
126
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
127
|
+
checksum = "aa0b9111761885449e894981fa8051dcf69e0bd114aecbd4bcdd830b6be4ed92"
|
|
128
|
+
dependencies = [
|
|
129
|
+
"libc",
|
|
130
|
+
"ndarray",
|
|
131
|
+
"num-complex",
|
|
132
|
+
"num-integer",
|
|
133
|
+
"num-traits",
|
|
134
|
+
"pyo3",
|
|
135
|
+
"pyo3-build-config",
|
|
136
|
+
"rustc-hash",
|
|
137
|
+
]
|
|
138
|
+
|
|
139
|
+
[[package]]
|
|
140
|
+
name = "once_cell"
|
|
141
|
+
version = "1.21.4"
|
|
142
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
143
|
+
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
|
144
|
+
|
|
145
|
+
[[package]]
|
|
146
|
+
name = "portable-atomic"
|
|
147
|
+
version = "1.15.0"
|
|
148
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
149
|
+
checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
|
|
150
|
+
|
|
151
|
+
[[package]]
|
|
152
|
+
name = "portable-atomic-util"
|
|
153
|
+
version = "0.2.8"
|
|
154
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
155
|
+
checksum = "10ab3eb7f3becc3a1cbc4f2c6f20267996cfc1a6467a873763411b136a122715"
|
|
156
|
+
dependencies = [
|
|
157
|
+
"portable-atomic",
|
|
158
|
+
]
|
|
159
|
+
|
|
160
|
+
[[package]]
|
|
161
|
+
name = "proc-macro2"
|
|
162
|
+
version = "1.0.107"
|
|
163
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
164
|
+
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
|
165
|
+
dependencies = [
|
|
166
|
+
"unicode-ident",
|
|
167
|
+
]
|
|
168
|
+
|
|
169
|
+
[[package]]
|
|
170
|
+
name = "pyo3"
|
|
171
|
+
version = "0.30.1"
|
|
172
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
173
|
+
checksum = "229ca13b8a15bb0d1487b81ab975bfcd97513416499a8312011b6036e42e0de4"
|
|
174
|
+
dependencies = [
|
|
175
|
+
"libc",
|
|
176
|
+
"once_cell",
|
|
177
|
+
"portable-atomic",
|
|
178
|
+
"pyo3-build-config",
|
|
179
|
+
"pyo3-ffi",
|
|
180
|
+
"pyo3-macros",
|
|
181
|
+
]
|
|
182
|
+
|
|
183
|
+
[[package]]
|
|
184
|
+
name = "pyo3-build-config"
|
|
185
|
+
version = "0.30.1"
|
|
186
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
187
|
+
checksum = "70130e2b6dd1ee589a8788026a429561ca5c40fa7a2fa7914c2282eddae24700"
|
|
188
|
+
dependencies = [
|
|
189
|
+
"target-lexicon",
|
|
190
|
+
]
|
|
191
|
+
|
|
192
|
+
[[package]]
|
|
193
|
+
name = "pyo3-ffi"
|
|
194
|
+
version = "0.30.1"
|
|
195
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
196
|
+
checksum = "5e7ff73c555c286d212d4ce7e5fbda845a973c4f61bd5f160f5f2c3c2a04d1c4"
|
|
197
|
+
dependencies = [
|
|
198
|
+
"libc",
|
|
199
|
+
"pyo3-build-config",
|
|
200
|
+
]
|
|
201
|
+
|
|
202
|
+
[[package]]
|
|
203
|
+
name = "pyo3-macros"
|
|
204
|
+
version = "0.30.1"
|
|
205
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
206
|
+
checksum = "c46d2e5862c7bb8207e3b639f713dda3c499933444784154c11d013c5a691b60"
|
|
207
|
+
dependencies = [
|
|
208
|
+
"proc-macro2",
|
|
209
|
+
"pyo3-macros-backend",
|
|
210
|
+
"quote",
|
|
211
|
+
"syn",
|
|
212
|
+
]
|
|
213
|
+
|
|
214
|
+
[[package]]
|
|
215
|
+
name = "pyo3-macros-backend"
|
|
216
|
+
version = "0.30.1"
|
|
217
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
218
|
+
checksum = "38b89f066f2ae72a2aef52d1537ca2fcd104440ebfadee06e97aa90738a25b9b"
|
|
219
|
+
dependencies = [
|
|
220
|
+
"heck",
|
|
221
|
+
"proc-macro2",
|
|
222
|
+
"quote",
|
|
223
|
+
"syn",
|
|
224
|
+
]
|
|
225
|
+
|
|
226
|
+
[[package]]
|
|
227
|
+
name = "quote"
|
|
228
|
+
version = "1.0.47"
|
|
229
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
230
|
+
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
|
231
|
+
dependencies = [
|
|
232
|
+
"proc-macro2",
|
|
233
|
+
]
|
|
234
|
+
|
|
235
|
+
[[package]]
|
|
236
|
+
name = "rawpointer"
|
|
237
|
+
version = "0.2.1"
|
|
238
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
239
|
+
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
|
|
240
|
+
|
|
241
|
+
[[package]]
|
|
242
|
+
name = "rayon"
|
|
243
|
+
version = "1.12.0"
|
|
244
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
245
|
+
checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d"
|
|
246
|
+
dependencies = [
|
|
247
|
+
"either",
|
|
248
|
+
"rayon-core",
|
|
249
|
+
]
|
|
250
|
+
|
|
251
|
+
[[package]]
|
|
252
|
+
name = "rayon-core"
|
|
253
|
+
version = "1.13.0"
|
|
254
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
255
|
+
checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
|
|
256
|
+
dependencies = [
|
|
257
|
+
"crossbeam-deque",
|
|
258
|
+
"crossbeam-utils",
|
|
259
|
+
]
|
|
260
|
+
|
|
261
|
+
[[package]]
|
|
262
|
+
name = "rustc-hash"
|
|
263
|
+
version = "2.1.3"
|
|
264
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
265
|
+
checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d"
|
|
266
|
+
|
|
267
|
+
[[package]]
|
|
268
|
+
name = "syn"
|
|
269
|
+
version = "3.0.10"
|
|
270
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
271
|
+
checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79"
|
|
272
|
+
dependencies = [
|
|
273
|
+
"proc-macro2",
|
|
274
|
+
"quote",
|
|
275
|
+
"unicode-ident",
|
|
276
|
+
]
|
|
277
|
+
|
|
278
|
+
[[package]]
|
|
279
|
+
name = "target-lexicon"
|
|
280
|
+
version = "0.13.5"
|
|
281
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
282
|
+
checksum = "adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca"
|
|
283
|
+
|
|
284
|
+
[[package]]
|
|
285
|
+
name = "unicode-ident"
|
|
286
|
+
version = "1.0.27"
|
|
287
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
288
|
+
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
emeans-0.1.1/Cargo.toml
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "emeans"
|
|
3
|
+
version = "0.1.1"
|
|
4
|
+
edition = "2024"
|
|
5
|
+
rust-version = "1.89"
|
|
6
|
+
description = "e-means clustering in Rust, a port of the CPU version from faiss/contrib/e_means"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
repository = "https://github.com/savannahar68/emeans"
|
|
10
|
+
documentation = "https://docs.rs/emeans"
|
|
11
|
+
keywords = ["kmeans", "clustering", "emeans", "vector", "faiss"]
|
|
12
|
+
categories = ["algorithms", "science", "mathematics"]
|
|
13
|
+
exclude = ["/scripts", "/tests/data", "/tests/parity.rs", "/.github", "/python"]
|
|
14
|
+
|
|
15
|
+
[features]
|
|
16
|
+
default = ["parallel"]
|
|
17
|
+
# Search the nearest centroids on all cores with rayon.
|
|
18
|
+
parallel = ["dep:rayon"]
|
|
19
|
+
# Use Intel MKL (libmkl_rt) for the matrix multiply, about 10-15% faster on Intel.
|
|
20
|
+
# Set MKL_LIB_DIR if libmkl_rt is not on the default search path.
|
|
21
|
+
mkl = []
|
|
22
|
+
# macOS only: use Apple Accelerate for the matrix multiply. About 2x faster on Apple
|
|
23
|
+
# chips, but Accelerate can use several hundred MB more memory on wide vectors.
|
|
24
|
+
accelerate = []
|
|
25
|
+
|
|
26
|
+
[dependencies]
|
|
27
|
+
matrixmultiply = "0.3.10"
|
|
28
|
+
rayon = { version = "1.10", optional = true }
|
|
29
|
+
|
|
30
|
+
[profile.release]
|
|
31
|
+
codegen-units = 1
|
|
32
|
+
lto = "thin"
|
|
33
|
+
|
|
34
|
+
[dev-dependencies]
|
|
35
|
+
rayon = "1.10"
|
|
36
|
+
|
|
37
|
+
[workspace]
|
|
38
|
+
members = ["python"]
|
emeans-0.1.1/LICENSE
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
4
|
+
Copyright (c) 2026 Savan Nahar
|
|
5
|
+
|
|
6
|
+
This crate is a Rust port of faiss/contrib/e_means, which is released by
|
|
7
|
+
Meta Platforms, Inc. under the MIT license.
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
emeans-0.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: emeans
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Classifier: Programming Language :: Rust
|
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
|
6
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
7
|
+
Classifier: Topic :: Scientific/Engineering
|
|
8
|
+
Requires-Dist: numpy>=1.21
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Summary: Fast e-means clustering (k-means in a tenth of the passes), written in Rust
|
|
11
|
+
License: MIT
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
14
|
+
Project-URL: Repository, https://github.com/savannahar68/emeans
|
|
15
|
+
|
|
16
|
+
# emeans
|
|
17
|
+
|
|
18
|
+
Fast [e-means](https://github.com/facebookresearch/faiss/tree/main/contrib/e_means) clustering, written in Rust. e-means is a k-means replacement from Meta FAIR that reaches a lower error in about a tenth of the passes.
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
pip install emeans
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Same class as faiss `NumpyEmeans`, so most code only needs a new import:
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
import numpy as np
|
|
28
|
+
from emeans import Emeans
|
|
29
|
+
|
|
30
|
+
x = np.random.rand(200_000, 64).astype("float32")
|
|
31
|
+
em = Emeans(64, 500)
|
|
32
|
+
centroids, labels = em.train(x)
|
|
33
|
+
distances, nearest = em.assign(x)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Needs only numpy. 1.7x to 6x faster than faiss `NumpyEmeans` in our tests, same error. Numbers and source: https://github.com/savannahar68/emeans
|
|
37
|
+
|
emeans-0.1.1/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# emeans
|
|
2
|
+
|
|
3
|
+
Rust port of [e-means](https://github.com/facebookresearch/faiss/tree/main/contrib/e_means) from faiss, with Python bindings. e-means is a k-means replacement from Meta FAIR that reaches a lower error in about a tenth of the passes ([paper](https://arxiv.org/abs/2611.09501)).
|
|
4
|
+
|
|
5
|
+
This ports the CPU version (`NumpyEmeans`). Results match the Python code: same input, order and starting centroids give the same centroids within 1e-4 (`tests/parity.rs`).
|
|
6
|
+
|
|
7
|
+
## What is faster
|
|
8
|
+
|
|
9
|
+
Almost all the time goes into finding the nearest centroid of each row. Here it is:
|
|
10
|
+
|
|
11
|
+
- a SIMD matrix multiply (`matrixmultiply`, AVX-512 / AVX2 / NEON picked at run time),
|
|
12
|
+
- a vectorized scan for the smallest distance, built for AVX-512, AVX2 and plain, picked at run time from the CPU,
|
|
13
|
+
- spread over all cores with `rayon`, with the same result for any number of threads.
|
|
14
|
+
|
|
15
|
+
No feature flag is needed for SIMD. Two optional features swap the matrix multiply:
|
|
16
|
+
|
|
17
|
+
- `mkl`: Intel MKL, 10-15% faster on Intel. Needs `libmkl_rt` (set `MKL_LIB_DIR` if it is not found).
|
|
18
|
+
- `accelerate` (macOS only): Apple's matrix unit, about 2x faster. Apple's library can take several hundred MB more memory on wide vectors (d=768), so it is not on by default.
|
|
19
|
+
|
|
20
|
+
Memory: on top of your data, training needs about 20-30 MB, plus 8 bytes per row for the labels.
|
|
21
|
+
|
|
22
|
+
## Numbers
|
|
23
|
+
|
|
24
|
+
5 passes on random data, time to train. faiss is `NumpyEmeans` from faiss-cpu 1.15. The final error is the same for all.
|
|
25
|
+
|
|
26
|
+
Intel Xeon 8481C, 8 vCPU (GCP c3-standard-8):
|
|
27
|
+
|
|
28
|
+
| n, d, k | faiss (Python) | emeans (Python) |
|
|
29
|
+
|---|---|---|
|
|
30
|
+
| 200k, 64, 500 | 1.38 s | 0.27 s |
|
|
31
|
+
| 1M, 128, 1024 | 16.77 s | 4.24 s |
|
|
32
|
+
| 1M, 32, 4096 | 6.94 s | 4.11 s |
|
|
33
|
+
| 500k, 768, 256 | 24.87 s | 4.08 s |
|
|
34
|
+
|
|
35
|
+
Apple M4, 10 cores:
|
|
36
|
+
|
|
37
|
+
| n, d, k | faiss (Python) | emeans (Python) | emeans + `accelerate` (Rust) |
|
|
38
|
+
|---|---|---|---|
|
|
39
|
+
| 200k, 64, 500 | 0.70 s | 0.17 s | 0.13 s |
|
|
40
|
+
| 1M, 128, 1024 | 7.01 s | 3.06 s | 1.50 s |
|
|
41
|
+
| 1M, 32, 4096 | 10.05 s | 3.04 s | 1.99 s |
|
|
42
|
+
| 500k, 768, 256 | 11.42 s | 3.08 s | 1.90 s |
|
|
43
|
+
|
|
44
|
+
Other Rust crates on the same Xeon, 5 iterations, time / error / peak memory:
|
|
45
|
+
|
|
46
|
+
| n, d, k | emeans | [fastkmeans-rs](https://crates.io/crates/fastkmeans-rs) | [kmeans_uni](https://crates.io/crates/kmeans_uni) |
|
|
47
|
+
|---|---|---|---|
|
|
48
|
+
| 200k, 64, 500 | 0.30 s / 4.502 / 63 MB | 0.56 s / 4.533 / 490 MB | 7.75 s / 4.533 / 59 MB |
|
|
49
|
+
| 1M, 128, 1024 | 4.52 s / 9.719 / 539 MB | 5.78 s / 9.751 / 2.68 GB | 128.8 s / 9.751 / 529 MB |
|
|
50
|
+
|
|
51
|
+
Run it yourself: `cargo run --release --example bench -- 1000000 128 1024`.
|
|
52
|
+
|
|
53
|
+
## Python
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
pip install emeans
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Same class as faiss `NumpyEmeans`, so most code only needs a new import:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from emeans import Emeans
|
|
63
|
+
|
|
64
|
+
em = Emeans(d, k) # nepochs=5, seed=1234, spherical=False, ...
|
|
65
|
+
centroids, labels = em.train(x) # x: (n, d) array
|
|
66
|
+
distances, nearest = em.assign(x_new)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Training releases the GIL. For data bigger than memory, the faiss loop works as is: `em.begin(n, init)`, then `em.update(em.accumulate(batch))`, `em.end_epoch()`, `em.finalize()`.
|
|
70
|
+
|
|
71
|
+
## Rust
|
|
72
|
+
|
|
73
|
+
```toml
|
|
74
|
+
[dependencies]
|
|
75
|
+
emeans = "0.1"
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```rust
|
|
79
|
+
use emeans::Emeans;
|
|
80
|
+
|
|
81
|
+
let model = Emeans::new(d, k).nepochs(5.0).train(&x)?; // x: n * d values, row major
|
|
82
|
+
let (distances, nearest) = model.assign(&x_new)?;
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
For data bigger than memory, use `Emeans::begin` and feed batches yourself (see the docs).
|
|
86
|
+
|
|
87
|
+
## Differences from faiss
|
|
88
|
+
|
|
89
|
+
- The random generator is not numpy's, so the same seed gives a different (equally good) run.
|
|
90
|
+
- Batch sums are added in f64.
|
|
91
|
+
- GPU kernels, multi GPU classes and 8/16 bit formats are not ported.
|
|
92
|
+
|
|
93
|
+
## License
|
|
94
|
+
|
|
95
|
+
MIT, like faiss. If you use e-means, please cite the [paper](https://arxiv.org/abs/2611.09501).
|
emeans-0.1.1/build.rs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Picks the matrix multiply: MKL with the `mkl` feature, Apple Accelerate with the
|
|
2
|
+
// `accelerate` feature, and the pure Rust `matrixmultiply` otherwise.
|
|
3
|
+
fn main() {
|
|
4
|
+
println!("cargo::rustc-check-cfg=cfg(emeans_blas)");
|
|
5
|
+
println!("cargo:rerun-if-env-changed=MKL_LIB_DIR");
|
|
6
|
+
let mkl = std::env::var_os("CARGO_FEATURE_MKL").is_some();
|
|
7
|
+
let accelerate = std::env::var_os("CARGO_FEATURE_ACCELERATE").is_some();
|
|
8
|
+
if mkl && accelerate {
|
|
9
|
+
panic!("pick only one of the mkl and accelerate features");
|
|
10
|
+
}
|
|
11
|
+
if mkl {
|
|
12
|
+
// Set MKL_LIB_DIR when libmkl_rt is not on the default search path.
|
|
13
|
+
if let Ok(dir) = std::env::var("MKL_LIB_DIR") {
|
|
14
|
+
println!("cargo:rustc-link-search=native={dir}");
|
|
15
|
+
}
|
|
16
|
+
println!("cargo:rustc-link-lib=mkl_rt");
|
|
17
|
+
println!("cargo:rustc-cfg=emeans_blas");
|
|
18
|
+
}
|
|
19
|
+
if accelerate {
|
|
20
|
+
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("macos") {
|
|
21
|
+
panic!("the accelerate feature only works on macOS");
|
|
22
|
+
}
|
|
23
|
+
println!("cargo:rustc-link-lib=framework=Accelerate");
|
|
24
|
+
println!("cargo:rustc-cfg=emeans_blas");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//! Time training on random data and print the final error.
|
|
2
|
+
//!
|
|
3
|
+
//! cargo run --release --example bench -- 200000 64 500
|
|
4
|
+
|
|
5
|
+
use std::time::Instant;
|
|
6
|
+
|
|
7
|
+
use emeans::Emeans;
|
|
8
|
+
|
|
9
|
+
fn main() {
|
|
10
|
+
let args: Vec<usize> = std::env::args()
|
|
11
|
+
.skip(1)
|
|
12
|
+
.map(|a| a.parse().expect("a number"))
|
|
13
|
+
.collect();
|
|
14
|
+
let (n, d, k) = match args[..] {
|
|
15
|
+
[n, d, k] => (n, d, k),
|
|
16
|
+
_ => (200_000, 64, 500),
|
|
17
|
+
};
|
|
18
|
+
let mut s = 0x2545_F491_4F6C_DD1Du64;
|
|
19
|
+
let x: Vec<f32> = (0..n * d)
|
|
20
|
+
.map(|_| {
|
|
21
|
+
s ^= s << 13;
|
|
22
|
+
s ^= s >> 7;
|
|
23
|
+
s ^= s << 17;
|
|
24
|
+
(s >> 40) as f32 / (1u64 << 24) as f32
|
|
25
|
+
})
|
|
26
|
+
.collect();
|
|
27
|
+
|
|
28
|
+
let start = Instant::now();
|
|
29
|
+
let model = Emeans::new(d, k).train(&x).unwrap();
|
|
30
|
+
let train = start.elapsed().as_secs_f64();
|
|
31
|
+
let (dist, _) = model.assign(&x).unwrap();
|
|
32
|
+
let mse = dist.iter().map(|&v| v as f64).sum::<f64>() / n as f64;
|
|
33
|
+
let r = model.recipe().unwrap();
|
|
34
|
+
println!(
|
|
35
|
+
"n={n} d={d} k={k} batch={} train={train:.3}s mse={mse:.5}",
|
|
36
|
+
r.batch_size
|
|
37
|
+
);
|
|
38
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["maturin>=1.7,<2"]
|
|
3
|
+
build-backend = "maturin"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "emeans"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "Fast e-means clustering (k-means in a tenth of the passes), written in Rust"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
dependencies = ["numpy>=1.21"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Programming Language :: Rust",
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Topic :: Scientific/Engineering",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Repository = "https://github.com/savannahar68/emeans"
|
|
22
|
+
|
|
23
|
+
[tool.maturin]
|
|
24
|
+
module-name = "emeans._emeans"
|
|
25
|
+
features = ["pyo3/extension-module"]
|
|
26
|
+
manifest-path = "python/Cargo.toml"
|
|
27
|
+
python-source = "python"
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "emeans-python"
|
|
3
|
+
version = "0.1.1"
|
|
4
|
+
edition = "2024"
|
|
5
|
+
license = "MIT"
|
|
6
|
+
publish = false
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
|
|
9
|
+
[lib]
|
|
10
|
+
name = "_emeans"
|
|
11
|
+
crate-type = ["cdylib"]
|
|
12
|
+
|
|
13
|
+
[features]
|
|
14
|
+
mkl = ["emeans/mkl"]
|
|
15
|
+
accelerate = ["emeans/accelerate"]
|
|
16
|
+
|
|
17
|
+
[dependencies]
|
|
18
|
+
emeans = { path = ".." }
|
|
19
|
+
numpy = "0.30"
|
|
20
|
+
pyo3 = { version = "0.30", features = ["extension-module", "abi3-py39"] }
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
4
|
+
Copyright (c) 2026 Savan Nahar
|
|
5
|
+
|
|
6
|
+
This crate is a Rust port of faiss/contrib/e_means, which is released by
|
|
7
|
+
Meta Platforms, Inc. under the MIT license.
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# emeans
|
|
2
|
+
|
|
3
|
+
Fast [e-means](https://github.com/facebookresearch/faiss/tree/main/contrib/e_means) clustering, written in Rust. e-means is a k-means replacement from Meta FAIR that reaches a lower error in about a tenth of the passes.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
pip install emeans
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Same class as faiss `NumpyEmeans`, so most code only needs a new import:
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
import numpy as np
|
|
13
|
+
from emeans import Emeans
|
|
14
|
+
|
|
15
|
+
x = np.random.rand(200_000, 64).astype("float32")
|
|
16
|
+
em = Emeans(64, 500)
|
|
17
|
+
centroids, labels = em.train(x)
|
|
18
|
+
distances, nearest = em.assign(x)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Needs only numpy. 1.7x to 6x faster than faiss `NumpyEmeans` in our tests, same error. Numbers and source: https://github.com/savannahar68/emeans
|