EmbeddingGemma 300M 版本說明
TL;DR
Google 發布了 EmbeddingGemma,一款 308 M 參數的多語言純文字嵌入模型,具備 2 K 令牌的上下文窗口,設計用於裝置端檢索、代理以及其他低延遲應用。它在 500 M 參數以下的模型中於 Massive Text Embedding Benchmark (MTEB) 中名列前茅,且支援超過 100 種語言。
Introduction – A compact, high‑performing multilingual embedder
EmbeddingGemma 是 Google DeepMind 最新推出的小規模多語言嵌入模型。量化後僅有 308 M 參數 且佔用 200 MB RAM 以下,於 Massive Multilingual Text Embedding Benchmark (MMTEB) 上提供最先進的表現,同時足夠小巧,可部署於行動裝置與邊緣設備。模型在每次前向傳播中處理 2048 令牌,輸出 768 維 向量,並可透過 Matryoshka Representation Learning (MRL) 將維度可選擇性截斷至 512、256 或 128。
Architecture – Encoder‑only Gemma3 with mean‑pooling and dense heads
EmbeddingGemma 重新使用 Gemma3 變換器骨幹,並將注意力機制從因果式改為 雙向注意力,將解碼器架構轉變為編碼器。此編碼器產生的 token 級別嵌入會 平均池化 成單一文本嵌入,接著經過兩層密集層將池化向量映射為 768 維 輸出。模型在一個精心挑選的 約 3200 億 token 多語言語料庫上進行訓練,該語料庫混合了公開的網頁文字、程式碼、技術文件以及合成的任務特定範例,並對 CSAM、敏感資料與低品質內容進行嚴格過濾。
Evaluation – Best‑in‑class results for sub‑500 M models
EmbeddingGemma 在 Multilingual MTEB (v2) 與 English MTEB (v2) 兩套基準上進行測試。儘管規模有限,它始終優於可比的基線模型,且在官方 MTEB 排行榜上是 500 M 參數以下的最高排名純文字多語言嵌入模型。部落格文章提供了多語言與英文軌道的效能圖表,並指出任何在 MTEB 資料超過 20 % 上進行訓練的模型皆被排除,以避免過度擬合。
Prompt design – Task‑specific prefixes are required
EmbeddingGemma 在訓練時使用了一組 任務特定提示詞,必須在輸入前加上這些前綴才能獲得最佳效能。最常見的提示詞如下:
query:"task: search result | query: "document:"title: none | text: "
其他提示詞涵蓋 BitextMining、Clustering、Classification、InstructionRetrieval、MultilabelClassification、PairClassification、Reranking、STS 與 Summarization。在 Sentence‑Transformers 函式庫中,model.encode_query 與 model.encode_document 會自動加入 query 與 document 提示詞;其他框架則需手動指定。
Usage across ecosystems – Ready‑to‑run examples
EmbeddingGemma 已整合至多個流行的檢索與 LLM 工具套件。以下提供簡潔且自包含的程式碼片段,示範如何取得嵌入並執行相似度搜尋。
Sentence‑Transformers (Python)
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("google/embeddinggemma-300m")
query = "Which planet is known as the Red Planet?"
documents = [
"Venus is often called Earth's twin because of its similar size and proximity.",
"Mars, known for its reddish appearance, is often referred to as the Red Planet.",
"Jupiter, the largest planet in our solar system, has a prominent red spot.",
"Saturn, famous for its rings, is sometimes mistaken for the Red Planet."
]
q_emb = model.encode_query(query)
d_emb = model.encode_document(documents)
print(q_emb.shape, d_emb.shape) # (768,) (4, 768)
print(model.similarity(q_emb, d_emb))
此範例正確地對文件進行排序,對於關於火星的句子給予最高相似度。
Dimensionality truncation (Matryoshka)
model = SentenceTransformer("google/embeddinggemma-300m", truncate_dim=256)
q_emb = model.encode_query(query)
d_emb = model.encode_document(documents)
print(q_emb.shape, d_emb.shape) # (256,) (4, 256)
將維度截斷至 256 可減少儲存與計算成本,同時保留排序順序。
LangChain (Python)
from langchain_huggingface.embeddings import HuggingFaceEmbeddings
embedder = HuggingFaceEmbeddings(
model_name="google/embeddinggemma-300m",
query_encode_kwargs={"prompt_name": "query"},
encode_kwargs={"prompt_name": "document"}
)
# Use with FAISS vector store for retrieval as shown in the blog post.
LangChain 使用者必須明確設定 query 與 document 提示詞。
LlamaIndex (Python)
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
emb = HuggingFaceEmbedding(
model_name="google/embeddinggemma-300m",
query_instruction="task: search result | query: ",
text_instruction="title: none | text: "
)
正確產生嵌入亦需使用相同的提示字串。
Haystack, txtai, Transformers.js, ONNX Runtime, and TEI
The blog provides ready‑to‑run scripts for each of these runtimes. All of them follow the same pattern: download the model from the Hub, configure the appropriate prompt, compute embeddings, and perform inner‑product similarity (the training objective). The Text Embeddings Inference (TEI) Docker images (cpu-1.8.1, cuda-1.8.1, etc.) expose an OpenAI‑compatible /v1/embeddings endpoint, and the /embed endpoint additionally supports the prompt_name and dimensions parameters for on‑the‑fly truncation.
Finetuning – Domain adaptation on the MIRIAD medical dataset
EmbeddingGemma 可以使用 Sentence‑Transformers 函式庫進行微調。部落格展示了一個完整的流程,包括:
- 載入基礎模型 (
google/embeddinggemma-300m)。 - 載入 MIRIAD 醫療指令與檢索資料集(10 萬訓練對,1 千驗證,1 千測試)。
- 使用 CachedMultipleNegativesRankingLoss 進行高效的批內負樣本抽樣。
- 在 RTX 3090 上訓練 1 個 epoch(≈5.5 小時),使用混合精度 (
fp16)。 - 使用 InformationRetrievalEvaluator 進行評估,報告 NDCG@10。
基礎模型在 MIRIAD 測試集上取得 0.8340 NDCG@10。微調後,模型 (sentence‑transformers/embeddinggemma-300m‑medical) 達到 0.8862 NDCG@10,超過所有列出的通用嵌入模型,包括較大的 Qwen3‑Embedding‑0.6B(596 M 參數)。
Implications – Enabling on‑device multilingual retrieval
EmbeddingGemma 結合了 小巧尺寸、2 K 上下文 與 多語言覆蓋,使其在延遲、記憶體與頻寬受限的情境中獨具優勢:
- 行動 RAG 流程 可在本地對查詢與文件進行嵌入,減少對雲端 API 的依賴。
- 邊緣代理(例如語音助理、AR/VR 應用)能在不傳輸原始文字的情況下執行語意搜尋。
- 跨語言檢索 由於支援超過 100 種語言,於低功耗裝置上亦可實現。
- Matryoshka 截斷 讓開發者可在精度與儲存、計算之間取得平衡,於一般硬體上建置大規模向量資料庫。
總體而言,EmbeddingGemma 降低了在要求速度與效率的生產環境中部署高品質多語言嵌入的門檻。
Further reading
- 模型卡片: https://huggingface.co/google/embeddinggemma-300m
- Google 官方部落格文章: https://developers.googleblog.com/en/introducing-embeddinggemma/
- 技術報告(arXiv): https://arxiv.org/abs/2509.20354
- Matryoshka 嵌入背景: https://huggingface.co/blog/matryoshka
- Sentence‑Transformers 訓練指南: https://huggingface.co/blog/train-sentence-transformers
Quick start checklist
- 安裝 預覽版 Transformers 套件 (
pip install git+https://github.com/huggingface/transformers@v4.56.0-Embedding-Gemma-preview)。 - 選擇 框架(Sentence‑Transformers、LangChain、LlamaIndex 等)。
- 載入 從 Hub 上的
google/embeddinggemma-300m。 - 套用 適當的提示詞(搜尋使用
query,語料項目使用document)。 - 可選地 透過
truncate_dim截斷嵌入(256、512 或 128)。 - 部署 使用 TEI 或 ONNX Runtime 以進行生產規模的擴展。
Conclusion
EmbeddingGemma 在低於 500 M 參數的套件中,提供了多語言能力、裝置端效能與基準領先品質的罕見結合。其開源可用性、與主要檢索框架的廣泛整合,以及簡潔的微調流程,使其成為開發者在受限硬體上構建下一代語意搜尋與檢索增強生成系統的實用選擇。