Concepts
What is product quantization (PQ)?
How SrvDB's PQ mode splits each vector into sub-vectors of 8 (or 4) dimensions, stores one learned centroid index per sub-vector, and scans with lookup tables - up to 32 times smaller than flat.
Product quantization (PQ) cuts each vector into short sub-vectors and replaces every sub-vector by the index of its nearest centroid in a learned 256-entry codebook, one byte each. In SrvDB that stores a 768-dimensional vector in 96 bytes, 32 times less than flat, and searches it with per-query lookup tables.
How it works
Section titled “How it works”- Split. SrvDB uses
M = D / 8sub-spaces whenDis divisible by 8, otherwiseD / 4. Other dimensions are refused withValueError. - Train. Each sub-space learns 256 centroids with k-means (20
iterations), trained in parallel. A database created with
index="pq"and notrainingtrains on its first batch, which must hold at least 256 vectors;training=trains on the sample you pass. The codebooks are saved incodebooks.bin. - Encode. Each sub-vector is replaced by the index of the centroid
with the highest cosine similarity. Codes go to
quantized_vectors.bin,Mbytes per vector. - Search (asymmetric distance). The query stays in full precision.
For each sub-space SrvDB computes its similarity to all 256 centroids
once, giving
Mtables. A stored vector’s score is then the average ofMtable lookups, scanned in parallel blocks of 256 vectors.
Try it in SrvDB
Section titled “Try it in SrvDB”import randomimport srvdb
rng = random.Random(7)vectors = [[rng.gauss(0, 1) for _ in range(128)] for _ in range(1000)]ids = [f"doc-{i}" for i in range(len(vectors))]
db = srvdb.create("./pq-db", dimension=128, index="pq", training=vectors[:300])db.add(ids, vectors, ["{}"] * len(ids))db.save()print(len(db.search(vectors[42], k=5)))# 5Trade-offs
Section titled “Trade-offs”- Bytes per vector.
D / 8: 16 at D = 128, 48 at D = 384, 96 at D = 768 (32 times less than flat). WithD / 4sub-spaces, 16 times less. - Recall. Approximate and bounded by the codebooks: two vectors with the same codes are indistinguishable. Tightly clustered embeddings can lose a lot of accuracy. Measure against flat.
- Scores. An average of per-sub-space cosines, not the true cosine. The full vectors are not stored, so there is no exact rerank.
- Speed. A linear scan of
Mlookups per vector; table building is256 × Dwork per query. - Training. k-means over every sub-space; the cost grows with the sample size. Codes depend on the codebooks, so they cannot be retrained in place.
When to choose it
Section titled “When to choose it”- The collection must be as small as possible on disk and in memory, and approximate ranking is acceptable.
- You can supply a representative training sample of 256 vectors or more.
- If you need exact scores, choose RaBitQ (no training, full vectors kept); for 4× compression with a simpler model, choose SQ8.
How small are PQ codes in SrvDB?
Section titled “How small are PQ codes in SrvDB?”One byte per sub-vector: dimension / 8 bytes when the dimension is divisible by 8, otherwise dimension / 4. At dimension 768 that is 96 bytes, 32 times less than flat.
How much data does PQ need for training?
Section titled “How much data does PQ need for training?”Each sub-space learns 256 centroids with k-means. A database opened with
index="pq" without training trains on the first batch added, which must hold at least 256
vectors.
Are PQ scores exact?
Section titled “Are PQ scores exact?”No. The score is the average, over sub-spaces, of the cosine between the query sub-vector and the stored centroid. The full vectors are not kept.
Related concepts
Section titled “Related concepts”Next: What is RaBitQ quantization?
Papers and further reading
Section titled “Papers and further reading”- Hervé Jégou, Matthijs Douze, Cordelia Schmid (2011). Product Quantization for Nearest Neighbor Search. IEEE Transactions on Pattern Analysis and Machine Intelligence 33(1). doi:10.1109/TPAMI.2010.57
- Fabien André, Anne-Marie Kermarrec, Nicolas Le Scouarnec (2015). Cache locality is not enough: High-performance nearest neighbor search with product quantization fast scan. VLDB 2015. The lookup-table shuffle that SrvDB’s RaBitQ fast-scan uses.
Was this page helpful? Tell us in Discussions.