Crates.io | arroy |
lib.rs | arroy |
version | 0.5.0 |
source | src |
created_at | 2023-11-30 17:06:16.875613 |
updated_at | 2024-10-01 14:48:29.373932 |
description | Annoy-inspired Approximate Nearest Neighbors in Rust, based on LMDB and optimized for memory usage |
homepage | |
repository | https://github.com/meilisearch/arroy |
max_upload_size | |
id | 1054317 |
size | 1,878,931 |
Arroy (Approximate Rearest Reighbors Oh Yeah) is a Rust library with an interface close of the Annoy Python library to search for vectors in space that are near a targeted vector. It is based on LMDB, a memory-mapped key-value store, so many processes may share the same data and atomically modify the vectors.
There are some other libraries to do nearest neighbor search. However, most of them are memory-bound, and none use LMDB for their storage. Annoy considered using LMDB as a backend since 2015. We built Meilisearch on top of LMDB; therefore, it was an obvious choice. As Annoy highly inspires it, we benefit from the same low memory footprint.
Why is this useful? If you want to find the nearest neighbors and have many CPUs, you only need to build the index once. Any thread will be able to query the LMDB-based index and will be able to do lookups immediately, even while another index is modifying it.
We use it inside Meilisearch. This library helps our users search for similar documents. Our users have many millions of them in a high-dimensional space (i.e., 768 on average and 1536 for OpenAI), so memory usage is a prime concern.
Arroy was built by @Kerollmops and @irevoire with the help of @dureuill in a week by porting the original C++ source code of Annoy.
sqrt(2-2*cos(u, v))
u16
)log(n)
lookups and non-aligned vectors due to LMDBOnly two main parameters are needed to tune Arroy: the number of trees n_trees
and the number of nodes to inspect during searching search_k
.
n_trees
is provided during build time and affects the build time and the index size. A larger value will give more accurate results but larger indexes.search_k
is provided in runtime and affects the search performance. A larger value will give more accurate results but will take a longer time to return.If search_k
is not provided, it will default to n * n_trees
where n
is the number of approximate nearest neighbors. Otherwise, search_k
and n_trees
are roughly independent, i.e., the value of n_trees
will not affect search time if search_k
is held constant and vice versa. Basically, it's recommended to set n_trees
as large as possible given the amount of memory you can afford, and it's recommended to set search_k
as large as possible given the time constraints you have for the queries.
Using random projections and by building up a tree. At every intermediate node in the tree, a random hyperplane is chosen, which divides the space into two subspaces. This hyperplane is determined by sampling two points from the subset and taking the hyperplane equidistant from them.
We do this k times so that we get a forest of trees. k has to be tuned to your needs by looking at what tradeoff you have between precision and performance.
Dot Product distance (originally contributed by @psobot and @pkorobov) reduces the provided vectors from dot (or "inner-product") space to a more query-friendly cosine space using a method by Bachrach et al., at Microsoft Research, published in 2014.
The benchmarks are available in another repository. It shows the performances of arroy in terms of recall, disk size usage, search and indexing performances with different parameters compared to other competitors.
It's all written in Rust and based on LMDB without a handful of ugly optimizations for performance and memory usage. You have been warned :)
The code should support Windows, thanks to LMDB and the Rust programming language.