Embedding Quantization: Vector Representation Compression
Summary
Embedding quantization — compresses vectors into lower-bit representations to reduce memory and accelerate vector search with controlled recall loss.
Why Quantize Embeddings
A dense index of a million vectors of dimension 1024 in float32 takes up about 4 GB. As the collection grows, memory and bandwidth become bottlenecks. Quantization reduces the size by 4–32 times.
Methods
| Method | Bit Depth | Compression | Recall Loss |
|---|---|---|---|
| Scalar (SQ) | int8 | 4x | 1–3% |
| Product (PQ) | 8–16 bytes per vector | 16–64x | 5–20% |
| Binary | 1 bit per dimension | 32x | 10–30% |
| Optimized (OPQ) | as PQ | 16–64x | 3–10% |
Scalar Quantization
Each dimension is independently mapped to an 8-bit integer based on the minimum and maximum values in the training set.
import numpy as np
def scalar_quantize(vectors, bits=8):
lo = vectors.min(axis=0)
hi = vectors.max(axis=0)
levels = 2 ** bits - 1
scaled = (vectors - lo) / (hi - lo + 1e-9)
codes = np.round(scaled * levels).astype(np.uint8)
return codes, lo, hi
Product Quantization
The vector is split into m subspaces, and a codebook of k centroids is trained for each. The vector is replaced by a sequence of indices of length m. Distances are computed using precomputed scalar products.
Binary Quantization
Each dimension is replaced by its sign. Similarity is computed through Hamming distance, providing an order of magnitude speedup but requiring fine-tuning the embedding model for binary representations.
Indexing Scheme
flowchart LR
A[Float32 dense vector] --> B{Method}
B -->|SQ| C[int8]
B -->|PQ| D[Subspace codes]
B -->|Binary| E[Bit string]
C --> F[Index]
D --> F
E --> F
F --> G[Search with re-ranking]
Loss Compensation
- Recompute exact distance for top-N candidates (re-rank using full vectors).
- Store residual vectors for PQ (IVF-PQ, OPQ).
- Hybrid: binary index for selection, dense for refinement.
Impact on Quality
- Cosine similarity after SQ is preserved almost exactly.
- PQ distorts geometry: close vectors may diverge by codes.
- Binary quantization requires a consistent embedding model.
- Retrieval evaluation metrics should be recomputed after changing the scheme.
Practical Notes
- Quantization is applied to the index, not the embedding model.
- The codebook training set should reflect the collection distribution.
- Codebooks are recompiled when the embedding model is updated.
Related materials: embedding-basics, quantization, vector-index, vector-quantization.