advanced vector-quantization 38 min read

Optimized Product Quantization and Score-Aware Quantization

Two ways to make a product quantizer optimal: rotate the space so its subspaces carry balanced information, and reshape the loss so it protects the inner products a query actually sees

Overview & motivation

The previous topic left a clean cliffhanger. Product quantization splits a vector into mm disjoint subvectors and quantizes each subspace independently, and it works because squared Euclidean distance separates over disjoint coordinate blocks. But that separation is also its weakness: the quantizer treats the subspaces as if they were independent and gives each the same number of bits. On a deliberately variance-imbalanced cloud — coordinates whose variance decays geometrically — raw product quantization paid a reconstruction distortion of about 85.685.6, because the equal bit budget over-quantizes the low-variance tail and starves the high-variance head. A rotation that balanced the variance across subspaces cut that distortion to about 15.415.4. We showed the direction of the effect and named the fix; this topic is the fix.

There are two independent ways to make a product quantizer better, and they are orthogonal in the literal sense. The first changes the axes: rotate the space before the split so each subspace carries a balanced share of the data’s information. This is optimized product quantization (OPQ), and it is the first half of the topic. The second changes the loss: when the downstream task is maximum-inner-product search rather than reconstruction, the error that matters is not how far the reconstruction sits from the vector, but how much it disturbs the inner products that high-scoring queries see. This is score-aware, or anisotropic, quantization — the idea behind ScaNN — and it is the second half. The laboratory below lets you switch between the four rotations, watch the alternating optimization converge, and raise the anisotropy to see a quantizer trade reconstruction for inner-product fidelity.

PQ distortion ∑‖x−Q(x)‖²
85.58
vs raw
0% lower
per-subspace variance (m = 4) — OPQ balances the product
subspace 11.35
subspace 20.17
subspace 30.02
subspace 40.00
distortion across rotations
raw (native axes)85.6
PCA-only (naive)83.3
balanced heuristic15.4
learned OPQ14.4
A naive PCA-alignment barely helps (85.6 → 83.3): it concentrates variance into subspace 1 and starves the rest. Balancing variance across subspaces collapses the distortion (→ 15.4), and the learned OPQ rotation refines it further (→ 14.4) via the alternating optimization in the next panel. The balanced and OPQ variance bars look alike because OPQ's extra gain is a within-block rotation, not a different split.

The first panel is the on-ramp made precise: switch among the raw axes, a naive PCA-alignment, the previous topic’s balancing heuristic, and the learned OPQ rotation, and read the distortion drop and the per-subspace variance bars. The second panel runs the alternating optimization and shows its distortion descending monotonically below the heuristic. The third panel is the score-aware story: a datapoint, its candidate codewords, and the residual split into the part that moves the inner product and the part that does not.

The rotation is a free isometry

Throughout, xRdx \in \mathbb{R}^d is a database vector, RO(d)R \in O(d) is an orthogonal matrix (RTR=IR^{\mathsf T} R = I), and the data splits into mm contiguous subspaces of width d/md/m. A rotated product quantizer first maps xRxx \mapsto Rx, then product-quantizes the rotated vector.

Definition 1 (Rotated product quantizer).

Fix an orthogonal RO(d)R \in O(d) and a product codebook C=C1××Cm\mathcal{C} = \mathcal{C}^1 \times \cdots \times \mathcal{C}^m with k\*k^\* centroids per subspace. The rotated product quantizer encodes xx by the tuple of sub-centroid indices of RxRx and reconstructs

QR(x)  =  RTc(Rx),c(Rx)C,Q_R(x) \;=\; R^{\mathsf T}\, c(Rx), \qquad c(Rx) \in \mathcal{C},

where c()c(\cdot) is the ordinary product-quantization codeword. Optimized product quantization minimizes the reconstruction distortion jointly over the rotation and the codebooks:

minRTR=I,  C1,,Cm iRxic(Rxi)2.\min_{R^{\mathsf T} R = I,\;\mathcal{C}^1,\dots,\mathcal{C}^m}\ \sum_{i} \big\lVert R x_i - c(R x_i) \big\rVert^2 .

Why is it legitimate to rotate the data before quantizing — does it not change the retrieval problem? It does not, and this is the whole license for the method.

Theorem 1 (Rotation is a distance- and inner-product-preserving isometry).

For any orthogonal RR and any x,yRdx, y \in \mathbb{R}^d,

RxRy=xy,Rx,Ry=x,y.\lVert Rx - Ry \rVert = \lVert x - y \rVert, \qquad \langle Rx, Ry \rangle = \langle x, y \rangle .

Consequently, rotating every database vector and every query by the same RR leaves all pairwise distances and all inner products unchanged, so the true nearest neighbors and the true maximum-inner-product results are identical. Since ordinary product quantization is the special case R=IR = I, and the feasible set of optimized product quantization strictly contains it, the optimized distortion never exceeds the unoptimized one:

EOPQ    EPQ.E_{\mathrm{OPQ}} \;\le\; E_{\mathrm{PQ}} .
Proof.

Orthogonality gives Rx2=(Rx)T(Rx)=xTRTRx=xTx=x2\lVert Rx \rVert^2 = (Rx)^{\mathsf T}(Rx) = x^{\mathsf T} R^{\mathsf T} R x = x^{\mathsf T} x = \lVert x \rVert^2, and the same computation applied to xyx - y and to the polarization identity x,y=12(x2+y2xy2)\langle x, y \rangle = \tfrac12(\lVert x \rVert^2 + \lVert y \rVert^2 - \lVert x - y \rVert^2) gives both equalities. Encoding xx as the nearest codeword of RxRx and decoding through RTR^{\mathsf T} is therefore a quantizer on the original space whose distortion equals that of ordinary product quantization applied to the rotated data. Optimizing over RR includes R=IR = I, so the minimum can only decrease. \blacksquare

The rotation is, in this exact sense, free: it cannot move a single true neighbor, but it adds d(d1)/2d(d-1)/2 degrees of freedom the quantizer can spend to reduce distortion. The companion code verifies the isometry directly and confirms that rotating both the database and the queries leaves every true nearest neighbor in place. The only question left is which RR to choose.

Parametric OPQ: balance the product, not the sum

When the data is well modeled by a Gaussian, the optimal rotation has a closed form. Write Σ\Sigma for the covariance of xx, and after rotation let Σ^=RΣRT\hat\Sigma = R \Sigma R^{\mathsf T}, partitioned into mm diagonal blocks Σ^jj\hat\Sigma_{jj} of size (d/m)×(d/m)(d/m) \times (d/m) — one per subspace. High-rate quantization theory bounds the distortion of a kk-centroid quantizer of a Gaussian by a constant times Σ1/d|\Sigma|^{1/d}, the determinant to the 1/d1/d power. Applying that bound per subspace and summing gives the quantity OPQ must minimize.

Theorem 2 (Parametric optimum: decorrelate and balance the determinant).

Under xN(0,Σ)x \sim \mathcal{N}(0, \Sigma) and the high-rate bound, the product-quantization distortion after rotation satisfies

E(R)    (k\*)2m/ddmj=1mΣ^jjm/d,E(R) \;\ge\; (k^\*)^{-2m/d}\,\frac{d}{m}\,\sum_{j=1}^{m} \big| \hat\Sigma_{jj} \big|^{m/d},

and minimizing the right-hand side over RR is equivalent to minimizing j=1mΣ^jjm/d\sum_{j=1}^{m} |\hat\Sigma_{jj}|^{m/d}. By the arithmetic–geometric-mean inequality and Fischer’s inequality,

j=1mΣ^jjm/d    m(j=1mΣ^jj)1/d    mΣ1/d,\sum_{j=1}^{m} \big| \hat\Sigma_{jj} \big|^{m/d} \;\ge\; m \Big(\prod_{j=1}^{m} \big|\hat\Sigma_{jj}\big|\Big)^{1/d} \;\ge\; m\,|\Sigma|^{1/d},

a bound that is constant in RR. It is attained if and only if (i) the off-diagonal blocks of Σ^\hat\Sigma vanish — the subspaces are decorrelated, achieved by aligning to the principal axes — and (ii) the block determinants Σ^jj|\hat\Sigma_{jj}| are equal across subspaces, i.e. the product of the eigenvalues assigned to each subspace is balanced.

The two conditions are distinct, and both are needed. Diagonalizing Σ\Sigma by PCA makes Fischer’s inequality tight — it removes cross-subspace correlation — but it does not say which eigen-axis belongs to which subspace. The arithmetic–geometric-mean step is tight only when the products of eigenvalues match across subspaces, and that is a question of allocation: distribute the dd eigenvalues into mm groups so the products are as equal as possible. The greedy eigenvalue allocation does exactly this — it repeatedly places the largest unassigned eigen-axis into the subspace with the smallest current product (equivalently, the smallest sum of log-eigenvalues).

This is where the previous topic’s heuristic and the true optimum part ways, and the difference is provable rather than cosmetic. The balancing heuristic equalized the sum of variances across subspaces — the trace of each block. The parametric optimum equalizes the product — the determinant. Under the high-rate bound the distortion is governed by the geometric mean of the variances, not the arithmetic mean, so balancing the sum is only an approximation while balancing the product is exact. In the companion code, the spread of per-subspace log-variances on the imbalanced cloud falls from about 12.212.2 under the raw contiguous blocks to about 0.30.3 under the eigenvalue allocation: the determinant is balanced almost perfectly, which the sum-balancing heuristic does not achieve.

The Gaussian assumption is doing real work here, and we flag it: on data that is not Gaussian, the eigenvalue allocation is a principled construction and an excellent starting point, but not a certified optimum. For that we need an algorithm that makes no distributional assumption at all.

Non-parametric OPQ: alternating optimization

Drop the Gaussian model and return to the raw objective of Definition 1. It couples the rotation and the codebooks, and it is not jointly convex — but it splits into two subproblems, each of which we can solve to its global optimum, and alternating between them is a descent.

Theorem 3 (The rotation step is an Orthogonal Procrustes problem).

Hold the codebooks fixed, and let YY collect the current reconstructions c(Rxi)c(Rx_i) — one per training vector — with the data XX and reconstructions YY stacked as columns. The rotation that minimizes the reconstruction error,

minRTR=I RXYF2,\min_{R^{\mathsf T} R = I}\ \big\lVert R X - Y \big\rVert_F^2,

has the closed form

XYT=UΣVTR=VUT,X Y^{\mathsf T} = U \Sigma V^{\mathsf T} \quad\Longrightarrow\quad R = V U^{\mathsf T},

read directly off the singular value decomposition of the cross-covariance XYTX Y^{\mathsf T}. Holding the rotation fixed instead, the codebook subproblem is ordinary product quantization on the rotated data RXRXmm independent Lloyd problems. Alternating the two steps produces a sequence of distortions that is monotone non-increasing and converges.

Proof.

Expand the objective: RXYF2=XF2+YF22tr(RXYT)\lVert RX - Y \rVert_F^2 = \lVert X \rVert_F^2 + \lVert Y \rVert_F^2 - 2\,\mathrm{tr}(R X Y^{\mathsf T}), so minimizing it is maximizing tr(RXYT)\mathrm{tr}(R\, X Y^{\mathsf T}). With XYT=UΣVTX Y^{\mathsf T} = U \Sigma V^{\mathsf T} and Z=VTRUZ = V^{\mathsf T} R U orthogonal, tr(RXYT)=tr(RUΣVT)=tr(ZTΣ)=iΣiiZiiiΣii\mathrm{tr}(R X Y^{\mathsf T}) = \mathrm{tr}(R U \Sigma V^{\mathsf T}) = \mathrm{tr}(Z^{\mathsf T} \Sigma) = \sum_i \Sigma_{ii} Z_{ii} \le \sum_i \Sigma_{ii}, since the singular values are non-negative and the diagonal of an orthogonal matrix is bounded by one in magnitude. Equality holds when Z=IZ = I, i.e. R=VUTR = V U^{\mathsf T}. Each step is the global optimum of its subproblem — the codebook step by Lloyd’s monotonicity, the rotation step by this closed form — so the joint distortion cannot increase. Bounded below by zero, the sequence converges. \blacksquare

The guarantee is monotone descent, not global optimality: alternating optimization over a non-convex coupled objective reaches a local optimum that depends on the initialization, and the standard remedy is to warm-start from the parametric eigenvalue-allocation rotation, which is what the companion code does. On the imbalanced cloud the trajectory starts at the heuristic’s distortion of about 15.415.4 and descends monotonically to about 14.414.4 — the second panel of the laboratory traces it. The extra gain over the balancing heuristic comes from the within-block rotation the Procrustes step finds; the per-subspace variance sums barely move, which is why the variance bars for the heuristic and for OPQ look almost identical even though the distortion differs. We cross-check the rotation step against scipy.linalg.orthogonal_procrustes rather than trust an opaque library, and assert the monotone descent as a test.

Score-aware quantization: protect the inner product

Everything so far minimized reconstruction error, xQ(x)2\lVert x - Q(x) \rVert^2. For maximum-inner-product search that is the wrong objective. What a search actually cares about is the inner-product error — how much quantization perturbs q,x\langle q, x \rangle — and, more sharply, it cares about that error only for the queries qq that score xx highly enough to retrieve it. A reconstruction that is excellent on average but distorts the high-scoring inner products is worse than one that is mediocre on average but preserves them.

Make the residual r=xx~r = x - \tilde x explicit and split it relative to the datapoint’s own direction.

Definition 2 (Parallel and orthogonal residual).

For a datapoint x0x \ne 0 and a reconstruction x~\tilde x, decompose the residual r=xx~r = x - \tilde x into its component parallel to xx and the orthogonal remainder:

r  =  r,xx2x,r  =  rr,r2=r2+r2.r_\parallel \;=\; \frac{\langle r, x \rangle}{\lVert x \rVert^2}\, x, \qquad r_\perp \;=\; r - r_\parallel, \qquad \lVert r \rVert^2 = \lVert r_\parallel \rVert^2 + \lVert r_\perp \rVert^2 .

The parallel component is the one that moves the inner product. For a query qq roughly aligned with xx — which is what a high-scoring query is — the inner-product error q,xx~\langle q, x - \tilde x \rangle is dominated by the part of the residual along xx, while the orthogonal part is largely invisible to it. Averaging over queries drawn uniformly from the unit sphere makes this exact.

Theorem 4 (Anisotropic decomposition of the score-aware loss).

Let qq be uniform on the unit sphere in Rd\mathbb{R}^d, and weight each query by a score-dependent weight w(q,x)w(\langle q, x \rangle). The expected weighted inner-product error decomposes as an anisotropic sum of the two residual magnitudes,

Eq[w(q,x)q,xx~2]  =  hr2  +  hr2,\mathbb{E}_q\big[\, w(\langle q, x \rangle)\,\langle q,\, x - \tilde x \rangle^2 \,\big] \;=\; h_\parallel\,\lVert r_\parallel \rVert^2 \;+\; h_\perp\,\lVert r_\perp \rVert^2,

and for any weight ww that is zero on negative scores and non-decreasing on positive ones, hhh_\parallel \ge h_\perp. For the threshold weight w(t)=1[tT]w(t) = \mathbf{1}[t \ge T], the ratio η=h/h\eta = h_\parallel / h_\perp grows with dimension and, for TT bounded away from 00, has the large-dd form

η    (d1)(T/x)21(T/x)2.\eta \;\approx\; (d-1)\,\frac{(T/\lVert x \rVert)^2}{1 - (T/\lVert x \rVert)^2} .

The ratio η1\eta \ge 1 is the entire content of the result: the parallel error is penalized at least as heavily as the orthogonal error, and strictly more as the score threshold TT rises. At T=0T = 0 every query counts equally and the exact ratio is η=1\eta = 1 — the isotropic endpoint that the displayed large-dd form, which tracks only how η\eta grows as TT rises, does not itself capture — and the loss collapses back to ordinary reconstruction. As TxT \to \lVert x \rVert only near-perfectly-aligned queries count and η\eta \to \infty, penalizing only the parallel error. So the score-aware quantizer minimizes

(x,x~,η)  =  ηr2  +  r2,η1,\ell(x, \tilde x, \eta) \;=\; \eta\,\lVert r_\parallel \rVert^2 \;+\; \lVert r_\perp \rVert^2, \qquad \eta \ge 1,

and chooses, for each datapoint, the codeword that best preserves its magnitude along its own direction even at the cost of larger orthogonal error. The third panel of the laboratory shows the swap: at η=1\eta = 1 the quantizer picks the Euclidean-nearest codeword; raise η\eta and it switches to a codeword lying exactly on the datapoint’s direction, with zero parallel residual.

The closed-form weight is a large-dimension limit, the threshold TT (equivalently η\eta) is a tuned knob, and the additive objective above is cleanest when the database has constant norm — all of which we flag. What survives those caveats is the mechanism, and it is robust: protecting the parallel component protects the inner products a query sees.

Honest accounting

The rotated, score-aware codebook is the atom the production indexes assemble. An inverted-file index quantizes the residual after a coarse partition; a multi-vector index quantizes each token embedding; and both inherit, unchanged, the rotation that balances the determinant and the loss that protects the inner product. Those are the topics that follow.

Connections

  • optimized product quantization IS product quantization with a learned orthogonal rotation applied before the subspace split: the additive distortion decomposition, the per-subspace Lloyd training, the asymmetric distance lookup, and the variance-imbalanced cloud that motivated the rotation are all inherited unchanged, and the companion code imports the previous topic's train_pq, pq_distortion, and rotation_distortion_study and re-derives its raw, PCA-only, and balanced baselines rather than reimplementing them product-quantization
  • the parametric rotation begins exactly where PCA does — diagonalize the covariance to decorrelate the subspaces — and the eigenvalue allocation that balances the variance product across subspaces is a permutation of the principal axes, so optimized PQ is a structured application of the eigendecomposition and variance-ordering developed there pca-dimensionality-reduction
  • score-aware quantization decomposes the residual relative to the datapoint's direction on the unit sphere and integrates the inner-product error over queries drawn uniformly from that sphere, so the directional and concentration geometry developed there is exactly the machinery the anisotropic weight ratio is derived from hypersphere-vmf-geometry

References & Further Reading

  • paper Optimized Product Quantization — Ge, He, Ke & Sun (2014) The source of both results in the rotation half: the parametric eigenvalue-allocation solution (decorrelate and balance the product of eigenvalues) and the non-parametric alternating algorithm whose rotation step is an Orthogonal Procrustes problem
  • paper Accelerating Large-Scale Inference with Anisotropic Vector Quantization — Guo, Sun, Lindgren, Geng, Simcha, Chern & Kumar (2020) The ScaNN paper: the score-aware quantization loss, the parallel/orthogonal residual decomposition, and the proof that for any score-increasing weight the parallel error is penalized at least as heavily as the orthogonal one
  • paper Product Quantization for Nearest Neighbor Search — Jégou, Douze & Schmid (2011) The product quantizer optimized here, and the source of the sum-of-variances balancing heuristic that the parametric optimum supersedes
  • paper A Generalized Solution of the Orthogonal Procrustes Problem — Schönemann (1966) The closed-form SVD solution R = VU^T to the orthogonal-matrix least-squares problem that is the non-parametric rotation step
  • documentation Faiss: Billion-Scale Similarity Search — Johnson, Douze & Jégou (2019) How production ANN indexes implement the OPQ pre-rotation as a matrix multiply composed with PQ encoding (IndexPreTransform + IndexPQ / IndexIVFPQ)