advanced ann-indexing 34 min read

Voronoi Partitioning and the Inverted-File Index

A coarse quantizer cuts the database into Voronoi cells so a query scans only the few nearest — and product-quantizing the residual after the partition turns the same bit budget into higher recall

Overview & motivation

The quantization track — Lloyd’s codebook, its product factorization, and the rotation that optimizes it — made a single distance cheap to estimate. None of it made search non-exhaustive: every method still compares the query against all nn database codes. The inverted-file index is the other half. A coarse quantizer partitions the space into nlistn_{\text{list}} Voronoi cells, files each vector under its nearest centroid, and lets a query touch only the vectors in the few cells nearest to it. The art is entirely in the trade — how few cells can you probe before you start missing neighbors?

The index has two movements. The first is the partition: build the cells, define the inverted lists, and quantify the candidate-set reduction and the recall it costs. The second is the residual: having spent a coarse centroid on each vector, product-quantize what the centroid leaves behind, which is cheaper to encode than the vector itself. The laboratory below lets you drag the number of probed cells and watch a true neighbor get found or missed, trace the recall-versus-scan frontier, and see the residual’s variance collapse.

cells probed
1 of 6
candidates scanned
9 of 60
true neighbor
missed ✗
The query (◆) is compared only against the points in its 1 nearest cell. Its true nearest neighbor (ringed) lives in the query's 3rd-nearest cell, so it is missed at this nprobe — the boundary effect. Raise nprobe to 3 and the ring turns green.

The first panel is the partition and its honest catch: the query’s true nearest neighbor sits in its third-nearest cell, so it is missed until you probe three. The second panel is the payoff — recall against the fraction of the database scanned. The third is IVFADC: the coarse quantizer removes most of the variance, so the residual product quantizer reaches higher recall at the same bit budget.

Movement 1 — the partition

Let X={x1,,xn}RdX = \{x_1, \dots, x_n\} \subset \mathbb{R}^d be the database. Train a coarse quantizer of nlistn_{\text{list}} centroids {μ1,,μnlist}\{\mu_1, \dots, \mu_{n_{\text{list}}}\} by Lloyd’s k-means on XX. Each centroid owns a Voronoi cell and an inverted list of the vectors assigned to it.

Definition 1 (Coarse quantizer and inverted lists).

The coarse quantizer assigns xx to its nearest centroid, i(x)=argminjxμji(x) = \arg\min_j \lVert x - \mu_j \rVert, partitioning Rd\mathbb{R}^d into the Voronoi cells Vj={z:i(z)=j}V_j = \{z : i(z) = j\}. The inverted list Lj={i:i(xi)=j}L_j = \{\,i : i(x_i) = j\,\} holds the indices of the database vectors in cell jj. To search, the query qq probes its nproben_{\text{probe}} nearest centroids and ranks only the vectors in those cells:

cand(q)  =  jNnprobe(q)Lj,Nnprobe(q)=the nprobe centroids nearest q.\mathrm{cand}(q) \;=\; \bigcup_{j \in \mathcal{N}_{n_{\text{probe}}}(q)} L_j, \qquad \mathcal{N}_{n_{\text{probe}}}(q) = \text{the } n_{\text{probe}} \text{ centroids nearest } q .

Theorem 1 (Candidate-set reduction).

Probing nproben_{\text{probe}} cells scans exactly the total occupancy jNnprobe(q)Lj\sum_{j \in \mathcal{N}_{n_{\text{probe}}}(q)} |L_j| of those cells. If the cells are balanced, Ljn/nlist|L_j| \approx n / n_{\text{list}}, the expected scan is

Ecand(q)    nprobenlistn,\mathbb{E}\,|\mathrm{cand}(q)| \;\approx\; \frac{n_{\text{probe}}}{n_{\text{list}}}\, n,

so with nlistnn_{\text{list}} \approx \sqrt{n} and small nproben_{\text{probe}} the index scans on the order of n\sqrt{n} vectors per query, against nn for the exhaustive scan. Probing all nlistn_{\text{list}} cells recovers the exhaustive scan, hence exact search.

The reduction is real — on the finance cloud with nlist=32n_{\text{list}} = 32, a single probe scans under 4%4\% of the database — but the n\sqrt{n} figure rides on the balanced-cell assumption, which a k-means partition of real data only approximates. That is the first honest caveat; the second is sharper.

Theorem 2 (The boundary effect).

The vectors a query actually ranks are those in its probed cells, so the recall at nproben_{\text{probe}} is the probability that a true neighbor lies in one of the query’s nproben_{\text{probe}} nearest cells. At nprobe=1n_{\text{probe}} = 1 this is the probability that the neighbor shares the query’s single nearest centroid — strictly below 11, because a query near a Voronoi boundary has neighbors on the far side, in a cell whose centroid is not the query’s nearest. Recall is non-decreasing in nproben_{\text{probe}} and reaches 11 at nprobe=nlistn_{\text{probe}} = n_{\text{list}}.

Proof.

A vector xix_i can be returned for query qq only if icand(q)i \in \mathrm{cand}(q), i.e. i(xi)Nnprobe(q)i(x_i) \in \mathcal{N}_{n_{\text{probe}}}(q). So recall@k@k equals the fraction of true top-kk neighbors whose cell is among the query’s nproben_{\text{probe}} nearest. Adding a cell to the probe set only enlarges cand(q)\mathrm{cand}(q), so recall cannot decrease in nproben_{\text{probe}}; at nprobe=nlistn_{\text{probe}} = n_{\text{list}} the candidate set is all of XX and the exact distance recovers every neighbor. At nprobe=1n_{\text{probe}} = 1, any neighbor xix_i with i(xi)i(q)i(x_i) \ne i(q) — a neighbor across the Voronoi boundary separating its cell from the query’s — is missed, and such neighbors exist whenever qq is closer to a boundary than to its nearest in-cell neighbor, which has positive probability. \blacksquare

On the finance cloud the frontier is steep in the index’s favor: recall@10 climbs from 47.5%47.5\% at a single probe to 98%98\% at eight probes, while the scanned fraction grows from under 4%4\% to about a quarter. The boundary effect is the price of the partition, and multi-probe is how you pay it down.

Movement 2 — the residual (IVFADC)

The coarse quantizer leaves a residual r=xμi(x)r = x - \mu_{i(x)}: what the cell centroid fails to capture. Instead of throwing it away, encode it with product quantization. The reason this helps is a variance identity, and it is exact.

Theorem 3 (Residual variance reduction).

Let C=i(X)C = i(X) be the cell assignment, a random variable over the database. Because the Lloyd centroid is the sample mean of its cell at convergence, μi(x)=E[xC]\mu_{i(x)} = \mathbb{E}[x \mid C], the residual is the within-cell deviation r=xE[xC]r = x - \mathbb{E}[x \mid C]. By the law of total variance,

Var(x)  =  E[Var(xC)]within-cell=Var(r)  +  Var(E[xC])between-cell0,\operatorname{Var}(x) \;=\; \underbrace{\mathbb{E}\big[\operatorname{Var}(x \mid C)\big]}_{\text{within-cell} \,=\, \operatorname{Var}(r)} \;+\; \underbrace{\operatorname{Var}\big(\mathbb{E}[x \mid C]\big)}_{\text{between-cell} \,\ge\, 0},

so Var(r)=Var(x)Var(μi(x))Var(x)\operatorname{Var}(r) = \operatorname{Var}(x) - \operatorname{Var}(\mu_{i(x)}) \le \operatorname{Var}(x), with strict inequality whenever the cells have distinct means. The coarse quantizer has absorbed the between-cell variance; product quantization then spends its bits on the smaller within-cell signal.

This is the whole of IVFADC. Build the inverted file, compute each vector’s residual against its own cell centroid, and product-quantize the residuals; at search time, for each probed cell jj, the query’s residual qμjq - \mu_j is matched against the stored residual codes by the asymmetric lookup of the previous topic. Because the residual carries less variance — on the finance cloud the coarse quantizer removes 74%74\% of it — the same product-quantization budget reconstructs it more faithfully, and recall rises.

Proposition 1 (IVFADC versus flat product quantization at equal bits).

At a fixed product-quantization budget of mlog2k\*m \log_2 k^\* bits, encoding the residual (IVFADC) attains reconstruction distortion at most that of encoding the raw vector (flat product quantization), because the residual has no greater variance in any subspace and product-quantization distortion is monotone in subspace variance. The lower distortion translates into higher retrieval recall.

On the finance cloud at 6464-bit codes, flat product quantization reaches 75.3%75.3\% recall@10 and IVFADC reaches 77.1%77.1\% — the same bits, a smaller signal, more recall. The gain is a measured direction, not a universal constant; its size depends on the cloud, the cell count, and the budget.

Honest accounting

The inverted file is the first index here that gives up exactness for speed, and it does so with a single coarse partition. The next index structures keep the goal — touch few vectors, miss few neighbors — but replace the flat partition with a navigable graph, where search is a walk rather than a cell lookup. That is where the small-world topics go.

Connections

  • the coarse quantizer is literally Lloyd's k-means: the nlist cells are its Voronoi partition, each cell's centroid is the sample mean of its members by the M-step optimality condition proved there, and the companion code imports best_codebook and assign rather than reimplementing them, so the residual-variance identity follows from the centroid-equals-cell-mean fact established in that topic vector-quantization-lloyd-max
  • IVFADC product-quantizes the coarse-quantization residual, so it imports the additive distortion decomposition, the asymmetric-distance lookup table, and the train/encode routines of that topic and applies them per probed cell — the inverted file supplies the non-exhaustive candidate set, product quantization supplies the cheap distance, and IndexIVFPQ is the composition product-quantization
  • exact maximum-inner-product and nearest-neighbor search is the provably hard scan that topic characterizes, and the inverted file is the practical sublinear escape: it gives up the exactness guarantee for a tunable recall-versus-speed trade, scanning a small fraction of the database at the cost of the boundary-effect misses quantified here mips-hardness-and-sublinearity-limits
  • the boundary effect is a high-dimensional phenomenon — as dimension grows, a Voronoi cell has ever more neighboring cells and a query's true nearest neighbor is ever more likely to fall just across a boundary, so the concentration and neighbor-count results developed there explain why nprobe must grow with dimension to hold recall high-dimensional-geometry

References & Further Reading

  • paper Product Quantization for Nearest Neighbor Search — Jégou, Douze & Schmid (2011) Introduced IVFADC: the coarse quantizer with inverted lists, multi-probe search, and product-quantized residuals — the index this topic derives
  • paper The Inverted Multi-Index — Babenko & Lempitsky (2015) A product-quantized coarse quantizer that yields an exponentially finer Voronoi partition at the same storage — the refinement of the single coarse quantizer derived here, mitigating cell imbalance
  • paper Quantization — Gray & Neuhoff (1998) The definitive survey: the nearest-neighbor (Voronoi) quantizer, its cells, and the rate-distortion framing the coarse quantizer is an instance of
  • documentation Faiss: Billion-Scale Similarity Search — Johnson, Douze & Jégou (2019) How production indexes implement IVFADC as IndexIVFPQ: the coarse k-means quantizer, the inverted lists, the nprobe multi-probe, and the per-cell residual product quantization composed here