Article

Two Ways to Serve ColBERT: Re-ranking vs. End-to-End Retrieval

ยท 9 min read

1. Introduction

You type a question into a search bar. Behind it, a system has a fraction of a second to dig through millions of passages and put the best one on top. For years, that job belonged to keyword algorithms like BM25. Then BERT showed up and made search noticeably smarter. It also made it painfully slow.

ColBERT was built to fix that. And once it did, it opened up a second option. You can use ColBERT to re-rank results that something else found, or you can let it find the results itself. Same model, same scoring, two very different systems. Let's walk through both, and what you gain or give up with each.

2. The Speed Problem BERT Created

But first, it helps to see the problem ColBERT was built to solve. In 2019, BERT-based rankers set new state-of-the-art results on MS MARCO, a benchmark of 8.8 million web passages gathered from Bing results. On its main metric, MRR@10, which measures how high the right passage lands in the top 10, BERT beat the best earlier methods by almost 7 points.

The catch is how BERT ranks. It reads the query and a passage together, as one input, and outputs a single relevance score. Want to rank 1,000 candidate passages? That's 1,000 separate trips through BERT, for every single query. For BERT-base, that adds up to about 10,700 milliseconds per query, even on a Tesla V100 GPU. Nobody waits ten seconds for a search result.

ColBERT, introduced by Omar Khattab and Matei Zaharia at SIGIR 2020, does the same ranking job in 61 milliseconds. Its MRR@10 is 34.9, against 34.7 for the original BERT-base ranker. That's more than 170 times faster, with the same quality. So how does it pull that off?

3. How ColBERT Scores a Match

The trick is that ColBERT never lets the query and the document meet inside BERT. Each one goes through BERT on its own, and instead of a single score, BERT hands back one vector per token. Each vector is shrunk to 128 numbers and describes what that token means in its context. So a document becomes a stack of vectors, one per token, with punctuation thrown away.

Queries get one small extra step. They're always brought to 32 tokens: short ones are padded with BERT's [mask] token, and long ones are cut off. The vectors at those padded spots give the model room to learn extra query terms, and the authors found that removing this trick noticeably hurts accuracy. So every query becomes exactly 32 vectors.

Now, how do you score a query against a document when both are stacks of vectors? For each query vector, find the document vector it's most similar to. Then add up those best matches. That's it. The paper calls each best-match step a MaxSim, and the score is their sum:

S(q, d)  =  32 Σ i = 1 max j ∈ Ed  Eqi · EdjT

Notice there's nothing to train in this step. It's just multiplication, a max, and a sum.

And here's where the speed comes from. Because documents never need to see the query, every passage can be turned into vectors once, ahead of time, and saved to disk. For all 8.8 million MS MARCO passages, that takes about three hours on a single server with four GPUs. At search time, only the query goes through BERT.

4. A Tiny Example

Let's make that concrete. Say the query is "bats hunt at night." To keep things readable, we'll look at just three of its tokens. Here are two passages to score:

Passage A: Many bats use echolocation to catch insects after dark.
Passage B: The team ordered new bats for the night game.

For each query token, ColBERT finds its single best match in the passage and ignores everything else. Here's what that looks like for Passage A:

QueryPassage AManybatsuseecholocationtocatchinsectsafterdarkbats0.95hunt0.71night0.68Score = 0.95 + 0.71 + 0.68 = 2.34
Each query token keeps only its single best match in the passage, and the score is the sum of those matches. Similarity values are illustrative.

Now do the same for Passage B and compare. The similarity numbers are made up for illustration, but the scoring works exactly like this:

Query token Best match in A Score Best match in B Score
batsbats0.95bats0.58
huntcatch0.71ordered0.19
nightdark0.68night0.88
Total2.341.65

In this example, Passage A wins, even though it never uses the words "hunt" or "night." "Catch" and "after dark" carry similar meaning, and that's the kind of match contextual vectors are built to capture. Passage B shares two exact words with the query, but its "bats" are the baseball kind, and because each vector reflects the words around it, the two "bats" don't have to look alike. BM25, which only counts matching words, would rank Passage B higher.

Keep two things about MaxSim in mind, because the rest of this post hangs on them. It's cheap. And each query vector's best match can be found on its own. The first makes re-ranking fast. The second makes something far more ambitious possible.

5. The First Way: BM25 Picks, ColBERT Ranks

Let's start with the simpler of the two. The easiest way to use ColBERT is as a re-ranker. BM25 pulls the top 1,000 candidates as usual, and ColBERT re-scores every one of them. With only a thousand documents in play, there's no need to get clever. ColBERT just scores them all.

When a query comes in, ColBERT turns it into its 32 vectors. At the same time, it grabs the stored vectors of the 1,000 candidates, pads them to the same length, stacks them together, and moves them onto the GPU. Then it runs our tiny example for every candidate in bulk: each query vector is compared with every token of every passage, the best matches are summed, and each passage ends up with one score. Sort those scores, and you're done.

6. Why This Is So Much Cheaper

So why is this so much faster than plain BERT? It all comes down to how many times BERT has to run. A BERT re-ranker runs BERT 1,000 times per query, each time on the query and a passage glued together. ColBERT runs BERT once, on a 32-token query. Everything after that is simple math on vectors that were computed long before you typed anything.

That gap grows with the number of candidates, because ColBERT's single BERT run costs the same whether you score 10 passages or 2,000. At 10 passages, BERT-base needs nearly 180 times more compute than ColBERT. At 2,000, it needs 23,000 times more.

Here's the surprising part: the scoring is so cheap that it isn't even where most of the time goes. Of ColBERT's 61 milliseconds, encoding the query and computing every score take just 13. According to the paper, most of the rest is spent gathering the stored vectors and copying them onto the GPU. The bottleneck isn't the math. It's moving data around.

7. The Problem Re-ranking Can't Solve

So re-ranking is fast. But it has one big blind spot. ColBERT can only reorder what BM25 hands it. If the right passage isn't in BM25's top 1,000, ColBERT never even sees it, and that's most likely to happen when the passage uses different words from the query.

On MS MARCO, the BM25 top 1,000 that the paper re-ranks contains the right passage 81.4% of the time. So close to one in five relevant passages is already gone before ColBERT starts scoring, and no amount of clever ranking can bring them back.

The fix is to stop relying on BM25 for candidates. Which raises the obvious question: can ColBERT find its own?

8. The Second Way: Let ColBERT Search Everything

It can, thanks to that second property of MaxSim. Scoring all 8.8 million passages one by one would be far too slow. But for each query vector, MaxSim only needs the single most similar document vector. That's a nearest-neighbour search, and there are fast tools built for exactly that.

So ColBERT flips the question around. Instead of asking "how well does this document match the query?", it asks, for each query vector, "which document vectors anywhere in the collection are closest to this one?" The documents those vectors belong to become the candidates. This is what the authors mean when they call MaxSim pruning-friendly.

To make this work, ColBERT loads every document vector into FAISS, Facebook's library for fast vector search, and remembers which document each vector came from. Then every query runs in two stages. First, all 32 query vectors are sent to FAISS at once, and each comes back with its top 1,000 matching document vectors. Those hits are mapped back to their documents, and duplicates are removed. Second, ColBERT scores that shortlist exactly, the same way it does in re-ranking.

Re-rankingBM25top 1,000 docsColBERT MaxSimscores all 1,000Ranked listtop kEnd-to-endFAISS searchover all 8.8MColBERT MaxSimscores the shortlistRanked listtop k
The scorer is the same in both modes. What changes is where the candidates come from: BM25's top 1,000, or ColBERT's own vector search over all 8.8 million passages.

How big is that shortlist? At most this many hits:

32 query vectors  ×  1,000 hits each  =  32,000

Several hits can land in the same document, so the number of unique documents can be smaller. Either way, it's a tiny slice of 8.8 million. And unlike BM25's shortlist, it isn't built from shared keywords, so a passage like our Passage A can make it in.

9. How FAISS Searches So Fast

At this point you might be wondering: searching 32 vectors against the vectors of 8.8 million passages still sounds like a lot. How is that fast? FAISS handles it with an index called IVFPQ. The name sounds intimidating, but it's just two simple speed-ups stacked together.

The first, IVF, works like organizing a library into sections. All the document vectors are grouped into clusters, and when a query vector comes in, FAISS only searches the few clusters closest to it and skips the rest.

The second, PQ, is compression. Each 128-number vector is squeezed into just 16 bytes, and FAISS compares vectors directly in that compressed form, so every comparison is cheaper.

10. So Which One Wins?

So does all that extra searching actually pay off? Here are the paper's numbers for the same ColBERT model used both ways on MS MARCO:

Metric Re-ranking BM25's top 1,000 End-to-end over all 8.8M
MRR@1034.836.0
Recall@5075.3%82.9%
Recall@20080.5%92.3%
Recall@100081.4%96.8%
Index size143 GiB154 GiB

The recall numbers tell the story. 82.9% of the time, end-to-end retrieval has the right passage in its top 50. BM25 needs 1,000 results to reach 81.4%. Fifty beats a thousand.

Better recall improves the top of the list too. MRR@10 climbs from 34.8 to 36.0, because some passages that belong in the top 10 were never in BM25's top 1,000 to begin with.

The price is speed. In the paper's timings, end-to-end retrieval takes 458 milliseconds per query, while BM25 plus ColBERT re-ranking takes about 123 (62 for BM25, 61 for ColBERT). The two were measured on different machines, so treat the gap as rough. The end-to-end index is also a little bigger, at 154 GiB against 143 GiB.

11. Which One Should You Use?

So which one should you actually use? Fair question. It comes down to one thing: is your bottleneck speed or recall?

If you already run BM25 or Elasticsearch, your latency budget is tight, and your keyword results usually contain what users need, re-ranking is the easy win. You keep the retriever you have and put a far better scorer on top of it.

If missing a relevant passage is costly, end-to-end retrieval is worth the extra milliseconds. That's especially true for RAG pipelines, where a passage that's never retrieved can never make it into the answer, and for searches where queries and documents describe the same idea in different words.

MaxSim is cheap enough to re-rank a thousand candidates in milliseconds, and simple enough to split into per-token searches over the whole collection.

12. Conclusion

So what's the big takeaway? ColBERT didn't get fast by shrinking BERT. It got fast by changing when BERT runs. Documents are encoded once, ahead of time. Queries are encoded once, on the spot. And the part that ties them together is just a max and a sum.

That one design choice gives you two systems from one model: a much smarter scorer on top of your old retriever, or a scorer that goes out and finds its own candidates.

The next time a search system surfaces exactly the passage you needed, even though it doesn't share your words, that's the kind of match a stack of 128-number vectors and a simple max were built to find.

Source: Omar Khattab and Matei Zaharia, ColBERT: Efficient and Effective Passage Search via Contextualized Late Interaction over BERT, SIGIR 2020.