AI engineering paper · 2018
HNSW
How HNSW finds the closest vectors among millions by hopping through a stack of graphs, and what its three settings trade between speed, accuracy and memory.
You’ll learn to
- Explain how a search drops through a stack of graphs, from a few long links at the top to every vector at the bottom.
- Trace a search by hand, and say why keeping a list of several candidates escapes dead ends that a single-path search cannot.
- Say what the three main settings change (links per vector, effort when building, effort when searching) and how much memory the links take.
- Name what changed after 2018: deletes, filters, whether the layers are needed, and graphs that live on disk.
The idea in short
Search by meaning turns every item into a long list of numbers, called a vector, and looks for the stored vectors closest to the query’s. Checking all of them gets too slow once there are millions. HNSW is a way to find close ones while looking at only a small share of the data.
It links each vector to a few close neighbours, like a road map. On top of that map it keeps smaller maps, called layers, that hold only a random sample of the vectors and so only long-distance links, like a motorway layer. A search starts on the top layer, hops towards the query, drops a layer, and finishes with a careful local search on the full map. A few settings trade speed, accuracy and memory.
The problem
A search by meaning starts by turning every item, such as a paragraph, a product or an image, into a : a fixed-length list of numbers made by a model. Another word for it is an . Items with similar meaning get vectors that sit close together. To answer a query, you turn the query into a vector too, then look for the stored vectors closest to it.
That job is : given a query, find the K stored vectors with the smallest distance to it. For most of the paper’s data the distance is the ordinary straight-line distance, extended to vectors with many numbers.
The simple method compares the query with every stored vector. It is exact, and its cost grows in step with the data. The paper times it. Scanning 1 million image vectors of 128 numbers each took 94 ms for one query. Scanning 1.2 million word vectors of 100 numbers each took 95 ms. A service that answers 100 searches a second would spend 9.4 seconds of computing every second on the first set, so about ten processor cores would do nothing else (our arithmetic).
Exact shortcuts exist, such as trees that rule out whole regions, but the authors say they pay off only when vectors have few numbers. (The count of numbers in a vector is its number of dimensions.) With many dimensions there is little to rule out. So real systems use , which allows a few misses. Its quality is measured by : the share of the true nearest neighbours that the search returns. If the true 10 nearest are known and a search returns 9 of them, recall is 0.9.
The paper lists the main families of approximate methods: trees, hashing, (storing a short compressed code instead of each vector), and . A proximity graph treats each stored vector as a node and links it to some nearby vectors. A search is : start at some node, measure the distance from the query to each of its neighbours, move to the closest, and repeat. Graphs gave the best results on vectors with many numbers. They had two flaws, though:
- The number of steps grew as a power of the data size. Double the data and the walk gets longer by a fixed factor, not by a fixed amount.
- Clustered data broke them. When vectors form separate groups, the links can stop connecting the groups, and a search that starts in the wrong group never leaves.
The authors had already built one fix, called (Navigable Small World). Vectors are added in random order, and each is linked to its M closest vectors already added, where M is a number you pick, such as 16. The first vectors added get long links, because the graph was sparse when they arrived, and those links let a greedy walk cross the graph in few hops. The cost of a search grew like the logarithm of the data size, multiplied by itself a few times (the paper says a power of the logarithm). A logarithm here means that each doubling of the data adds a fixed number of steps: a million vectors to a billion is a thousand times more data, but only about 10 more doublings (our arithmetic).
NSW was still not good enough. On low-dimensional data the paper says it could lose to tree-based methods by factors of 100 or more. So the question this paper answers is: can a graph index be built so that a search costs only a logarithm of the data size, on any kind of data, clustered or not?