Skip to content

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

ExhaustiveMlxIndex(
    n_neighbors: int = 15,
    metric: str = "euclidean",
    verbose: int = 0,
)

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 k for kneighbors.

15
metric str

"euclidean"/"l2", "sqeuclidean" or "cosine".

'euclidean'
verbose int

0 quiet, 1 normal, 2 detailed. Prints to the process stdout, not sys.stdout.

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 k for kneighbors.

15
metric str

"euclidean"/"l2", "sqeuclidean" or "cosine".

'euclidean'
nlist int | None

Number of Voronoi cells. None defaults to sqrt(n).

None
nprobe int | None

Cells visited per query, the recall knob. None defaults to sqrt(nlist). Search-time: override it per call as index.kneighbors(nprobe=32).

None
nquery int | None

Queries per device tile. None lets the library size it. Search-time.

None
kmeans_algorithm str

"emeans" (default) or "lloyd". e-means is mini-batch k-means with an EMA update and fitted batch and step laws (Bessa et al., 2026).

'emeans'
kmeans_epochs int | float | None

Passes over the training sample under e-means, may be fractional. None defaults to 5. e-means only.

None
kmeans_iters int | None

Lloyd's only. Iterations. None defaults to 30. Every one runs; MLX k-means has no early stop.

None
kmeans_balanced bool

Lloyd's only. Reseed starved centroids each iteration.

False
seed int

Fixes k-means initialisation.

42
verbose int

0 quiet, 1 normal, 2 detailed. Prints to the process stdout, not sys.stdout.

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 k for kneighbors. Not the graph degree, which is graph_degree.

15
metric str

"euclidean"/"l2", "sqeuclidean" or "cosine".

'euclidean'
graph_degree int | None

Neighbours stored per node. None defaults to 30.

None
build_k int | None

Working degree before pruning. None defaults to 1.5 * graph_degree.

None
max_iters int | None

Descent iteration cap. None defaults to 15.

None
n_trees int | None

Random projection trees for the initial graph. None defaults to min(5 + round(n ** 0.25), 20).

None
delta float | None

Convergence threshold. None defaults to 0.001.

None
rho float | None

Local-join sampling rate. None defaults to 1.0.

None
refine_knn int | None

Two-hop refinement sweeps. None defaults to 0.

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. True or False forces it; None makes the copy on the first query only if it keeps the device under 80% of its memory budget. The copy costs half the vector bytes again on the device. Search-time.

None
seed int

Fixes the seed graph and the sampling.

42
verbose int

0 quiet, 1 normal, 2 detailed. Prints to the process stdout, not sys.stdout.

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 k for kneighbors.

15
metric str

"euclidean"/"l2", "sqeuclidean" or "cosine".

'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. True or False forces it; None makes the copy on the first query only if it keeps the device under 80% of its memory budget. Search-time.

None
seed int

Fixes the partition and so the graph.

42
verbose int

0 quiet, 1 normal, 2 detailed. Prints to the process stdout, not sys.stdout.

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 include_self is set, matching kneighbors.

15
metric str

"euclidean"/"l2", "sqeuclidean" or "cosine".

'euclidean'
include_self bool

Whether row i starts with i at distance zero.

True
return_distance bool

Whether to return distances alongside indices.

True
build_k int | None

Working degree the descent keeps. None defaults to 1.5 * k.

None
max_iters int | None

Descent iteration cap. None defaults to 15.

None
n_trees int | None

Random projection trees seeding the graph. None sizes the forest from n.

None
delta float | None

Convergence threshold. None defaults to 0.001.

None
rho float | None

Local-join sampling rate. None defaults to 1.0, no sampling.

None
refine_knn int | None

Two-hop refinement sweeps. None defaults to 0.

None
seed int

Fixes the seed graph and the sampling.

42
verbose int

0 quiet, 1 normal, 2 detailed. Prints to the process stdout, not sys.stdout.

0

Returns:

Type Description
tuple[ndarray, ndarray] | ndarray

(distances, indices) of shape (n, n_neighbors), or just

tuple[ndarray, ndarray] | ndarray

indices when return_distance is False.