Skip to content

LearnIndex & store

Product quantization

PQ

Compresses a vector by cutting it into sub-vectors and replacing each with the id of its nearest entry in a small learned codebook.

Related techniqueGlossary entry

On this page

Product quantization (PQ) compresses vectors by splitting each one into sub-vectors and replacing every sub-vector with the id of its nearest entry in a small codebook learned from the data.

Take a 768-dimension embedding stored as 32-bit floats: 3,072 bytes. Split it into 96 sub-vectors of 8 dimensions. For each of the 96 positions, k-means learns 256 representative sub-vectors (a codebook). Each sub-vector is stored as the one-byte id of its nearest codebook entry. The vector is now 96 bytes, 32 times smaller.

Distances are computed without decompressing: for a query, precompute its distance to every codebook entry at every position (96 × 256 small numbers), then the distance to any stored vector is the sum of 96 table lookups. Jégou, Douze and Schmid introduced the method in 2011; it is a core part of the Faiss library and of IVF-PQ indexes.

The gotchas

  • Distances are estimates. Each sub-vector is replaced by an approximation, so close candidates are often misordered. Systems usually retrieve extra candidates and re-score them with the original vectors (refining).
  • It needs training. Codebooks are learned from a sample; with too few vectors (IVF-PQ in LanceDB requires at least 256 rows), or after the data drifts, quality drops.
  • It trades more accuracy than scalar quantization. 8-bit scalar quantization is 4 times smaller with tiny errors; PQ is often 16 to 64 times smaller with larger ones. Pick PQ when memory is the hard constraint.

DocSlurp does not use PQ: its LanceDB index uses 8-bit scalar quantization inside HNSW.

In DocSlurp

Related technique

DocSlurp does not use PQ. Its index is HNSW with scalar quantization, which, per the comment in core/worker/src/stages/lancedb-maintenance.ts, needs no PQ training minimum (IVF_PQ requires at least 256 rows) and keeps recall high without a refine step.

Sources

Jump to