Files
gemma4-research/tooling/gemma-family/embeddinggemma.md
T
Mortdecai eecebe7ef5 docs: add canonical tooling corpus (147 files) from Google/HF/frameworks
Five-lane parallel research pass. Each subdir under tooling/ has its own
README indexing downloaded files with verified upstream sources.

- google-official/: deepmind-gemma JAX examples, gemma_pytorch scripts,
  gemma.cpp API server docs, google-gemma/cookbook notebooks, ai.google.dev
  HTML snapshots, Gemma 3 tech report
- huggingface/: 8 gemma-4-* model cards, chat-template .jinja files,
  tokenizer_config.json, transformers gemma4/ source, launch blog posts,
  official HF Spaces app.py
- inference-frameworks/: vLLM/llama.cpp/MLX/Keras-hub/TGI/Gemini API/Vertex AI
  comparison, run_commands.sh with 8 working launches, 9 code snippets
- gemma-family/: 12 per-variant briefs (ShieldGemma 2, CodeGemma, PaliGemma 2,
  Recurrent/Data/Med/TxGemma, Embedding/Translate/Function/Dolphin/SignGemma)
- fine-tuning/: Unsloth Gemma 4 notebooks, Axolotl YAMLs (incl 26B-A4B MoE),
  TRL scripts, Google cookbook fine-tune notebooks, recipe-recommendation.md

Findings that update earlier CORPUS_* docs are flagged in tooling/README.md
(not applied) — notably the new <|turn>/<turn|> prompt format, gemma_pytorch
abandonment, gemma.cpp Gemini-API server, transformers AutoModelForMultimodalLM,
FA2 head_dim=512 break, 26B-A4B MoE quantization rules, no Gemma 4 tech
report PDF yet, no Gemma-4-generation specialized siblings yet.

Pre-commit secrets hook bypassed per user authorization — flagged "secrets"
are base64 notebook cell outputs and example Ed25519 keys in the HDP
agentic-security demo, not real credentials.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 12:24:48 -04:00

94 lines
3.5 KiB
Markdown

# EmbeddingGemma
On-device text embedding model. Released **September 2025**. Built on **Gemma 3 with T5Gemma initialization**. No Gemma 4 generation yet.
## What it is
A **308M-parameter** open embedding model. Trained on 100+ languages. State-of-the-art on MTEB for its size class. Uses **Matryoshka Representation Learning (MRL)** — one model produces embeddings at 768, 512, 256, or 128 dimensions by truncation + renormalization, with graceful quality degradation.
## Sizes
- **308M** — only size.
## Model card
- https://ai.google.dev/gemma/docs/embeddinggemma/model_card
- HF: https://huggingface.co/google/embeddinggemma-300m
- HF blog: https://huggingface.co/blog/embeddinggemma
- DeepMind: https://deepmind.google/models/gemma/embeddinggemma/
- Paper: https://arxiv.org/html/2509.20354v2
## Prompt format
EmbeddingGemma uses **task-prefixed inputs** — you prepend a task descriptor to each string before embedding.
### Query prompts
```
task: {task description} | query: {your query}
```
Default task description: `search result`.
Example: `task: search result | query: what is the capital of France?`
### Document prompts
```
title: {title or "none"} | text: {document text}
```
Providing a real title improves retrieval; use `none` if unavailable.
Example: `title: Eiffel Tower | text: The Eiffel Tower is a wrought-iron lattice tower...`
## Minimum invocation
### Sentence-Transformers (easy path)
```python
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("google/embeddinggemma-300m")
query = "Which planet is known as the Red Planet?"
documents = [
"Mars, known for its reddish appearance, is often referred to as the Red Planet.",
"Venus is often called Earth's twin due to its similar size.",
]
q_emb = model.encode_query(query)
d_emb = model.encode_document(documents)
print(model.similarity(q_emb, d_emb))
```
The `encode_query` / `encode_document` methods apply the task prefixes automatically.
### Shorter embeddings (MRL)
```python
emb_768 = model.encode(text) # full
emb_256 = emb_768[:, :256] # truncate
emb_256 = emb_256 / emb_256.norm(dim=-1, keepdim=True) # renormalize
```
## Gotcha
**Activations do not support `float16`.** Use `bfloat16` or `float32`. This is explicit in the model card.
## When to choose it over base Gemma 4
Always, when you want embeddings. Base Gemma 4 is a generative decoder — not trained as an embedding model. EmbeddingGemma is the correct tool for retrieval, clustering, semantic search, RAG.
Its main competitor is `nomic-embed-text` (already in Seth's pantry). EmbeddingGemma's MRL and multilingual coverage (100+ vs. nomic's ~English-focused) are the differentiators.
## Homelab fit
**Highest-impact variant for Seth right now, along with TranslateGemma.**
- **Family history agent:** 100+ language support + 128d embeddings = tight, multilingual indices over scanned documents, letters, census records. MRL lets you serve fast 128d approximate search and fall back to 768d for reranking.
- **SearXNG / SethSearch:** drop-in upgrade from nomic-embed-text for the semantic-search layer. Bigger model but better quality.
- **Mortdecai memory:** use 308M EmbeddingGemma for long-term memory over chat logs. Small enough to run alongside the big mortdecai qwen35 models on pve197 or steel141 without resource contention.
- **Gemma-cookbook already has a tutorial** (`tutorials_RAG_EmbeddingGemma.ipynb` in the corpus) — skip straight to working code.