MLX indices¶
Present only in a source build with the mlx feature. See
GPU for how to build it and what
differs.
mlx
¶
MLX estimators, present only in a source build with the mlx feature.
Experimental and Apple Silicon only. MLX drives Metal directly through mlx-c
rather than going through wgpu, and comes out faster than the ann_search.gpu
counterparts at the same recall; the GPU page of the docs has the numbers. The
published wheel does not carry them, since it would have to bundle mlx-c.
Build from source instead::
brew install mlx-c
maturin develop --release --features mlx
ann_search.mlx_available() says whether the build picked it up.
Same constraints as the GPU estimators: float32 only (fit casts), no
persistence, no Manhattan. One more on top: MLX streams are tied to the thread
that created them, so a fitted index raises if used from any other Python
thread. Host-side work inside the MLX paths uses rayon's global pool and does
not follow ann_search.set_num_threads.
ExhaustiveMlxIndex
¶
Bases: _BaseMlxIndex
Brute-force exact search on MLX: its GEMM plus a Metal top-k kernel.
Recall is 1 by construction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int
|
Neighbours per query, and the default |
15
|
metric
|
str
|
|
'euclidean'
|
verbose
|
int
|
|
0
|
IvfMlxIndex
¶
IvfMlxIndex(
n_neighbors: int = 15,
metric: str = "euclidean",
nlist: int | None = None,
nprobe: int | None = None,
nquery: int | None = None,
kmeans_algorithm: str = "emeans",
kmeans_epochs: int | float | None = None,
kmeans_iters: int | None = None,
kmeans_balanced: bool = False,
seed: int = 42,
verbose: int = 0,
)
Bases: _BaseMlxIndex
Inverted file on MLX: k-means, the vectors and the cell scan on device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int
|
Neighbours per query, and the default |
15
|
metric
|
str
|
|
'euclidean'
|
nlist
|
int | None
|
Number of Voronoi cells. |
None
|
nprobe
|
int | None
|
Cells visited per query, the recall knob. |
None
|
nquery
|
int | None
|
Queries per device tile. |
None
|
kmeans_algorithm
|
str
|
|
'emeans'
|
kmeans_epochs
|
int | float | None
|
Passes over the training sample under e-means, may be
fractional. |
None
|
kmeans_iters
|
int | None
|
Lloyd's only. Iterations. |
None
|
kmeans_balanced
|
bool
|
Lloyd's only. Reseed starved centroids each iteration. |
False
|
seed
|
int
|
Fixes k-means initialisation. |
42
|
verbose
|
int
|
|
0
|
CagraMlxIndex
¶
CagraMlxIndex(
n_neighbors: int = 15,
metric: str = "euclidean",
graph_degree: int | None = None,
build_k: int | None = None,
max_iters: int | None = None,
n_trees: int | None = None,
delta: float | None = None,
rho: float | None = None,
refine_knn: int | None = None,
beam_width: int | None = None,
max_beam_iters: int | None = None,
n_entry_points: int | None = None,
expand_per_iter: int | None = None,
half_vectors: bool | None = None,
seed: int = 42,
verbose: int = 0,
)
Bases: ExtractKnnMixin, _BaseMlxIndex
CAGRA graph on MLX: NN-Descent, the prune and the beam search on device.
Same parameters and beam semantics as CagraGpuIndex, minus
retain_gpu: the MLX index is resident from the build on. External
queries pick their entry points through a random projection forest on the
device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int
|
Neighbours per query, and the default |
15
|
metric
|
str
|
|
'euclidean'
|
graph_degree
|
int | None
|
Neighbours stored per node. |
None
|
build_k
|
int | None
|
Working degree before pruning. |
None
|
max_iters
|
int | None
|
Descent iteration cap. |
None
|
n_trees
|
int | None
|
Random projection trees for the initial graph. |
None
|
delta
|
float | None
|
Convergence threshold. |
None
|
rho
|
float | None
|
Local-join sampling rate. |
None
|
refine_knn
|
int | None
|
Two-hop refinement sweeps. |
None
|
beam_width
|
int | None
|
Beam width at query time, the recall knob. Search-time. |
None
|
max_beam_iters
|
int | None
|
Iteration cap on the beam search. Search-time. |
None
|
n_entry_points
|
int | None
|
Entry points per query. Search-time. |
None
|
expand_per_iter
|
int | None
|
Beam entries expanded per iteration. Search-time. |
None
|
half_vectors
|
bool | None
|
Walk the beam on an fp16 copy of the vectors and rescore
its head in float32, so returned distances stay exact. |
None
|
seed
|
int
|
Fixes the seed graph and the sampling. |
42
|
verbose
|
int
|
|
0
|
Note
The four beam parameters are all-or-nothing, exactly as on
CagraGpuIndex. Leave them all None and the beam is sized from
k (beam_width = 2 * max(k, 16), max_beam_iters =
3 * beam_width). Set any one and the rest fall back to flat
constants: 16, 48, 8 entry points and 3 expansions. If you touch one,
set beam_width too.
half_vectors sits outside that rule: set alone, it keeps the
k-sized beam.
PipnnMlxIndex
¶
PipnnMlxIndex(
n_neighbors: int = 15,
metric: str = "euclidean",
r: int = 32,
alpha: float = 1.2,
leaf_k: int = 3,
c_max: int = 512,
c_min: int = 128,
l_max: int = 64,
fanout: Sequence[int] = (8, 2),
replicas: int = 1,
random_links: int = 4,
beam_width: int | None = None,
max_beam_iters: int | None = None,
n_entry_points: int | None = None,
expand_per_iter: int | None = None,
half_vectors: bool | None = None,
seed: int = 42,
verbose: int = 0,
)
Bases: _BaseMlxIndex
PiPNN graph built on the CPU, beam-searched on MLX.
The build is the CPU PipnnIndex build; the vectors and graph then go to
the device and stay there. Thread affine like every MLX index: build and
query on the same thread.
The graph and its build parameters are those of PipnnIndex; the search
is the CAGRA beam, with the same four all-or-nothing knobs and
half_vectors. No extract_knn: PiPNN keeps no kNN graph, only the
pruned navigational one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int
|
Neighbours per query, and the default |
15
|
metric
|
str
|
|
'euclidean'
|
r
|
int
|
Maximum out-degree. The memory knob. |
32
|
alpha
|
float
|
RobustPrune slack on squared distances. |
1.2
|
leaf_k
|
int
|
Nearest in-leaf neighbours each point sends, both ways. The main build-quality knob. |
3
|
c_max
|
int
|
Largest leaf. |
512
|
c_min
|
int
|
Partition children smaller than this are merged. |
128
|
l_max
|
int
|
Candidate reservoir slots per node, at most 255. |
64
|
fanout
|
Sequence[int]
|
Leaders each point joins at each recursion depth. |
(8, 2)
|
replicas
|
int
|
Independent partitions streamed into the same reservoirs. |
1
|
random_links
|
int
|
Random candidates added to each node's final prune. |
4
|
beam_width
|
int | None
|
Beam width at query time, the recall knob. Search-time. |
None
|
max_beam_iters
|
int | None
|
Iteration cap on the beam search. Search-time. |
None
|
n_entry_points
|
int | None
|
Entry points per query. Search-time. |
None
|
expand_per_iter
|
int | None
|
Beam entries expanded per iteration. Search-time. |
None
|
half_vectors
|
bool | None
|
Walk the beam on an fp16 copy of the vectors and rescore
its head in float32, so returned distances stay exact. |
None
|
seed
|
int
|
Fixes the partition and so the graph. |
42
|
verbose
|
int
|
|
0
|
Note
The beam knobs follow the CagraGpuIndex rule: leave all four
None and the beam is sized from k; set any one and the rest
fall back to flat constants. If you touch one, set beam_width too.
knn_graph_nndescent_mlx
¶
knn_graph_nndescent_mlx(
X: Any,
n_neighbors: int = 15,
*,
metric: str = "euclidean",
include_self: bool = True,
return_distance: bool = True,
build_k: int | None = None,
max_iters: int | None = None,
n_trees: int | None = None,
delta: float | None = None,
rho: float | None = None,
refine_knn: int | None = None,
seed: int = 42,
verbose: int = 0,
) -> tuple[ndarray, ndarray] | ndarray
Self-kNN graph by NN-Descent on MLX, without building an index.
The graph-only counterpart of CagraMlxIndex: no CAGRA prune, and
nothing is kept on the device, so unlike the estimators there is no
thread to stay on afterwards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Any
|
Samples by features. Cast to float32. |
required |
n_neighbors
|
int
|
Row length, the self-edge included when |
15
|
metric
|
str
|
|
'euclidean'
|
include_self
|
bool
|
Whether row |
True
|
return_distance
|
bool
|
Whether to return distances alongside indices. |
True
|
build_k
|
int | None
|
Working degree the descent keeps. |
None
|
max_iters
|
int | None
|
Descent iteration cap. |
None
|
n_trees
|
int | None
|
Random projection trees seeding the graph. |
None
|
delta
|
float | None
|
Convergence threshold. |
None
|
rho
|
float | None
|
Local-join sampling rate. |
None
|
refine_knn
|
int | None
|
Two-hop refinement sweeps. |
None
|
seed
|
int
|
Fixes the seed graph and the sampling. |
42
|
verbose
|
int
|
|
0
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray] | ndarray
|
|
tuple[ndarray, ndarray] | ndarray
|
|