TL;DR
Many search systems, from product recommendations to retrieval for language models, ask which stored vector has the largest dot product with a query. ScaNN is a way to store those vectors in a few bits each. Ordinary compression minimises the error on each stored vector. ScaNN instead minimises the error on the pairs a query is likely to rank highly, which means it penalises the part of the error that points along each vector more than the part that points across it. The result is more correct top matches for the same number of bits.
Builds on: Maximum Inner Product Search, Product Quantization, Orthogonal Projection · Leads to: Product Quantization is the codebook method it modifies, and HNSW: Efficient and Robust Approximate Nearest Neighbor Search is the graph-based alternative for the same search problem
Why it mattered
Maximum inner product search (MIPS) sits behind many large systems. In recommendation, users and items are embedded in one vector space and the items with the highest dot product with a user are shown. In extreme classification, a model with millions of classes picks its label by a dot product against a weight vector per class. Training itself uses the same operation for large output layers and for sparse memory lookups.
At that scale, scoring every datapoint is too slow, so quantization is used to shrink each vector and to speed up scoring. ScaNN's main contribution is a change to the quantization objective rather than a new search structure. The authors report state-of-the-art results on the public ANN-Benchmarks suite, and the implementation was released as open-source code.
The problem
Quantization replaces each datapoint with an approximation taken from a small codebook. Standard methods choose the codeword that minimises the reconstruction error . The paper shows that, if the queries are spread uniformly over directions, this objective is the same as minimising the average inner-product error over all datapoint and query pairs. That average treats every pair as equally important.
They are not. For a fixed datapoint, a query whose inner product with it is large is the one most likely to rank it at the top, so the error on that pair matters most. An approximation error on a pair with a low score is nearly irrelevant to the answer. Reconstruction error throws this difference away, so a codeword that is close in Euclidean distance can still give a worse top-ranking score than a codeword that is slightly further away but errs in the right direction.
How it works
The method runs in an offline phase and an online phase.
- Offline input: the database and a weight rule. Take the datapoints and choose a threshold on the inner product. Pairs whose score is below are ignored; pairs above it count. Tiny instance: with and unit-norm datapoints, a query that scores 0.5 with a point contributes to its loss, and one that scores 0.1 does not.
- Offline: learn the codebook with the anisotropic loss. Start from random datapoints as codewords. For each datapoint, pick the codeword with the smallest anisotropic loss, which weights the error along the datapoint more than the error across it. Then recompute each codeword as the weighted average of the datapoints assigned to it, with a closed-form update. Repeat until nothing changes. Check: the procedure has the same shape as k-means, and it can only lower the loss at each step.
- Offline: encode. Each datapoint is stored as a short list of codeword indices. With product quantization, the vector is split into subspaces and each subspace gets its own codebook, so the list has one index per subspace.
- Online: build a lookup table for the query. For each subspace, compute the dot product of the query's piece with every codeword in that subspace's codebook. The cost is proportional to the codebook size times the dimension, not to the number of datapoints.
- Online: score by table lookup. For each stored datapoint, add up the table entries its indices point to. That gives an approximate inner product. Check: the work per datapoint is one lookup per subspace, not one multiply per dimension.
- Output: the highest-scoring datapoints. Rank by the approximate scores and return the top results.
The tiny instance that makes the difference concrete: a unit-norm datapoint and two candidate codewords. Candidate A leaves an error of length 0.2 across the datapoint (orthogonal). Candidate B leaves an error of length 0.15 along it (parallel). Euclidean reconstruction prefers B, since . The anisotropic loss, with the paper's weight at in dimension 100, gives A the loss and B the loss , so it prefers A.
flowchart LR A[Datapoints and threshold T] --> B[Anisotropic codebook learning] B --> C[Encode each point as codeword indices] Q[Query] --> D[Lookup table of codeword dot products] C --> E[Sum table entries per datapoint] D --> E E --> F[Top matches]
The math
Let be the quantised version of , and let be a query drawn from a distribution . The score-aware quantization loss weights the squared error on the inner product by a function of the true score, . With a constant and isotropic queries, the loss reduces to ordinary reconstruction loss, which is why the standard methods optimise it. The choice counts only queries whose score is at least .
Step 2 in symbols: splitting the error into two parts
Write the residual as . Its component along is the parallel part , and the rest is the orthogonal part . The paper's Theorem 3.2 shows that for any weight and queries uniform on the sphere, the loss splits as , with weights that depend only on and .
Step 2 in symbols: the anisotropic ratio
For the threshold weight in high dimension , the ratio has the limit . Theorem 3.3 shows for any non-decreasing weight, so the parallel error is never penalised less than the orthogonal one. Up to a constant, the loss is .
Step 2 in symbols: the codebook update
Within a partition , the codeword update is the closed form . When the two weights are equal, this reduces to the ordinary weighted mean, which is the k-means update.
Step 4 in symbols: the score of a stored point
With product quantization, is split into pieces, each quantised by one of codebooks of entries. The approximate score is , where is the index chosen for piece . The table holds the values , computed once per query.
Worked examples
Let have norm 1 in dimension , with threshold , so . The value of follows from Theorem 3.4: , and multiplying by gives .
- Candidate A has orthogonal error 0.2 and parallel error 0. Loss: .
- Candidate B has parallel error 0.15 and orthogonal error 0. Loss: .
- Euclidean reconstruction loss: A gives and B gives , so reconstruction prefers B.
- Interpretation: the anisotropic loss prefers A, which errs across the datapoint, because B's error would change the scores of the queries most likely to rank at the top.
Take datapoints in dimension . Exact scoring costs multiply-adds. With product quantization using codebooks of entries, the query table costs multiply-adds once, and each datapoint costs lookups and additions.
- Exact work with and : multiply-adds.
- Table work with : , a negligible one-off cost.
- Lookup work with : lookups.
- Interpretation: the lookups replace multiply-adds by a quarter, so the scan is about four times less arithmetic. The values of , , and here are chosen for the arithmetic; the paper's experiments use their own settings.
The paper's main dataset is Glove with 1.2 million 100-dimensional word vectors, searched by cosine similarity. Because the datapoints are unit-normalised, the inner product and the cosine give the same ranking, so the method applies directly.
- Dimension and count: 100 dimensions and 1,200,000 vectors.
- Exact scoring of one query: multiply-adds.
- Threshold chosen by the authors: , giving , which they use for all later experiments.
- Interpretation: the anisotropic loss changes how codewords are chosen, not how queries are scored. The lookup step is the same as for a standard product quantizer with the same codebook sizes, so any gain comes from the codebook.
- Try it: with 25 codebooks, how many lookups does one query need across all 1.2 million vectors, and what fraction of the exact multiply-adds is that? Answer: lookups, which is , a quarter of the exact work.
Results
The experiments compare the losses, then whole systems, all on Glove with 1.2 million vectors unless noted.
- Same quantizer, different loss. With product quantization fixed, the score-aware loss gives significantly higher Recall 1@10 than reconstruction loss once is chosen reasonably, and the authors fix for the rest of the paper. It also gives lower relative error in estimating the top inner product at every bit rate tested. The paper points out that this matters for softmax approximations, where the inner products are used as logits.
- Against other quantizers at fixed bits. At 100 and 200 bits per datapoint, the anisotropic method has higher Recall 1@N than LSQ and the three QUIPS variants, for all values of N tested.
- End-to-end speed against recall. On the ANN-Benchmarks protocol with 11 baseline algorithms, run on a single CPU thread, the method is the fastest at high recall on Glove. The paper argues that this end-to-end timing captures overheads such as learned rotation and lookup-table construction, which fixed-bit comparisons leave out.
- Other data. The paper reports similar gains on an extreme-classification dataset, Amazon-670k, in its appendix, and applies the same loss change to binary quantization with a one-line modification.
Limits. The main claims rest on one dataset with cosine similarity, and the analysis assumes queries are uniform on the sphere when no query distribution is known. The threshold is a hand-chosen hyperparameter, and recall gains depend on choosing it reasonably. The speed comparison is on one CPU thread, and the implementation relies on SIMD distance computation. The conclusion is that anisotropic loss helps quantized MIPS, not that it dominates graph-based methods on every workload.
Reading guide
- Section 1, Introduction. Read with Maximum Inner Product Search and Recommender System open. The examples there are the reasons the search problem matters.
- Section 2, Background. Needs Dot Product, Vector Quantization and K-Means Clustering. The list of quantization families is a map of alternatives, not a set of prerequisites.
- Section 3, Problem formulation. Needs Expected Value and Orthogonal Projection. Theorem 3.2 is the key: it splits the loss into parallel and orthogonal parts, and Figure 1 shows why the split matters.
- Section 4, Application to quantization. Needs Product Quantization. Read the vector quantization update first; the product version reuses it with subspaces.
- Section 5, Experiments. Figures 3 and 4 are the evidence; check each caption for the bit rate or the metric before reading the lines.
- Appendix, Proofs. Only needed if you want the derivation of the splitting in Theorem 3.2.
Check yourself
QuestionRecall: what is the difference between reconstruction loss and the score-aware loss?Show answer
Reconstruction loss minimises the squared error on each stored vector, treating every query as equally likely. The score-aware loss weights the squared error on each query by a function of its true score, so errors on high-scoring pairs count more. For the threshold weight, that makes errors along the datapoint count more than errors across it.
QuestionWith and for a unit-norm datapoint, what is the anisotropic ratio ?Show answer
Use the limit . Multiplying by gives , the value the paper uses.
QuestionWhy does product quantization make the query-time work cheaper than exact scoring, even though each datapoint still needs one operation per subspace?Show answer
The per-subspace dot products with the codewords are computed once for the query, in a table of size proportional to the codebook size times the dimension. After that, each datapoint needs only one table lookup per subspace, which is far fewer operations than one multiply per dimension when the number of subspaces is much smaller than the dimension.
QuestionA quantizer with the anisotropic loss has slightly higher reconstruction error than a standard quantizer. Is that a failure?Show answer
Not by the paper's standard. Reconstruction error is not the target. What matters is the Recall 1@N of the top matches and the accuracy of the top inner-product estimates, and the paper reports that the anisotropic quantizer wins on those measures. A higher reconstruction error is expected when the codebook trades off error across the datapoint for error along it.
Sources
- Guo et al., "Accelerating Large-Scale Inference with Anisotropic Vector Quantization", 2019 (arXiv:1908.10396).
- ANN-Benchmarks, the public benchmark of approximate nearest neighbor search.
- Vector quantization, Wikipedia.