Algorithm Search⌘K

Modeladvanced

Dual encoder retrieval model

Also called bi-encoder, two-tower model.

Encodes queries and items separately into one vector space, so item vectors are precomputed and retrieval is a nearest neighbor search.

A dual encoder scores a query qq and an item dd by s(q,d)=⟨EQ(q),ED(d)⟩s(q, d) = \langle E_Q(q), E_D(d) \rangle, where EQE_Q and EDE_D are two encoders into Rk\mathbb R^k that never see each other's input. Because ED(d)E_D(d) does not depend on the query, all item vectors are computed once, offline, and answering a query is one call of EQE_Q followed by a maximum inner product search over the stored vectors. The maximum-inner-product-search and HNSW pages are about that search; this page is about the scoring model that produces the vectors: what it costs, what scores it can and cannot express, and how it relates to the other neural ranking models. Dense passage retrieval is one way to train a dual encoder. The encoders themselves, and how good their vectors are, lie outside this model.

The machine

The model has three parts.

  • Two encoders. EQE_Q maps a query and EDE_D maps an item to a vector in Rk\mathbb R^k. They may share weights (a Siamese encoder) or not. Any function is allowed, but it sees only its own input.
  • A similarity. Usually the inner product ⟨u,v⟩\langle u, v \rangle; the cosine similarity is the inner product of the normalized vectors. Some systems divide by a temperature during training, which does not change the order of the scores.
  • An index. The NN item vectors are stored, and a query asks for the KK items of largest score. An exact search scans all NN vectors; an approximate nearest neighbor index such as HNSW or product quantization answers faster with some recall loss.

The model splits the work into an offline phase, encoding all items and building the index, and an online phase, encoding one query and searching.

What it counts

The model counts encoder calls, vector operations and stored numbers.

  • Offline: NN calls of EDE_D, and NkN k stored numbers, plus the index.
  • Per query: one call of EQE_Q, and the search, which costs O(Nk)O(N k) for an exact scan plus O(Nlog⁡K)O(N \log K) to keep the top KK, or less with an index.
  • Per new item: one call of EDE_D and one insertion into the index. Nothing else changes.

A cross-encoder, which reads the query and the item together, needs NN encoder calls per query for the same full ranking, since nothing can be computed before the query arrives. This gap, one call against NN, is what makes dual encoders the standard first stage of neural retrieval.

A worked case. Take EQ(q)=(1,0.5)E_Q(q) = (1, 0.5) and three items with ED(a)=(2,0)E_D(a) = (2, 0), ED(b)=(1,1)E_D(b) = (1, 1), ED(c)=(0,2)E_D(c) = (0, 2).

item inner product cosine
a 2.0 0.8944
b 1.5 0.9487
c 1.0 0.4472

By inner product the order is a, b, c; by cosine it is b, a, c. The norm of an item vector acts as a query-independent prior on the item, which the inner product keeps and the cosine removes. A system must train and search with the same similarity.

Relation to other models

  • Vector space model and BM25. Classic lexical retrieval is a dual encoder with hand-made sparse vectors: a query vector with one entry per query term and a document vector of term weights, and the inverted index is its search structure. BM25 is the inner product of a query vector of IDF weights and a document vector of saturated term frequencies.
  • SPLADE. A learned sparse dual encoder: both encoders output vectors over the vocabulary, most entries zero, scored by the inner product on an inverted index.
  • ColBERT. Keeps the separate encoders but outputs one vector per token and scores by a sum of maxima. This is late interaction: more expressive than one inner product, at the cost of storing one vector per token.
  • Cross-encoder. Reads query and item together and can use any interaction between them. It is more expressive and cannot precompute anything, so it is used to rerank a short list from a dual encoder.
  • Knowledge distillation. Dual encoders are often trained to match the scores of a cross-encoder, which links the two models in practice.

Known results in the model

Low rank. For mm queries and NN items, the matrix of scores S=QD⊤S = Q D^\top has rank at most kk. Conversely, any m×Nm \times N matrix of rank at most kk is the score matrix of some dual encoder of dimension kk on these queries and items: factor it as S=UV⊤S = U V^\top, for example by the singular value decomposition, and let the encoders output the rows of UU and VV. So the dimension kk bounds exactly which score tables a dual encoder can reproduce on a fixed set of inputs.

Few orders of the items. Fix NN item vectors in Rk\mathbb R^k. The order of the items for a query vector uu changes only when uu crosses one of the M=N(N−1)/2M = N(N - 1)/2 hyperplanes {u:⟨u,di−dj⟩=0}\{u : \langle u, d_i - d_j \rangle = 0\}, all through the origin. So the number of distinct orders is at most the number of regions of this arrangement. For k=1k = 1 there are at most 2 orders: the items sorted by their single coordinate, upward or downward. For k=2k = 2, the direction of uu moves on a circle, which the MM lines cut into at most 2M2M arcs, so there are at most N(N−1)N(N - 1) orders. For N=4N = 4 items that is at most 12 of the 24 orders, and 4 random points in the plane give exactly 12. For general kk, the standard count of regions of MM hyperplanes through the origin bounds the number of orders by 2∑i=0k−1(M−1i)2 \sum_{i=0}^{k-1} \binom{M - 1}{i}, which is polynomial in NN for fixed kk, while N!N! is not. A dual encoder of small dimension therefore cannot give every query its own arbitrary order of the items, whatever the encoders are. A query-dependent model such as a cross-encoder has no such limit.

Exact search is a reduction. Retrieval under the model is exactly maximum inner product search over NN fixed vectors; any guarantee of the search structure, exact or approximate, carries over to the retrieval step and no further, since the vectors may rank relevant items low.

Measured effectiveness. How well trained dual encoders retrieve relevant items is an empirical matter. For example, Karpukhin et al. (2020), Table 2, measured a top-20 retrieval accuracy of 78.4 for their dual encoder against 59.1 for BM25 on the Natural Questions test set.

References

  1. Po-Sen Huang, Xiaodong He, Jianfeng Gao, Li Deng, Alex Acero and Larry Heck, Learning deep structured semantic models for web search using clickthrough data, Proceedings of the 22nd ACM CIKM, 2013, pp. 2333-2338 (an early neural model that maps queries and documents separately to vectors and scores them by their similarity)
  2. Vladimir Karpukhin, Barlas Oguz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen and Wen-tau Yih, Dense passage retrieval for open-domain question answering, Proceedings of EMNLP 2020, pp. 6769-6781; Section 3.1 and Equation (1) (the inner product of a question vector and a passage vector), Section 5.4 (an index over precomputed passage vectors)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/FHA02A/text.yaml