Reranking: Cross-Encoders for Precise Information Retrieval › Cross-Encoder Architecture

Michael BrenndoerferJanuary 30, 20269 min read
Language AI HandbookAll chapters
Part 44 of 67Retrieval-Augmented Generation
Chapter 343Reranking: Cross-Encoders for Precise Information Retrieval › Cross-Encoder Architecture
Reading section 3 of 12 · Cross-Encoder Architecture · 9 minSection 3 of 12 · 9 min

Book contents

Section 3 of 12 · 9 min read

Reranking: Cross-Encoders for Precise Information Retrieval › Cross-Encoder ArchitectureOne section · Sections in this chapter3 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Cross-encoders eliminate the independence assumption by processing query and document jointly. Rather than compressing each into a vector separately, they concatenate the texts and feed them through a transformer, letting every query token to attend directly to every document token. This joint processing enables the model to perform fine-grained comparisons that are impossible when representations are computed in isolation. When the query token "temperature" can directly attend to the document token "32°C," the model can verify the presence of specific information rather than merely estimating semantic overlap.

The architecture is conceptually simple but computationally powerful. You take a pre-trained transformer encoder, the same kind of BERT model we examined in BERT Architecture, and fine-tune it on relevance-labeled query-document pairs. The transformer's self-attention mechanism, applied across the concatenated input, creates a rich interaction matrix between all query and document tokens. Every query token can ask "Is this information present in the document?" and receive an answer informed by the full document context. Every document token can ask "Does this piece of information satisfy the query?" and receive an answer informed by the full query intent.

Input Representation

The standard cross-encoder format concatenates the query and document with special separator tokens:

input=[CLS]⊕query⊕[SEP]⊕document⊕[SEP]\text{input} = [\text{CLS}] \oplus \text{query} \oplus [\text{SEP}] \oplus \text{document} \oplus [\text{SEP}]

where:

  • [CLS][\text{CLS}]: classification token marking the start of the sequence (following BERT conventions from BERT Architecture)
  • query\text{query}: token sequence of the query
  • document\text{document}: token sequence of the document
  • [SEP][\text{SEP}]: separator token marking boundaries between segments
  • ⊕\oplus: token sequence concatenation operator

The entire sequence is processed through a transformer encoder, typically BERT or a similar masked language model. The use of special separator tokens is important because it allows the transformer to distinguish between query and document content. Without these explicit boundaries, the model would struggle to determine which part of the concatenated sequence represents your information need versus the potential source of that information. The [CLS] token, originally introduced for next-sentence prediction tasks in BERT, is an aggregate representation of the interaction between the two texts.

The segment embeddings that BERT uses for next-sentence prediction tasks serve a secondary role here. Segment A typically corresponds to the query, and segment B corresponds to the document. These segment embeddings provide the model with a structural prior about which text is the "question" and which is the "answer," even before the attention mechanism processes the content. In practice, models fine-tuned specifically for passage reranking often discard the segment embeddings or treat the entire concatenation as a single segment, finding that the [SEP] token alone is sufficient to signal the boundary.

Cross-Attention vs Self-Attention in Rerankers

Unlike the encoder-decoder cross-attention we explored in Cross-Attention, cross-encoders usually employ full bidirectional self-attention across the concatenated query-document sequence. Every token can attend to every other token, creating a dense interaction matrix of size (Lq+Ld)2(L_q + L_d)^2, where LqL_q and LdL_d are the query and document lengths respectively. This allows the model to capture fine-grained alignments, such as matching specific numerical values or entity relationships between query and document. While encoder-decoder architectures use unidirectional cross-attention from encoder outputs to decoder states, cross-encoders maintain bidirectional flow throughout, letting richer contextualization of every token based on the full context of both texts. The term "cross-encoder" refers to the fact that information crosses between the query and document during encoding, not that the architecture uses a separate cross-attention module.

Scoring Head

After processing through the transformer layers, the final hidden state of the [CLS] token, hCLS∈Rd\mathbf{h}_{\text{CLS}} \in \mathbb{R}^d, is the pooled representation of the query-document pair. This single vector condenses all the pairwise interactions between query and document tokens into a fixed-size summary. A scoring head, typically a simple feed-forward network or even a linear projection, maps this to a relevance score:

s(q,d)=wThCLS+bs(q, d) = \mathbf{w}^T \mathbf{h}_{\text{CLS}} + b

where:

  • hCLS∈Rd\mathbf{h}_{\text{CLS}} \in \mathbb{R}^d: the final hidden state of the [CLS] token, serving as the pooled representation
  • w∈Rd\mathbf{w} \in \mathbb{R}^d: weight vector of the scoring head
  • b∈Rb \in \mathbb{R}: bias term of the scoring head
  • s(q,d)s(q, d): the predicted relevance score (logit) for query-document pair (q,d)(q, d)

The [CLS] token is an interesting choice for pooling. During pre-training, BERT trained this token to predict whether two sentences were consecutive, which forced it to encode the relationship between the two input segments. Fine-tuning on relevance prediction adapts this representational capacity to encode query-document compatibility rather than sentence adjacency. Alternative pooling strategies, such as mean-pooling over all tokens or max-pooling over position-wise representations, are possible but empirically the [CLS] approach works well, especially when the model is initialized from a BERT checkpoint that already has strong [CLS]-based representations.

For classification-based training, the score is often passed through a sigmoid to produce a probability of relevance:

P(relevant∣q,d)=σ(s(q,d))=11+exp⁡(−s(q,d))P(\text{relevant} | q, d) = \sigma(s(q, d)) = \frac{1}{1 + \exp(-s(q, d))}

where:

  • s(q,d)s(q, d): the relevance score (logit) computed by the scoring head
  • σ(⋅)\sigma(\cdot): the sigmoid function, mapping logits to probabilities in (0,1)(0, 1)
  • P(relevant∣q,d)P(\text{relevant} | q, d): probability that document dd is relevant to query qq

The sigmoid transformation is necessary when you need calibrated probability estimates rather than ordinal rankings. For a well-calibrated reranker, P(relevant∣q,d)=0.8P(\text{relevant} | q, d) = 0.8 means that documents assigned this score are relevant 80% of the time. Calibration matters for downstream decisions like whether to include a document in the context window for generation, or whether to return "no relevant document found" rather than returning the top result by default.

Alternatively, some rerankers output logits for multiple relevance grades (e.g., not relevant, somewhat relevant, highly relevant) using a softmax over multiple output classes. This graded approach allows the model to express degrees of relevance rather than binary judgments, which can be particularly useful when training on data with multiple relevance levels, such as human judgments on a three-point or five-point scale. The choice between regression, binary classification, or ordinal classification depends on the nature of your training data and the specific requirements of your ranking task.

Computational Complexity

The richness of interaction comes at steep computational cost. While bi-encoders require O(Lq+Ld)O(L_q + L_d) transformer passes per query-document pair, reduced to O(Ld)O(L_d) if documents are pre-encoded, cross-encoders require O((Lq+Ld)2)O((L_q + L_d)^2) attention computations per pair.

Specifically, for a sequence of length L=Lq+Ld+special tokensL = L_q + L_d + \text{special tokens}, the self-attention mechanism computes:

Attention(Q,K,V)=softmax(QKTdk)V\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V

where:

  • Q∈RL×dkQ \in \mathbb{R}^{L \times d_k}: query matrix representing projected input tokens
  • K∈RL×dkK \in \mathbb{R}^{L \times d_k}: key matrix representing projected input tokens
  • V∈RL×dkV \in \mathbb{R}^{L \times d_k}: value matrix representing projected input tokens
  • LL: sequence length (total tokens in query + document + special tokens)
  • dkd_k: dimension of the key/query projections
  • softmax(⋅)\text{softmax}(\cdot): normalization function converting attention scores to probabilities

The matrix multiplication QKTQK^T alone costs O(L2dk)O(L^2 d_k). If your query is 10 tokens and your document is 512 tokens, a bi-encoder processes them separately (cost proportional to 102+5122≈262,00010^2 + 512^2 \approx 262,000), while a cross-encoder processes the concatenation (cost proportional to 5222≈272,000522^2 \approx 272,000). However, since bi-encoders pre-encode documents, the per-query cost is just O(102)O(10^2) for the query encoding plus O(1)O(1) for the dot product, making them thousands of times faster at query time.

This computational asymmetry fundamentally shapes system architecture. While the bi-encoder pays a high upfront cost to encode the corpus (which can be amortized over billions of queries), the cross-encoder must perform expensive computation for every query-document pair at query time. This means that if you attempt to rerank thousands of documents per query, latency becomes prohibitive, often reaching several seconds or even minutes. The quadratic scaling with sequence length also means that long documents dramatically increase computational cost, creating pressure to truncate texts aggressively, which risks losing relevant information located in later portions of the document.

Think of the computational cost as a restaurant analogy. A bi-encoder is like a restaurant that prepares all the dishes in advance and stores them in a display case. When a customer arrives, serving them is nearly instantaneous. A cross-encoder is like a restaurant that cooks every dish to order, personalized for each specific customer. The food is better, but every customer has to wait. The two-stage architecture is like a restaurant that prepares a large batch of semi-finished dishes in advance, then does a final personalized preparation for each customer from a shortlist. You get quality close to fully custom-cooked, at a speed close to the display case.

Out[3]:
Visualization
Log-scale line chart comparing bi-encoder and cross-encoder computational operations against document length in tokens. The bi-encoder line is flat, which reflects constant query-time cost with precomputed documents, while the cross-encoder line rises steeply, showing quadratic growth as document length increases.
Computational operations for bi-encoder versus cross-encoder scoring as document length increases. The bi-encoder maintains constant per-query cost because document embeddings are precomputed, while the cross-encoder cost grows quadratically with the total sequence length. At 512 document tokens, the plotted cross-encoder proxy requires roughly 2,700 times more operations per query than the bi-encoder proxy.