Vector Embeddings in Large Language Models: How Contrastive Learning, Bi-Encoders, and Matryoshka Projections Map Semantic Space

Vector Embeddings in Large Language Models: How Contrastive Learning, Bi-Encoders, and Matryoshka Projections Map Semantic Space Large language models process text as discrete tokens: integers mapped to lookup tables. While causal transformers excel at autoregressive generation by predicting the next token, generation alone does not solve the challenge of semantic search, clustering, or dense retrieval. Searching through millions of documents requires comparing sequence-level meaning in constan

8 min
Vector Embeddings in Large Language Models: How Contrastive Learning, Bi-Encoders, and Matryoshka Projections Map Semantic Space

Vector Embeddings in Large Language Models: How Contrastive Learning, Bi-Encoders, and Matryoshka Projections Map Semantic Space

Large language models process text as discrete tokens: integers mapped to lookup tables. While causal transformers excel at autoregressive generation by predicting the next token, generation alone does not solve the challenge of semantic search, clustering, or dense retrieval. Searching through millions of documents requires comparing sequence-level meaning in constant or sub-millisecond time, a task that token-by-token generation cannot support.

Vector embeddings solve this by projecting arbitrary text sequences into continuous, fixed-dimensional vector spaces where geometric distance directly corresponds to semantic relatedness. Constructing robust embedding spaces requires specialized architectures, contrastive loss formulations, geometric regularization to prevent representation collapse, and dimension-adaptive training techniques.

Technical architecture diagram of bi-encoders and semantic vector space

The Representation Problem: Why Raw LLM States Fail

A common assumption is that the internal hidden states of standard pretrained language models can serve directly as sentence embeddings. In practice, taking the mean or final hidden state of an uncalibrated decoder model produces poor representations.

This failure stems from anisotropy, also known as representation collapse. In raw autoregressive and masked language models, token representations occupy a narrow, conical subset of the total vector space. Because high-frequency words dominate pretraining gradients, their vector norms and directional biases skew the latent space. As demonstrated by Ethayarajh (2019) and Gao et al. (2021), cosine similarities between two randomly selected, unrelated sentences from an uncalibrated model typically range between 0.70 and 0.95.

Without explicit contrastive calibration:

  • The vector space lacks directional uniformity.
  • Cosine distance ceases to provide a discriminative measure of semantic similarity.
  • Information becomes concentrated in a few dominant singular value dimensions, effectively reducing the expressive capacity of the model.

Architectural Trade-Offs: Bi-Encoders vs. Cross-Encoders

Dense text comparison relies on two fundamental neural architectures: cross-encoders and bi-encoders.

Cross-Encoders

A cross-encoder accepts a query qq and a document dd simultaneously, concatenating them into a single input sequence separated by delimiter tokens: [CLS] query [SEP] document [SEP].

Every token in the query attends to every token in the document across every self-attention layer of the transformer. This full cross-attention mechanism captures fine-grained lexical and contextual interactions, making cross-encoders highly accurate scoring models.

However, cross-encoders cannot generate isolated document representations. Scoring a single query against a corpus of NN documents requires NN complete forward passes through the transformer network. At web scale, evaluating millions of candidate pairs in real time is computationally prohibitive.

Bi-Encoders

Bi-encoders, popularized by Sentence-BERT (Reimers & Gurevych, 2019) and Contriever (Izacard et al., 2021), decouple the encoding of queries and documents.

  1. An encoder network fθf_\theta maps a query string into a dense vector u=fθ(q)Rdu = f_\theta(q) \in \mathbb{R}^d.
  2. The same network (or a paired document encoder) maps a candidate document into v=fθ(d)Rdv = f_\theta(d) \in \mathbb{R}^d.
  3. Relevance is computed via a dot product or cosine similarity: s(q,d)=u,vs(q, d) = \langle u, v \rangle.

Because document embeddings vv depend solely on the document text, an entire corpus can be encoded once offline and indexed into approximate nearest neighbor (ANN) data structures such as Hierarchical Navigable Small World (HNSW) graphs or Inverted File with Product Quantization (IVF-PQ). At query time, only the single query vector uu is computed online, reducing retrieval across millions of records to a sub-millisecond vector index lookup.

Pooling Mechanisms: From Token Sequences to Single Vectors

A transformer outputs a tensor of shape (batch_size, sequence_length, hidden_dimension). Bi-encoders must condense this variable-length sequence of token representations into a single fixed-size vector. Three primary pooling methods are deployed in production systems:

1. Mean Pooling

Mean pooling calculates the element-wise average of token vectors across the sequence length, masking out padding tokens:

v=i=1Lmihii=1Lmiv = \frac{\sum_{i=1}^L m_i h_i}{\sum_{i=1}^L m_i}

Where hih_i represents the hidden state at position ii, and mi{0,1}m_i \in \{0, 1\} is the attention mask indicator. Mean pooling captures distributed semantic signals across the full passage and remains the standard pooling mechanism for bidirectional encoder models like BGE, E5, and modern Sentence-Transformer variants.

2. [CLS] Token Pooling

In models based on BERT architectures, a special [CLS] token is prepended to the sequence. The final hidden state corresponding to this index, h0h_0, is passed through an optional projection layer. While computationally straightforward, [CLS] pooling often yields lower zero-shot transfer performance compared to mean pooling unless specifically optimized during fine-tuning.

3. Last-Token Pooling

Decoder-only embedding models based on architectures such as Llama, Mistral, or Qwen (including GritLM and NV-Embed) use causal attention masks. Because each token can only attend to earlier positions, the final token position in the sequence aggregates contextual information from all preceding tokens. Modern decoder-based embedding models therefore extract the hidden state of the final non-padding token as the sequence representation.

Contrastive Training and the InfoNCE Objective

To transform raw token aggregations into a metric space where vector distance reflects semantic relevance, models are trained using contrastive learning.

The dominant objective is the InfoNCE loss (Information Noise-Contrastive Estimation), formalized by van den Oord et al. (2018) and adapted for text embeddings in models like SimCSE (Gao et al., 2021) and CLIP (Radford et al., 2021).

Given a batch of BB query-positive passage pairs {(qi,pi+)}i=1B\{(q_i, p_i^+)\}_{i=1}^B, the InfoNCE loss treats all other passages in the batch {pj+}ji\{p_j^+\}_{j \neq i} as negative examples:

Li=logexp(sim(qi,pi+)/τ)exp(sim(qi,pi+)/τ)+jiexp(sim(qi,pj+)/τ)\mathcal{L}_i = -\log \frac{\exp(\text{sim}(q_i, p_i^+) / \tau)}{\exp(\text{sim}(q_i, p_i^+) / \tau) + \sum_{j \neq i} \exp(\text{sim}(q_i, p_j^+) / \tau)}

Where:

  • sim(u,v)=uvu2v2\text{sim}(u, v) = \frac{u^\top v}{\|u\|_2 \|v\|_2} represents the cosine similarity.
  • τ\tau is a learned or fixed temperature hyperparameter (typically set between 0.01 and 0.07).

The Role of Temperature (τ\tau)

The temperature parameter acts as a hardness amplifier. Small values of τ\tau scale the input to the softmax function, concentrating gradients on the negative passages closest to the query in vector space (hard negatives). High values of τ\tau produce a smoother probability distribution, distributing gradient updates more uniformly across all negatives. Setting τ\tau too low risks training instability due to gradient explosions, while setting it too high fails to separate semantically close distractors.

Hard Negatives and In-Batch Negatives

In-batch negatives provide computational efficiency by reusing vectors already computed in the batch forward pass. However, random in-batch negatives are often easily distinguishable from the target passage (e.g., matching a query about database sharding against a random passage about French history).

To enforce sharp decision boundaries, training pipelines incorporate hard negative mining:

  1. Lexical retrieval (such as BM25) or baseline dense models retrieve candidate documents that share substantial keyword overlap with the query but do not contain the answer.
  2. Cross-encoders score and filter these candidates to verify they do not contain false negatives.
  3. The verified hard negatives are added directly into the denominator of the contrastive loss function.

Hyperspherical Geometry: Alignment and Uniformity

Understanding why contrastive learning resolves representation collapse requires analyzing the geometry of the embedding space. As shown by Wang and Isola (2020), optimizing the InfoNCE loss asymptotically optimizes two distinct geometric properties on the unit hypersphere Sd1\mathcal{S}^{d-1}:

  1. Alignment: Positive pairs should map to nearby points on the sphere:

LalignE(x,y)ppos[f(x)f(y)2]\mathcal{L}_{\text{align}} \triangleq \mathbb{E}_{(x, y) \sim p_{\text{pos}}} \left[ \|f(x) - f(y)\|^2 \right]

  1. Uniformity: The overall distribution of embeddings should be uniformly scattered across the hypersphere, maximizing entropy and preserving maximal information:

LuniformlogEx,yi.i.d.pdata[e2f(x)f(y)2]\mathcal{L}_{\text{uniform}} \triangleq \log \mathbb{E}_{x, y \overset{\text{i.i.d.}}{\sim} p_{\text{data}}} \left[ e^{-2\|f(x) - f(y)\|^2} \right]

The repulsion term in the InfoNCE denominator forces negative examples apart, spreading vectors across the entire surface of the hypersphere. This directly counteracts anisotropy, eliminating the narrow cone problem and ensuring that cosine similarities span the full [1.0,1.0][-1.0, 1.0] range.

Matryoshka Representation Learning (MRL)

Historically, choosing an embedding dimensionality involved an absolute trade-off between retrieval accuracy and infrastructure costs. A 4096-dimensional vector preserves fine-grained nuance but quadruples memory footprint, disk storage, and vector distance computation time compared to a 1024-dimensional vector.

Kusupati et al. (NeurIPS 2022) introduced Matryoshka Representation Learning (MRL) to eliminate this fixed constraint. Named after Russian nesting dolls, MRL forces a model to encode the most critical semantic features into the earliest vector dimensions.

Nested Loss Mechanics

During training, rather than computing loss exclusively on the final full-dimensional output vector zRdz \in \mathbb{R}^d, the vector is sliced into multiple nested prefix dimensions M={d1,d2,,dK}\mathcal{M} = \{d_1, d_2, \dots, d_K\} (for example, d{64,128,256,512,1024,1536}d \in \{64, 128, 256, 512, 1024, 1536\}).

Each prefix slice z1:mz_{1:m} is normalized and evaluated under the loss function independently:

LMRL=mMcmLInfoNCE(z1:mz1:m2)\mathcal{L}_{\text{MRL}} = \sum_{m \in \mathcal{M}} c_m \mathcal{L}_{\text{InfoNCE}}\left( \frac{z_{1:m}}{\|z_{1:m}\|_2} \right)

Where cmc_m is a weighting coefficient for each dimensionality scale.

Production Efficiency Gains

MRL enables adaptive deployment strategies without retraining or maintaining separate models:

  • Vector Truncation: A model trained with MRL can be truncated to 256 or 512 dimensions by taking the first kk elements and renormalizing. Benchmarks show that truncating a 1536-dimensional model to 256 dimensions (an 83% reduction in memory and disk usage) typically preserves over 97% to 99% of downstream retrieval performance.
  • Hierarchical Search (Two-Phase Retrieval): Large search systems execute initial candidate filtering across millions of documents using compact 128-dimensional vectors stored in RAM-efficient indices. The top 100 candidate document IDs are then reranked using the full 1536-dimensional representations stored on disk.

Evaluation: The MTEB Standard

Evaluating text embedding quality across diverse domains requires standardized benchmarks. The standard evaluation framework is the Massive Text Embedding Benchmark (MTEB), introduced by Muennighoff et al. (2023).

MTEB evaluates models across 8 distinct task types:

  • Retrieval: Finding relevant passages from a large corpus given a query (evaluated via NDCG@10).
  • Reranking: Reordering a candidate list of passages by relevance.
  • Semantic Textual Similarity (STS): Predicting continuous human-annotated similarity scores between sentence pairs (evaluated via Spearman rank correlation).
  • Classification: Using frozen embeddings as features for linear classifiers.
  • Clustering: Grouping sentence vectors into coherent thematic clusters (evaluated via V-measure).
  • Pair Classification: Determining whether two sentences are duplicates or paraphrases.
  • Summarization: Scoring candidate summary quality against reference texts.
  • Bitext Mining: Identifying translation pairs across parallel multilingual corpora.

Architectural Limits and Failure Modes

While dense vector embeddings form the foundation of semantic search and Retrieval-Augmented Generation (RAG), several structural limitations persist:

  1. Exact-Match and Keyword Blindspots: Dense embeddings project terms into conceptual neighborhoods. When queries contain exact identifiers, such as part numbers, code variable names, or specialized acronyms, dense retrieval can confuse near-spelling variations. Combining dense embeddings with sparse lexical search (e.g., BM25 or SPLADE) via hybrid search pipelines remains essential for production reliability.
  2. Information Bottlenecks in Long Documents: Compressing a multi-page document into a single 1024-dimensional vector causes loss of fine-grained detail. Chunking strategies or late-interaction architectures (such as ColBERT) address this by computing token-level multi-vector representations.
  3. Out-of-Domain Generalization: Dense models trained predominantly on general web text can degrade on highly specialized terminology (such as legal statutes or clinical trials) unless adapted with in-domain contrastive fine-tuning.

Sources

Written by

More to read

  • Document Chunking Strategies for Production RAG: Fixed-Size, Semantic, Hierarchical, and Late Chunking Trade-Offs

    Document Chunking Strategies for Production RAG: Fixed-Size, Semantic, Hierarchical, and Late Chunking Trade-Offs In production retrieval-augmented generation (RAG), document chunking is often treated as a trivial preprocessing step. In practice, the method used to partition raw text directly dictates the upper bound of retrieval recall, embedding representation quality, and downstream generation accuracy. Retrieval systems face a fundamental tension. Dense vector search models perform best wh

    1 min
  • Knowledge Distillation for Large Language Models: From Soft Targets to On-Policy Reverse KL

    Knowledge Distillation for Large Language Models: From Soft Targets to On-Policy Reverse KL Knowledge distillation (KD) has become the primary mechanism for transferring capabilities from massive proprietary models to smaller, deployable open-weight models. The technique originated in classification, but applying it to auto-regressive language models exposed fundamental mismatches: token-level forward KL forces students to cover the teacher's full output distribution, while supervised training

    1 min
  • Velaura AI Raises 10M Series A at B Valuation for Low-Power AI Silicon

    Velaura AI Raises $110M Series A at $1B Valuation for Low-Power AI Silicon Velaura AI has closed a $110 million Series A funding round at a valuation exceeding $1 billion. The financing was led by Seligman Ventures, with participation from Capricorn Investment Group alongside existing backers including Samsung Catalyst Fund, StepStone Group, Maverick Silicon, Celesta Capital, and Mayfield. The capital will fund the commercialization and deployment of Velaura's silicon IP and physical design te

    1 min