↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

Approximate nearest neighbours

Exact search compares the question's vector with every stored vector: 2,067 distances here, a billion in a large index. Approximate nearest-neighbour (ANN) search gives up a little recall to compare with far fewer. HNSW, a hierarchical navigable small-world graph, does it by linking every vector to some of its near neighbours and walking the links towards the question.

This tab builds faiss's HNSW index over the Embeddings tab's 2,067 bge-small paragraph vectors (384 numbers, rounded to float16) and searches it with the 300 questions. faiss measures squared Euclidean distance; for vectors of length 1, , so the nearest vectors are exactly those with the highest cosine, and the page shows cosines. The map behind every panel is a t-SNE layout of the vectors; it places similar paragraphs near each other but its distances are not the search's.

Layers

Every paragraph is a node on layer 0. A random draw gives each node a top layer , with

so with about 1 node in 16 also appears on layer 1, 1 in 256 on layer 2, and so on. Here the draw put 119 nodes on layer 1, 5 on layer 2 and 1 on layer 3, the entry point. Each layer links a node to at most near nodes of that layer, on layer 0. Upper layers are sparse, so their links are long; layer 0 is dense and its links are short. This is a skip list (Pugh) built from proximity graphs: the top layers cross the space in a few long hops, and the bottom one finishes the job locally. The 4 layers, one descent panel draws each layer with one question's path through it.

Searching

On the upper layers the search is greedy, as the Upper layers: greedy panel shows: from the current node, move to whichever of its links is closest to the question, and stop when none is closer; that node is where the next layer starts. On layer 0 it keeps a list of the best nodes found so far, the efSearch setting:

  • take the nearest node in not yet expanded;
  • if is full and that node is farther than the farthest node in , stop;
  • otherwise compute the distance to each of its links not seen before and put each into if it is closer than 's farthest, dropping the farthest when overflows.

The answer is the nearest nodes of . The Layer 0: a list of ef panel steps through this list, and the counter beside it adds up the distances computed. A larger explores more of the graph before stopping: more distances, fewer misses. A greedy walk can end in a local minimum, a node closer to the question than all its links but not the nearest overall; the list of candidates is what lets the search climb out.

Building the graph

Nodes are inserted one at a time. Each new node is searched for like a question, with a list of , on every layer up to its top one, and then linked to some of the nodes found. HNSW does not simply take the nearest : going through the candidates from nearest to farthest, it keeps a candidate only if is closer to the new node than to every link kept so far. A candidate hidden behind an already kept neighbour is skipped, so the links point in different directions and clusters stay connected to each other. Links are made in both directions; when a node's list overflows, it is pruned with the same rule.

Selecting a node in the A node's links panel shows how its insertion chose its links and which came back from later nodes. faiss built this graph single-threaded in 0.04 s; per paragraph it stores the 1,536-byte float32 vector plus 50.4 bytes of links. The page's own code rebuilds the graph link for link and its searches return faiss's results and distance counts for every question.

Recall against cost

Recall@10 against exact search is the share of the true 10 nearest vectors the index returns. The Recall@10 against exact search · all 300 questions chart plots it against distances computed per question, one line per . At , efSearch 10 finds 0.909 of the true neighbours with 151 distances on average, the default efSearch 16 finds 0.949 with 184, efSearch 64 finds 0.994 with 417, and efSearch 256 finds all of them with 1,083, against exact search's 2,067. A smaller is cheaper per step and less accurate: finds 0.822 at efSearch 16, finds 0.979.

What matters to a user is whether the question's own paragraph comes back. Exact search puts it in the top 10 for 94.7 % of questions, HNSW at and efSearch 10 for 91.0 %: most of the approximation's misses are neighbours the question did not need. On a corpus this small, exact search is cheap; the logarithmic growth of HNSW's search cost that its paper reports is what matters at millions of vectors.

Where it came from

Navigable small-world graphs (Malkov et al., 2014) were single-layer graphs in which links made early in the build act as long-range shortcuts. HNSW (Malkov and Yashunin, 2018) separated the links by length into layers and added the neighbour-selection heuristic, and became one of the strongest methods in the ANN-Benchmarks comparisons and the default index of many vector databases. faiss, the library used here, implements it beside the clustered and quantised indexes of the IVF-PQ tab.

Papers

  1. Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs Yu. A. Malkov, D. A. Yashunin · IEEE Transactions on Pattern Analysis and Machine Intelligence 2018 Introduces HNSW: random layers with exponentially decaying probability, greedy descent, best-first search with a list, and the neighbour-selection heuristic.
  2. Approximate nearest neighbor algorithm based on navigable small world graphs Yury Malkov, Alexander Ponomarenko, Andrey Logvinov, Vladimir Krylov · Information Systems 2014 The single-layer navigable small-world graph HNSW grew from.
  3. Skip lists: a probabilistic alternative to balanced trees William Pugh · Communications of the ACM 1990 Randomly promoted levels searched from the top down, the structure HNSW's layers follow.
  4. The Faiss library Matthijs Douze et al. · arXiv 2024 The library whose HNSW index this tab records and reproduces.
  5. ANN-Benchmarks: A Benchmarking Tool for Approximate Nearest Neighbor Algorithms Martin Aumüller, Erik Bernhardsson, Alexander Faithfull · Information Systems 2020 Recall against queries per second for many ANN methods, the trade-off the recall chart draws.
  6. C-Pack: Packed Resources For General Chinese Embeddings Shitao Xiao, Zheng Liu, Peitian Zhang, Niklas Muennighoff, Defu Lian, Jian-Yun Nie · SIGIR 2024 The BGE models; bge-small-en-v1.5 gives the vectors this index holds.

See also