Skip to main content
ZICQ

Wiki Concepts

Embedding

Concepts
Aliases: embeddings dense vector vector representation dense embedding ·2026-10-10

Embedding

An embedding is a mathematical technique that maps discrete, high-dimensional symbols (such as text tokens, code snippets, images, or graph nodes) into a continuous, low-dimensional dense vector space $\mathbb{R}^d$. Learned via deep neural networks—predominantly Transformer-based encoder architectures—embeddings encode complex semantic nuances, grammatical structures, and contextual relationships into geometric distances and orientations within vector space.

In modern AI systems, embeddings form the foundational layer for semantic search, Retrieval-Augmented Generation (RAG), recommendation systems, and clustering workflows.


Bi-Encoder Architecture and Contrastive Learning Math

Standard text retrieval embedding models adopt a Bi-Encoder (Siamese Network) architecture. The query $q$ and document $d$ are independently mapped into dense vectors $\mathbf{u} = E_Q(q) \in \mathbb{R}^d$ and $\mathbf{v} = E_D(d) \in \mathbb{R}^d$ using shared or asymmetric encoders.

Query (q)    ──► [Encoder EQ] ──► u (d-dim vector) ─┐
                                                    ├─► Similarity Metric (Dot / Cosine) ──► Relevance Score
Document (d) ──► [Encoder ED] ──► v (d-dim vector) ─┘
(Document vectors can be precomputed offline and stored in vector databases)

InfoNCE Contrastive Loss

Modern embedding models are trained via contrastive learning, aiming to pull semantically positive pairs together while pushing negative pairs apart. The benchmark training objective is the InfoNCE loss function:

$$\mathcal{L}{\text{InfoNCE}} = -\log \frac{\exp\left(\frac{\text{sim}(\mathbf{q}, \mathbf{d}^+)}{\tau}\right)}{\exp\left(\frac{\text{sim}(\mathbf{q}, \mathbf{d}^+)}{\tau}\right) + \sum{j=1}^K \exp\left(\frac{\text{sim}(\mathbf{q}, \mathbf{d}_j^-)}{\tau}\right)}$$

Where:

  • $\text{sim}(\mathbf{q}, \mathbf{d})$ denotes the similarity metric between query and document vectors (typically dot product or cosine similarity).
  • $\mathbf{d}^+$ represents the semantically relevant positive passage.
  • $\mathbf{d}_j^-$ represents irrelevant negative passages.
  • $\tau > 0$ is the temperature hyperparameter controlling the sharpness of the probability distribution. A lower $\tau$ penalizes hard negatives more aggressively, producing tighter cluster boundaries.

In-batch Negatives and Hard Negative Mining

Training exclusively on random negatives causes models to rely on superficial lexical overlap rather than true semantic comprehension. Production-grade embedding models (such as BGE, E5, and GTE) employ multi-tiered negative sampling:

  1. In-batch Negatives: Within a mini-batch of size $B$, positive documents associated with other queries serve as negative examples for the current query. This yields $B-1$ free negative examples per query without requiring extra forward passes.
  2. Hard Negatives: Systems leverage BM25 to retrieve lexical false positives (documents sharing keywords but factually unrelated) or employ high-precision Cross-Encoders to isolate adversarial negative pairs. Gradients from hard negatives compel the encoder to distinguish fine-grained factual and logical boundaries.

Symmetric vs. Asymmetric Retrieval

In engineering practice, retrieval tasks are categorized into two structural patterns based on input format and semantics:

Dimension Symmetric Retrieval Asymmetric Retrieval
Task Archetype Semantic Textual Similarity (STS), duplicate detection, news clustering Open-domain QA, RAG passage retrieval, code search
Input Structure Query and target texts share similar lengths and sentence structures Short queries (concise questions) vs. long targets (dense factual passages)
Relational Nature Synonymous and bidirectional substitutability "Question-to-Answer" relation; non-synonymous, non-invertible
Instruction Prefixes Typically none or generic alignment templates Requires explicit task prefixes (e.g., query: and passage: )

For models like E5 and BGE in asymmetric settings, omitting the required instruction prefix (such as Represent this sentence for searching relevant passages: or query: ) projects the query vector into an outlier semantic manifold, causing top-K recall to deteriorate significantly.


Geometric Distance Metrics and Normalization

Vector distance metrics dictate the physical scale of semantic similarity:

  1. Dot Product (Inner Product): $$\mathbf{u} \cdot \mathbf{v} = \sum_{i=1}^d u_i v_i$$
  2. Cosine Similarity: $$\cos(\theta) = \frac{\mathbf{u} \cdot \mathbf{v}}{|\mathbf{u}|2 |\mathbf{v}|2} = \frac{\sum{i=1}^d u_i v_i}{\sqrt{\sum{i=1}^d u_i^2} \sqrt{\sum_{i=1}^d v_i^2}}$$
  3. Euclidean Distance (L2 Distance): $$|\mathbf{u} - \mathbf{v}|2 = \sqrt{\sum{i=1}^d (u_i - v_i)^2}$$

Engineering Significance of L2 Normalization

Before ingesting vectors into a vector database, normalizing vectors to unit length ($|\mathbf{u}|_2 = 1, |\mathbf{v}|_2 = 1$) establishes exact mathematical equivalence:

$$|\mathbf{u} - \mathbf{v}|_2^2 = |\mathbf{u}|_2^2 + |\mathbf{v}|_2^2 - 2(\mathbf{u} \cdot \mathbf{v}) = 2 - 2(\mathbf{u} \cdot \mathbf{v}) = 2 - 2\cos(\theta)$$

This provides substantial engineering benefits:

  • For unit vectors, the dot product is strictly identical to cosine similarity.
  • Maximizing the dot product is monotonically equivalent to minimizing squared Euclidean distance.
  • Hardware Speedup: Dot product computation requires only fused multiply-add (FMA) instructions, completely eliminating redundant Euclidean norm calculations ($\sqrt{\sum x_i^2}$) and divisions. This increases hardware throughput on SIMD, AVX-512, and GPU kernels by $3\times$ to $5\times$.

Matryoshka Representation Learning (MRL)

Fixed embedding dimensions (e.g., 768, 1536, 4096) impose substantial memory and disk overhead at massive scale. Matryoshka Representation Learning (MRL) trains vectors with nested multi-scale losses across prefixes $m \in {64, 128, 256, 512, 1024, \dots}$:

$$\mathcal{L}{\text{MRL}} = \sum{m \in \mathcal{M}} w_m \mathcal{L}{\text{InfoNCE}}\left(E(q){1:m}, E(d)_{1:m}\right)$$

Full Vector (1536d): [  d1 ~ d256  |  d257 ~ d512  |  d513 ~ d1536  ]
                          ▲               ▲
                          │               └─► Medium Truncation (512d, 99.2% recall retained)
                          └─────────────────► Aggressive Truncation (256d, 97.8% recall retained)

With MRL, the leading $m$-dimensional slice functions as a self-contained, high-quality embedding. Systems can truncate vectors to 256 dimensions for coarse candidate filtering—reducing memory footprint by $6\times$—and use the full dimensionality only for re-scoring top candidates.


Capacity Planning and Memory Sizing

Raw vector storage requirements can be calculated using:

$$\text{Raw Bytes} = N \times d \times \text{sizeof}(\text{dtype})$$

For an index of $10,000,000$ (10 million) chunks using a standard $1536$-dimensional model:

Data Type (dtype) Bytes per Vector Raw Vector Storage (10M Vectors) Estimated Resident RAM with HNSW Index
float32 (32-bit single precision) $1536 \times 4 = 6,144\text{ B}$ 57.22 GiB (61.44 GB) $\approx 75 \sim 90\text{ GiB}$
float16 / bfloat16 (16-bit half precision) $1536 \times 2 = 3,072\text{ B}$ 28.61 GiB (30.72 GB) $\approx 45 \sim 55\text{ GiB}$
int8 (scalar quantization SQ8) $1536 \times 1 = 1,536\text{ B}$ 14.31 GiB (15.36 GB) $\approx 22 \sim 30\text{ GiB}$
MRL 256d + int8 $256 \times 1 = 256\text{ B}$ 2.38 GiB (2.56 GB) $\approx 4 \sim 6\text{ GiB}$

Note: HNSW indexes maintain bidirectional graph adjacency lists (typically $M=16 \sim 64$ neighbor pointers per node), adding an additional 25% to 50% memory overhead beyond raw vector payloads.


Production Pitfalls and Evaluation

  1. Model Drift and Re-indexing Overhead: Vector representations are non-linear projections tied to frozen model weights. Updating the embedding model, modifying fine-tuning data, or changing tokenizers shifts the coordinate geometry entirely. Computing vector distances across different models is mathematically invalid. Upgrades mandate re-encoding and re-indexing the entire corpus.
  2. Silent Truncation: Embedding models enforce strict token context windows (e.g., 512 or 8192 tokens). When input texts exceed this budget, tokenizers silently truncate the tail without throwing exceptions, dropping critical text unobserved. Systems must implement deterministic upstream chunking with sliding window overlaps.
  3. MTEB Benchmarks vs. Domain Adaptation: High scores on the Massive Text Embedding Benchmark (MTEB) do not guarantee out-of-the-box accuracy in specialized domains. Proprietary technical terms in legal, medical, or observability logs require domain-adapted fine-tuning or hybrid retrieval combining lexical BM25 with dense semantics.

Sources and Key Literature