Notes

Bi-Encoder

Searching a million documents with a transformer is only possible if you do not have to run the transformer a million times per query. A bi-encoder is the design that makes that true: encode each piece of text independently into a single vector, then compare vectors instead of texts.

How it is built
Two towers — usually the same encoder with shared weights — map a query and a passage separately to fixed-length embeddings, and relevance is their dot product or cosine similarity. Training pairs things that belong together against negatives that do not, under a contrastive loss that pulls matched pairs close and pushes the rest apart; hard negatives mined from a first-pass retriever matter more than almost any other design choice. Sentence-BERT and dense passage retrieval are the reference implementations.

Why the independence is the whole point
Because a passage's vector does not depend on the query, the corpus can be encoded once, stored in a vector index such as FAISS, and searched in milliseconds by approximate nearest neighbour. That is what dense retrieval, semantic search and retrieval-augmented pipelines actually run on.

What it gives up
A cross-encoder reads query and document together and can attend from one into the other, so it ranks more accurately — at the cost of a forward pass per candidate, which rules it out at corpus scale. The standard arrangement uses both: the bi-encoder retrieves a few hundred candidates, the cross-encoder re-ranks the top of that list. Compressing a document into one vector also loses detail, which is what multi-vector approaches such as ColBERT recover.

A bi-encoder embeds two pieces of text independently with the same encoder and compares the resulting vectors by a cheap operation such as cosine similarity. Because passage vectors are computed once and indexed in advance, a query is matched against millions of documents in milliseconds, which is what makes dense retrieval and semantic search practical. The independence that buys that speed costs accuracy: neither text conditions the other, so a cross-encoder reranks the shortlist.

Picture a library where every book already carries a short tag describing what it is about. You walk in and describe what you want, the librarian turns your request into a tag of the same kind, and then looks for the closest match on the shelf. Nobody has to open a single book.

A bi-encoder works like that. It boils each piece of text down to a list of numbers on its own, ahead of time, so searching becomes comparing numbers instead of reading. That is how a search box can sift millions of documents the instant you press enter. The trade-off is that your question and the document were summarised separately, so fine shades of meaning can slip past.