RAG and vector searchLoading progress…

Reranking

Calculate vector-code storage and distinguish scalar, product and binary quantization from reranking.

Reranking

A common pattern: search the compressed index cheaply for a generous shortlist (say 100 candidates), then rescore only those with the original vectors and keep the best k. Reranking 100 candidates costs 76,800 multiply-adds in 768 dimensions, against 768 million for an exact scan of a million vectors. But a rerank only reorders its shortlist: a relevant document that never entered it cannot come back.

shortlist size n (candidates reranked)2
idapproximate scoreexact scorein shortlist?true top-2?
091yes
185yesyes
272no
369noyes
453no
544no
shortlist by approximate score: [0, 1] rerank by exact score → top-2: [1, 0] true top-2: [3, 1] recall 0.5 id 3 never entered the shortlist, so no amount of exact reranking can recover it

At scale: reranking 100 candidates in 768 dimensions is 76,800 multiply-adds, against 768,000,000 for an exact scan of a million vectors (10,000× less).

Grow the shortlist until the true neighbours are inside it.

Predict first. Does exact reranking guarantee the exact global top-k when the candidates are incomplete?

Chapter context

Compressed vectors reduce storage and memory bandwidth, but approximate distance calculations can change rankings. Separate the compressed search representation from any original vectors retained for a later accurate rerank.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

For d float32 coordinates, raw vector bytes are 4d. For M PQ subspaces and b bits per subspace, code bytes are Mb/8Mb/8 when packed. For equal-size subspaces, float32 codebooks total 2bd×42^b d\times4 bytes, excluding additional index data.

Worked example

A 768D float32 vector uses 768×4=3072 bytes. With 96 subspaces and 8-bit codes it uses 96 bytes: 32 times smaller for the per-vector representation. The shared codebooks contain 256×768 float32 values, requiring 786,432 bytes. Original vectors retained for reranking are an additional cost.

Reference code

The following example uses the Python standard library and does not call a remote service.

dimension,parts,bits=768,96,8
raw=dimension*4
code=parts*bits//8
book=(2**bits)*dimension*4
assert raw == 3072 and code == 96
assert raw/code == 32
assert book == 786432
assert sum(a!=b for a,b in zip('1010','1111')) == 2

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Compression ratio for code bytes is not the total-system ratio. PQ codes are indices into learned representatives, not simply truncated float bits. Reranking cannot find missing candidates.