Two ideas in one index
HNSW keeps every vector in full and saves time by walking a graph. IVF-PQ saves both time and memory with two older ideas. An inverted file (IVF) splits the vectors into clusters and searches only the clusters nearest the question. Product quantisation (PQ) stores each vector as a few bytes and estimates distances from small tables instead of the vectors themselves. Jégou, Douze and Schmid combined them in 2011; faiss's IndexIVFPQ, recorded here, is their method.
The vectors are the HNSW tab's: the Embeddings tab's 2,067 bge-small paragraph vectors of 384 numbers, searched with its 300 questions by squared distance, which for unit vectors orders them exactly as cosine does. The index has 32 cells and stores each vector in 16 bytes.
Cells: the inverted file
k-means (Lloyd's algorithm, 10 iterations) places centroids and puts each paragraph in the cell of its nearest centroid; each cell keeps a list of its members, the inverted list. A question is compared with the 32 centroids only, and the nearest cells are scanned. The 32 cells, n probed panel colours the map by cell and brightens the probed ones; Nearest centroids lists each centroid's distance and cell size, which ranges here from 2 to 145 paragraphs.
Probing one cell scans 83 codes per question on average, 4 % of the corpus; probing all 32 scans everything. The risk is at the borders: a question's nearest paragraph can sit just across the boundary in a cell the search did not probe. Raising trades speed for recall smoothly.
One paragraph in 16 bytes
Inside its cell a vector is stored relative to its centroid, as the residual , which is smaller and easier to quantise than . PQ cuts the residual into slices of 24 numbers, , runs a separate k-means with 256 centroids on each slice (25 iterations), and stores each slice as the index of its nearest sub-centroid, one byte:
Sixteen bytes can name different reconstructions while the codebooks hold only sub-centroids. The One paragraph in 16 bytes panel shows a residual, its codes, the sub-centroids they name and what the code loses. Per paragraph the index stores 16 bytes of code and 8 of id, against 1,536 bytes for the float32 vector; the centroids and codebooks, 442,368 bytes, are shared. The mean squared reconstruction error falls from 0.227 with 8 sub-vectors to 0.187 with 16 and 0.025 with 96.
Distances from tables
The question is not quantised, so the search is asymmetric (asymmetric distance computation, ADC). For a probed cell with centroid the question's residual is cut into the same 16 slices, and one table holds the squared distance from each slice to each of its 256 sub-centroids. Because squared distance adds up over slices, a code's estimated distance is 16 lookups and additions:
The table costs multiply-adds per probed cell, once; after that every code in the cell costs 16 lookups instead of 384 multiplications. The Distances from tables panel shows a question's table for one cell and adds up any paragraph's 16 lookups next to its true distance.
Recall against cost
The recall chart plots recall@10 against exact search against the codes scanned per question, one line per . Here PQ's errors are large next to the gaps between near neighbours: with recall@10 is 0.546 probing one cell and stays at 0.617 even scanning every cell, while IVF-Flat, the same cells with exact distances, reaches 0.769 with one cell and 1.0 with all. More sub-vectors help, to 0.863 with and every cell.
The usual remedy is to re-rank: take more candidates than needed, here 40 for the top 10, and recompute their exact distances from the float vectors, kept on disk or in slower memory. That lifts recall@10 with and every cell to 0.923. The question's own paragraph is in the top 10 for 94.7 % of questions with exact search, and for 72.7 % to 86.7 % with IVF-PQ as goes from 1 to 32. On 2,067 vectors none of this saves much; PQ was built for collections where 16 bytes instead of 1,536 per vector is what lets the index fit in memory.
Where it came from
The inverted file comes from text search and was carried over to image retrieval by Video Google (Sivic and Zisserman, 2003), which clustered image features into visual words. Product quantisation and its IVFADC index (Jégou et al., 2011) made it practical to search very large collections held in memory as short codes; optimised PQ (Ge et al., 2013) learns a rotation of the vectors that lowers the quantisation error. Johnson, Douze and Jégou moved the method onto GPUs in faiss, the library this tab reproduces. The Matryoshka tab's binary codes are the crudest quantisation of all, one bit per number, and the HNSW tab is the graph-based alternative; faiss can also combine the two, with an HNSW graph over the IVF centroids.
Papers
- Product Quantization for Nearest Neighbor Search Introduces product quantisation, asymmetric distance tables and the IVFADC index of residuals in coarse cells, the method faiss's IndexIVFPQ implements.
- Least squares quantization in PCM Lloyd's algorithm, the k-means that trains both the cells and the codebooks.
- Video Google: a text retrieval approach to object matching in videos An inverted file over k-means clusters of vectors, the IVF half of the index.
- Optimized Product Quantization for Approximate Nearest Neighbor Search Rotates the vectors before cutting them into slices, lowering product quantisation's error.
- Billion-scale similarity search with GPUs IVF-PQ on GPUs, the paper behind faiss.
- The Faiss library The library recorded here: its index types, the trade-offs between them and how they are combined.