21 releases (10 breaking)

0.11.0 Jul 19, 2021
0.9.1 Jul 11, 2021
0.6.1 Apr 4, 2020
0.6.0 Mar 6, 2020
0.3.1 Sep 6, 2019

#53 in Algorithms

Download history 411/week @ 2021-04-08 277/week @ 2021-04-15 1131/week @ 2021-04-22 172/week @ 2021-04-29 88/week @ 2021-05-06 122/week @ 2021-05-13 22/week @ 2021-05-20 44/week @ 2021-05-27 27/week @ 2021-06-03 139/week @ 2021-06-10 242/week @ 2021-06-17 315/week @ 2021-06-24 102/week @ 2021-07-01 656/week @ 2021-07-08 332/week @ 2021-07-15 941/week @ 2021-07-22

1,101 downloads per month
Used in 8 crates (4 directly)

MIT license

35KB
606 lines

hnsw

Discord Crates.io MIT/Apache docs.rs LoC

Hierarchical Navigable Small World Graph for fast ANN search

Enable the serde feature to serialize and deserialize HNSW.

Tips

A good default for M and M0 parameters is 12 and 24 respectively. According to the paper, M0 should always be double M, but you can change both of them freely.

Example

To see how this might be used with hamming space, see tests/simple_discrete.rs. To see how this might be used with euclidean space, see tests/simple.rs.

Note that the euclidean implementation in the test may have some numerical errors and fail to implement the triangle inequality, especially on high dimensionality. Use a Kahan sum instead for proper usage. It also may not utilize SIMD, but using an array may help with that.

Please refer to the space documentation for the trait and types regarding distance. It also contains special Bits128 - Bits4096 tuple structs that wrap an array of bytes and enable SIMD capability. Benchmarks provided use these SIMD impls.

Benchmarks

Here is a recall graph that you can compare to its alternatives:

Recall Graph

For more benchmarks and how to benchmark, see benchmarks.md.

Implementation

This is based on the paper "Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs" by Yu. A. Malkov and D. A. Yashunin. This paper builds on the original paper for NSW. There are multiple papers written by the authors on NSW, which preceeded HNSW.

For more details about parameters and details of the implementation, see implementation.md.

Credit

This is in no way a direct copy or reimplementation of the original implementation. This was made purely based on the paper without reference to the original headers. The paper is very well written and easy to understand, with some minor exceptions. Thank you to the authors for your valuble contribution.

Questions? Contributions? Excited?

Please visit the Rust CV Discord.

Dependencies

~1–1.4MB
~28K SLoC