Replacing a vector with the address of a similar one
Cluster the vectors, keep the centroids in a codebook, and store only which centroid each vector is nearest to.
With 256 centroids that is one byte, replacing 3,072 bytes of float32. The compression is enormous and the error is fatal, because 256 points cannot describe a 768-dimensional space.
Product quantisation
Split the vector into sub-vectors and quantise each one separately with its own codebook of centroids.
With and , the representable set is vectors while the storage is 96 bytes, down from 3,072.
That exponential is the whole idea: the codebooks combine multiplicatively and the storage adds.
Distances without decompressing
The squared distance decomposes across the sub-vectors, so it can be summed from per-segment distances rather than reconstructed.
Precompute the distance from each query segment to each of that segment's centroids. Scoring a stored vector is then table lookups and additions, with no multiplication at all.
That is why a quantised scan is not merely smaller but several times faster per vector than a float scan.
One index, in order
- Train the codebooks by running k-means on each segment of a sample.
- Encode every corpus vector to its centroid ids.
- At query time, build the lookup table from the query.
- Scan the codes, summing table entries to get approximate distances.
- Rescore the top candidates against the original vectors, if they are kept.
Step five is what keeps the ranking honest. Quantisation decides who the candidates are, and full precision decides their order.
The binary extreme
Binary quantisation keeps only the sign of each dimension, giving one bit per dimension and a 32-fold reduction.
Comparison becomes an exclusive-or and a bit count, which a CPU does on 512 bits per instruction.
It loses more than product quantisation and it is astonishingly fast, so it is used as a first filter with exact rescoring behind it.
What it costs
Recall drops a few points at 32 times compression.
The loss is real and it is usually acceptable, because retrieval feeds a reranker that fixes the ordering of whatever it is handed.
Codebooks are trained on a sample, so a corpus whose distribution shifts over time slowly degrades until they are retrained.
Quantisation error is also not uniform. Dimensions with high variance suffer most, which is why rotating the space first, as OPQ does, measurably improves the result for no extra storage.