MaartenGr hschechter commited on
Commit
2eb44e9
·
1 Parent(s): 034952c

Update README.md (#1)

Browse files

- Update README.md (c3f2237ce16fe33d1bb9dab8692c29c79865bdeb)


Co-authored-by: Henrique Schechter Vera <hschechter@users.noreply.huggingface.co>

Files changed (1) hide show
  1. README.md +95 -44
README.md CHANGED
@@ -42,7 +42,7 @@ EmbeddingGemma 2 builds upon the architectural and capability advancements of Ge
42
  * **Multilinguality and code:** EmbeddingGemma 2 understands 100+ languages, and achieves a \~14% improvement on code tasks relative to our last EmbeddingGemma (MTEB code).&nbsp;
43
  * **Cross-modality semantics:** Trained to capture semantic similarity, across the modality boundaries. This means equivalent concepts, whether expressed in text, source code, images, video, or audio, are co-located in the embedding space.
44
  * **Flexible footprint:** Combines a 270M parameter text backbone (130M transformer \+ 140M embedder) with selectively loadable vision (170M) and audio (300M) encoders, allowing developers to load only the modalities required for their use case.
45
- * **Matryoshka Representation Learning (MRL):** Native support for truncated vector projections across 128d, 256d, 512d, and 768d, enabling up to a **6x reduction** in vector storage costs with minimal impact on retrieval quality.
46
  * **Context length:** 8K token context window, capable of processing interleaved multimodal input.
47
  * **Task-steered representations:** Uses lightweight text instruction prefixes to optimize vector projections for different tasks (search, classification, clustering, semantic similarity, etc).
48
 
@@ -82,7 +82,7 @@ During inference, all modalities draw from one shared 8,192-token context window
82
 
83
  ## **Benchmark Results**
84
 
85
- EmbeddingGemma 2 was evaluated across text, code, vision, visual document, video, and audio retrieval benchmarks. All results reported below use the full-precision checkpoint.
86
 
87
  ### Overall Evaluation Results (768d)
88
 
@@ -90,7 +90,7 @@ EmbeddingGemma 2 was evaluated across text, code, vision, visual document, video
90
  | :---- | :---- | :---- | :---- | :---- |
91
  | *Text* | Massive Text Embedding Benchmark (MTEB, multilingual, v2) *Hugging Face* | Mean (Task) | 61.36 | 61.15 |
92
  | | Massive Text Embedding Benchmark (MTEB, code, v1) *Hugging Face* | Mean (Task) | 78.54 | 68.76 |
93
- | *Image* | Massive Image Embedding Benchmark (MIEB, lite) *Hugging Face* | Mean (Task) | 58.74 | \- |
94
  | | Massive Multimodal Embedding Benchmark (MMEB v2 \- Image) *Tiger-Lab* | Recall@1 | 57.28 | \- |
95
  | | Massive Multimodal Embedding Benchmark (MMEB v2 \- VisDoc) *Tiger-Lab* | NDCG@5 | 67.84 | \- |
96
  | *Video* | Massive Multimodal Embedding Benchmark (MMEB v2 \- Video) *Tiger-Lab* | Recall@1 | 50.67 | \- |
@@ -102,12 +102,35 @@ EmbeddingGemma 2 was evaluated across text, code, vision, visual document, video
102
  With MRL, EmbeddingGemma 2 embedding representations can be truncated below the native 768d, to 128d, 256d, and 512d representations and re-normalized.
103
  With this, model users can reduce storage requirements, with minimal quality impact down to 256d. 128d is best suited to text-only workloads.
104
 
105
- | Output Dimension | Compression Ratio | MTEB *(multilingual, v2) Mean(Task)* | MTEB *(eng, v2) Mean(Task)* | MTEB *(code, v1) Mean(Task)* | MMEB (v2) *Overall* | MSEB *(Retrieval) Overall* | MAEB *Overall* |
106
- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
107
- | 768d (Full) | 1:1 | 61.36 | 68.46 | 78.54 | 59.01 | 69.54 | 49.39 |
108
- | 512d | 1:1.5 | 61.17 | 68.41 | 77.24 | 58.38 | 69.18 | 49.21 |
109
- | 256d | 1:3 | 60.41 | 67.78 | 76.18 | 56.24 | 66.76 | 48.91 |
110
- | 128d | 1:6 | 57.89 | 65.68 | 71.41 | 45.65 | 56.71 | 46.92 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
  ## **Best Practices**
113
 
@@ -115,7 +138,7 @@ For optimal embedding quality and runtime efficiency, follow these configuration
115
 
116
  ### 1. Task Instruction Prefixes
117
 
118
- EmbeddingGemma 2 is trained with short task instruction prefixes prepended to text inputs. Using the right prefix improves retrieval quality; omitting it still works but reduces precision. Prefixes apply to text only. Pass images, video, and audio without any prefix.
119
 
120
  Documents with a real title should be formatted as `title: {title} | text: {content}`. Use `title: none` when no title is available.
121
 
@@ -126,41 +149,51 @@ We offer two types of task prefixes, depending on how embeddings are used in the
126
  * **Asymmetric Tasks (e.g. retrieval):** Use a query prefix for queries and a document prefix for corpus items.
127
  * **Symmetric Tasks (e.g. classification, similarity)**: Apply the same task prefix to all inputs being compared.
128
 
129
- | Use Case | Task Type | Query Task Instruction | Document Task Instruction *(use `none` if no title)* |
130
- | :---- | :---- | :---- | :---- |
131
- | Web / document search | Asymmetric | `task: search result \| query: {query}` | `title: {title} \| text: {content}` |
132
- | Question answering | Asymmetric | `task: question answering \| query: {question}` | `title: {title} \| text: {passage}` |
133
- | Fact checking | Asymmetric | `task: fact checking \| query: {claim}` | `title: {title} \| text: {evidence}` |
134
- | Code search | Asymmetric | `task: code retrieval \| query: {query}` | `title: {title or filename} \| text: {code}` |
135
- | Text classification | Symmetric | `task: classification \| query: {content}` | N/A |
136
- | Clustering | Symmetric | `task: clustering \| query: {content}` | N/A |
137
- | Measuring similarity | Symmetric | `task: sentence similarity \| query: {content}` | N/A |
138
 
139
- **Please note:** Prefixes are used on text inputs only when encoding images, video, or audio pass them directly.
140
 
141
  ### 2. Selective Encoder Loading
142
 
143
- The vision and audio encoders are independent components. If EmbeddingGemma 2 use cases do not require these components, they can be omitted, so memory footprint scales with the modalities actually used.
144
 
145
  **Please note:** configuring EmbeddingGemma 2 to load with omission of some encoders differs among model libraries; please refer to the appropriate documentation for more information on this.
146
 
147
- | Active Modalities | Effective Size |
148
- | :---- | :---- |
149
- | Text only | 270M |
150
- | Text and image | 440M |
151
- | Text and audio | 570M |
152
- | Full multimodal | 740M |
153
 
154
- ### 3. Matryoshka Dimension Truncation (`truncate_dim`)
155
 
156
- EmbeddingGemma 2 is trained with Matryoshka Representation Learning, so the 768-dimensional output vector can be shortened by keeping only its leading dimensions. The supported dimensions are 768, 512, 256, and 128\. Shorter vectors reduce storage and speed up similarity search at some cost to retrieval quality.
157
 
158
  At runtime, please adhere to the following guidelines:
159
 
160
  * **Re-normalize after truncating:** slicing a unit-length vector does not preserve unit length. The shortened vector must be L2-normalized before it is used for cosine similarity. Skipping this step degrades ranking quality silently — it produces plausible-looking scores rather than an error.
161
- * **Queries and documents must share a dimension.** A 128-dimensional query cannot be scored against a 768-dimensional corpus. Changing the dimension means re-encoding the corpus.
 
 
162
 
163
- Retrieval quality at each dimension is reported in the truncation table in the **Benchmark Results** section above. Quality is close to lossless down to 256 dimensions. 128 dimensions degrades multimodal and speech retrieval substantially and should be validated against your own workload before adoption.
 
 
 
 
 
 
 
 
164
 
165
  ### 4. Numerical Precision
166
 
@@ -170,7 +203,17 @@ EmbeddingGemma 2's activation range exceeds the dynamic range of `float16`. In `
170
 
171
  `bfloat16` is safe, and carries the same 8-bit exponent as `float32`. This is the recommended default on hardware with native support, and halves memory use relative to `float32`. Use `float32` elsewhere, including on most CPUs.
172
 
173
- ### 5. Interleaved Multimodal Input
 
 
 
 
 
 
 
 
 
 
174
 
175
  A single input may mix text with images, video, and audio, using a single shared 8,192-token context.
176
 
@@ -180,13 +223,19 @@ The position of each media item within the sequence is marked in the text using
180
  * `<|video|>` marks the position of video input
181
  * `<|audio|>` marks the position of audio input&nbsp;
182
 
183
- For example, a product listing indexed for search might be written as:
184
 
185
- “`Waterproof trail running shoe with a breathable mesh upper. <|image|> Grip test on wet rock: <|video|>`”
 
 
 
 
 
 
186
 
187
- In this example, the image and video are replaced with one product photo and one short clip. Put together, this produces a single embedding that represents the text, image and video together. A text-only query such as "waterproof shoes for trail running" can then be matched against it directly.
188
 
189
- ### 6. Context Budget by Modality
190
 
191
  All modalities share a single 8,192-token context window. Each modality consumes that budget at a fixed rate:
192
 
@@ -197,20 +246,22 @@ All modalities share a single 8,192-token context window. Each modality consumes
197
  | *Video* | 140 tokens per frame *(default)* | ~58 frames |
198
  | *Audio* | 25 tokens per second | ~327 seconds |
199
 
 
 
200
  The maximums above assume a single modality with no accompanying text. Interleaved inputs draw from the same budget, so mixing modalities reduces the amount of each that fits. Note also max input for images and video can be as high as \~114 images or frames when using a lower vision token budget (described below).
201
 
202
- **Input Sampling Defaults**
203
 
204
- Audio and video inputs have the following default sampling rates:
205
 
206
- * By default, video is processed as sampled frames through the vision encoder at **1 frame per second;** FPS sampling rate is configurable.
207
- * Audio should be supplied as **mono at 16 kHz.**
208
 
209
- **Vision Token Budget**
210
 
211
- Model users can forgo the default sequence length for input images / video frames to represent images with “soft token” amounts ranging from 70 to 1120\. Increasing the vision budget for input images trades latency and token count for quality.&nbsp;
212
 
213
- In general, higher input sequence lengths capture more information. Scaling up input sequence length means more expressive input representations; so, increasing input sequence length will improve fine-grained visual understanding, uplifting embedding quality and performance in downstream use cases.
 
214
 
215
  ## **Model Data**
216
 
@@ -266,7 +317,7 @@ The following list of potential uses is not comprehensive. The purpose of this l
266
  * Open-ended or highly complex tasks might be challenging.
267
  * A model's performance can be influenced by the amount of context provided (longer context generally leads to better outputs, up to a certain point).
268
  * **Language Ambiguity and Nuance:** Natural language is inherently complex. Models might struggle to grasp subtle nuances, sarcasm, or figurative language.
269
- * **Task Instruction Usage:** For text retrieval, omitting the recommended task prefix may lead to sub-optimal embedding quality.
270
 
271
  ### Ethical Considerations and Risks
272
 
 
42
  * **Multilinguality and code:** EmbeddingGemma 2 understands 100+ languages, and achieves a \~14% improvement on code tasks relative to our last EmbeddingGemma (MTEB code).&nbsp;
43
  * **Cross-modality semantics:** Trained to capture semantic similarity, across the modality boundaries. This means equivalent concepts, whether expressed in text, source code, images, video, or audio, are co-located in the embedding space.
44
  * **Flexible footprint:** Combines a 270M parameter text backbone (130M transformer \+ 140M embedder) with selectively loadable vision (170M) and audio (300M) encoders, allowing developers to load only the modalities required for their use case.
45
+ * **Matryoshka Representation Learning (MRL):** Native support for truncated vector projections across 128d, 256d, 512d, and 768d, enabling up to a **6x reduction** in vector storage costs with minimal impact on quality.
46
  * **Context length:** 8K token context window, capable of processing interleaved multimodal input.
47
  * **Task-steered representations:** Uses lightweight text instruction prefixes to optimize vector projections for different tasks (search, classification, clustering, semantic similarity, etc).
48
 
 
82
 
83
  ## **Benchmark Results**
84
 
85
+ EmbeddingGemma 2 was evaluated across text, code, vision, visual document, video, and audio embedding benchmarks. All results reported below use the full-precision checkpoint.
86
 
87
  ### Overall Evaluation Results (768d)
88
 
 
90
  | :---- | :---- | :---- | :---- | :---- |
91
  | *Text* | Massive Text Embedding Benchmark (MTEB, multilingual, v2) *Hugging Face* | Mean (Task) | 61.36 | 61.15 |
92
  | | Massive Text Embedding Benchmark (MTEB, code, v1) *Hugging Face* | Mean (Task) | 78.54 | 68.76 |
93
+ | *Image* | Massive Image Embedding Benchmark (MIEB, lite) *Hugging Face* | Mean (TaskType) | 64.64 | \- |
94
  | | Massive Multimodal Embedding Benchmark (MMEB v2 \- Image) *Tiger-Lab* | Recall@1 | 57.28 | \- |
95
  | | Massive Multimodal Embedding Benchmark (MMEB v2 \- VisDoc) *Tiger-Lab* | NDCG@5 | 67.84 | \- |
96
  | *Video* | Massive Multimodal Embedding Benchmark (MMEB v2 \- Video) *Tiger-Lab* | Recall@1 | 50.67 | \- |
 
102
  With MRL, EmbeddingGemma 2 embedding representations can be truncated below the native 768d, to 128d, 256d, and 512d representations and re-normalized.
103
  With this, model users can reduce storage requirements, with minimal quality impact down to 256d. 128d is best suited to text-only workloads.
104
 
105
+ | Output Dimension | Compression Ratio | MTEB *(multilingual, v2) Mean(Task)* | MTEB *(eng, v2) Mean(Task)* | MTEB *(code, v1) Mean(Task)* | MIEB *(lite) Mean(TaskType)* | MMEB (v2) *Overall* | MSEB *(Retrieval) Overall* | MAEB *Overall* |
106
+ | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
107
+ | 768d (Full) | 1:1 | 61.36 | 68.46 | 78.54 | 64.64 | 59.01 | 69.54 | 49.39 |
108
+ | 512d | 1:1.5 | 61.17 | 68.41 | 77.24 | 64.32 | 58.38 | 69.18 | 49.21 |
109
+ | 256d | 1:3 | 60.41 | 67.78 | 76.18 | 63.13 | 56.24 | 66.76 | 48.91 |
110
+ | 128d | 1:6 | 57.89 | 65.68 | 71.41 | 59.06 | 45.65 | 56.71 | 46.92 |
111
+
112
+ ## **Quick Start**
113
+
114
+ Install the `sentence-transformers` library:
115
+
116
+ ```bash
117
+ pip install -U sentence-transformers transformers
118
+ ```
119
+
120
+ Generate text embeddings:
121
+ ```python
122
+ from sentence_transformers import SentenceTransformer
123
+
124
+ model = SentenceTransformer("google/embeddinggemma-2")
125
+
126
+ query = "What causes the northern lights?"
127
+ document = "The northern lights are caused by charged particles from the sun."
128
+
129
+ query_emb = model.encode(query, prompt_name="SearchQuery")
130
+ doc_emb = model.encode(document, prompt_name="Document")
131
+ print(model.similarity(query_emb, doc_emb))
132
+ ```
133
+
134
 
135
  ## **Best Practices**
136
 
 
138
 
139
  ### 1. Task Instruction Prefixes
140
 
141
+ EmbeddingGemma 2 is trained with short task instruction prefixes prepended to text inputs. Using the right prefix improves quality; omitting it still works but reduces precision. Prefixes apply to text only. Pass images, video, and audio without any prefix.
142
 
143
  Documents with a real title should be formatted as `title: {title} | text: {content}`. Use `title: none` when no title is available.
144
 
 
149
  * **Asymmetric Tasks (e.g. retrieval):** Use a query prefix for queries and a document prefix for corpus items.
150
  * **Symmetric Tasks (e.g. classification, similarity)**: Apply the same task prefix to all inputs being compared.
151
 
152
+ | Use Case | Task Type | Prompt Name | Query Task Instruction | Document Task Instruction *(use `none` if no title)* |
153
+ | :---- | :---- | :---- | :---- | :---- |
154
+ | Web / document search | Asymmetric | SearchQuery | `task: search result \| query: {query}` | `title: {title} \| text: {content}` |
155
+ | Question answering | Asymmetric | QuestionAnswering | `task: question answering \| query: {question}` | `title: {title} \| text: {passage}` |
156
+ | Fact checking | Asymmetric | FactChecking | `task: fact checking \| query: {claim}` | `title: {title} \| text: {evidence}` |
157
+ | Code search | Asymmetric | CodeRetrieval | `task: code retrieval \| query: {query}` | `title: {title or filename} \| text: {code}` |
158
+ | Text classification | Symmetric | Classification | `task: classification \| query: {content}` | N/A |
159
+ | Clustering | Symmetric | Clustering | `task: clustering \| query: {content}` | N/A |
160
+ | Measuring similarity | Symmetric | SentenceSimilarity | `task: sentence similarity \| query: {content}` | N/A |
161
 
162
+ **Please note:** `prompt_name="Document"` applies `title: none`; titled documents must still be formatted manually, like `model.encode(f"title: {title} | text: {document}")`
163
 
164
  ### 2. Selective Encoder Loading
165
 
166
+ The vision and audio encoders are independent components. To reduce memory consumption when deploying text-only or single-modality pipelines, disable unused modality encoders via SentenceTransformer’s `config_kwargs`:
167
 
168
  **Please note:** configuring EmbeddingGemma 2 to load with omission of some encoders differs among model libraries; please refer to the appropriate documentation for more information on this.
169
 
170
+ | Active Modalities | `config_kwargs` | Effective Size |
171
+ | :---- | :---- | :---- |
172
+ | Text only | `{"vision_config": None, "audio_config": None}` | 270M |
173
+ | Text and image | `{"audio_config": None}` | 440M |
174
+ | Text and audio | `{"vision_config": None}` | 570M |
175
+ | Full multimodal | `{}` | 740M |
176
 
177
+ ### 3. Matryoshka Dimension Truncation
178
 
179
+ EmbeddingGemma 2 is trained with Matryoshka Representation Learning, so the 768-dimensional output vector can be shortened by keeping only its leading dimensions. The supported dimensions are 768, 512, 256, and 128\. Shorter vectors reduce storage and speed up similarity search at some cost to quality.
180
 
181
  At runtime, please adhere to the following guidelines:
182
 
183
  * **Re-normalize after truncating:** slicing a unit-length vector does not preserve unit length. The shortened vector must be L2-normalized before it is used for cosine similarity. Skipping this step degrades ranking quality silently — it produces plausible-looking scores rather than an error.
184
+ * **Queries and documents must share a dimension.** A 768-dimensional query cannot be scored against a 128-dimensional corpus.
185
+ *
186
+ To avoid truncating and normalizing yourself, you should pass in the `truncate_dim` and `normalize_embeddings` fields when calling `model.encode()`:
187
 
188
+ ```python
189
+ query_emb = model.encode(
190
+ query,
191
+ truncate_dim=128, # or 512, 256
192
+ normalize_embeddings=True,
193
+ )
194
+ ```
195
+
196
+ Model quality at each dimension is reported in the truncation table in the **Benchmark Results** section above. Quality is close to lossless down to 256 dimensions. 128 dimensions degrades multimodal quality substantially and should be validated against your own workload before adoption.
197
 
198
  ### 4. Numerical Precision
199
 
 
203
 
204
  `bfloat16` is safe, and carries the same 8-bit exponent as `float32`. This is the recommended default on hardware with native support, and halves memory use relative to `float32`. Use `float32` elsewhere, including on most CPUs.
205
 
206
+ In SentenceTransformers, you can set this via `model_kwargs`, and determine it programmatically via `is_bf16_supported`:
207
+
208
+ ```python
209
+ dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float32
210
+
211
+ model = SentenceTransformer("google/embeddinggemma-2", model_kwargs={"torch_dtype": dtype})
212
+ ```
213
+
214
+ ### 5. Multimodal Input
215
+
216
+ #### Interleaving
217
 
218
  A single input may mix text with images, video, and audio, using a single shared 8,192-token context.
219
 
 
223
  * `<|video|>` marks the position of video input
224
  * `<|audio|>` marks the position of audio input&nbsp;
225
 
226
+ For example, a product listing indexed for search might be encoded as:
227
 
228
+ ```python
229
+ emb_interleaved = model.encode({
230
+ "text": "Waterproof running shoes. <|image|> Featuring a breathable mesh upper. <|image|> Grip test on wet rock: <|video|>",
231
+ "image": ["shoe.jpg", "mesh.jpg"],
232
+ "video": "demo.mp4",
233
+ })
234
+ ```
235
 
236
+ Each placeholder in text is filled from the corresponding key, in order. The call returns a single embedding that represents the text, images and video together. This embedding can be compared directly against any other EmbeddingGemma 2 embedding; for example, a text-only query such as “waterproof shoes for trail running”.
237
 
238
+ #### Context Limits
239
 
240
  All modalities share a single 8,192-token context window. Each modality consumes that budget at a fixed rate:
241
 
 
246
  | *Video* | 140 tokens per frame *(default)* | ~58 frames |
247
  | *Audio* | 25 tokens per second | ~327 seconds |
248
 
249
+ Note max input for images and video can be as high as ~114 images or frames when using a lower vision token budget (described below).
250
+
251
  The maximums above assume a single modality with no accompanying text. Interleaved inputs draw from the same budget, so mixing modalities reduces the amount of each that fits. Note also max input for images and video can be as high as \~114 images or frames when using a lower vision token budget (described below).
252
 
253
+ #### Vision Token Budget
254
 
255
+ Model users can forgo the default sequence length for input images / video frames to represent images with “soft token” amounts ranging from 70 to 1120\. Increasing the vision budget for input images trades latency and token count for quality.&nbsp;
256
 
257
+ In general, higher input sequence lengths capture more information. Scaling up input sequence length means more expressive input representations; so, increasing input sequence length will improve fine-grained visual understanding, uplifting embedding quality and performance in downstream use cases.
 
258
 
259
+ #### Input Sampling Defaults
260
 
261
+ Audio and video inputs have the following default sampling rates:
262
 
263
+ * By default, video is processed as sampled frames through the vision encoder at **1 frame per second;** FPS sampling rate is configurable.
264
+ * Audio should be supplied as **mono at 16 kHz.**
265
 
266
  ## **Model Data**
267
 
 
317
  * Open-ended or highly complex tasks might be challenging.
318
  * A model's performance can be influenced by the amount of context provided (longer context generally leads to better outputs, up to a certain point).
319
  * **Language Ambiguity and Nuance:** Natural language is inherently complex. Models might struggle to grasp subtle nuances, sarcasm, or figurative language.
320
+ * **Task Instruction Usage:** For text tasks, omitting the recommended task prefix may lead to sub-optimal embedding quality.
321
 
322
  ### Ethical Considerations and Risks
323