TL;DR
Finding the closest vectors to a query, out of a billion stored vectors, is too slow to do by brute force on one processor. This paper makes it fast on GPUs in two ways: a new method for picking the k smallest values from a huge array, which keeps its working set in the chip's fastest memory and reads the data only once, and a search layout built on product quantization that compresses each vector to a few bytes. The authors report a k-NN graph over 1 billion vectors built in under 12 hours on four GPUs, and a search 8.5 times faster than the previous best GPU method at billion scale.
Builds on: K-Nearest Neighbors, Product Quantization, Inverted Index, GPU Architecture · Leads to: Approximate Nearest Neighbor Search
Why it mattered
Embedding models turn images, videos, and text into lists of numbers, and "find the most similar item" becomes "find the nearest vector". Systems that retrieve passages for language models, match product photos, or cluster huge image collections all run this search at scale. The paper's library, FAISS, is open-source, and the authors released their approach so others could compare against it.
The paper changed what was practical in two ways. First, it showed that a GPU can build a k-NN graph of a 95-million-image collection (the YFCC100M dataset) in 35 minutes, at an accuracy above 0.8. Second, it made billion-vector indexes fit on a handful of GPUs with a usable speed-accuracy trade-off, where earlier GPU work covered only small or exhaustive search. For the neighbouring ideas, read Approximate Nearest Neighbor Search for the wider family of methods and Vector Quantization for the compression idea underneath.
The problem
A typical embedding has 50 to 1000 dimensions. The database is large enough that it stops fitting in memory in its raw form. Each query must be compared with every vector, and the work grows with the database size times the dimension. For the SIFT1M benchmark (one million 128-dimensional vectors, 10,000 queries) the full distance computation is about 1.28 trillion floating-point operations, which a GPU does in under a second. The harder part is what comes after the distances: picking the k smallest of them for every query.
Three obstacles appear when the job moves to a GPU.
- Selection is the bottleneck. Standard k-selection methods make several passes over the data in slow global memory. Heaps, the usual choice on a CPU for small k, expose little parallelism because each insert changes the tree in a data-dependent way. GPU heaps and priority queues added warp divergence, extra kernel launches, and synchronisation.
- Compressed search needs a GPU layout. Product quantization with an inverted file (IVFADC) is the standard way to shrink billion-vector indexes. Its list scans and table lookups have to be arranged for the GPU's memory system, which the paper's Section 5 does.
- Prior GPU work did not reach billion scale. Most GPU similarity-search code used binary codes, small datasets, or exhaustive search. The one GPU method that handled billion-vector quantized search was the state of the art the authors had to beat.
GPUs have very high arithmetic and memory throughput, but they penalise irregular control flow. The standard selection algorithms assume the opposite, so the fix had to be a selection structure that a GPU can run without branching.
How it works
The search runs in five stages. Each query is compared against a set of compressed vectors grouped into lists, and only the closest few lists are scanned.
flowchart LR A[Batch of queries] --> B[Coarse quantizer picks tau nearest lists] B --> C[Scan those inverted lists of PQ codes] C --> D[Sum one table lookup per code byte] D --> E[WarpSelect keeps the k smallest] E --> F[k nearest vector IDs]
Here is one tiny instance, with a toy codebook of two entries per sub-quantizer instead of the paper's 256, so every number can be checked by hand. The query is a four-dimensional vector that has already had its coarse centroid subtracted. The stored vectors are coded in two bytes each.
- Split the query residual into two halves. The query residual is . The first half is and the second is . Check: the halves together have four numbers.
- Build a lookup table for each half. The first sub-quantizer has codewords and . The squared distance from to each is and , so the table is . The second sub-quantizer has codewords and . The squared distances from are and , so . Check: each table has one entry per codeword.
- Read a stored code as two indices. Suppose the list holds three codes, , and . The code means codeword 0 of the first sub-quantizer and codeword 1 of the second. No vector is decoded.
- Add two table lookups. For the approximate squared distance is . Check: the code has two bytes, so there are exactly two lookups.
- Repeat for the other codes. For the sum is . For it is . Check: three codes, three sums, and the work per code is still two lookups.
- Keep the smallest. With , the selection keeps the code with distance , which is the nearest neighbour under the approximation. In the paper, the same selection runs in registers across all 32 lanes of a warp, in one pass over the data.
The point of the walkthrough is step 4. Each stored vector costs two table reads and two additions, and the expensive multiplications were done once per query in the tables. The paper's graph experiments on YFCC100M use 16, 32 or 64 bytes per vector, so each vector costs that many lookups instead of a full sum over all dimensions.
The math
The symbols used below: is a query vector in , is the -th database vector, is the number of database vectors in a list, is the number of queries in a batch, and is the dot product.
Step 1 in symbols: distances as a matrix product
The exact search computes all squared distances between a batch of queries and the database. It uses the identity behind Euclidean Distance:
The first two terms are precomputed once per vector. The third term for a whole batch is the matrix product , so the expensive part is a Matrix Multiplication that GPUs run very efficiently. The authors call it through cuBLAS, the GPU matrix library. The term is the same for every candidate of one query, so it can be dropped before selection without changing the ranking.
Step 2 in symbols: coarse lists
IVFADC splits the database into inverted lists using a first quantizer , trained with K-Means Clustering; the paper uses coarse centroids on YFCC100M and on Deep1B. Each vector is stored with its residual after , and the search only looks inside the lists whose centroids are closest to the query:
where is the set of the nearest coarse centroids. The number is the multi-probe parameter: larger means more accuracy and more scanning.
Step 3 in symbols: product quantization and table lookups
The fine quantizer is a product quantizer. A vector is cut into sub-vectors, and each sub-vector is replaced by the index of its nearest codeword out of 256, so the code is bytes and reproduction values exist. Once and the coarse centroid are known, the squared distance to any stored code is a sum of table entries:
where and are the -th sub-vectors of the query and stored residuals. Each table has 256 entries, one for each codeword of sub-quantizer .
The cost comparison is what makes the method fast. Computing distances directly takes multiply-adds. With tables it takes multiply-adds once, then lookup-adds. For and this is multiply-adds per vector against lookup-adds per vector, about sixteen times less work per stored vector. For one query against a list of vectors, that is multiply-adds against lookup-adds, plus one table build of multiply-adds. Counted directly from the formulas, these totals are an illustration; the paper does not report them.
Step 4 in symbols: the cost of WarpSelect
WarpSelect processes the candidate values in groups of 32, one per lane. Write for the length of each lane's thread queue. Three kinds of work happen, with costs (compare 32 values with the queue heads), (insert into a thread queue), and (sort and merge the queues). They happen , and times on random data. The total is . A larger makes the merges rarer but each insertion more expensive, so the paper picks by experiment: for , for , for , and for .
Step 5 in symbols: the memory bound
A memory-bound kernel cannot run faster than its input can be read. If the input is bytes and the device delivers bandwidth bytes per second, the time is at least . The authors use this roofline bound to judge how close their selection comes to the limit. For a single pass over global memory, that limit is the reference point for "percent of peak".
Results
The numbers below are the paper's own results. Times are on NVIDIA Titan X GPUs unless stated.
| Experiment | This paper | Comparison | Source of comparison |
|---|---|---|---|
| k-selection, , | 1.62 times faster than fgknn | fgknn merge queue | Section 6.1 |
| k-selection, , | 2.01 times faster than fgknn | fgknn merge queue | Section 6.1 |
| Peak fraction of WarpSelect | 55 percent at ; 16 percent at | Memory-bandwidth limit | Section 6.1 |
| Exact search, SIFT1M | 85 percent of peak | cuBLAS GEMM and selection | Section 6.3 |
| k-means, MNIST8m, 256 centroids | 140 s on 1 GPU; 84 s on 4 GPUs | BIDMach, 320 s | Section 6.2 |
| k-means, MNIST8m, 4096 centroids | 316 s on 1 GPU; 100 s on 4 GPUs | BIDMach, 735 s | Section 6.2 |
| SIFT1M, same time budget, 0.02 ms per query | R@1 = 0.80, R@100 = 0.95 | R@1 = 0.51, R@100 = 0.86 | Section 6.4 |
| SIFT1B, one GPU, 8 bytes per vector | R@10 = 0.376 in 17.7 µs per query | R@10 = 0.35 in 150 µs | Section 6.4 |
| Deep1B, 4 GPUs | R@1 = 0.4517 in 0.0133 ms per vector | CPU paper: R@1 = 0.45 in 20 ms, one thread | Section 6.4 |
| k-NN graph, 95 million images | Accuracy above 0.8 in 35 minutes | none | Section 6.5 |
What they mean. The selection gains matter most at large and large arrays, and the memory-bound peak tells you how much headroom is left. The billion-scale search results show that a quantized index with careful GPU layout beats the previous GPU method on accuracy and speed at a similar memory budget. The k-NN graph results show that a graph over 95 million or 1 billion vectors, which earlier GPU work had not reached, can be built in hours on a few GPUs.
When to use this design. The design suits large databases of dense vectors of moderate dimension, where k is small and the index must fit in GPU memory. For exact search over a small collection, a plain GPU search is simpler and exact. The authors' own results show the tradeoffs: WarpSelect drops to 16 percent of peak at , and compression trades accuracy for memory, with and setting the balance between recall and speed.
Limits. The 8.5 times figure compares against one GPU baseline on SIFT1B, and it is a per-query time at a specified accuracy, not a whole-system benchmark. The Deep1B comparison with a CPU uses different hardware, and the paper says so. The k-NN graph comparison with NN-Descent is across different machines and dataset sizes. Multi-GPU scaling is near linear for replication on k-means, but for the k-NN graph, going from four to eight GPUs gave about 1.6 times speedup for 20-byte codes and about 1.7 times for 40-byte codes. Finally, the selection kernel's efficiency drops sharply for large , so the best settings depend on the workload.
Reading guide
The paper has seven sections and an appendix. Read them in this order, with the lessons each one needs.
- Section 1, Introduction. Motivation and the list of contributions. Needs Approximate Nearest Neighbor Search and Product Quantization.
- Section 2, Problem statement. Defines the search, k-selection, batching, exact search, and IVFADC. Needs K-Nearest Neighbors, Euclidean Distance, Inverted Index and Product Quantization.
- Section 3, GPU overview. Explains lanes, warps, blocks, registers, and shared memory, then the problems with heaps and priority queues on a GPU. Needs GPU Architecture, GPU Memory Hierarchy and Priority Queue.
- Section 4, Fast k-selection. The in-register sorting networks (Algorithms 1 and 2) and WarpSelect (Algorithm 3). Needs Sorting Network and Sorting Algorithm. Read Section 4.1 slowly; it is the most technical part.
- Section 5, Computation layout. Exact search through GEMM, the lookup-table decomposition, the GPU implementation, and multi-GPU replication and sharding. Needs Matrix Multiplication, K-Means Clustering and the material above.
- Section 6, Experiments. Each subsection answers one question: selection speed, clustering, exact search, billion-scale search, the k-NN graph, and using that graph to find smooth paths between images. Read the tables and the discussion paragraphs; skip the figure captions until you have the context.
- Section 7 and the appendix. The conclusion is short. The appendix derives the WarpSelect cost, which uses the reasoning in Section 4.3.
Check yourself
QuestionWhat does k-selection return, and why is it harder on a GPU than on a CPU?Show answer
QuestionA product quantizer uses bytes per code, and each stored vector costs one table lookup per byte. How many lookup-adds does a list of codes need for one query?Show answer
QuestionWhy does the paper fuse the k-selection into the distance kernel instead of writing the full distance matrix to global memory?Show answer
QuestionYou have a database of 50 million 256-dimensional vectors and a GPU with 16 GB of memory. Would you store the raw vectors, and what does the paper suggest instead?Show answer
Sources
- Johnson, Douze and Jégou, Billion-scale similarity search with GPUs, arXiv:1702.08734: arxiv.org/abs/1702.08734
- DOI record: doi.org/10.48550/arXiv.1702.08734