Search

Search papers and lessons

Maximum Inner Product Search

Level 3 · 9 min read

Maximum inner product search finds the stored vector with the largest dot product with a query, which is nearest neighbour search in disguise once the vectors are scaled correctly.

On this page

  • Colour = topic mix
  • Linear algebra
  • Algorithms
  • Discrete math
  • Sign in to see your progress

TL;DR

Maximum inner product search (MIPS) takes a query vector q\mathbf{q} and a large collection of stored vectors x1,…,xN\mathbf{x}_1, \dots, \mathbf{x}_N, and returns the stored vector with the largest dot product with the query. It looks like nearest neighbour search, but it is not the same problem: a long vector can have a big dot product without being close to the query. Padding each stored vector with one extra coordinate turns MIPS into exact nearest neighbour search, so the fast indexes built for distance search can be reused.

Where you'll see it: in recommender systems, where a user vector is scored against millions of item vectors, and in the retrieval step of retrieval-augmented generation, where a question embedding is scored against a document index.

Intuition

Score, not distance

Imagine a shop that recommends products. Each product has a vector that describes it, and each user has a vector that describes their taste. A product's score for a user is how well the two line up, which the dot product measures. The best recommendation is the product with the highest score, not the product whose vector sits closest to the user's.

Distance and score are different things. Distance asks "how far apart are these points?" A score asks "how much do these point the same way, weighted by how big they are?" A product with a very large vector can have a high score even when it is far from the user, because the size of its vector pushes the score up.

flowchart LR
  A[Stored vectors x_i] --> B[Append sqrt of M squared minus norm squared]
  Q[Query q] --> C[Append 0]
  B --> D[Nearest neighbour index on augmented vectors]
  C --> D
  D --> E[Top result = largest dot product]

Search systems usually store vectors ahead of time and answer each query by scanning them. The scan has to rank by one number per stored vector, and that number should match what the model was trained to produce. If the model was trained with dot products, ranking by distance can return different results. MIPS is the name for doing the ranking by dot product directly and quickly.

The math

Definition: Maximum inner product search

Given a set X={x1,…,xN}⊂RdX = \{\mathbf{x}_1, \dots, \mathbf{x}_N\} \subset \mathbb{R}^d and a query q∈Rd\mathbf{q} \in \mathbb{R}^d, MIPS returns

arg⁡max⁡i  q⊤xi.\arg\max_{i} \; \mathbf{q}^\top \mathbf{x}_i.

The query is fixed at search time, while the stored vectors are fixed in advance and can be indexed.

Expand the squared distance between the query and a stored vector. The norm of each vector is ∥v∥\lVert \mathbf{v} \rVert, and ∥v∥2=v⊤v\lVert \mathbf{v} \rVert^2 = \mathbf{v}^\top \mathbf{v}:

∥q−xi∥2=∥q∥2−2 q⊤xi+∥xi∥2.\lVert \mathbf{q} - \mathbf{x}_i \rVert^2 = \lVert \mathbf{q} \rVert^2 - 2\, \mathbf{q}^\top \mathbf{x}_i + \lVert \mathbf{x}_i \rVert^2.

The first term is the same for every stored vector, so it does not affect the ranking. Minimising distance is therefore the same as maximising q⊤xi−12∥xi∥2\mathbf{q}^\top \mathbf{x}_i - \tfrac{1}{2}\lVert \mathbf{x}_i \rVert^2. If every stored vector has the same norm, the norm term is constant and the two problems agree. If the norms differ, they can disagree, and a plain nearest neighbour index returns the wrong answer for MIPS.

Let M=max⁡i∥xi∥M = \max_i \lVert \mathbf{x}_i \rVert. Append one coordinate to each stored vector and a zero to the query:

xi′=(xiM2−∥xi∥2),q′=(q0).\mathbf{x}_i' = \begin{pmatrix} \mathbf{x}_i \\ \sqrt{M^2 - \lVert \mathbf{x}_i \rVert^2} \end{pmatrix}, \qquad \mathbf{q}' = \begin{pmatrix} \mathbf{q} \\ 0 \end{pmatrix}.

Every augmented stored vector now has norm exactly MM. Compute the distance in the new space:

∥q′−xi′∥2=∥q∥2+M2−2 q⊤xi.\lVert \mathbf{q}' - \mathbf{x}_i' \rVert^2 = \lVert \mathbf{q} \rVert^2 + M^2 - 2\, \mathbf{q}^\top \mathbf{x}_i.

Only the last term depends on ii, so the nearest augmented vector is the one with the largest dot product. This transformation is exact. Any nearest neighbour index applied to the augmented vectors solves MIPS exactly.

The transformation is also a trick with a cost: the extra coordinate can make vectors with small norms harder to separate in high-dimensional indexes, and the query has to be handled differently from the stored vectors. Asymmetric hashing methods were built to avoid this cost.

Cost of the exact answer

Scanning every vector takes NN dot products of length dd, so about NdN d multiplications per query. For N=1,000,000N = 1{,}000{,}000 and d=768d = 768 that is about 7.7×1087.7 \times 10^8 multiplications for one query. Exact MIPS is therefore too slow for interactive search at that scale, and approximate methods are used instead.

Worked examples

Example
Same query, two different winners

Query q=(1,0)\mathbf{q} = (1, 0). Stored vectors x1=(3,0)\mathbf{x}_1 = (3, 0) and x2=(1,0.5)\mathbf{x}_2 = (1, 0.5).

  1. Dot products: q⊤x1=3\mathbf{q}^\top \mathbf{x}_1 = 3 and q⊤x2=1\mathbf{q}^\top \mathbf{x}_2 = 1.
  2. Maximum inner product winner: x1\mathbf{x}_1, with score 33.
  3. Distances: q−x1=(−2,0)\mathbf{q} - \mathbf{x}_1 = (-2, 0), length 22. q−x2=(0,−0.5)\mathbf{q} - \mathbf{x}_2 = (0, -0.5), length 0.50.5.
  4. Nearest neighbour winner: x2\mathbf{x}_2, at distance 0.50.5.

The two methods disagree. x1\mathbf{x}_1 is far away but long, and its large norm gives it the top score.

Example
Augmenting the vectors

Same vectors, with M=max⁡(∥x1∥,∥x2∥)=3M = \max(\lVert \mathbf{x}_1 \rVert, \lVert \mathbf{x}_2 \rVert) = 3.

  1. Norms: ∥x1∥2=9\lVert \mathbf{x}_1 \rVert^2 = 9 and ∥x2∥2=1.25\lVert \mathbf{x}_2 \rVert^2 = 1.25.
  2. Extra coordinates: for x1\mathbf{x}_1, 9−9=0\sqrt{9 - 9} = 0. For x2\mathbf{x}_2, 9−1.25=7.75≈2.784\sqrt{9 - 1.25} = \sqrt{7.75} \approx 2.784.
  3. Augmented vectors: x1′=(3,0,0)\mathbf{x}_1' = (3, 0, 0) and x2′=(1,0.5,2.784)\mathbf{x}_2' = (1, 0.5, 2.784). Both now have norm 33.
  4. Augmented query: q′=(1,0,0)\mathbf{q}' = (1, 0, 0).
  5. Squared distances: ∥q′−x1′∥2=(3−1)2=4\lVert \mathbf{q}' - \mathbf{x}_1' \rVert^2 = (3 - 1)^2 = 4. ∥q′−x2′∥2=0+0.25+7.75=8\lVert \mathbf{q}' - \mathbf{x}_2' \rVert^2 = 0 + 0.25 + 7.75 = 8.
  6. Check with the formula: ∥q∥2+M2−2 q⊤x1=1+9−6=4\lVert \mathbf{q} \rVert^2 + M^2 - 2\,\mathbf{q}^\top \mathbf{x}_1 = 1 + 9 - 6 = 4, and 1+9−2=81 + 9 - 2 = 8 for x2\mathbf{x}_2. Both match.
  7. Nearest augmented vector: x1′\mathbf{x}_1', which is the maximum inner product winner from the previous example.

Interpretation: a plain distance index built on the original vectors would return x2\mathbf{x}_2. The same index built on the augmented vectors returns x1\mathbf{x}_1, which is the correct MIPS answer.

Example
A realistic recommender

A catalogue of 10610^6 items with d=128d = 128 dimensions, and one user vector.

  1. Exact scan: 106×128=1.28×10810^6 \times 128 = 1.28 \times 10^8 multiplications per user.
  2. A serving system with 10410^4 users per second would need about 1.28×10121.28 \times 10^{12} multiplications per second for exact scans, far beyond one CPU core.
  3. An approximate index that examines about 1% of the items reduces the work to about 1.28×1061.28 \times 10^6 multiplications per user, a factor of 100.
  4. The index may miss the true best item some of the time. Measure that miss rate on held-out users before deploying, because the recall, not the speed, decides whether the recommendations are good.

Tradeoffs and failure modes

SituationProblemBetter choice
Few items, low dimensionIndex overhead is larger than the scanExact brute-force scan
Norms vary widelyDistance index ranks the wrong itemsAugmented-vector transform, then a graph or tree index
Vectors are unit lengthDot product equals cosine similarityPlain nearest neighbour search, no transform needed
Millions of high-dimensional vectorsTree indexes degrade toward a scanGraph indexes such as HNSW, or quantised indexes
Memory is tightFull-precision vectors do not fitProduct quantisation, which stores compressed codes
Scores must be exact at the topApproximate index misses the best itemRe-rank a short candidate list with exact dot products

A common failure is measuring speed without measuring recall. An index that is ten times faster but misses the best item one time in five can make search worse. Always report recall against exact MIPS on a sample of queries.

History

Dot-product scoring predates the term. Latent factor models in collaborative filtering, which scored users and items by the dot product of learned vectors, were widely used in the late 2000s. The problem of finding the top items quickly was studied as a separate question by the mid-2010s.

In 2014, Bachrach and colleagues at Microsoft showed the augmentation trick above for a recommender system, which made it possible to reuse Euclidean nearest neighbour indexes for dot-product scores. Shortly after, Shrivastava and Li proposed asymmetric locality-sensitive hashing for the same problem, and Neyshabur and Srebro analysed when symmetric hashing could be used. Graph indexes such as HNSW, published in 2016, became the standard approximate method for distance search and are now commonly applied to MIPS through the same transform.

The problem became urgent again around 2020, when dense retrievers such as the one in Karpukhin and colleagues' work scored passages with dot products over large indexes. Retrieval-augmented systems inherited the same need, and MIPS became a standard component of their serving stacks. The transformation caught on because it let production teams keep their existing nearest neighbour infrastructure.

Check yourself

QuestionRecall: in one sentence, how does MIPS differ from nearest neighbour search?Show answer

Nearest neighbour search ranks stored vectors by distance to the query, while MIPS ranks them by dot product with the query. The two agree only when all stored vectors have the same norm, because the norm term in the squared distance then does not change the order.

QuestionComputation: with q=(2,1)\mathbf{q} = (2, 1), the vector xa=(1,1)\mathbf{x}_a = (1, 1) and xb=(0,4)\mathbf{x}_b = (0, 4), which vector has the larger dot product with q\mathbf{q}, and which is closer in Euclidean distance?Show answer

Dot products: q⊤xa=2+1=3\mathbf{q}^\top \mathbf{x}_a = 2 + 1 = 3 and q⊤xb=0+4=4\mathbf{q}^\top \mathbf{x}_b = 0 + 4 = 4, so xb\mathbf{x}_b wins MIPS. Distances: q−xa=(1,0)\mathbf{q} - \mathbf{x}_a = (1, 0), length 11. q−xb=(2,−3)\mathbf{q} - \mathbf{x}_b = (2, -3), length 13≈3.6\sqrt{13} \approx 3.6. So xa\mathbf{x}_a is the nearest neighbour. The two methods disagree here because xb\mathbf{x}_b has the larger norm.

QuestionWhy: why does appending the coordinate M2−∥xi∥2\sqrt{M^2 - \lVert \mathbf{x}_i \rVert^2} make every stored vector have the same norm?Show answer

The squared norm of the augmented vector is ∥xi∥2+(M2−∥xi∥2)2=∥xi∥2+M2−∥xi∥2=M2\lVert \mathbf{x}_i \rVert^2 + \left(\sqrt{M^2 - \lVert \mathbf{x}_i \rVert^2}\right)^2 = \lVert \mathbf{x}_i \rVert^2 + M^2 - \lVert \mathbf{x}_i \rVert^2 = M^2. The norms are equal because the appended coordinate was chosen to fill the gap between each vector's norm and the largest norm MM.

QuestionApplied: a team uses a graph index on unit-length embeddings and sees good recall. They switch to unnormalised embeddings and recall drops. What is the likely cause, and what is the fix?Show answer

Unit-length vectors make the dot product equal to cosine similarity, so distance and score agree and a plain nearest neighbour index is correct. Unnormalised vectors bring back the norm term, so the index ranks by distance while the model was trained to rank by dot product. The fix is to apply the augmented-vector transform before indexing, or to re-rank the index's candidates by exact dot product.

Sources