AI Infra
0%
Part VI · Chapter 45

Embeddings and Representation Learning

AuthorChangkun Ou
Reading time~20 min

An embedding is a versioned scoring interface. It turns text, its role, and an optional task instruction into a fixed-length vector. A retrieval system can then compare one query vector with millions of stored document vectors without running a language model over every pair. The useful promise is narrow: text pairs judged relevant by the training process should receive higher scores than irrelevant pairs. Nearness does not by itself mean that two texts are synonyms, factually correct, authorized for the same user, or useful for every task.

Chapter Chapter 44 treated dense retrieval as one candidate-generation channel. This chapter opens that channel. The central engineering problem is not to discover a universal geometry of meaning. It is to define one ranking contract, learn it from imperfect pairs, preserve it through indexing, and know when a model or index change has broken it.

2026-06-23T18:50:32.729209 image/svg+xml Matplotlib v3.11.0, https://matplotlib.org/ 0.0 0.2 0.4 0.6 0.8 1.0 embedding dimension 1 0.0 0.2 0.4 0.6 0.8 1.0 embedding dimension 2 cat kitten invoice receipt hard negative
Figure 45.1. Schematic embedding plane showing clusters, near neighbors, and a hard negative. Representation learning is about shaping distances so retrieval and transfer use the right geometry. Idealized positions, not measured embeddings.

Start with the scoring contract

An embedding has meaning only together with the transformation that produced it. Two arrays of 768 floating-point numbers are not necessarily comparable. They may come from different tokenizers, pooling rules, task prompts, model checkpoints, or normalization conventions. Record those choices as data:

EmbeddingSpec {
  model_id, model_revision, tokenizer_revision
  query_role, document_role, instruction_version
  pooling, source_layer, max_tokens, truncation_policy
  dimension, normalization, similarity, dtype, quantization
}

EmbeddingRecord {
  tenant, document_id, chunk_id, source_version, content_hash
  embedding_spec_hash, vector, acl_version, index_generation
}

The spec is part of the index schema. A query must use a compatible query-side spec, and every stored vector must use the corresponding document-side spec. For an asymmetric retrieval model, those two roles may add different prefixes or use different encoder weights. Omitting a required query instruction is not a harmless formatting change; it asks the model to apply a different scoring function.

Let EqE_q denote the query encoder and EdE_d the document encoder. They may share weights, but they need not. For query text xx and document text yy, define

u=Eq(x),v=Ed(y),s(x,y)=uTv.u = E_q(x), \qquad v = E_d(y), \qquad s(x,y) = u^\mathsf{T}v.

Here u,vRdu,v\in\mathbb{R}^{d} are dd-dimensional vectors, uTvu^\mathsf{T}v is their inner product, and s(x,y)s(x,y) is the score used for ranking. If the contract applies 2\ell_2 normalization,

u^=uu2,v^=vv2,u^Tv^=112u^v^22.\widehat{u}=\frac{u}{\lVert u\rVert_2}, \qquad \widehat{v}=\frac{v}{\lVert v\rVert_2}, \qquad \widehat{u}^{\mathsf{T}}\widehat{v} = 1-\frac{1}{2}\lVert\widehat{u}-\widehat{v}\rVert_2^2.

The hats denote unit-length vectors and 2\lVert\cdot\rVert_2 is Euclidean norm. On unit vectors, cosine similarity, inner product, and squared Euclidean distance induce the same ranking, with distance ordered in the opposite direction. Without normalization, vector length affects the inner product. Normalization must therefore match training and indexing; it is not a generic post-processing improvement. Dense Passage Retrieval, for example, trained and searched with an unnormalized inner product (Karpukhin et al. 2020).

Pooling decides what one vector contains

A transformer produces one hidden state per input token. Retrieval usually needs one vector per query or chunk, so a pooling rule must compress those token states. For masked mean pooling,

z(x)=t=1Tathtt=1Tat.z(x)= \frac{\sum_{t=1}^{T} a_t h_t} {\sum_{t=1}^{T} a_t}.

Here xx contains TT token positions, htRdh_t\in\mathbb{R}^{d} is the hidden state at position tt, and at{0,1}a_t\in\{0,1\} excludes padding or any other token the contract says not to pool. A special-token pooler instead chooses one hth_t; max pooling takes a coordinate-wise maximum; learned attention assigns data-dependent weights. The chosen layer, inclusion of special tokens, padding mask, and normalization after pooling all affect the result.

Sentence-BERT showed why this extra training and pooling interface matters. Its siamese encoder produced sentence vectors that could be compared directly, avoiding pairwise BERT inference for semantic search. Mean pooling was its default and worked well in its reported ablations, but that experiment does not make mean pooling universal (Reimers and Gurevych 2019). Decoder-based embedders may use the last token, bidirectional attention, or a learned pooler. Those are separate designs, not stages in a single historical progression.

Unadapted language-model states are not a retrieval metric by default. The pretraining objective supervises token prediction, not the ordering of pooled query-document scores. Early contextual models also exhibited anisotropy: randomly sampled token representations often had a positive mean cosine rather than spreading evenly around the origin (Ethayarajh 2019). That result concerned contextual token occurrences in particular BERT, ELMo, and GPT-2 checkpoints. It is a geometric warning, not proof that anisotropy causes every pooling failure or that an isotropic space must retrieve well. Later analyses found that correcting anisotropy alone does not necessarily restore semantic isometry (Fuster Baggetto and Fresno 2022). The operational test is still ranking quality on the intended query and corpus distribution.

Contrastive learning shapes the ranking

Contrastive training makes the scoring contract explicit. Suppose a batch has BB labeled pairs (xi,yi)(x_i,y_i), where yiy_i is relevant to query xix_i. Let ui=Eq(xi)u_i=E_q(x_i) and vj=Ed(yj)v_j=E_d(y_j). A common in-batch loss for query ii is

Li=logexp ⁣(s(ui,vi)/τ)j=1Bexp ⁣(s(ui,vj)/τ).\mathcal{L}_i = -\log \frac{\exp\!\left(s(u_i,v_i)/\tau\right)} {\sum_{j=1}^{B}\exp\!\left(s(u_i,v_j)/\tau\right)}.

Here ss is the contracted similarity function, viv_i is the labeled positive, the other vjv_j are candidate negatives for query ii, and τ>0\tau>0 is the temperature. The batch loss is B1i=1BLiB^{-1}\sum_{i=1}^{B}\mathcal{L}_i. The softmax compares the positive with the particular negative set presented during training. It does not compare with every document the model will meet in production.

InfoNCE was introduced in contrastive predictive coding (Oord et al. 2018). Under its specific sampling construction, one candidate comes from the conditional distribution p(dq)p(d\mid q) and the others are independent draws from the marginal p(d)p(d). The expected objective then gives a mutual-information lower bound whose ceiling grows as logB\log B. With an unrestricted scorer, the optimal logit differs by a query-only constant from log ⁣[p(dq)/p(d)]\log\!\left[p(d\mid q)/p(d)\right]. If negatives instead come from a mining proposal ν(d)\nu(d), that ratio changes to p(dq)/ν(d)p(d\mid q)/\nu(d); query-dependent hard mining may invalidate the standard mutual-information interpretation entirely. More candidates raise the bound's ceiling, but they do not guarantee a tighter bound or a better retriever (Poole et al. 2019).

The geometric view is often more useful in practice. On normalized vectors, contrastive training rewards alignment of labeled positive pairs and uniformity of the overall representation distribution on the sphere. Wang and Isola proved an asymptotic relationship under a symmetric sampling model and showed these quantities were useful diagnostics on their evaluated vision and language tasks (Wang and Isola 2020). Query-document retrieval can be asymmetric, so this is a diagnostic analogy rather than a theorem about every dual encoder. SimCSE demonstrated the mechanism for sentence similarity: dropout created two views of one sentence in its unsupervised setting, while entailment and contradiction pairs supplied positives and hard negatives in its supervised setting (Gao et al. 2021).

Temperature controls how the model distributes gradient across candidates. A high-scoring negative receives more softmax mass and therefore more pressure. Lowering τ\tau concentrates relative weight on the highest-scoring negatives, but it also amplifies mislabeled negatives and can destroy useful local neighborhoods. There is no task-independent best temperature. Tune it together with normalization, batch composition, duplicate masking, and the negative miner; a numerical value copied from another model has no stable meaning when the score scale changes.

Interaction placement fixes the cost envelope

The moment at which query and document tokens can interact determines what can be precomputed.

Architecture Pair score What can be indexed Main cost
Dual encoder one-vector inner product or cosine one vector per document loses token-level cross-attention
Late interaction token-vector aggregation document token vectors larger index and more online scoring
Cross-encoder joint transformer over the pair no query-independent document score one model pass per candidate pair

A dual encoder is the usual first-stage dense retriever because the corpus side is independent of the query. A cross-encoder is more expressive because it reads the pair jointly, but that expressiveness does not guarantee the best result on every workload. Its common production role is reranking a bounded candidate set, where the number of pairwise passes is affordable.

ColBERT occupies the middle. It encodes query and document tokens independently and applies late interaction through MaxSim:

SLI(q,d)=i=1Tqmax1jTdq^iTd^j.S_{\mathrm{LI}}(q,d) =\sum_{i=1}^{T_q} \max_{1\le j\le T_d} \widehat{q}_i^\mathsf{T}\widehat{d}_j.

Here TqT_q and TdT_d are the query and document token counts, and q^i,d^jRd\widehat{q}_i,\widehat{d}_j\in\mathbb{R}^{d} are normalized token vectors. For each query token, MaxSim keeps the best document-token match and then sums those matches. Document vectors remain precomputable, but the index stores many vectors per document. ColBERTv2 reduced the footprint of an uncompressed late-interaction index by six to ten times in its experiments through residual compression and denoised supervision; that comparison was not against a single-vector index (Santhanam et al. 2022).

query query + role qenc query transform query->qenc source chunk + source version denc document transform source->denc qvec query vector qenc->qvec index versioned vector index denc->index score compatible scorer qvec->score index->score ranked ranked candidates score->ranked
Figure 45.2. The same text can produce incompatible vectors under different roles or embedding specifications. Only a matching query and index generation may meet at the scorer.

Training data defines relevance

The loss cannot decide what “related” should mean. The positive-pair policy does that. A search click, a question with an answer-bearing passage, two duplicate records, a translation pair, and two sentences with similar meaning express different relations. Combining them without task labels asks one vector space to satisfy incompatible orderings. Every training row therefore needs the query and document roles, task, languages, source and license, source version, label origin and confidence, deduplication group, split assignment, and any teacher or miner revision.

In-batch negatives reuse the other B1B-1 documents in the matrix above. Dense Passage Retrieval used this efficiently and added BM25-mined candidates in its open-domain QA experiments (Karpukhin et al. 2020). Reuse is not free of semantics: two batch rows may share a relevant document, duplicate one another, or contain different valid answers to the same query. Known positives and duplicate groups must be masked before the loss treats off-diagonal cells as negatives. Larger batches add compute, communication, and false-negative exposure as well as more candidates.

Once random negatives become easy, a frozen checkpoint can retrieve high-scoring unjudged documents from the corpus. ANCE refreshed such an index asynchronously so the miner tracked the model over training (Xiong et al. 2021). Those candidates are unjudged, not known to be irrelevant. RocketQA reported that sparse labels made many top retrieved passages false negatives and used a cross-encoder to filter candidates (Qu et al. 2021). A teacher is still fallible. Preserve known positives, near duplicates, answer-bearing passages, and multi-positive labels; send uncertain high-impact examples to human judgment rather than converting a teacher score directly into truth.

Figure 45.3. A positive and candidate negatives under a contrastive score. Moving negatives closer raises their softmax weight and the loss. That is useful only when those candidates are genuinely irrelevant; a hard false negative supplies a strong signal in the wrong direction. Illustrative.
import numpy as np

rng = np.random.default_rng(0)

def unit(vector):
    norm = np.linalg.norm(vector)
    return vector / norm if norm else vector

def infonce(query, positive, negatives, temperature=0.08):
    candidates = [positive, *negatives]
    logits = np.array([query @ item for item in candidates]) / temperature
    logits -= logits.max()
    probabilities = np.exp(logits) / np.exp(logits).sum()
    return -np.log(probabilities[0])

query = unit(rng.normal(size=32))
positive = unit(query + 0.4 * rng.normal(size=32))

for hardness in [0.0, 0.3, 0.6]:
    negatives = [
        unit(hardness * query + (1 - hardness) * rng.normal(size=32))
        for _ in range(16)
    ]
    print(hardness, round(infonce(query, positive, negatives), 3))

Weak supervision expands positive coverage at the cost of label noise. E5 filtered naturally occurring web pairs before contrastive pretraining (Wang et al. 2022). Later work generated diverse tasks and pairs with a language model, then trained an embedder on the synthetic data (Wang et al. 2024). Synthetic generation can cover rare intents and languages, but it also copies the generator's factual errors, style, and task assumptions. Keep prompts, generator revisions, filters, and source rights with the dataset. Split by source and time before mining so near duplicates cannot leak across train and evaluation.

Distillation is another label source. A cross-encoder can score a bounded set of query-document pairs, and the dual encoder can learn its pairwise margins rather than its raw score scale. Distillation inherits the teacher's bias and is limited to the candidate set the teacher sees. It cannot teach a distinction that mining never presents.

One model can expose several contracts

General-purpose embedders often support task instructions. INSTRUCTOR trained a single model across a large mixture by embedding each input with a description of its task and domain (Su et al. 2023). This does not mean arbitrary prose is understood as a policy. FollowIR found that many tested retrievers used detailed instructions largely as extra keywords, while targeted fine-tuning improved constraint following (Weller et al. 2025). Treat the exact instruction template as a versioned part of the spec and test paraphrases, negation, exclusions, and conflicting constraints.

Dimension can also be a trained interface. Matryoshka representation learning attaches losses to selected vector prefixes. For a retrieval adaptation, write

z(m)=z1:mz1:m2,LMRL=mMcmLNCE(m).z^{(m)}= \frac{z_{1:m}}{\lVert z_{1:m}\rVert_2}, \qquad \mathcal{L}_{\mathrm{MRL}} =\sum_{m\in\mathcal{M}} c_m\, \mathcal{L}_{\mathrm{NCE}}^{(m)}.

Here zRdz\in\mathbb{R}^{d} is the full embedding, z1:mz_{1:m} is its first mm coordinates, M{1,,d}\mathcal{M}\subseteq\{1,\ldots,d\} is the set of trained prefix dimensions, cm0c_m\ge0 is the weight for prefix mm, and LNCE(m)\mathcal{L}_{\mathrm{NCE}}^{(m)} applies the contrastive loss using independently normalized mm-dimensional query and document prefixes. Only trained dimensions are promised. Slicing a normalized full vector does not leave a normalized prefix, and arbitrary intermediate dimensions are not guaranteed to work.

The original Matryoshka work learned coarse-to-fine representations across vision and language evaluations and reported strong size and retrieval trade-offs on its tested datasets (Kusupati et al. 2022). Its main systems benefit is one model forward pass that can feed indexes of several supported dimensions. The multiple training losses are not literally free, and a shorter prefix is not always strictly worse or always equal to a separately trained model. Measure every offered dimension on the deployment workload.

Indexes make compatibility operational

For NN stored vectors, dimension dd, and bb bytes per coordinate, the raw vector payload is

Sraw=Ndbbytes.S_{\mathrm{raw}} = N d b \quad\text{bytes}.

Here SrawS_{\mathrm{raw}} excludes document metadata, graph or inverted-index overhead, replicas, centroids, and temporary migration copies. Lower dimension, reduced precision, product quantization, and late-interaction compression change storage, memory bandwidth, latency, and neighbor recall in different ways. A smaller payload is useful only if the resulting end-to-end retrieval quality is still acceptable.

Changing any EmbeddingSpec field creates a new index generation. Do not compare query vectors from one model with document vectors from another, even when the dimensions match. Build the replacement beside the current index, re-embed the complete authorized corpus, and dual-read representative traffic. Compare exact vector rankings first, then approximate-index rankings, so representation drift is not confused with ANN loss. Cut traffic over explicitly and retain a rollback window. Tombstones, source updates, and ACL changes must reach every live generation and every embedding cache.

Embeddings are derived content, not anonymized content. They inherit the source's tenant, authorization, retention, residency, and deletion policy. A vector index must not become a path around the authorization invariant in Chapter 44. Cache keys include the spec hash, input content hash, role, instruction version, and tenant or other isolation boundary. Provider logs and batching paths need the same data-handling review as any other model call.

Evaluation must match the use

An aggregate embedding leaderboard is useful for screening, not deployment selection. MTEB demonstrated that models which perform well on semantic textual similarity do not necessarily dominate clustering, classification, reranking, and retrieval (Muennighoff et al. 2023). MMTEB expanded the language and task coverage and showed why a single English average is even less informative (Enevoldsen and others 2025). These benchmarks compare many workloads efficiently; they cannot reproduce a private corpus, its permissions, its update pattern, or its exact definition of relevance.

For a retrieval embedder, freeze the corpus version, chunking, query set, relevance judgments, query and document roles, spec hash, and index parameters. Then evaluate these boundaries separately:

  • Representation: exact-search recall@k and nDCG@k, hard-negative error, calibration by score band, and performance against BM25 and the current model.
  • Approximation: ANN recall against exact neighbors, empty-result rate, latency, memory, and index-build time at each compression or dimension setting.
  • Slices: query intent, document source, length and truncation, language, script, cross-lingual direction, code switching, freshness, and permission cohort. Report the worst material slice as well as the mean.
  • Operations: query encoding latency, document throughput, accelerator and CPU cost, raw and total index bytes, cache hit rate, generation age, migration duration, and rollback success.
  • End to end: evidence recall after packing, answer quality, citation support, abstention, latency, and cost with the generator and reranker held fixed.

Use paired per-query comparisons and bootstrap confidence intervals. Include queries with multiple valid passages and unanswerable queries. Perturb task instructions, shorten and lengthen documents, add near duplicates, change a fact, delete a source, revoke access, and shift the query language. A model that wins a clean average but fails deletion, one language, or a high-value query cohort is not the better production embedder.

What's contested

Whether one general embedding model can serve every task remains unsettled. Shared training can transfer useful structure and simplify operations. Instruction conditioning can expose several task-specific geometries through one checkpoint. Yet symmetric similarity, asymmetric relevance, clustering, and classification impose different neighborhoods. MTEB found no single method dominated every task in its study, while later large and multilingual models changed the frontier without removing that task dependence (Muennighoff et al. 2023; Enevoldsen and others 2025). The stable conclusion is not that a particular architecture wins. It is that model choice is conditional on the task mixture, languages, corpus, and cost envelope being measured.

Lower-layer constraint

The vector contract is paid for in storage and serving. Dimension multiplies the raw index payload; pooling and model size set encoding throughput; normalization and similarity constrain the search primitive; late interaction multiplies the number of stored vectors. These costs reach backward into training through Matryoshka losses, distillation, quantization-aware evaluation, and the choice of interaction architecture. They reach forward into the retrieval budgets and index migrations of Chapter 44.

The resulting boundary is precise. An embedder produces a versioned score under one task contract. Retrieval uses that score to select evidence; it still owns authorization, freshness, fusion, and abstention. The next chapter, Chapter 46, starts after retrieval has selected evidence and asks how instructions, history, tool results, and retrieved material should share a bounded working context.

Further reading

  • Reimers & Gurevych, “Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks” (siamese dual-encoder sentence embeddings comparable by cosine), 2019. arXiv:1908.10084
    Sentence-BERT trains siamese BERT encoders to produce fixed-size sentence vectors that can be compared directly, replacing exhaustive pairwise transformer scoring for semantic search.
  • Karpukhin et al., “Dense Passage Retrieval for Open-Domain Question Answering” (DPR; dual-encoder with in-batch negatives), 2020. arXiv:2004.04906
    DPR trains separate query and passage encoders and demonstrates strong dense retrieval on several open-domain question-answering datasets.
  • Gao et al., “SimCSE: Simple Contrastive Learning of Sentence Embeddings” (dropout as minimal augmentation; NLI pairs for supervision), 2021. arXiv:2104.08821
    SimCSE uses dropout views for unsupervised positives and natural-language-inference pairs for supervised contrastive sentence embeddings.
  • Kusupati et al., “Matryoshka Representation Learning” (trains selected prefixes of one representation at several capacities), 2022. arXiv:2205.13147
    Matryoshka Representation Learning attaches objectives to selected vector prefixes so one model can expose several tested representation dimensions without another inference pass.
  • Muennighoff et al., “MTEB: Massive Text Embedding Benchmark” (broad multi-task embedding benchmark; no single model dominates), 2023. arXiv:2210.07316
    MTEB evaluates text embeddings across eight task families and finds that performance on one task, such as semantic similarity, does not establish dominance on the others.
  • Su et al., “One Embedder, Any Task: Instruction-Finetuned Text Embeddings” (Instructor; one model conditioned on a task instruction), 2023. arXiv:2212.09741
    INSTRUCTOR trains one text encoder on task and domain descriptions so the same input can receive a task-conditioned representation without per-task fine-tuning.
  • Wang & Isola, “Understanding Contrastive Representation Learning through Alignment and Uniformity on the Hypersphere” (contrastive loss decomposes into alignment and uniformity on the sphere), 2020. arXiv:2005.10242
    Wang and Isola formalize alignment of positive pairs and uniformity on the hypersphere as two useful properties of normalized contrastive representations under stated sampling assumptions.
  • Xiong et al., “Approximate Nearest Neighbor Negative Contrastive Learning for Dense Text Retrieval” (ANCE; hard negatives mined from an asynchronously refreshed ANN index), 2021. arXiv:2007.00808
    ANCE mines high-scoring unjudged training candidates from an asynchronously refreshed whole-corpus ANN index, replacing mostly uninformative random negatives.
  • Santhanam et al., “ColBERTv2: Effective and Efficient Retrieval via Lightweight Late Interaction” (residual compression and denoised distillation cut the late-interaction index 6-10x), 2022. arXiv:2112.01488
    ColBERTv2 combines residual compression and denoised supervision, reducing its uncompressed late-interaction index footprint by six to ten times in the reported experiments.
  • Enevoldsen & others, “MMTEB: Massive Multilingual Text Embedding Benchmark” (500+ tasks, 250+ languages; best public model a 560M encoder over larger LLMs), 2025. arXiv:2502.13595
    MMTEB expands embedding evaluation across hundreds of tasks and many languages, and provides reduced benchmark subsets for more affordable model comparison.
  • Weller et al., “FollowIR: Evaluating and Teaching Information Retrieval Models to Follow Instructions” (retrievers use instructions as keywords, not constraints; fine-tuning can teach it), 2025. arXiv:2403.15246
    FollowIR uses detailed TREC relevance instructions to test whether retrievers follow constraints rather than treating the added text as keywords, and supplies targeted training data.

Comments

Log in to comment