mlxvs 0.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.
- mlxvs-0.1.0/CMakeLists.txt +74 -0
- mlxvs-0.1.0/LICENSE +202 -0
- mlxvs-0.1.0/MANIFEST.in +5 -0
- mlxvs-0.1.0/NOTICE +13 -0
- mlxvs-0.1.0/PKG-INFO +213 -0
- mlxvs-0.1.0/README.md +187 -0
- mlxvs-0.1.0/include/mlxvs/mlxvs.h +330 -0
- mlxvs-0.1.0/pyproject.toml +46 -0
- mlxvs-0.1.0/python/bindings.cpp +452 -0
- mlxvs-0.1.0/python/mlxvs/__init__.py +155 -0
- mlxvs-0.1.0/python/mlxvs/_neighbors.py +96 -0
- mlxvs-0.1.0/python/mlxvs.egg-info/PKG-INFO +213 -0
- mlxvs-0.1.0/python/mlxvs.egg-info/SOURCES.txt +29 -0
- mlxvs-0.1.0/python/mlxvs.egg-info/dependency_links.txt +1 -0
- mlxvs-0.1.0/python/mlxvs.egg-info/not-zip-safe +1 -0
- mlxvs-0.1.0/python/mlxvs.egg-info/requires.txt +2 -0
- mlxvs-0.1.0/python/mlxvs.egg-info/top_level.txt +1 -0
- mlxvs-0.1.0/python/tests/test_mlxvs.py +377 -0
- mlxvs-0.1.0/setup.cfg +4 -0
- mlxvs-0.1.0/setup.py +18 -0
- mlxvs-0.1.0/src/all_neighbors.cpp +426 -0
- mlxvs-0.1.0/src/cagra.cpp +162 -0
- mlxvs-0.1.0/src/cagra_kernels.h +232 -0
- mlxvs-0.1.0/src/kernels.h +174 -0
- mlxvs-0.1.0/src/kmeans.cpp +171 -0
- mlxvs-0.1.0/src/knn.cpp +144 -0
- mlxvs-0.1.0/src/nnd_kernels.h +459 -0
- mlxvs-0.1.0/src/nndescent.cpp +283 -0
- mlxvs-0.1.0/src/search.cpp +175 -0
- mlxvs-0.1.0/src/search_kernels.h +304 -0
- mlxvs-0.1.0/src/utils.h +40 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.25)
|
|
2
|
+
project(mlxvs VERSION 0.1.0 LANGUAGES CXX)
|
|
3
|
+
|
|
4
|
+
set(CMAKE_CXX_STANDARD 20)
|
|
5
|
+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
6
|
+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
|
7
|
+
if(NOT CMAKE_BUILD_TYPE)
|
|
8
|
+
set(CMAKE_BUILD_TYPE Release)
|
|
9
|
+
endif()
|
|
10
|
+
|
|
11
|
+
option(MLXVS_BUILD_PYTHON "Build the Python bindings (nanobind)" OFF)
|
|
12
|
+
option(MLXVS_BUILD_TESTS "Build the C++ tests and benchmarks" ${PROJECT_IS_TOP_LEVEL})
|
|
13
|
+
|
|
14
|
+
# MLX's C++ library. For the Python bindings, the one the `mlx` wheel ships, so
|
|
15
|
+
# that one process holds one libmlx; otherwise an installed MLX (e.g. `brew
|
|
16
|
+
# install mlx`), or -DMLX_ROOT. mlxvs pins the MLX version it was built against.
|
|
17
|
+
if(MLXVS_BUILD_PYTHON)
|
|
18
|
+
find_package(
|
|
19
|
+
Python 3.10
|
|
20
|
+
COMPONENTS Interpreter Development.Module
|
|
21
|
+
REQUIRED)
|
|
22
|
+
execute_process(
|
|
23
|
+
COMMAND "${Python_EXECUTABLE}" -m mlx --cmake-dir
|
|
24
|
+
OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
25
|
+
OUTPUT_VARIABLE MLX_ROOT)
|
|
26
|
+
endif()
|
|
27
|
+
find_package(MLX CONFIG REQUIRED)
|
|
28
|
+
|
|
29
|
+
add_library(mlxvs STATIC src/knn.cpp src/kmeans.cpp src/nndescent.cpp
|
|
30
|
+
src/all_neighbors.cpp src/search.cpp src/cagra.cpp)
|
|
31
|
+
target_include_directories(mlxvs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
32
|
+
target_link_libraries(mlxvs PUBLIC mlx)
|
|
33
|
+
|
|
34
|
+
if(MLXVS_BUILD_TESTS)
|
|
35
|
+
add_executable(test_knn tests/test_knn.cpp)
|
|
36
|
+
target_link_libraries(test_knn PRIVATE mlxvs)
|
|
37
|
+
add_executable(test_nnd tests/test_nnd.cpp)
|
|
38
|
+
target_link_libraries(test_nnd PRIVATE mlxvs)
|
|
39
|
+
add_executable(test_all_neighbors tests/test_all_neighbors.cpp)
|
|
40
|
+
target_link_libraries(test_all_neighbors PRIVATE mlxvs)
|
|
41
|
+
add_executable(bench_knn bench/bench_knn.cpp)
|
|
42
|
+
target_link_libraries(bench_knn PRIVATE mlxvs)
|
|
43
|
+
enable_testing()
|
|
44
|
+
add_test(NAME test_knn COMMAND test_knn)
|
|
45
|
+
add_test(NAME test_nnd COMMAND test_nnd)
|
|
46
|
+
add_executable(test_search tests/test_search.cpp)
|
|
47
|
+
target_link_libraries(test_search PRIVATE mlxvs)
|
|
48
|
+
add_test(NAME test_all_neighbors COMMAND test_all_neighbors)
|
|
49
|
+
add_test(NAME test_search COMMAND test_search)
|
|
50
|
+
add_executable(test_cagra tests/test_cagra.cpp)
|
|
51
|
+
target_link_libraries(test_cagra PRIVATE mlxvs)
|
|
52
|
+
add_test(NAME test_cagra COMMAND test_cagra)
|
|
53
|
+
endif()
|
|
54
|
+
|
|
55
|
+
if(MLXVS_BUILD_PYTHON)
|
|
56
|
+
# nanobind from the `nanobind` wheel (python -m nanobind --cmake_dir)
|
|
57
|
+
execute_process(
|
|
58
|
+
COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
|
|
59
|
+
OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
60
|
+
OUTPUT_VARIABLE nanobind_ROOT)
|
|
61
|
+
find_package(nanobind CONFIG REQUIRED)
|
|
62
|
+
# NB_DOMAIN mlx: mlx.core's array, Stream and Device types are shared with
|
|
63
|
+
# the extension, as MLX's own extension examples do
|
|
64
|
+
nanobind_add_module(
|
|
65
|
+
_core
|
|
66
|
+
NB_STATIC
|
|
67
|
+
LTO
|
|
68
|
+
NOMINSIZE
|
|
69
|
+
NB_DOMAIN
|
|
70
|
+
mlx
|
|
71
|
+
python/bindings.cpp)
|
|
72
|
+
target_link_libraries(_core PRIVATE mlxvs)
|
|
73
|
+
install(TARGETS _core LIBRARY DESTINATION mlxvs)
|
|
74
|
+
endif()
|
mlxvs-0.1.0/LICENSE
ADDED
|
@@ -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 [yyyy] [name of copyright owner]
|
|
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.
|
mlxvs-0.1.0/MANIFEST.in
ADDED
mlxvs-0.1.0/NOTICE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
mlxvs
|
|
2
|
+
Copyright 2026 mikepapadim
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0.
|
|
5
|
+
|
|
6
|
+
The NN-Descent kernels (src/nnd_kernels.h, src/nndescent.cpp) are a port to
|
|
7
|
+
MLX of the Metal NN-Descent in JVector's jvector-gpu module
|
|
8
|
+
(https://github.com/datastax/jvector, Apache License 2.0, Copyright DataStax,
|
|
9
|
+
Inc.), from which the algorithm's structure, parameters and kernel design are
|
|
10
|
+
taken: random start over permutation blocks, 16 samples per node, 64-member
|
|
11
|
+
joins scored with SIMD-group matrices, hashed proposal slots and the bitonic
|
|
12
|
+
merge. The prune-side top-k selection in src/kernels.h follows the same
|
|
13
|
+
module's exact k-NN selection.
|
mlxvs-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mlxvs
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: GPU k-NN search and k-NN graph construction on Apple silicon, built on MLX
|
|
5
|
+
Author: mikepapadim
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/mikepapadim/mlxvs
|
|
8
|
+
Project-URL: Issues, https://github.com/mikepapadim/mlxvs/issues
|
|
9
|
+
Keywords: nearest-neighbors,knn,knn-graph,nn-descent,vector-search,k-means,gpu,metal,apple-silicon,mlx,cuvs,umap
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: C++
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
16
|
+
Classifier: Environment :: GPU
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
18
|
+
Classifier: Topic :: Database :: Database Engines/Servers
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
License-File: NOTICE
|
|
23
|
+
Requires-Dist: mlx<0.33,>=0.32.0
|
|
24
|
+
Requires-Dist: numpy>=1.24
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# mlxVS
|
|
28
|
+
|
|
29
|
+
**cuVS for your Mac: GPU vector search on Apple silicon.**
|
|
30
|
+
|
|
31
|
+
[](https://github.com/mikepapadim/mlxvs/actions/workflows/ci.yml)
|
|
32
|
+
[](LICENSE)
|
|
33
|
+
|
|
34
|
+
Index a million vectors in 10 seconds, then answer 70,000 queries per second at 97.6% recall, on a MacBook Pro's
|
|
35
|
+
GPU: twice the queries per second of HNSW on all 12 CPU cores at the same recall, from an index built 8 times faster
|
|
36
|
+
([numbers](#benchmarks)). mlxVS brings NVIDIA [cuVS](https://github.com/rapidsai/cuvs)'s operations and parameter
|
|
37
|
+
names to the Mac, built on [MLX](https://github.com/ml-explore/mlx): exact k-NN, k-means, NN-Descent, batched
|
|
38
|
+
all-neighbors graphs, CAGRA graph pruning and search. NumPy or MLX arrays in, any M-series Mac.
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
import mlxvs
|
|
42
|
+
graph = mlxvs.cagra_build(vectors, graph_degree=32) # 1M x 128 in ~10 s
|
|
43
|
+
ids, dist = mlxvs.graph_search(vectors, graph, queries, k=10) # ~70k queries/s at 97.6% recall
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
<picture>
|
|
47
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/mikepapadim/mlxvs/main/docs/img/sift1m-dark.png">
|
|
48
|
+
<img alt="SIFT1M on a MacBook Pro M3 Pro: queries per second against recall@10. mlxVS CAGRA degree 32 (built in 10.4 s) reaches 69,500 queries/s at 0.976 recall; hnswlib on 12 CPU cores (built in 88.1 s) reaches 32,200 at 0.964. Numbers in the Benchmarks table." src="https://raw.githubusercontent.com/mikepapadim/mlxvs/main/docs/img/sift1m-light.png" width="720">
|
|
49
|
+
</picture>
|
|
50
|
+
|
|
51
|
+
Use it for approximate nearest-neighbor search, for the k-NN graph behind UMAP, t-SNE, clustering and
|
|
52
|
+
deduplication, for exact k-NN ground truth, or as the build step of a graph index (HNSW, Vamana, CAGRA).
|
|
53
|
+
|
|
54
|
+
## What's in it
|
|
55
|
+
|
|
56
|
+
| Function | What it does | cuVS counterpart |
|
|
57
|
+
|---|---|---|
|
|
58
|
+
| `brute_force_knn(base, queries, k)` | Exact k-NN: matrix multiply and a fused top-k kernel, tiled over the base | `brute_force::search` |
|
|
59
|
+
| `kmeans_fit(x, clusters)`, `kmeans_predict(x, centroids, m)` | k-means++ and Lloyd; the `m` nearest centroids per row | `cluster::kmeans` |
|
|
60
|
+
| `nn_descent(rows, k)` | Approximate k-NN graph of the rows, FP16 SIMD-group joins | `nn_descent::build` |
|
|
61
|
+
| `all_neighbors(rows, k)` | k-NN graph built in overlapping k-means clusters, one on the GPU at a time; reads a NumPy array or memory-mapped file larger than memory where it is | `all_neighbors::build` |
|
|
62
|
+
| `cagra_build(rows, graph_degree)` | Search graph: k-NN graph (`all_neighbors`) pruned by `cagra_optimize` | `cagra::build` |
|
|
63
|
+
| `cagra_optimize(knn_graph, graph_degree)` | CAGRA's rank-based pruning (fewest two-hop detours) plus reverse edges | `cagra::optimize` |
|
|
64
|
+
| `graph_search(base, graph, queries, k)` | Approximate k-NN of queries by beam search of a graph, one threadgroup per query (CAGRA's single-CTA search) | `cagra::search` |
|
|
65
|
+
| `NearestNeighbors(n_neighbors).fit(X).kneighbors(Q)` | scikit-learn's interface over the above, NumPy in and out | |
|
|
66
|
+
|
|
67
|
+
Metrics: `"l2"` (squared), `"cosine"`, `"inner_product"`. Next: Vamana pruning and export to HNSW and JVector; see
|
|
68
|
+
[Roadmap](#roadmap).
|
|
69
|
+
|
|
70
|
+
## 1. Install
|
|
71
|
+
|
|
72
|
+
Needs an Apple-silicon Mac on macOS 14 or later (as MLX) and Python 3.10+.
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
pip install mlxvs
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Wheels for Python 3.10 to 3.14 on Apple silicon, built against the `mlx` release they pin (MLX has no stable C++
|
|
79
|
+
ABI). The latest code from GitHub builds the extension against your `mlx` wheel; that needs the Xcode Command Line
|
|
80
|
+
Tools (`xcode-select --install`) and takes about a minute:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pip install git+https://github.com/mikepapadim/mlxvs
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## 2. Use
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
import numpy as np
|
|
90
|
+
import mlxvs
|
|
91
|
+
|
|
92
|
+
base = np.random.rand(1_000_000, 128).astype(np.float32)
|
|
93
|
+
queries = base[:10_000]
|
|
94
|
+
|
|
95
|
+
# exact k-NN: ids [nq, k], distances [nq, k], nearest first
|
|
96
|
+
ids, dist = mlxvs.brute_force_knn(base, queries, k=10, metric="l2")
|
|
97
|
+
|
|
98
|
+
# approximate k-NN graph: every row's 32 nearest other rows
|
|
99
|
+
graph, graph_dist = mlxvs.nn_descent(base, k=32, metric="cosine")
|
|
100
|
+
|
|
101
|
+
# the same graph in overlapping k-means clusters, as many as the GPU's memory asks for
|
|
102
|
+
graph, graph_dist = mlxvs.all_neighbors(base, k=32, metric="cosine")
|
|
103
|
+
|
|
104
|
+
# a search index: the k-NN graph pruned by CAGRA's rules, then queries searched over it;
|
|
105
|
+
# itopk_size trades speed for recall
|
|
106
|
+
index = mlxvs.cagra_build(base, graph_degree=32, metric="cosine")
|
|
107
|
+
ids, dist = mlxvs.graph_search(base, index, queries, k=10, metric="cosine", itopk_size=64)
|
|
108
|
+
|
|
109
|
+
graph = np.asarray(graph) # results are mlx arrays; np.asarray converts without a copy
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- Inputs: MLX arrays, NumPy arrays, or anything `np.asarray` accepts, in any real dtype (computed in float32).
|
|
113
|
+
- Outputs: `int32` ids, `-1` where fewer neighbors exist; `float32` distances, squared for `"l2"`, similarities
|
|
114
|
+
(larger is closer) for `"cosine"` and `"inner_product"`.
|
|
115
|
+
- `help(mlxvs.nn_descent)` and the others document every parameter.
|
|
116
|
+
|
|
117
|
+
Data larger than memory: pass a `numpy.memmap`. The rows stay on disk; `all_neighbors` copies one cluster at a time
|
|
118
|
+
to the GPU.
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
rows = np.memmap("embeddings.f32", dtype=np.float32, mode="r").reshape(-1, 1024)
|
|
122
|
+
graph, graph_dist = mlxvs.all_neighbors(rows, k=32, metric="cosine", verbose=True)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Coming from scikit-learn, change the import:
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
nn = mlxvs.NearestNeighbors(n_neighbors=10, metric="euclidean").fit(base)
|
|
129
|
+
distances, indices = nn.kneighbors(queries) # NumPy arrays, scikit-learn's units
|
|
130
|
+
distances, indices = nn.kneighbors() # the base rows' own neighbors
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Examples: [UMAP from a GPU k-NN graph](examples/umap_knn.py) and
|
|
134
|
+
[exact ground truth for an ANN benchmark](examples/ground_truth.py) (`.fvecs` in, `.ivecs` out).
|
|
135
|
+
|
|
136
|
+
C++: the same functions in [`include/mlxvs/mlxvs.h`](include/mlxvs/mlxvs.h), taking `mx::array` and a trailing
|
|
137
|
+
`mx::StreamOrDevice s = {}` as MLX ops do. In CMake (MLX found through `CMAKE_PREFIX_PATH`, e.g. Homebrew's):
|
|
138
|
+
|
|
139
|
+
```cmake
|
|
140
|
+
add_subdirectory(mlxvs) # or FetchContent
|
|
141
|
+
target_link_libraries(app PRIVATE mlxvs)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## 3. Run the benchmarks
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
curl -O ftp://ftp.irisa.fr/local/texmex/corpus/sift.tar.gz && tar xzf sift.tar.gz
|
|
148
|
+
pip install faiss-cpu pynndescent # optional CPU baselines
|
|
149
|
+
python bench/bench_graph.py sift/sift_base.fvecs --k 32 --clusters 1,8 --baselines faiss,pynndescent
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
It builds the k-NN graph with each method and scores 2,000 random rows against the exact neighbors.
|
|
153
|
+
|
|
154
|
+
Search: build a graph, then recall@10 against queries per second over a sweep of `itopk_size`, optionally against
|
|
155
|
+
hnswlib on the CPU.
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
pip install hnswlib # optional CPU baseline
|
|
159
|
+
python bench/bench_search.py sift/sift_base.fvecs sift/sift_query.fvecs --degree 32 --intermediate 64 \
|
|
160
|
+
--baselines hnswlib
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Benchmarks
|
|
164
|
+
|
|
165
|
+
SIFT1M (985,462 x 128, L2, 10,000 queries in one batch, recall@10), MacBook Pro M3 Pro (18-core GPU, 12-core CPU),
|
|
166
|
+
on power. mlxVS 0.1.0; hnswlib 0.8 with `M=16`, `ef_construction=200` on all 12 cores.
|
|
167
|
+
|
|
168
|
+
| Index | Build | Search setting | Recall@10 | Queries/s |
|
|
169
|
+
|---|---:|---|---:|---:|
|
|
170
|
+
| mlxVS `cagra_build`, degree 32 (from 64) | 10.4 s | `itopk_size` 32 | 0.932 | 119,800 |
|
|
171
|
+
| | | `itopk_size` 64 | 0.976 | 69,500 |
|
|
172
|
+
| | | `itopk_size` 128 | 0.993 | 37,800 |
|
|
173
|
+
| | | `itopk_size` 256 | 0.998 | 27,000 |
|
|
174
|
+
| mlxVS `cagra_build`, degree 64 (from 128) | 19.6 s | `itopk_size` 32 | 0.977 | 79,500 |
|
|
175
|
+
| | | `itopk_size` 64 | 0.994 | 43,600 |
|
|
176
|
+
| mlxVS `nn_descent` k-NN graph, degree 32 (unpruned) | 8.8 s | `itopk_size` 128 | 0.939 | 42,400 |
|
|
177
|
+
| hnswlib (CPU) | 88.1 s | `ef` 32 | 0.904 | 53,300 |
|
|
178
|
+
| | | `ef` 64 | 0.964 | 32,200 |
|
|
179
|
+
| | | `ef` 128 | 0.990 | 17,600 |
|
|
180
|
+
| | | `ef` 256 | 0.998 | 10,200 |
|
|
181
|
+
|
|
182
|
+
## Coming from cuVS
|
|
183
|
+
|
|
184
|
+
| mlxvs | cuVS | Note |
|
|
185
|
+
|---|---|---|
|
|
186
|
+
| `nn_descent(rows, k, ...)` | `nn_descent::index_params::graph_degree` | cuVS default 64 |
|
|
187
|
+
| lists of `max(64, k)` entries | `intermediate_graph_degree` | cuVS default 128 |
|
|
188
|
+
| `max_iterations` = 24 | `max_iterations` = 20 | mlxvs samples 16 new neighbors per round and stops early when the lists settle |
|
|
189
|
+
| `termination_threshold` = 1e-4 | `termination_threshold` = 1e-3 | tuned for graph quality in JVector; pass 1e-3 for cuVS's behaviour |
|
|
190
|
+
| `all_neighbors(..., n_clusters, overlap_factor)` | `all_neighbors::index_params::{n_clusters, overlap_factor}` | overlap 2 as cuVS; `n_clusters` defaults to 0 (chosen from the GPU's memory), cuVS's to 1 |
|
|
191
|
+
| `algo="brute_force"` / `"nn_descent"` | `all_neighbors::index_params::graph_build_params` | cuVS also offers IVF-PQ |
|
|
192
|
+
| `"cosine"` returns the cosine similarity | `CosineExpanded` returns 1 - cosine | similarities are "larger is closer"; `"l2"` is squared, as `L2Expanded` |
|
|
193
|
+
| `cagra_build(rows, graph_degree=32, intermediate_graph_degree=64)` | `cagra::index_params::{graph_degree, intermediate_graph_degree}` | cuVS defaults 64 and 128; NN-Descent here caps the intermediate degree at 128 |
|
|
194
|
+
| `cagra_optimize(knn_graph, graph_degree)` | `cagra::optimize` | rank-based pruning and reverse edges, as cuVS; the reverse half fills from the lowest-rank edges |
|
|
195
|
+
| `graph_search(base, graph, queries, k, ...)` | `cagra::search` | takes the dataset and graph directly, no index object yet |
|
|
196
|
+
| `itopk_size` = 64, `search_width` = 1 | `search_params::{itopk_size, search_width}` | same defaults; `itopk_size` at most 256 |
|
|
197
|
+
| `max_iterations` = 0 (automatic) | `search_params::max_iterations` | stops earlier once every list entry is expanded |
|
|
198
|
+
| `num_random_samplings` = 1 | `search_params::num_random_samplings` | random start nodes; no seed nodes yet |
|
|
199
|
+
| `int32` ids | `int64_t` ids | n < 2^31 rows |
|
|
200
|
+
|
|
201
|
+
## Roadmap
|
|
202
|
+
|
|
203
|
+
1. Vamana alpha pruning; export to HNSW (hnswlib) and JVector layouts (`cagra::serialize_to_hnswlib`).
|
|
204
|
+
2. An index object holding the dataset and graph; float16 and int8 datasets for search.
|
|
205
|
+
3. A C API for other languages (Java via Panama, as cuVS-java does).
|
|
206
|
+
|
|
207
|
+
How the kernels work: [docs/DESIGN.md](docs/DESIGN.md). Building, testing and code style:
|
|
208
|
+
[CONTRIBUTING.md](CONTRIBUTING.md).
|
|
209
|
+
|
|
210
|
+
## License
|
|
211
|
+
|
|
212
|
+
Apache-2.0. The NN-Descent kernels are a port of JVector's `jvector-gpu` module (Apache-2.0, DataStax, Inc.); see
|
|
213
|
+
[NOTICE](NOTICE).
|