Can HNSW run efficiently on GPUs?

Created: Updated: 4 min read

HNSW does not run especially well on GPUs in its ordinary form, because the irregular, unpredictable memory access pattern that graph traversal fundamentally requires clashes with the kind of dense, uniform, predictable computation GPUs are actually built to accelerate — which doesn’t mean GPUs are useless for this problem, but it does mean the benefit shows up in different places than a straightforward port of the CPU algorithm would suggest.

Why doesn’t graph traversal map naturally onto GPU hardware?

A GPU achieves its enormous throughput by running the same instruction across thousands of data elements simultaneously, which works best when every one of those parallel computations follows an identical, predictable path — exactly the situation in dense matrix multiplication, where every output element is computed the same way regardless of its position. Graph traversal is the opposite kind of workload: which node gets visited next depends entirely on the data itself, specifically on which neighbor turns out to be closest to the query, so different parallel search threads exploring different parts of a graph end up following completely different, unpredictable paths through memory rather than one shared, uniform pattern. This mismatch between the algorithm’s inherently data-dependent branching and the hardware’s preference for uniform, predictable execution is the core reason graph-based search doesn’t translate as directly to GPUs as dense numerical workloads do.

What’s warp divergence, and why does HNSW’s search pattern trigger it?

GPUs execute threads in small groups that are expected to follow the same instruction path in lockstep; when threads within one of these groups need to take different branches — some finding a closer neighbor and continuing, others finding none and stopping, for instance — the hardware has to execute both branches separately for the whole group, effectively serializing what was supposed to be parallel work. This is called warp divergence, and HNSW’s search is a natural source of it: every query in a batch might be at a different point in its traversal, examining a different number of neighbors, or terminating after a different number of hops, none of which lines up neatly across queries the way a fixed-size matrix operation would. The random, scattered memory access pattern of following graph edges compounds this further, since GPUs also reward accessing contiguous blocks of memory together and penalize the kind of scattered, pointer-chasing access that visiting arbitrary graph neighbors requires.

Which part of HNSW actually benefits most from GPU acceleration?

Rather than running graph search itself on a GPU, the more common and more effective pattern uses GPU parallelism specifically for constructing a graph quickly, then transfers that finished graph into an ordinary CPU-side representation for serving actual queries — separate GPU-native graph designs, built from scratch around parallel hardware rather than adapted from a CPU-oriented algorithm, exist specifically to make this construction phase fast, even though the resulting graph is then generally searched on a CPU. Batch parallelism offers a second, more modest opportunity even without any specialized graph design: when many independent queries are available to process together, distributing different queries across different parallel execution units on a GPU can still extract meaningful throughput, since different queries genuinely are independent of each other even if a single query’s own internal traversal remains stubbornly sequential and hard to parallelize.

When is brute-force GPU search simply the better choice?

A GPU’s raw arithmetic throughput is enormous, and for a dataset small enough, or a workload tolerant enough of the cost, running an exact brute-force comparison against every vector using that raw throughput can be simpler to implement and reason about than dealing with a graph-based index at all — no graph to build, no parameters to tune, and none of the irregular access patterns described above, since brute-force distance computation is exactly the kind of dense, uniform arithmetic GPUs excel at. This becomes a less attractive option as the dataset grows large enough that even GPU-accelerated brute force can’t keep pace with a graph-based index’s dramatically smaller number of comparisons per query, at which point the trade-offs discussed throughout this page become relevant again.

GPUs are one way to push past ordinary hardware limits, and the next page looks at another: purpose-built hardware designed around HNSW’s specific traversal and priority-queue operations rather than adapted from a general-purpose processor. For a closer look at how a GPU-built graph actually gets handed off to CPU-side serving in practice, this site’s earlier coverage of composing HNSW with other index types works through that same workflow from the systems side.