Zenaique

Walk through how Product Quantization compresses a 1024-dim float32 vector down to 32 bytes, step by step.

Short answer·Medium·4.0 · 0·~3 min·Asked atMphasisZilliz·Relevant atMicrosoftNVIDIA
Attempt it

Explain the Product Quantization compression pipeline that takes a 1024-dim float32 vector (4096 bytes) and produces a 32-byte PQ encoded representation. Cover the offline training step and the per vector encoding step.

Free · 2 AI evals / day
TL;DR

Split the 1024-dim vector into 32 sub-vectors of 32 dims. For each subspace, k-means produced 256 centroids during offline training. Encode each sub-vector as the 1-byte index of its nearest centroid.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine your 1024-number vector is a long bookshelf with 1024 books. **The chunked-codebook method** (called PQ) chops the shelf into 32 mini-shelves of 32 books each. For each mini-shelf, a librarian has already prepared a catalogue of 256 typical mini-shelf arrangements (the codebook). To compress your bookshelf, you look at each of your 32 mini-shelves, find the catalogue entry that looks most like it, and write down that catalogue number. Catalogue numbers go from 0 to 255, which fits in one byte. So your entire 1024-book shelf becomes 32 small numbers, one per mini-shelf. The cost is that two different mini-shelf arrangements may round to the same catalogue entry, so some detail is lost; that is the rounding error.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

Product Quantization is the workhorse compression scheme behind billion-scale ANN. The interview question is testing whether the candidate can cleanly separate the offline codebook-training phase from the per vector encoding phase, and whether they understand the byte-alignment trick that makes 256 centroids the canonical choice.

The Cartesian product trick

A naive vector quantizer for R^1024 would need an enormous codebook to cover the space (something like 256^32 entries to match PQ's effective resolution). PQ sidesteps that by factoring the 1024-dim space into a product of 32 lower-dim subspaces and quantizing each independently. Each subspace gets its own small codebook of 256 entries; the joint quantizer is the Cartesian product of the 32 per-subspace quantizers, with effective resolution 256^32 representable vectors at storage cost 32 * 256 entries.

The implicit assumption is that the data distribution factorizes (approximately) across the subspaces. For raw float vectors that assumption is often violated because embedding dims are correlated; OPQ learns a rotation to make the assumption hold better.

Why 256 centroids and 1 byte per subspace
Where the recall cost comes from
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
AspectOffline codebook trainingPer-vector encoding
When it runsOnce per corpus (re-run on distribution shift)On every insert
Input100k-1M training vectorsOne float32 vector
Cost per callMinutes (k-means convergence)Microseconds (32 * 256 distance ops)
Output32 codebooks, 1 MB total32-byte PQ code
Failure modeTraining set unrepresentative -> bad centroidsNone at encoding; recall loss is from codebook quality

Real products, models, and research that use this idea.

  • Faiss IndexPQ and IndexIVFPQ both implement this exactly; pq_m is the constructor argument M and nbits defaults to 8.
  • Milvus IVF_PQ index type exposes m (subspaces) and nbits as tuning knobs; OPQ rotation is included in the OPQ_PQ variant.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy exactly 256 centroids? Why not 1024 or 64?
A

256 = 2^8 fits in exactly one byte. Going higher (e.g. 65536 = 2^16, 2 bytes per subspace) doubles storage; going lower (e.g. 16 = 2^4, half a byte) requires bit-packing and loses recall. 256 is the sweet spot for byte-aligned storage.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Confusing the offline codebook training (run once per corpus, expensive) with per vector encoding (run on every insert, cheap). They are different phases with different costs and different inputs.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Two phases: offline codebook training (once) and per vector encoding (every insert)

  • pq_m = number of subspaces; subspace size = total_dim / pq_m

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
HNSW vs IVF, when…
Flashcard·Medium