Building a $k$-nearest neighbor ($k$-NN) graph—where every point in a dataset is connected to its $k$ closest peers—is a foundational task in machine learning, used for applications ranging from manifold learning to recommendation engines. However, as datasets scale into the billions, the computational cost of constructing these graphs becomes astronomical.
In the paper "Large-Scale Approximate $k$-NN Graph Construction on GPU," researchers present GNND, a redesigned version of the classic NN-Descent algorithm optimized specifically for the massive parallelism of modern GPUs. By overhauling memory access patterns and introducing novel insertion strategies, the authors achieve speeds 100–250× faster than single-threaded CPU versions, enabling billion-scale graph construction on a single consumer GPU.
The Challenge: Adapting NN-Descent for GPUs
The gold standard for approximate $k$-NN graph construction on CPUs is NN-Descent. It relies on the "neighbor-of-a-neighbor" heuristic: your neighbor’s neighbors are likely your neighbors too. While effective, NN-Descent is difficult to port to GPUs because it requires frequent, random memory accesses and atomic updates to shared lists. These operations cause "warp divergence" and severe memory bottlenecks on GPU architectures.
As shown in the architecture diagram above, modern GPUs rely on a complex hierarchy of memory (Registers, L1/Shared Memory, L2 Cache, and DRAM). Efficient GPU algorithms must maximize high-bandwidth memory usage while minimizing slow global memory writes. Existing GPU solutions often fail by being either brute-force or relying on quantization, which sacrifices accuracy.
Technical Innovations in GNND
The authors introduce GNND (GPU NN-Descent) and GGM (GPU-based Graph Merge) to address these hardware constraints through three key innovations:
1. Tiled Distance Calculation
Instead of calculating distances individually, GNND treats cross-matching as a matrix multiplication-like problem. By using "tiled" memory access, threads in a warp load contiguous data into shared memory, maximizing bandwidth.
2. Selective Update and Multiple Spinlocks
In the original algorithm, every discovered neighbor updates the graph. GNND is more selective, only choosing the best candidates to reduce slow global memory writes. To handle parallel updates, GNND divides each $k$-NN list into segments protected by individual spinlocks. This allows multiple threads to insert neighbors into the same list simultaneously without significant contention.
3. Divide-and-Conquer (GGM)
To handle datasets exceeding GPU memory, the GGM algorithm splits data into shards, builds sub-graphs, and merges them pair-wise. This approach is significantly more efficient than previous methods because it treats merging as a refinement of existing work rather than a fresh search.
Performance and Results
The researchers evaluated GNND across eight benchmarks, ranging from million-scale (SIFT1M) to billion-scale (SIFT1B) datasets. Key findings include:
- Speed: On SIFT1M, GNND achieves 0.99 recall in under 4 seconds—240× faster than single-threaded NN-Descent and 5× faster than FAISS-BF.
- Efficiency: GNND consistently outperformed previous state-of-the-art GPU methods like GGNN by 2.5–5×.
- Scalability: GNND successfully constructed a $k$-NN graph for 1 billion vectors (SIFT1B) in approximately 77 hours on a single NVIDIA RTX 3090, a task that previously required massive CPU clusters.
Conclusion
GNND demonstrates that by shifting focus from minimizing raw computation to organizing memory access patterns, the "neighbor-of-a-neighbor" heuristic can be effectively scaled on GPUs. While the current implementation is tailored to NVIDIA's post-Volta architecture, it sets a new benchmark for high-accuracy, large-scale graph construction on commodity hardware.
Summary updated