Skip to content

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.

PQ: a vector is split into sub-vectors; each is replaced by the index of its nearest of 256 centroids vector, D dimensions, split into M = D / 8 sub-vectors 8 floats8 floats8 floats... codebook 1codebook 2codebook 3 1720388 256 centroids each, learned by k-means 1 byte per sub-vector
The code is M bytes. At search time each sub-space gets a 256-entry table of query-to-centroid scores, so scoring a vector is M table lookups.
  1. Split. SrvDB uses M = D / 8 sub-spaces when D is divisible by 8, otherwise D / 4. Other dimensions are refused with ValueError.
  2. Train. Each sub-space learns 256 centroids with k-means (20 iterations), trained in parallel. A database created with index="pq" and no training trains on its first batch, which must hold at least 256 vectors; training= trains on the sample you pass. The codebooks are saved in codebooks.bin.
  3. Encode. Each sub-vector is replaced by the index of the centroid with the highest cosine similarity. Codes go to quantized_vectors.bin, M bytes per vector.
  4. Search (asymmetric distance). The query stays in full precision. For each sub-space SrvDB computes its similarity to all 256 centroids once, giving M tables. A stored vector’s score is then the average of M table lookups, scanned in parallel blocks of 256 vectors.
import random
import 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)))
# 5
  • Bytes per vector. D / 8: 16 at D = 128, 48 at D = 384, 96 at D = 768 (32 times less than flat). With D / 4 sub-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 M lookups per vector; table building is 256 × D work 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.
  • 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.

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.

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.

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.

Next: What is RaBitQ quantization?

  • 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.