Module 2 · Vector databases
Binary/scalar quantization and costs at scale
5 min read
A 384-dimension float32 vector weighs 1.5 KB; at 1,024 dimensions, 4 KB. Multiplied by tens of millions of passages, plus the HNSW graph, you quickly exceed the memory of a reasonable machine. Quantization reduces the precision of each dimension to divide memory by 4, 8 or 32. This lesson measures what you lose, and the technique that recovers almost everything: rescoring.
Three levels of precision
| Representation | Bytes per dimension | 384 dimensions | Memory gain |
|---|---|---|---|
| float32 | 4 | 1,536 bytes | baseline |
| float16 / halfvec | 2 | 768 bytes | ×2 |
| int8 (scalar) | 1 | 384 bytes | ×4 |
| binary | 1/8 (1 bit) | 48 bytes | ×32 |
Scalar quantization (int8) maps each dimension onto 256 values, with one scale factor per dimension:
self.scale = np.abs(vectors).max(axis=0) / 127.0 + 1e-12
self.codes = np.round(vectors / self.scale).astype(np.int8)
Binary quantization keeps only the sign of each dimension. Similarity becomes the number of identical bits (Hamming distance), which the processor computes very quickly:
self.codes = np.packbits(vectors > 0, axis=1) # un bit par dimension : le signe
...
q_bits = np.packbits(query > 0)
differing = np.unpackbits(np.bitwise_xor(self.codes, q_bits), axis=1).sum(axis=1)
return -differing.astype(np.float32)
What you lose: measurements on Prism
| Configuration | Recall@5 | MRR | English | Rephrased | Multi-source |
|---|---|---|---|---|---|
| Dense float32 | 0.90 | 0.82 | 0.92 | 0.90 | 0.75 |
| Dense int8 | 0.90 | 0.82 | 0.92 | 0.90 | 0.75 |
| Dense binary | 0.84 | 0.78 | 1.00 | 0.80 | 0.42 |
| Binary + rescoring (30 candidates) | 0.90 | 0.82 | 0.92 | 0.90 | 0.75 |
- int8 loses nothing measurable: four times less memory, for free. It is almost always a good deal.
- Binary loses six points of recall, and above all collapses on multi-source questions (0.42): when two articles must be found, the coarse approximation often ranks only one in the top five. The gain on English (1.00) is a fluke over six questions, not a property of binary quantization.
- Rescoring recovers everything: keep the 30 best candidates according to the binary index, then re-rank them with the full float32 vectors.
if rescore:
exact = self.float_vectors[candidates] @ query
order = np.argsort(-exact)[:k]
return [Hit(self.chunks[candidates[i]], float(exact[i])) for i in order]
Binary is used to preselect quickly and in little memory; precision comes from rescoring. The full vectors can then stay on disk: only a few dozen are read per query.
One nuance: binary works better the more dimensions the model has. With 384 dimensions, 384 bits remain a poor representation; with 1,024 or more, and on models trained to tolerate binarization, the loss before rescoring is often much smaller. Measure with your model.
In SQL with pgvector
pgvector offers both forms as expression indexes: the column keeps the full vector, the index stores the compressed version.
CREATE INDEX chunks_embedding_half ON chunks
USING hnsw ((embedding::halfvec(384)) halfvec_cosine_ops);
CREATE INDEX chunks_embedding_bin ON chunks
USING hnsw ((binary_quantize(embedding)::bit(384)) bit_hamming_ops);
Binary with rescoring fits in one query:
SELECT id, article_id, 1 - (embedding <=> $1) AS similarity
FROM (
SELECT * FROM chunks
ORDER BY binary_quantize(embedding)::bit(384) <~> binary_quantize($1::vector) -- cast obligatoire
LIMIT 40
) AS candidates
ORDER BY embedding <=> $1
LIMIT 5;
The ::vector on the parameter (« cast obligatoire »: the cast is mandatory) is not decorative: without it, PostgreSQL does not know which variant of binary_quantize to call (it exists for several types) and the query fails with an ambiguity error. This is the kind of detail only an end-to-end test reveals.
For the expression index to be used, the query must repeat exactly the same expression as the index (binary_quantize(embedding)::bit(384)).
Computing costs at scale
For 20 million passages of 1,024 dimensions:
| Strategy | Index in RAM | Comment |
|---|---|---|
| float32 + HNSW | ≈ 82 GB + graph | Large memory instance |
| halfvec + HNSW | ≈ 41 GB + graph | Usually negligible loss |
| int8 + HNSW | ≈ 20 GB + graph | Usually small loss |
| binary + HNSW, rescoring from disk | ≈ 2.6 GB + graph | Modest instance, candidates read from disk |
(The HNSW graph adds roughly M × 2 × 4 bytes per vector for level 0, i.e. 128 bytes with M=16; it does not compress.)
Memory is not the only cost. The other cost items of a RAG at scale:
- Embedding: at every ingestion and every model change, the whole corpus (free locally, billed per token through an API).
- Queries: embedding the question, possibly rewriting and re-ranking (lessons 3.2 and 3.3).
- Generation: often the dominant item; prompt caching and semantic caching reduce it (lesson 5.3).
Key takeaways
- int8 divides memory by 4 with no measurable loss on Prism: adopt it almost always.
- Binary divides it by 32 but loses recall, especially on multi-source questions.
- Binary + float32 rescoring of a few dozen candidates recovers all the quality.
- pgvector: halfvec and binary as expression indexes; cast the parameter (
$1::vector) and repeat the index's exact expression.- Put numbers on memory, embedding, queries and generation before sizing.
Practice
- Vary the number of rescored candidates (10, 20, 30, 50): from what point do you get float32 quality back?
- Compare
memory_bytes()of Prism's three indexes and check the ×4 and ×32 ratios. - Compute the memory of your own project in float32, int8 and binary, graph included.