Search

Search papers and lessons

ColBERT: Efficient and Effective Passage Search

Omar Khattab, Matei Zaharia · 2020

ColBERT encodes queries and passages separately with BERT, keeps one vector per token, and sums each query token's best match, so BERT-quality ranking becomes fast enough to search whole collections.

On this page

  • Colour = topic mix
  • Discrete math
  • Neural network basics
  • Calculus
  • Linear algebra
  • Probability
  • Architectures
  • ReadReady to readNot yet

TL;DR

ColBERT is a way to search a large collection of text passages with a BERT-based model without paying BERT's full cost for every query. It encodes the query and each passage separately, keeps one vector per token of every passage, and scores a passage by matching each query token to its best passage token and adding those matches up. The passage vectors are computed once, ahead of time, so a query only needs a short pass through BERT plus a cheap scoring step. On the MS MARCO benchmark it ranks almost as well as the BERT re-rankers of its day, over 170 times faster per query in re-ranking, and it can search the whole collection instead of only re-ranking a short list.

Builds on: Late Interaction, BERT, Approximate Nearest Neighbor Search · Leads to: ColPali, which applies the same scoring to images of document pages

Why it mattered

By 2019, BERT-based rankers had the best scores on MS MARCO, the standard benchmark for passage search, but they were slow. The paper measured one BERT base re-ranker at 10,700 milliseconds per query on a single Tesla V100 GPU, to re-rank 1,000 candidates. ColBERT did the same job in 61 milliseconds with almost the same score. That gap is what made BERT-quality ranking practical for a search service that must answer in well under a second.

The idea lasted beyond the paper. ColPali (2024) uses the same late interaction scoring over vectors made from page images, so a retriever can search PDFs without first extracting their text. The paper also reports that Google and Bing had adapted BERT for their own search, which shows how much interest there was in putting BERT-quality ranking into production.

The history of the idea runs as follows. Neural ranking models such as KNRM and Duet were cheap but less accurate than what BERT later offered. BERT, released in 2018, made it possible to fine-tune one deep model for many language tasks, and in 2019 Nogueira and Cho applied it to passage re-ranking with large gains over earlier models. Those gains came with a heavy price per query, which is the problem ColBERT set out to fix. Document expansion methods such as doc2query and DeepCT moved expensive language-model work into the indexing step, but they still relied on word-based scoring. Hofstätter and colleagues published the Transformer-Kernel model at about the same time. The ColBERT paper, by Omar Khattab and Matei Zaharia, was published at SIGIR in 2020. It caught on because it kept the advance computation of bi-encoders and the token-level matching of BERT re-rankers in one scoring rule, and it could plug into vector search libraries.

The problem

Search has two jobs: find candidates quickly, then order them well. Classic candidate finders such as BM25 count words and weigh them by rarity. They are fast but miss passages that answer a question in different words. BERT fixes this by reading the query and the passage together, so each word can attend to every other word. The price is that every query-passage pair needs its own full BERT pass. The paper's table gives BERT base about 97 trillion floating point operations per query to re-rank 1,000 candidates.

Two existing designs sit at the ends of this trade-off:

  • A Bi-Encoder encodes the query and the passage separately, each into one vector. The passage vectors can be computed in advance, so search is a fast lookup. But one vector per passage has to summarize every word, so fine details are lost.
  • A Cross-Encoder reads the query and passage together. It is accurate, but it cannot be run ahead of time, and each candidate costs a full model pass.

The question ColBERT asks is whether a model can keep the fine matching of the second design while doing most of its work in advance, like the first. A second, practical question follows: can the same scoring run directly on a vector index, so that search does not depend on a candidate list from BM25 at all?

How it works

Here is the method as the paper runs it, in order: encode the passages offline, store them, encode the query, match, sum, rank. We follow one tiny example through the steps. The numbers are illustrative: each vector has two numbers and length 1, where the real model uses 128 numbers per vector. Each dot product below is a plain multiply-and-add.

  1. Encode a passage once, offline. Take passage X, which has three kept tokens after punctuation is removed. BERT reads the passage, a linear layer turns each token into a short vector, and each vector is scaled to length 1. The result is three vectors: x1=(1,0)x_1 = (1, 0), x2=(0,1)x_2 = (0, 1) and x3=(0.6,0.8)x_3 = (0.6, 0.8). Check: 0.62+0.82=10.6^2 + 0.8^2 = 1, so x3x_3 has length 1.
  2. Store the vectors. Keep all of them on disk, 16 or 32 bits per number. Check: passage X now costs three vectors, not one. Storage grows with the number of tokens.
  3. Encode the query. The query has two tokens. Its vectors are q1=(0.8,0.6)q_1 = (0.8, 0.6) and q2=(0,1)q_2 = (0, 1). The real encoder pads the query to 32 positions with [mask] tokens, and each position gets its own vector. Check: 0.82+0.62=10.8^2 + 0.6^2 = 1, so q1q_1 has length 1.
  4. Find the best match for each query vector. For q1q_1, the dot products with passage X are 0.80.8, 0.60.6 and 0.960.96. The best is 0.960.96, from x3x_3. For q2q_2, they are 00, 11 and 0.80.8. The best is 11, from x2x_2. Check: each query vector picks its own best passage vector, and the two picks can come from different tokens.
  5. Add the best matches. Passage X scores 0.96+1=1.960.96 + 1 = 1.96. Check: each query vector contributes exactly once.
  6. Score passage Y the same way. Passage Y has vectors y1=(0.6,0.8)y_1 = (0.6, 0.8) and y2=(0,−1)y_2 = (0, -1). For q1q_1 the dot products are 0.960.96 and −0.6-0.6, so the best is 0.960.96. For q2q_2 they are 0.80.8 and −1-1, so the best is 0.80.8. Passage Y scores 0.96+0.8=1.760.96 + 0.8 = 1.76. Check: Y's best match for q2q_2 is only partial, while X's is exact.
  7. Rank. X scores 1.96 and Y scores 1.76, so X comes first. Check: the ranking uses only these sums.
flowchart LR
  P["Passage tokens, encoded once offline"] --> PV["One vector per passage token, stored"]
  Q["Query tokens, encoded per query"] --> QV["One vector per query token"]
  PV --> M["Dot product of each query vector with each passage vector"]
  QV --> M
  M --> X["Keep the best passage vector for each query vector"]
  X --> S["Sum the best matches = relevance score"]

There are two ways to use these scores. In re-ranking, a candidate list from BM25 (the paper uses the top 1,000) is scored exhaustively, as in the example above. In end-to-end retrieval, step 4 is replaced by a lookup in a vector index over all passage vectors, followed by exact scoring of the candidates it returns. The math section explains the second route.

Worked examples

Example
1. One query vector, two passage vectors (tiny)

The query has one vector q=(0.6,0.8)q = (0.6, 0.8). The passage has two vectors, p1=(0.6,0.8)p_1 = (0.6, 0.8) and p2=(1,0)p_2 = (1, 0). Find the score.

  1. Dot product with p1p_1: 0.6×0.6+0.8×0.8=0.36+0.64=10.6 \times 0.6 + 0.8 \times 0.8 = 0.36 + 0.64 = 1.
  2. Dot product with p2p_2: 0.6×1+0.8×0=0.60.6 \times 1 + 0.8 \times 0 = 0.6.
  3. The maximum is 11, from p1p_1. There is only one query vector, so the sum has one term.
  4. Score: Sq,d=1S_{q,d} = 1.
  5. Interpretation: the query vector matches one passage vector exactly. The second passage vector is related but weaker, and it does not affect the score.
Example
2. Two query vectors, two passages (standard case)

The query has q1=(1,0)q_1 = (1, 0) and q2=(0,1)q_2 = (0, 1). Passage A has a1=(0.6,0.8)a_1 = (0.6, 0.8) and a2=(1,0)a_2 = (1, 0). Passage B has b1=(0,1)b_1 = (0, 1) and b2=(0.6,0.8)b_2 = (0.6, 0.8). Rank A and B.

  1. For A and q1q_1: the dot products are 0.60.6 and 11. The maximum is 11.
  2. For A and q2q_2: the dot products are 0.80.8 and 00. The maximum is 0.80.8.
  3. Score of A: 1+0.8=1.81 + 0.8 = 1.8.
  4. For B and q1q_1: the dot products are 00 and 0.60.6. The maximum is 0.60.6.
  5. For B and q2q_2: the dot products are 11 and 0.80.8. The maximum is 11.
  6. Score of B: 0.6+1=1.60.6 + 1 = 1.6. So A ranks first, with 1.81.8 against 1.61.6.
  7. Interpretation: A matches the first query vector exactly and the second partly. B matches the second exactly and the first only weakly. The score rewards the sum of each query vector's best match.
Example
3. Latency from the paper's numbers (realistic)

Table 1 of the paper reports 10,700 milliseconds for BERT base re-ranking and 61 milliseconds for ColBERT, both per query on one V100 GPU.

  1. Speed-up in latency: 10,700/61≈17510{,}700 / 61 \approx 175. The paper states it as "over 170 times".
  2. Latency per query for ColBERT: 61 milliseconds, so one GPU serving queries one at a time handles 1000/61≈16.41000 / 61 \approx 16.4 queries per second.
  3. Interpretation: the 61 milliseconds includes gathering the passage vectors from memory and moving them to the GPU. The paper notes that this transfer can be the most expensive step, and that query encoding and scoring take only 13 milliseconds of the total.
  4. Try it: a service re-ranks 1,000 candidates per query on one GPU, and each query has a budget of 2 seconds. Each ColBERT re-ranking takes 61 milliseconds. How many queries can one GPU answer, one after another, within 2 seconds?

Answer: 2000/61≈32.82000 / 61 \approx 32.8, so about 32 queries in the 2 seconds. Running several queries at once on the GPU would serve more, but the paper does not measure that.

The math

Definition: The ColBERT relevance score (MaxSim)

Let EqE_q be the set of query vectors and EdE_d the set of passage vectors, each of length 1. The relevance score of passage dd for query qq is

Sq,d=∑i=1∣Eq∣max⁡j  Eqi⋅Edj⊤,S_{q,d} = \sum_{i=1}^{|E_q|} \max_{j} \; E_{q_i} \cdot E_{d_j}^{\top},

where "⋅\cdot" is the dot product. When both vectors have length 1, the dot product equals the cosine similarity. The inner maximum is called MaxSim: it is the best match for one query vector. The outer sum adds one MaxSim per query vector.

Step 2 in symbols: the encoders

The passage encoder runs BERT on the passage with a [D] marker after [CLS], applies a linear layer WW that maps BERT's hidden size (768 for BERT base) down to m=128m = 128 numbers, normalizes each vector, and then drops the vectors of punctuation tokens:

Ed=Filter(Normalize(W⋅BERT([D] d1…dn))).E_d = \mathrm{Filter}\big(\mathrm{Normalize}(W \cdot \mathrm{BERT}(\texttt{[D]}\, d_1 \dots d_n))\big).

The query encoder puts a [Q] marker after [CLS], pads the query with [mask] tokens to a fixed length Nq=32N_q = 32, and keeps a vector for every position:

Eq=Normalize(W⋅BERT([Q] q1…ql [mask]…[mask])).E_q = \mathrm{Normalize}\big(W \cdot \mathrm{BERT}(\texttt{[Q]}\, q_1 \dots q_l\, \texttt{[mask]} \dots \texttt{[mask]})\big).

Normalize divides a vector by its length, v/∥v∥\mathbf{v} / \lVert \mathbf{v} \rVert. Padding with [mask] lets BERT write useful contextual vectors into positions that have no word of their own. The paper calls this query augmentation.

Step 4 in symbols: the best match

For one query vector EqiE_{q_i}, the maximum over jj takes ∣Ed∣|E_d| dot products, one per passage vector. A query with Nq=32N_q = 32 vectors against a passage with ∣Ed∣=100|E_d| = 100 vectors therefore needs 32×100=3,20032 \times 100 = 3{,}200 dot products, each of 128 numbers. That is cheap compared with running BERT again on every pair.

Step 5 in symbols: training

Training uses triples ⟨q,d+,d−⟩\langle q, d^{+}, d^{-} \rangle: a query, a relevant passage and a non-relevant passage. Each passage gets a score from the same formula, and the loss is the softmax cross-entropy over the two scores. A softmax turns the two scores into two probabilities that sum to 1, and the loss is the negative log of the probability given to the relevant passage:

L=−log⁡exp⁡(Sq,d+)exp⁡(Sq,d+)+exp⁡(Sq,d−).\mathcal{L} = -\log \frac{\exp(S_{q,d^{+}})}{\exp(S_{q,d^{+}}) + \exp(S_{q,d^{-}})}.

The paper fine-tunes BERT together with the linear layer and the [Q] and [D] marker embeddings, using Adam with a learning rate of 3×10−63 \times 10^{-6} and batches of 32. The MaxSim step itself has no trainable parameters.

End-to-end search in symbols

Let all passage vectors be stored in an index. For each query vector EqiE_{q_i}, the index returns the k′k' nearest passage vectors, so the candidate set is the union of the passages those vectors came from. The paper uses k′=k=1000k' = k = 1000. The candidate set KK is then rescored with the exact Sq,dS_{q,d}. For end-to-end retrieval the paper uses squared L2 distance between vectors, ∥u−v∥2\lVert u - v \rVert^2. For unit vectors this equals 2−2 u⋅v2 - 2\,u \cdot v, so ranking by smallest squared distance is the same as ranking by largest dot product.

The index is an IVFPQ index, short for inverted file with product quantization. K-means clustering splits the vector space into P=2,000P = 2{,}000 cells. Each query vector searches only its p=10p = 10 nearest cells. Each 128-number vector is split into s=16s = 16 sub-vectors, and each sub-vector is compressed to one byte with product quantization. Distances are then computed on the compressed codes, which is much faster but less exact. The exact rescoring step corrects for part of that loss.

Results

Re-ranking the official BM25 top 1,000 on MS MARCO (development set). MRR@10 is the mean over queries of 1/rank1/\text{rank} of the first relevant passage, counting only ranks up to 10:

MethodMRR@10 (Dev)Re-ranking latency (ms)FLOPs per query
BM25 (official)16.7not applicablenot applicable
Duet24.322159 billion
BERT base (Nogueira and Cho, 2019)34.710,70097 trillion
BERT base (the authors' training)36.010,70097 trillion
BERT large (Nogueira and Cho, 2019)36.532,900340 trillion
ColBERT (over BERT base)34.9617 billion

Recall@kk is the share of queries whose relevant passage appears among the top kk results. Recall@50 asks whether the right passage is in the first 50.

End-to-end retrieval from all 8.8 million passages:

MethodMRR@10 (Dev)Latency (ms)Recall@50Recall@1000
BM25 (Anserini)18.76259.285.7
docTTTTTquery27.78775.694.7
ColBERT, L2 (end-to-end)36.045882.996.8

The end-to-end score is higher than the re-ranking score (34.8 for the same model), which the paper attributes to better recall: passages that BM25 never put in its top 1,000 can now be found. The paper also reports that ColBERT's Recall@50 (82.9) is higher than the official BM25 Recall@1000 (81.4).

On TREC CAR, a Wikipedia-based dataset, BM25 with ColBERT reaches a MAP of 31.3, against 31.0 for BM25 with BERT base and 33.5 with BERT large, and 15.3 for plain BM25.

The ablations (Figure 5 of the paper) compare variants of the same model. The paper reports that using a single vector per query and passage is considerably less effective, that replacing the maximum with an average lowers accuracy, and that removing query augmentation also lowers MRR@10.

The paper's storage results (Table 4) show the cost of the index. With 128-dimensional vectors stored as 4-byte numbers, the collection needs 286 GiB. With 24-dimensional vectors stored as 2-byte numbers it needs 27 GiB, and MRR@10 drops from 34.9 to 33.9.

Tradeoffs and common mistakes

It is tempting to think ColBERT is just BM25 with better word weights. In fact, each vector is contextual: the vector for "bank" depends on the words around it, so "bank" near "river" and "bank" near "loan" get different vectors. A query vector can match a passage token that is not the same word, because the best match is judged by the vectors, not by spelling.

It is tempting to think storing one vector per token is a small cost. In fact the index grows with the number of tokens, which is why the storage numbers above matter in practice.

It is tempting to think the speed comes from a smaller model. In fact both encoders are BERT base. The speed comes from moving passage encoding offline and running BERT once on the query, so the per-candidate work is a set of dot products rather than a BERT pass.

When to use it. Use ColBERT-style scoring when the ranking must be accurate, the collection is too large for a cross-encoder to score every candidate, and there is memory for many vectors per passage.

When not to use it. If the index memory budget is tight, a single-vector Bi-Encoder is simpler and smaller. If the query is a few exact keywords, BM25 may already be enough. If only a short list must be ordered and accuracy matters most, a Cross-Encoder is the simplest accurate option.

ApproachWhat is computed ahead of timeWork per queryIndex size
BM25Word counts and lengthsLookup in an inverted indexSmall
Bi-encoderOne vector per passageOne query encoding and a vector searchOne vector per passage
Cross-encoderNothingOne BERT pass per candidateNone beyond the text
ColBERTOne vector per passage tokenOne query encoding, dot products, sumsOne vector per token

Limits. The benchmarks are English passage search tasks. MS MARCO gives one or very few relevant passages per query, so the scores measure finding those passages, not full relevance. The latency numbers depend on the hardware the authors used and include data transfer to the GPU. The authors say that quantization, shorter query padding, and keeping vectors on the GPU could make ColBERT faster, and they leave these to future work.

Reading guide

Read the paper in this order, with these prerequisites in hand:

Read next. ColPali applies the same scoring to page images. DPR uses one vector per passage, which is the design ColBERT improves on. Retrieval-augmented generation uses retrieved passages to answer questions.

Check yourself

QuestionIn ColBERT, which vectors are computed ahead of time, and which are computed when a query arrives?Show answer
Passage token vectors are computed ahead of time, after BERT, the linear layer and normalization, and they are stored. Query vectors are computed when the query arrives, and the dot products with the stored passage vectors are also computed at query time.
QuestionA query has 4 vectors and a passage has 5 vectors. How many dot products does the MaxSim step compute for that one passage?Show answer
It computes 4×5=204 \times 5 = 20 dot products, one for each pair of a query vector and a passage vector. The maximum for each query vector then comes from its 5 dot products.
QuestionWhy does ColBERT sum the maximum matches over the query vectors rather than over the passage vectors?Show answer
The score should measure how much of the query the passage covers, with each query vector counted once. If the sum ran over passage vectors, a long passage with many tokens would gain score just from its length, whether or not it matches the query.
QuestionA team wants to store ColBERT-style vectors for 100 million passages, and their memory is limited. Which setting from Table 4 would you start from, and what does it cost? Assume the storage grows linearly with the number of passages.Show answer
Start from 24-dimensional vectors stored as 2-byte numbers. On 8.8 million passages it needs 27 GiB, against 286 GiB for the 128-dimensional 4-byte setting. Scaling linearly to 100 million passages gives about 27×100/8.8≈30727 \times 100 / 8.8 \approx 307 GiB, which is an estimate and not a measured result. The cost is about one point of MRR@10 on the development set (33.9 against 34.9).

Sources

Read the full paper

Omar Khattab, Matei Zaharia, 2020. arXiv:2004.12832