Skip to content

Contextual Document Embeddings (CDE)

optionalcorefundamentalpaperrubric 11/122024 · arXiv (Cornell) · ~35 min

John X. Morris, Alexander M. Rush

Status

TL;DR. Standard bi-encoders embed every document “out of context”: the same text gets the same vector whether it sits in a biomedical corpus or a legal one. CDE makes the embedding corpus-aware in two ways:

  1. Contextual batching. Training batches are built from clusters of neighbouring documents, so in-batch negatives are hard.
  2. A two-stage architecture. The first stage embeds a sample of corpus documents. The second stage encodes the target document (or query) with those embeddings as extra input.

CDE reaches the MTEB frontier among models under 250M parameters. Its best checkpoint uses one hard negative per query; the paper also demonstrates size-class state-of-the-art performance without hard negatives. The abstract’s “state-of-the-art” claim is this size-class comparison, not the unrestricted leaderboard.

Why it matters now. For an IR person this is learned IDF. BM25 adapts to a corpus through document frequencies, and dense retrievers usually don’t. CDE is the cleanest neural answer, and it explains why dense models lose out-of-domain on BEIR-style evaluations.

Four ways to give a chunk embedding its context Four panels. A: each chunk is encoded alone, so context is lost. B: an LLM writes a short context note that is prepended to the chunk before encoding. C: late chunking encodes the whole document once and pools token vectors per chunk span. D: corpus-conditioned embeddings first embed sampled neighbour documents and feed them to a second-stage encoder. A · Naive chunking (baseline) chunk 1 chunk 2 chunk 3 encoder ×3, separately v₁ v₂ v₃ "Its revenue grew 3%" — whose? when? The chunk no longer carries the entity or date. B · LLM-written context prefix whole doc + chunk LLM context note chunk text encoder + BM25 Index-time cost: one LLM call per chunk (prompt caching keeps the document cheap). C · Late chunking (pool after encoding) whole document → one forward pass long-context encoder: token vectors pool → v₁ pool → v₂ pool → v₃ Every token has attended to the whole document. Training add-ons: InSeNT, relevance distillation. D · Corpus-conditioned (CDE) sampled corpus docs stage 1 embed stage 2 doc + context this document Context = the corpus, not the parent document. Adapts term weighting to a new domain.
Panel D is CDE. Context comes from the corpus, not the parent document.

Table 2 of the paper, full MTEB (original English), comparing models with 250M or fewer parameters. “Random” feeds the second stage random training documents instead of documents from the target corpus.

ModelRetrievalMean (all tasks)
nomic-embed-v152.862.39
bge-base-en-v1.553.363.56
gte-base-en-v1.554.164.11
cde-small-v1, random context51.163.81
cde-small-v1, contextual53.365.00

The 0.89-point lead over gte-base comes mostly from non-retrieval tasks; on retrieval itself cde-small-v1 (53.3) is below gte-base (54.1). Swapping in random context documents costs about 1.2 points on the mean and 2.2 on retrieval, which is the direct evidence that corpus context, not just the training recipe, carries the gain. No seed variance is reported.

What the conditioning costs at serving time

Section titled “What the conditioning costs at serving time”
  • Query time. Scores are ϕ(d,D)⋅ψ(q,D)\phi(d,D)\cdot\psi(q,D): the query must be encoded with the same corpus context DD as the documents, or the two sides stop aligning. With 512 context vectors, a 20-token query becomes about 532 positions, roughly 27× the tokens and far more attention work. The serving penalty is mainly query latency, not index size.
  • What can be cached. Only the first-stage context vectors. Second-stage states cannot be reused in general, because context and text positions attend to each other in every layer.
  • Re-indexing. Changing the context sample (corpus drift, a new tenant, a new domain) moves every document embedding, so you either re-index the whole corpus or accept a stale context.
  • You need a representative corpus sample at index time. Streaming or very heterogeneous corpora make this ill-defined.
  • It is orthogonal to chunk-level context: CDE does nothing for a chunk that lost its antecedent.
  • The gap to the best large models has shrunk as LLM-based embedders grew. The idea, not the checkpoint, is the takeaway.
  • Before: contrastive dense retrieval (DPR, E5), hard-negative mining, BEIR’s out-of-domain gap.
  • Same name, different context: Late Chunking and ConTEB use the parent document as context.
  • Open problem: combining corpus context with document context. See the overview.

Questions to think through

These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.

  1. Derive / compute

    The full CDE model uses 512 first-stage context embeddings and a 512-token text limit, so its second stage sees up to 1,024 positions. For a corpus of 1M documents of 400 tokens and queries of 20 tokens, estimate the encoding cost relative to a plain bi-encoder of the same size (count positions and the quadratic attention term), at indexing time and per query. What can be cached and what cannot?

    What a strong answer covers
    • First stage: 512 document encodings once per corpus, negligible next to 1M documents.
    • Indexing: 912 instead of 400 positions per document, about 2.3× the tokens and about 5.2× the attention term (912² vs 400²).
    • Query: 532 instead of 20 positions, about 27× the tokens and about 700× the attention term. The serving penalty is mainly query latency, not index cost.
    • Cacheable: the 512 first-stage vectors, which the paper caches. Not cacheable in general: second-stage states, because context and text positions attend to each other in every layer.
    • Storage is unchanged: one vector of the usual size per document.
  2. Mechanism

    Explain how conditioning on embeddings of other corpus documents can act as a learned IDF. Using the paper's NFL example, say why a fixed bi-encoder cannot weight "NFL" correctly for both a general web corpus and a sports corpus, and why the query must be encoded with the same corpus context as the documents.

    What a strong answer covers
    • A fixed encoder must commit to one global weighting learned on the training distribution, where "NFL" is rare and therefore informative.
    • The first-stage vectors summarise what is common in the target corpus. The second stage can down-weight features many context documents share and keep distinctive ones, as IDF does for terms.
    • Scores are dot products ϕ(d, D)·ψ(q, D). Re-weighting only documents would leave the query emphasising a feature that is now uninformative, and the two sides would no longer align.
    • Context positions carry no position information, so the conditioning is a function of the set of sampled documents, which matches an unordered corpus.
    • Sequence dropout (p = 0.005) with a null token lets the same model run as a plain bi-encoder when no corpus is available.
  3. What if…

    For contextual batching, the paper reports that without false-negative filtering larger batches and larger clusters help, but with filtering smaller (harder) clusters are clearly better and larger batches add little. Explain this interaction, and predict what small clusters without filtering would do on a dataset like MS MARCO.

    What a strong answer covers
    • Small clusters fill a batch with near neighbours, which makes in-batch negatives hard and the contrastive loss a better approximation of the full softmax.
    • Near neighbours are also often unlabelled positives. The paper cites Qu et al.: over 70% of top-retrieved MS MARCO passages are false negatives.
    • Without filtering, small clusters therefore inject many false negatives and teach the model to push relevant documents apart, so larger, easier batches look better.
    • Filtering drops negatives that a surrogate model scores above the positive plus a margin, which unlocks the benefit of hard clusters. Prediction (not directly reported): small unfiltered clusters on MS MARCO could underperform random batching.
    • Once batches are hard, extra easy negatives add little gradient, so batch size matters less.
  4. Critique

    CDE assumes a representative sample of the target corpus at index time. Identify where that breaks (tiny corpora, heterogeneous or multi-tenant corpora, streaming corpora), use the paper's evidence on random and cross-domain context documents to estimate how badly, and propose the experiment that would expose the failure.

    What a strong answer covers
    • Random training documents instead of in-domain context cost about 1.2 points on average across MTEB tasks, and STS is nearly context-agnostic. The model degrades gracefully rather than failing.
    • Cross-domain context on CQADupstack: in-domain context is generally best, with some crossover. A mixed corpus gets an averaged context that fits no subdomain.
    • Tiny corpora: few documents give a noisy estimate of corpus statistics. The paper shows the model works with partial context but is best with a full context window.
    • Streaming: when the corpus drifts the context should change, but changing it moves every embedding, forcing a full re-index or a stale context.
    • Experiment: vary the domain mixture of the context sample and of the corpus independently, and simulate drift over time; measure nDCG and how far a fixed document's embedding moves.
  5. Design

    You serve 3,000 enterprise tenants whose corpora range from 500 to 5M documents in very different domains. Would you deploy CDE with one context sample per tenant, one global context, or a plain bi-encoder? Address re-indexing, query latency, and how you would decide from data.

    What a strong answer covers
    • Per-tenant context gives the domain adaptation CDE is built for; the paper's largest architecture gains were on small, out-of-domain sets (ArguAna, SciFact).
    • Each tenant's context defines its own embedding space, and queries must use the same context. That is fine because search is tenant-scoped.
    • Changing a tenant's context sample means re-embedding that tenant's corpus, so freeze the sample per index version and refresh it on a schedule.
    • Every query carries 512 extra positions; check latency at peak load, or use fewer context documents where the budget is tight.
    • Decide with offline sets or A/B tests per tenant segment, comparing CDE and the bi-encoder by corpus size and by distance from the training domains.
  6. Connect

    The paper says the query side of CDE can be seen as a form of pseudo-relevance feedback. State precisely how CDE's conditioning differs from Rocchio-style PRF and from BM25's IDF, and describe the variant of CDE that would behave like dense PRF.

    What a strong answer covers
    • PRF is query-dependent: it rewrites the query from the top-k documents retrieved for that query, at query time.
    • CDE context is query-independent: one corpus sample shared by all queries and documents, closer to a corpus statistic like IDF.
    • IDF is a closed-form per-term statistic; CDE learns what to extract from the sample and applies it to both queries and documents.
    • Dense-PRF variant: choose context documents per query from a first-round top-k and re-encode only the query. It needs a second retrieval round, and documents indexed under the corpus context would be scored against differently conditioned queries.

Done with this page?

Status