Metadata-Version: 2.4
Name: siann
Version: 0.6.0
Requires-Dist: numpy>=2.5.5
Requires-Dist: mlx>=0.32.4 ; platform_machine == 'arm64' and sys_platform == 'darwin'
Requires-Dist: mlxvs>=0.16.1 ; platform_machine == 'arm64' and sys_platform == 'darwin' and extra == 'graph'
Requires-Dist: torch>=2.15.0 ; extra == 'torch'
Provides-Extra: graph
Provides-Extra: torch
License-File: LICENSE
Summary: Fast nearest-neighbor search on any CPU and on Apple and NVIDIA GPUs: exact, IVF and CAGRA graphs, few dependencies, and a Rust crate to serve the indexes
Author: Victor Glazer
License-Expression: MIT
Requires-Python: >=3.12
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Documentation, https://siann.readthedocs.io
Project-URL: Homepage, https://github.com/vglazer/siann
Project-URL: Repository, https://github.com/vglazer/siann

<!-- Generated from README.md by scripts/make_pypi_readme.py; edit README.md instead. -->
# siann

[![CI](https://github.com/vglazer/siann/actions/workflows/ci.yml/badge.svg)](https://github.com/vglazer/siann/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/siann)](https://pypi.org/project/siann/)
[![Documentation](https://img.shields.io/readthedocs/siann/latest)](https://siann.readthedocs.io)

**Fast nearest-neighbor search on any CPU and on Apple and NVIDIA GPUs, with few dependencies.** Do your
research on a Mac, build graphs on an NVIDIA GPU, and serve them from a Rust service on any CPU: the same index,
the same neighbors.

- **Fast.** Against FAISS on the same hardware: exact search on a Mac's GPU 2.7-6.0 times as fast for L2 and
  cosine, 18-25 times for L1; on an NVIDIA T4, at the same recall, siann's IVF 1.4-3.1 times FAISS's GPU IVF and
  its graph search 1.3-3.0 times; on the CPU, single queries through the graph 1.32 times FAISS's HNSW on SIFT.
  Where another library is faster, [How it compares](https://github.com/vglazer/siann#how-it-compares) says so.
- **Universal.** Exact search, an IVF and CAGRA graphs (the graph index of NVIDIA's cuVS), by L2, cosine, inner
  product, L1 and L-infinity, on any CPU (Linux, macOS and Windows; x86-64 and ARM), on Apple's GPU and on
  NVIDIA GPUs, with the same methods and the same neighbors up to near-ties: every path is siann's own code
  (Rust on the CPU, Metal on Apple's GPU, Triton on NVIDIA's). AMD GPUs should work in principle, through
  PyTorch's ROCm builds and Triton's AMD backend running the same kernels, but are untested. An index saved on
  one machine loads on any other, in Python or in Rust through the [siann crate](https://github.com/vglazer/siann/blob/main/crates/siann/README.md),
  which finds the same neighbors as Python's CPU search.
- **Convenient.** A Python API over NumPy arrays. Give a recall target and siann calibrates how deep to search,
  picks the method and, for each batch size, the device, from timings on your data and machine; `index.plan`
  says what it chose, and every choice can be overridden. The target holds on average for queries like the
  calibration sample: pass representative `calibration_queries=` and check recall where it matters.
- **Few dependencies.** NumPy, MLX and PyTorch, and nothing else: no FAISS, no CUDA toolkit or compiler, no BLAS
  library of its own. siann's GPU kernels are compiled as it runs, by MLX and PyTorch's MPS backend on Apple's
  GPU and by Triton (which comes with PyTorch's CUDA builds) on NVIDIA's; on the CPU it is Rust inside its
  wheels, with NumPy's BLAS for matrix multiplies. MLX installs with siann on Apple silicon; PyTorch is needed
  only for NVIDIA GPUs. cuVS and mlxvs are optional plug-ins, used where they are installed and measured
  faster. The Rust crate needs only rayon and serde_json.

Start with the [quickstart notebook](https://github.com/vglazer/siann/blob/main/examples/quickstart.ipynb).

![One query (an orange dot) among 600 points, and its 25 nearest by three metrics, found by siann: under L2 they fill a circle around the query, under L1 a diamond, under L-infinity a square](https://raw.githubusercontent.com/vglazer/siann/main/assets/metrics.png)

*One query's 25 nearest points by Euclidean (L2), Manhattan (L1) and Chebyshev (L-infinity) distance.*

## Install

```sh
uv add siann            # or: pip install siann
```

Python 3.12 or later. siann runs on the CPU on Linux, macOS and Windows, and on the GPU too on Macs with Apple
silicon, where it installs [MLX](https://github.com/ml-explore/mlx), Apple's array framework; with
[PyTorch](https://pytorch.org) installed, it also searches on an NVIDIA GPU (`gpu="torch"`: exact search, the IVF
and graphs). Wheels (built packages) cover Linux on x86-64 and ARM, macOS on Apple silicon and Windows on x86-64;
the x86-64 ones need a processor with AVX2 (Intel from 2013, AMD from 2015), and
`pip install --no-binary siann siann` builds from source elsewhere, with a [Rust](https://rustup.rs) toolchain.

**Import siann before NumPy.** On Linux and Windows, siann sets up NumPy's BLAS as it loads, which can make
some CPU searches twice as fast; it warns if NumPy came first ([TUNING](https://github.com/vglazer/siann/blob/main/TUNING.md#numpys-blas-on-linux-and-windows)).

## Python

```python
import siann                 # before NumPy: on Linux and Windows it sets NumPy's BLAS up first
import numpy as np

xb = np.random.default_rng(0).standard_normal((100_000, 128), dtype=np.float32)
xq = np.random.default_rng(1).standard_normal((1_000, 128), dtype=np.float32)

index = siann.Index(xb, metric="cosine")     # exact; or "l2", "l1", "linf", "ip"
res = index.search(xq, k=10)                 # res.indices, res.scores: (1000, 10) each, best first
print(index.plan)                            # what siann chose, and why

approx = siann.Index(xb, metric="cosine", recall=0.95, calibration_queries=xq)  # approximate
ids = approx.add(xq)                         # add vectors; searches return their ids
approx.remove(ids[:10])                      # remove them by id
approx.save("approx.siann")                  # siann.Index.load("approx.siann"), on any machine
```

Scores are each metric's natural value: the distance for L2 (not squared), L1 and L-infinity, the similarity
for cosine and inner product. Every choice can be overridden: `kind="exact"`, `"ivf"` or `"graph"`,
`device="cpu"` or `"gpu"`, `nprobe=` for one search, `gpu="torch"` for PyTorch instead of MLX (on Apple's GPU or an NVIDIA one), and a profile
from `siann.tune` ([TUNING](https://github.com/vglazer/siann/blob/main/TUNING.md)).

## What siann chooses for you

- **How hard to search.** A recall target is calibrated when the index is built: the fewest IVF lists to scan
  and the narrowest graph search that reach it on a sample of queries (`calibration_queries=`, or the index's
  own vectors). It holds on average for queries like the sample: on new queries recall lands within a few
  thousandths of the target either way (1,000 calibration queries carry about 0.006 of sampling noise), and
  calibrating on the index's own vectors falls 0.005-0.01 short when the queries differ from them.
  `search(..., nprobe=)` sets the IVF's depth for one search.
- **Which method, on which device, for each batch size.** Every path, exact search included, is timed at batch
  sizes from 1 to 4,096 when the index is built, and each search takes the fastest for its size: typically
  the graph on the CPU for single queries and small batches, the GPU for large ones.
- **Graphs on any machine, by any metric.** siann builds and searches its own graphs (CAGRA graphs, the kind
  NVIDIA's cuVS builds) on the CPU and the GPU, on Linux and Windows too, for L1 and L-infinity as well: by L1
  on SIFT1M, 6.2 times siann's IVF at recall 0.95. A graph built elsewhere (by cuVS, mlxvs or siann on a GPU)
  is searched at the width calibrated for it. A graph that cannot reach the target is declined.
- **As the index changes,** it merges added vectors, recalibrates, re-clusters, and switches methods once
  another is faster; it can start empty, keep your own ids, and be searched from several threads while it
  changes.

GPU searches stay within a memory budget, and an index too large for the GPU stays on the CPU
([IMPLEMENTATION](https://github.com/vglazer/siann/blob/main/IMPLEMENTATION.md) explains each choice).

## Backends, plug-ins and FAISS

siann's own code runs on every device through its backends ([BACKENDS](https://github.com/vglazer/siann/blob/main/BACKENDS.md)): Rust on the CPU, MLX or
PyTorch on Apple's GPU, PyTorch with Triton on NVIDIA GPUs, the same methods and the same results on each. Another
library's implementation is a plug-in: optional, used where it is installed and measured faster, or where chosen.

- **[FAISS](https://github.com/facebookresearch/faiss)** (Meta) is the standard for similarity search on the CPU,
  and the main baseline below. siann does not call it.
- **[cuVS](https://github.com/rapidsai/cuvs)** (NVIDIA), a plug-in, is GPU search on NVIDIA GPUs; CAGRA is its
  graph index, and siann's graphs are CAGRA graphs. siann does not need it: on NVIDIA GPUs it runs its own exact
  search, IVF and graph search, through PyTorch (`gpu="torch"`). With cuVS installed, cuVS builds the graphs and
  its search is calibrated and timed beside siann's, each batch size going to the faster; `graph_builder="cuvs"`
  or `graph_search="cuvs"` chooses it. siann reads and writes cuVS's CAGRA files.
- **[mlxvs](https://github.com/mikepapadim/mlxvs)**, a plug-in, brings cuVS's methods to the Mac, on MLX. siann
  does not need it: it builds and searches its graphs itself on every device. With mlxvs installed
  (`pip install siann[graph]`, Apple silicon), mlxvs builds the graphs, 2.4-3.3 times faster than siann's
  builder, and its GPU search is calibrated and timed beside siann's, each batch size going to the faster.
  `graph_builder=` and `graph_search=` (`"siann"` or `"mlxvs"`) choose either, even where it is slower.

## How it compares

Queries per second of siann's routed path divided by the other library's, above 1 where siann is faster, on
each platform. [BENCHMARKS](https://github.com/vglazer/siann/blob/main/BENCHMARKS.md) has the measurements and the scripts that reproduce them.

### On a Mac with Apple silicon

On an Apple M3 Max, exact search returning the same neighbors and approximate search compared at matched
recall (0.95 to 0.96). Against FAISS 1.16's faster path (CPU or Metal GPU) and mlxvs 0.16.1 on the GPU. FAISS's HNSW
and mlxvs's CAGRA are graph indexes. The datasets ([DATASETS](https://github.com/vglazer/siann/blob/main/DATASETS.md)): SIFT1M (image descriptors, L2),
GloVe-100 (word embeddings, cosine), Music-100 (inner product), DBpedia OpenAI embeddings (1,536 dimensions).

| Workload | Against FAISS | Against mlxvs |
| --- | --- | --- |
| Exact, L1 or L-infinity | L1 **18.5-25x**, L-infinity **47-49x** (its CPU; neither GPU library has these metrics) | |
| Exact, L2, cosine or inner product, batches of 100 | **2.7-2.9x** (its GPU) | **2.6-3.8x** in batches; 1.1-1.2x at 1,536 dimensions |
| Exact, batches of 10,000 | L2 and cosine **5.6-6.0x** (its GPU), inner product **3.5x** (its CPU) | |
| Approximate, single queries | SIFT **1.32x**, Music-100 1.05x, GloVe **1.5-3.1x** (HNSW, its fastest variant) | CAGRA on the GPU: SIFT and Music-100 **10-11x** |
| Approximate, batches of 100 | SIFT 0.98x, Music-100 0.93x, GloVe **1.2-1.8x** (HNSW) | CAGRA: SIFT 0.84x, Music-100 0.93x, GloVe level |
| Approximate, batches of 9,000-10,000 | SIFT **3.2x**, Music-100 **2.9x**, GloVe **5.0x** (HNSW) | CAGRA: SIFT 0.83x, Music-100 0.97x, GloVe level |
| Approximate, 1,536 dimensions (a million vectors) | single queries 0.36x; batches of 100 **3.9x**, of 9,000 **9.2x** (HNSW) | CAGRA: single queries level, batches 0.64-0.95x |

### On the CPU alone

Against FAISS on the CPU, the same neighbors for exact search, matched recall for approximate search:

| Workload | Against FAISS |
| --- | --- |
| Exact, L1 or L-infinity | L1 **3.8-4.3x**, L-infinity **7.3x** |
| Exact, L2, cosine or inner product | L2 and cosine **1.7-3.6x**, inner product 0.9-2.7x |
| Approximate on 4 Linux CPUs: single queries, batches of 10, of 100 | SIFT **1.16x**, 0.84x, 0.99x; Music-100 0.78x, 0.94x, **1.78x** (HNSW) |

### On an NVIDIA GPU

`gpu="torch"` on a Tesla T4: exact search on the GPU, against FAISS 1.14's GPU brute force, 200,000 vectors of
128 dimensions, the same neighbors.

| Metric | Batches of 100 | Batches of 1,000 |
| --- | --- | --- |
| L1, L-infinity | **1.12-1.14x** | **1.39-1.41x** |
| L2 | 0.64x | **1.05x** |
| Inner product, cosine | 0.73-0.75x | 0.88-0.92x |

Approximate search on the same T4, on the GPU, against FAISS 1.14's GPU IVF (`GpuIndexIVFFlat`) at the same
recall, SIFT's first 200,000 vectors ([The IVF on NVIDIA GPUs](https://github.com/vglazer/siann/blob/main/BENCHMARKS.md#the-ivf-on-nvidia-gpus), [Graphs on
NVIDIA GPUs](https://github.com/vglazer/siann/blob/main/BENCHMARKS.md#graphs-on-nvidia-gpus)):

| siann's index, recall target (reached) | Batches of 100 | Batches of 1,000 |
| --- | --- | --- |
| IVF, 0.95 (0.953) | **1.44x** | **2.79x** |
| IVF, 0.99 (0.990) | **2.16x** | **3.11x** |
| Graph, 0.95 (0.951) | **1.29x** | **1.89x** |
| Graph, 0.99 (0.990) | **1.94x** | **2.95x** |

## Documentation

| Document | What it covers |
| --- | --- |
| [Quickstart notebook](https://github.com/vglazer/siann/blob/main/examples/quickstart.ipynb) | Every feature, hands-on |
| [IMPLEMENTATION](https://github.com/vglazer/siann/blob/main/IMPLEMENTATION.md) | How siann chooses, the search methods, the kernels, where other libraries are faster |
| [TUNING](https://github.com/vglazer/siann/blob/main/TUNING.md) | What siann measures, on what, and how to measure it on your own queries, data and hardware |
| [BENCHMARKS](https://github.com/vglazer/siann/blob/main/BENCHMARKS.md) | Every measurement, with the scripts that reproduce them |
| [BACKENDS](https://github.com/vglazer/siann/blob/main/BACKENDS.md) | MLX, PyTorch (on Apple's GPU and on NVIDIA's) and siann's Rust code, and FAISS as the baseline: what each offers, and their pitfalls |
| [DATASETS](https://github.com/vglazer/siann/blob/main/DATASETS.md) | SIFT1M, GloVe-100, Music-100, DBpedia OpenAI and Gaussian vectors |
| [DEVELOPMENT](https://github.com/vglazer/siann/blob/main/DEVELOPMENT.md) | Building, testing, releasing, the rules for changing the code, and the ideas that do not pay |
| [API reference](https://siann.readthedocs.io/en/latest/API/) | Every public class, method and function |

These pages are also at [siann.readthedocs.io](https://siann.readthedocs.io).

## License

MIT ([LICENSE](https://github.com/vglazer/siann/blob/main/LICENSE))

