EmbeddingGemma 2
A multimodal embedding model on Gemma 4 for text, image, audio, and video
One model, one vector space
EmbeddingGemma 2 encodes text, images, audio, and video—individually or combined—into a shared 768-dimensional dense vector space. It supports cross-modal retrieval, semantic similarity, clustering, and classification, built on the Gemma 4 architecture.
What makes it powerful
- Unified multimodal space: compare any modality or combination.
- MRL: truncate 768-d embeddings to 512/256/128 with minimal quality loss.
- Configurable budgets: tune image/frame tokens and video sampling.
- Selective towers: disable unused vision/audio modules to save memory.
Unified multimodal vector space
Vision (Gemma4VisionModel) and audio (Gemma4AudioModel) towers project into a bidirectional text encoder (EmbeddingGemma2TextModel) with full + sliding-window attention and Per-Layer Embeddings (PLE). Single or mixed inputs map to one comparable embedding.
Matryoshka embeddings (MRL)
Full output dimension from the embedding projection head.
Shorter prefixes for faster retrieval and smaller indexes.
Most compact option; slice then re-normalize to unit length.
Budgets and selective loading
Visual/video budgets: set max_soft_tokens per image/frame (70–1120). Video defaults: fps=1, max_frames=32, uniform overflow, no timestamps.
Selective tower loading: set vision_config=None and/or audio_config=None to skip unused towers and reduce memory for text-only or single-modality deployments.
Text retrieval with Sentence Transformers
Sentence Transformers (≥6.1.0) handles prompts, pooling, and normalization automatically.
Manual pooling with AutoModel
- Load AutoModel and AutoTokenizer for google/embeddinggemma-2.
- Prepend task prompts to plain text (or use chat template/processor for multimodal).
- Mask-aware mean-pool last_hidden_state, then L2-normalize in float32.
- Compare embeddings via cosine similarity (dot product after normalization).
Optional prompts for better alignment
- query / document: search and retrieval use cases.
- Classification / Clustering: labeled grouping tasks.
- STS / SentenceSimilarity: semantic matching tasks.
- QuestionAnswering / FactChecking
- CodeRetrieval / InstructionRetrieval
- Prompts are optional—evaluate with and without on your domain.
Slice, re-normalize, compare
- Encode to 768-d, then truncate to the same prefix length for queries and documents (e.g. 256).
- Always normalize after slicing because the prefix is no longer unit-length.
- In Sentence Transformers, use truncate_dim and re-normalize the tensor output.
- Result: smaller index, faster retrieval, largely preserved ranking quality.
Single modalities & cross-modal retrieval
Each modality can be embedded alone and compared against any other in the same 768-d space. Example: encode text, image, audio, and video candidates, then rank them against a text query with cosine similarity for true cross-modal search.
Use dictionaries like {"image": ...}, {"audio": ...}, {"video": ...} in Sentence Transformers.
Composed embeddings
- Combine modalities into one joint embedding: text + image, text + audio, image + audio.
- Multiple items are supported (e.g. several images plus audio and text in one input).
- No text is required for some combinations, enabling flexible multimodal queries and documents.
Ordering and placeholders
Automatic ordering: when no placeholders appear, modalities follow the input/content order after any system prompt.
Manual placeholders: place <|image|>, <|video|>, <|audio|> in text to interleave media at exact positions; counts must match supplied inputs.
Heterogeneous batching
A single batch can mix plain text, single-modality media, and composed multimodal inputs in one forward pass. Each row yields the same embedding as encoding items individually, simplifying pipelines that index or search across diverse content types.
Disable unused modality towers
Full model parameters with all towers loaded.
- 439M without audio tower.
- 271M with neither vision nor audio tower.
- Set via config (vision_config=None, audio_config=None).
Visual token budget
EmbeddingGemma2Processor controls preprocessing for text, images, audio, and video. Tune max_soft_tokens per image or frame to balance detail vs speed/length.
Lower budgets reduce sequence length and latency.
Video frame sampling
- fps (default 1): target sampling rate (needs valid video metadata).
- max_frames (default 32): cap frames per clip.
- overflow_strategy: uniform (resample) or truncate.
- add_timestamps: prefix frames with mm:ss (requires valid fps metadata).
Chat template and direct calls
Recommended: use processor.apply_chat_template(...) for multimodal conversations and precise control over interleaving and ordering.
Direct calls: processor(...) can synthesize placeholders when text=None, and supports text-only, media-only, nested batches, or manual placeholders.
Why use EmbeddingGemma 2?
- Unified embeddings for text, image, audio, and video in one shared space.
- Flexible retrieval: cross-modal, composed, and mixed-modality batching.
- Scalable performance via MRL truncation and modality tower selection.
- Fine-grained preprocessing controls for visual tokens and video sampling.
Explore & Build
Checkpoint: google/embeddinggemma-2
- Hugging Face docs: /docs/transformers/en/model_doc/embedding_gemma2
- Collection: huggingface.co/collections/google/embeddinggemma
- Use Sentence Transformers for easiest multimodal embedding workflows.