924 words
5 minutes
EmbeddingGemma

EmbeddingGemma 2 was released recently, but there is no technical report yet, so I read the paper on its predecessor first: EmbeddingGemma: Powerful and Lightweight Text Representations. One major difference between the two: EmbeddingGemma 2 is natively multimodal, while EmbeddingGemma handles text only.

EmbeddingGemma: Powerful and Lightweight Text Representations
arXiv:2509.20354 [cs.CL]
Henrique Schechter Vera et al.
2025-09-24

Decoder-only or encoder?#

I didn’t know much about EmbeddingGemma before, and was surprised to find that it is an encoder-only model.

Intuitively, an encoder with bidirectional attention is better suited to embeddings. But most LLMs today are decoder-only, and LLM-based embedding models usually keep that architecture, taking the representation of the final EOS or last token as the vector: that position sits at the end of the sequence, so even with causal attention it sees all the preceding text and can summarize the whole passage. Causal attention also saves compute: the attention matrix is lower-triangular, so the attention-score computation is in theory about half that of bidirectional attention (the FFN and projection layers are unaffected, so the model as a whole is not twice as fast).

EmbeddingGemma doesn’t take that route. Instead, it first converts the decoder-only Gemma 3 into an encoder-decoder, then takes out the encoder.

From Gemma 3 to an encoder#

EmbeddingGemma starts from the 300M Gemma 3 and obtains its encoder following the T5Gemma recipe:

  1. Build the encoder: reuse Gemma 3’s self-attention, FFN and other weights, switching causal attention to bidirectional attention.
  2. Build the decoder: also reuse the original weights, keep causal self-attention, and add cross-attention to read the encoder output.
  3. Continue pre-training with UL2: run a mixture-of-denoisers objective on Gemma 3’s pre-training data; the encoder understands the input, the decoder reconstructs the target text, and both are updated jointly.
  4. Take out the encoder: it becomes the backbone of the embedding model, and the decoder is simply discarded.

The encoder has 24 layers with a hidden size of 768. Its output token representations are mean-pooled, then passed through two randomly initialized linear projections (768 → 3072 → 768) to produce the final vector.

Ablations (mean score over MTEB multilingual tasks) back up these choices:

  • Initialization: encoder-decoder 60.4 > decoder-only 59.7. The authors attribute this to bidirectional attention, and to the encoder’s parameters being able to specialize in understanding the input.
  • Pooling: mean 60.4 > attention 60.2 > first-token 59.9 > last-token 59.7. Attention pooling, despite its learnable parameters, does worse than a simple average.

Training objectives#

Training optimizes three losses at once, responsible respectively for telling related texts apart, spreading the vectors out across the space, and absorbing the representational power of a larger model.

Notation: qiq_i is the query vector, pi+p_i^+ the vector of the relevant document (positive), pi−p_i^- the vector of a hard negative that looks relevant but isn’t, and BB the batch size.

Building on standard InfoNCE, the loss for a single sample is:

ℓi=−log⁡esim⁡(qi,pi+)/τesim⁡(qi,pi+)/τ+wi esim⁡(qi,pi−)/τ+∑j≠i1TN(i,j) esim⁡(qi,pj+)/τ\ell_i=-\log\frac{e^{\operatorname{sim}(q_i,p_i^+)/\tau}}{e^{\operatorname{sim}(q_i,p_i^+)/\tau}+w_i\,e^{\operatorname{sim}(q_i,p_i^-)/\tau}+\sum_{j\ne i}\mathbb 1_{\text{TN}}(i,j)\,e^{\operatorname{sim}(q_i,p_j^+)/\tau}}

Here sim⁡\operatorname{sim} is cosine similarity and τ\tau is the temperature. The higher the positive scores relative to all candidates, the lower the loss. Besides the positive itself, the denominator contains two kinds of negatives:

  • Hard negatives: weighted by wi=exp⁡(α sg(sim⁡(qi,pi−)))w_i=\exp\left(\alpha\,\mathrm{sg}(\operatorname{sim}(q_i,p_i^-))\right) with α=5.0\alpha=5.0. The more a negative resembles the query, the larger its weight and the more attention the model pays to it; sg\mathrm{sg} is stop-gradient, so the weight only reflects the current difficulty and is not differentiated itself. Hard negatives are optional: the large, noisy pre-finetuning stage uses only (query, target) pairs, and they are added during fine-tuning.
  • In-batch negatives: the positives pj+p_j^+ of the other samples in the same batch. Each batch is drawn from a single dataset, which makes these negatives harder to tell apart and more valuable; 1TN\mathbb 1_{\text{TN}} masks out entries whose query or positive duplicates the current sample’s, so they aren’t mistaken for negatives.

2. Spread-out regularizer LS\mathcal L_S: keep vectors from crowding into a few directions#

For distinct pairs of queries and distinct pairs of positives within a batch, it penalizes the square of their inner products:

LS=∑i≠j(qi⊤qj)2B(B−1)+∑i≠j((pi+)⊤pj+)2B(B−1)\mathcal L_S= \frac{\sum_{i\ne j}(q_i^\top q_j)^2}{B(B-1)} + \frac{\sum_{i\ne j}((p_i^+)^\top p_j^+)^2}{B(B-1)}

The closer the inner product is to 0 (nearly orthogonal), the smaller the penalty, so the vectors spread out across the space as a whole. The idea comes from GOR (global orthogonal regularizer): make the vectors of two random texts match, in the mean and second moment of their inner product, two points sampled independently and uniformly on the unit sphere; the paper keeps only the second-moment term.

It constrains overall statistical properties and does not require semantically similar texts to be orthogonal to each other: pulling related texts together is still the job of the contrastive loss, and the two balance each other. The authors also expect this distribution to make the model more robust to quantization and better suited to approximate nearest neighbor (ANN) search.

3. Embedding matching LD\mathcal L_D: learn the teacher’s vectors directly#

The teacher is the larger Gemini Embedding. Most earlier distillation work only has the student imitate the teacher’s query-document relevance scores; here, following EmbedDistill, the student’s vectors are aligned directly with the teacher’s, so the student learns the geometry of the teacher’s entire vector space.

Unlike EmbedDistill, the paper aligns not only queries and positives but also hard negatives, and finds that this clearly improves results. Intuitively, it plays the same role as hard negatives in the contrastive loss: the student learns how the teacher separates a query from its hard negatives. The three terms are added with equal weight:

LD=LDQ+LDP++LDP−\mathcal L_D=\mathcal L_D^{Q}+\mathcal L_D^{P^+}+\mathcal L_D^{P^-}

Addendum: MRL#

LC\mathcal L_C and LS\mathcal L_S also apply MRL (Matryoshka Representation Learning): the losses are computed separately on several nested prefix dimensions (768 / 512 / 256 / 128) and simply summed. Vectors trained this way can be truncated to lower dimensions and used directly.

EmbeddingGemma
https://blog.leemengyu.com/en/posts/embeddinggemma/
Author
Li Mengyu
Published at
2026-10-12
License
CC BY-NC-SA 4.0