TL;DR
Product quantization stores a large collection of vectors in a very small amount of memory, and still lets you find the vectors closest to a query. It splits each vector into short pieces, replaces each piece with the index of its nearest entry in a small codebook, and estimates the distance from a query to every stored vector by adding up table lookups. A vector that took 512 bytes can be stored in 8 bytes, at the cost of approximate distances.
Builds on: Euclidean Distance, Vector Quantization · Leads to: FAISS, ScaNN
Where it's used
- Billion-scale search. The FAISS library, described in its paper, uses product quantization to hold collections of about a billion vectors on GPUs and search them quickly. Without compression, a billion 128-dimensional vectors would need hundreds of gigabytes of memory.
- Retrieval for language models. Retrieval-augmented generation systems feed documents to a language model and store one embedding per passage. For a large corpus, compressing those embeddings decides whether the index fits on one machine or must be split across many.
- Recommendation and ad matching. Systems that run maximum inner product search of a user vector against millions of item vectors per request use compressed codes so that each lookup touches a few bytes per item rather than a full float vector.
How it works
Take one small vector and trace it through encoding and then a search. The vector has six numbers, split into three pieces of two numbers each. Each piece has its own codebook with two entries.
- Split the vector. Let . Cut it into three pieces: , , . Check: the pieces, put back together, give the original six numbers.
- Look up the nearest codebook entry for each piece. The codebooks are , and . For , the squared distance to is 1 and to is 5, so the code is entry 0. Check: the code is the entry with the smaller distance.
- Finish encoding. For the code is entry 0, since is at distance 1 and is at distance 8. For the code is entry 1, since is at distance 1 and is at distance 2. The stored vector is the code : three small indices instead of six numbers.
- Measure the error. Rebuild the vector from the codes: . The difference from the original is , whose squared length is . The stored code is an approximation, and this is the price of compression.
- Prepare the lookup tables for a query. A query is not compressed. For each piece, compute its squared distance to every codebook entry. For the first piece : entry 0 gives 1 and entry 1 gives 5. For the second piece : entry 0 gives 2 and entry 1 gives 5. For the third piece : entry 0 gives 1 and entry 1 gives 4. Check: each piece has one number per codebook entry, six numbers in all.
- Estimate the distance by lookup. For the stored code , read the tables at those indices and add: . Check: the exact squared distance from to the rebuilt vector is , the same number. For a second stored vector with code , the lookups give , so the search ranks the first vector closer.
The search never touches the original vectors. It only reads the codes and the tables, which is why it is fast and small.
flowchart LR A["Database vector"] --> B["Split into pieces"] B --> C["Nearest codebook index per piece"] C --> D["Stored code: a few small integers"] Q["Query vector"] --> E["Distance from each piece to every codebook entry"] E --> F["Lookup tables, computed once per query"] D --> G["Sum table entries for each stored code"] F --> G G --> H["Approximate distances, then the nearest"]
The math
A product quantizer splits each vector into pieces of numbers each, . For each piece it learns a codebook of vectors, usually by k-means clustering on a training sample. A vector is stored as indices, one per piece, which take bits in total.
Split
A vector with numbers becomes pieces, and the pieces are taken in order: the first numbers form piece 1, the next form piece 2, and so on. In the trace, , , so each piece has two numbers. In the realistic case below, and , so each piece has 16 numbers.
Encode
The code for piece is the index of its nearest codebook entry:
Here is entry of codebook , and is the squared Euclidean distance. The full code is . In step 2 of the trace, and , so .
Reconstruct
The approximation of is the concatenation of the chosen entries:
Step 4 of the trace computes .
Estimate distance by lookup
For a query , split it the same way and precompute, for each piece and each codebook entry, the table value . Because squared Euclidean distance over concatenated pieces is the sum of the distances over the pieces, the distance to the reconstruction is
This identity is exact for the reconstruction. The only approximation is replacing by . Step 6 of the trace gives , which equals .
The cost per stored vector is table reads and additions, which does not depend on the vector's original length .
Worked examples
A 6-number vector split into 3 pieces, each with a codebook of 2 entries.
- Each piece needs one index, and an index into 2 entries takes bit.
- Three pieces need bits.
- The original vector needs 6 numbers. If each is a 32-bit float, that is bits.
- The code is times smaller.
Interpretation: the compression ratio depends on how many bits each piece's index takes compared with the bits of the numbers it replaces.
This is a common setting for image and text embeddings.
- A 128-number vector stored as 32-bit floats takes bytes.
- Split it into pieces of numbers each.
- Use entries per codebook. An index into 256 entries takes bits, which is one byte.
- The code takes bytes.
- The compression ratio is .
- The codebooks are shared by all vectors, so their cost is paid once: bytes, about 128 kilobytes.
- For each query, the lookup tables hold numbers. That is cheap to build compared with comparing the query to every stored vector.
Interpretation: 64 times less memory per vector, and a query costs about 2000 table entries plus 8 reads per stored vector.
A collection of a billion vectors, each with 128 floats.
- Uncompressed, the collection is bytes gigabytes.
- With the 8-byte codes from the standard example, the codes take bytes gigabytes.
- The codebooks add about 128 kilobytes, which is negligible.
- A search that scans all codes reads 8 bytes per vector instead of 512, and does 8 lookups per vector instead of 128 multiplications and additions.
- Try it: suppose you use 16 pieces instead of 8, each still with 256 entries. How many gigabytes do the codes take for a billion vectors, and what is the compression ratio?
Answer: 16 bytes per vector, so bytes gigabytes, and the compression ratio is . Doubling the pieces halves the compression, but it doubles the bits per vector, which is what buys back accuracy.
Common mistakes and tradeoffs
It is tempting to think that compressed vectors give the same distances as the originals. In fact the distance is to the reconstruction , not to , so two vectors can swap order when their distances are close. The standard fix is to re-rank: use the codes to shortlist a few hundred candidates, then compute exact distances to the original vectors for those candidates only.
It is tempting to think that each piece of the vector is independent of the others. In fact the method assumes that the distance adds up over the pieces, which is exact for the reconstruction, but a codebook built on one piece cannot exploit correlations between pieces. When features are strongly correlated across pieces, the codebooks waste entries. Optimized product quantization fixes this by learning a rotation of the vectors before the split, so that the pieces are closer to independent.
It is tempting to think that more pieces are always better. More pieces means more bits per vector and bigger lookup tables, so memory and per-query cost grow. For a fixed bit budget the opposite can happen: shorter codes with more pieces and fewer entries per codebook can approximate worse. The right number balances the memory budget against the accuracy the application needs. Scalar quantization, which rounds each number to a few bits, compresses by a factor of about 4 to 8 with little loss and needs no codebook training. Graph-based indexes such as HNSW are more accurate at a given speed, but they keep the full vectors in memory, so they cannot shrink the collection.
| Method | Typical compression | Accuracy | Best when |
|---|---|---|---|
| Scalar quantization | 4 to 8 times | High | Memory is tight but the collection is moderate |
| Product quantization | 16 to 64 times or more | Lower; re-ranking recovers much of it | The collection is very large and must fit in memory |
| Graph index (HNSW) | None: full vectors kept | Very high | The collection fits in memory and speed matters most |
History
Vector quantization, which replaces a vector by the nearest entry of a list, came out of signal compression work in the 1980s. Its codebooks are trained with the k-means procedure, whose basic idea dates to the 1950s.
In 2011, Jégou, Douze and Schmid published product quantization for nearest neighbor search, showing that splitting vectors into pieces made large codebooks affordable and that distances could be estimated by table lookup. It became the standard way to compress vectors for search.
In 2017, Johnson, Douze and Jégou published the FAISS library and its paper on billion-scale similarity search on GPUs. FAISS combined product quantization with fast GPU search, and it made searching a billion vectors practical for many teams. ScaNN, published in 2019, refined the idea by training the codebooks to minimize the errors that matter most for maximum inner product search, which is the form of search used in many recommendation and retrieval systems.
Recap
- Product quantization splits each vector into pieces and stores one codebook index per piece.
- Distances are estimated by adding table lookups, with the tables built once per query.
- A 128-number float vector (512 bytes) can shrink to 8 bytes with 8 pieces and 256 entries each.
- The estimate is to the reconstruction, not the original, so re-ranking a shortlist with exact distances is standard practice.
- Read next: Billion-scale similarity search with GPUs for the system that made billion-scale search practical, and Approximate Nearest Neighbor Search for the wider family of methods this belongs to.
Check yourself
QuestionA vector has 64 numbers. It is split into 8 pieces of 8 numbers, each with a codebook of 16 entries. How many bits does the code for one vector take?Show answer
Each piece needs an index into 16 entries, which takes bits. Eight pieces take bits, or 4 bytes. The original 64 floats take bits, so the code is 64 times smaller.
QuestionIn the trace, the query's table values for the stored code are , and . What is the estimated squared distance, and why is it exact for the reconstruction?Show answer
The estimate is . It equals the exact squared distance from the query to the reconstructed vector , because squared Euclidean distance over the concatenated pieces is the sum of the squared distances over the pieces. The estimate differs from the distance to the original only because was replaced by .
QuestionWhy might a search engine using product quantization return a slightly wrong nearest neighbor, and what is the usual remedy?Show answer
The distances are computed to the reconstructed vectors, not the originals, so two candidates whose true distances are close can swap places. The usual remedy is to shortlist a few hundred candidates with the compressed codes, then recompute exact distances to the original vectors for only that shortlist.
QuestionA team has 50 million embeddings of 768 [[floating-point-numbers|floats]] each, stored as 32-bit floats, and must fit them on a machine with 16 gigabytes of memory for search. Roughly how many bytes per vector can they spend, and is product quantization a natural choice?Show answer
Uncompressed, each vector takes bytes, so 50 million vectors take about 154 gigabytes. Sixteen gigabytes over 50 million vectors leaves about 320 bytes per vector. That is a compression of roughly 10 times, comfortably within what product quantization gives (for example 96 pieces with 1 byte each is 96 bytes per vector). Product quantization is a natural choice here, and re-ranking a shortlist with the original vectors would need them on disk rather than in memory.
Sources
- Johnson, J., Douze, M., and Jégou, H. (2017). Billion-scale similarity search with GPUs. https://arxiv.org/abs/1702.08734
- Guo, R., et al. (2019). Accelerating Large-Scale Inference with Anisotropic Vector Quantization. https://arxiv.org/abs/1908.10396
- Vector quantization, Wikipedia. https://en.wikipedia.org/wiki/Vector_quantization