Product Quantization (PQ) is a vector compression technique that addresses the memory and latency challenges of large-scale vector similarity search. Instead of storing full 32-bit float vectors, PQ splits each vector into m subvectors, trains a small codebook of k centroids per subspace using k-means, and encodes each vector as a sequence of centroid indices. This achieves dramatic compression (e.g., 64x) while enabling approximate distance computation via precomputed lookup tables — either Symmetric (SDC) or Asymmetric (ADC) Distance Computation. The post walks through the math, Python implementation of training, encoding, decoding, and distance computation, plus real-world integration examples in Milvus, Weaviate, and Qdrant. For RAG pipelines, PQ can reduce a 61 GB embedding index to ~480 MB, enabling billion-scale search on commodity hardware with recall as the primary tradeoff.

10m read timeFrom arpitbhayani.me
Post cover image
Table of contents
Memory Problem in Vector SearchVector QuantizationProduct QuantizationTraining CodebooksVectors to PQ codesReconstructing Vectors from PQ codesDistances with PQ codesProduct Quantization in ActionApplications in RAGFootnote
2 Impressions