Compression and quantization
Quantization saves storage by representing a coordinate or a group of coordinates with a small code. Decoding the code gives an approximation of the original values.
The previous lesson counted the memory cost. Here you'll round individual coordinates, replace pairs with codebook entries, and check how those approximations change a distance calculation.
Give one coordinate a small ruler
Start with a deliberately coarse example. All values lie between zero and one, and we allow only five levels: 0, 0.25, 0.5, 0.75, and 1.
To encode 0.62, divide by 0.25 and round to the nearest integer. The stored code is 2. To reconstruct it, multiply by 0.25: 0.5. The reconstruction error has magnitude 0.12.
For 0.88, the code is 4 and reconstruction is 1. The error again has magnitude 0.12. These are invented values, chosen away from exact halfway cases.
This is a toy scalar quantizer: it treats one numeric coordinate at a time. It is not an implementation of a database's int8 format.
An int8 coordinate has 256 possible codes, so its ruler can be much finer. During calibration, the system uses representative embeddings to choose the range those codes cover. Scale and offset information map the codes back to approximate numeric values.
Check the boundary: a calibration range ends at 1, but tomorrow's data contains 1.4. What must the encoder decide?
It must handle the out-of-range value, often by clipping it to the allowed range or using another documented policy. Clipping loses the difference between 1.0 and 1.4. Poorly chosen ranges can waste precision or damage useful distinctions.
Share a dictionary of short vectors
Product quantization, usually shortened to PQ, works on groups of coordinates. Split a four-coordinate vector into two pairs. For each pair position, learn a small dictionary, called a codebook, of representative pairs.
Take the vector [0.1, 0.2, 0.9, 0.8]. Suppose the first codebook contains ID 0 → [0, 0] and ID 1 → [1, 1]. The second contains the same representatives for this toy example.
The first pair is closest to ID 0. The second is closest to ID 1. We store two IDs, [0, 1], and reconstruct [0, 0, 1, 1] when needed.
The codebooks are shared across many vectors. Each stored vector needs its selected IDs, rather than a private copy of every representative.
In a more realistic size calculation, split a 128-dimensional vector into 16 groups and give each group a 256-entry codebook. Each selected ID fits in one byte. The per-vector code is then 16 bytes, compared with 512 bytes for 128 float32 coordinates.
That is a 32-fold reduction in the coordinate code alone. It excludes codebooks, IDs, and index structures. It also says nothing by itself about retrieval quality.
Search without reconstructing everything
A query can remain in full precision. For each query group, calculate distances to that group's codebook representatives. Store those small distance tables temporarily.
To estimate the query's squared Euclidean distance to a coded vector, look up its selected entry in each table and add the contributions. This works because squared Euclidean distance is the sum of the coordinate-wise squared differences, which can be grouped.
The approximation comes from replacing each stored group with a representative. The query need not suffer the same replacement. This setup is often called asymmetric distance computation.
For query [0, 0, 1, 1], the selected first representative is [0, 0], contributing zero squared distance. The selected second representative is [1, 1], also contributing zero. The two table lookups therefore sum to zero.
Against the original vector, the squared distance is 0.01 + 0.04 + 0.01 + 0.04 = 0.10.
What did zero prove? Only that the reconstruction matches the query. It did not prove the original vector matched it.
Pick a test before a format
Scalar quantization is a relatively direct first experiment: fewer bytes per coordinate, with calibration choices to inspect. PQ can make the codes much smaller, but introduces codebook training and its own approximation behaviour.
Neither deserves a blanket “safe” label. Representative calibration data, the embedding distribution, and query difficulty affect the result. A changed corpus can also make old codebooks less suitable.
Compare the same queries against the original full-precision baseline. Inspect recall, relevant answers, memory, and latency. If you retrieve a larger compressed shortlist and rescore originals, include the extra storage and reads in that measurement.
To test your understanding, explain why a 16-byte code doesn't mean the database needs only 16 bytes per document. Your answer should include the shared codebooks, document identity, metadata, and whatever search structure locates the candidate in the first place.
Lesson notes
Work through scalar and product quantization to see exactly what gets stored, how much space it saves, and where search error enters.
Key ideas
- Scalar quantization: cheap, safe 4×
- Product quantization: codebooks, centroid IDs, big wins
- Recall@k as the acceptance test before shipping compression