embeddinggemma-2 / PYTHON.md
changh95's picture
tt-model push embeddinggemma-2 (container)
5c33f00 verified
|
Raw History Blame Contribute Delete
3.5 kB

EmbeddingGemma 2 on Tenstorrent p150: Python API

tt_embgemma runs google/embeddinggemma-2 (text incl. code, images, video, audio -> one 768-d space, Matryoshka 512 / 256 / 128) on one Tenstorrent Blackhole p150. It needs tt-metal's ttnn built at the commit in patches/README.md with both patches applied (ETH dispatch, 12 x 10 grid), plus numpy<2, safetensors, huggingface_hub, tokenizers, pillow (and optionally scipy, soundfile) (pip install -e . from the repo root).

from tt_embgemma.api import EmbeddingGemma2

m = EmbeddingGemma2.from_pretrained()     # weights (pinned revision) -> device (ETH, 12x10) -> compile + trace buckets
q = m.embed(["What causes the northern lights?"], prompt_name="SearchQuery")            # [1, 768] float32, unit norm
d = m.embed(["The northern lights are caused by charged particles."], prompt_name="Document")
print(q @ d.T)
from PIL import Image
v = m.embed([{"image": Image.open("photo.jpg")}], dimensions=256)                     # [1, 256], re-normalized
a = m.embed([{"audio": {"array": waveform, "sampling_rate": 16000}}])                 # speech / sounds
x = m.embed([{"text": "Product photo: <|image|> and a demo <|video|>", "image": [img],
              "video": {"frames": frames, "fps": 25.0}}])                             # one interleaved embedding
t = m.embed(["one text"], output="tokens")                                            # [[n_tokens, 768]]
m.close()

embed(inputs, prompt_name=None, prompt=None, output="sentence", dimensions=768, normalize=True, max_length=None, truncate="right", image_tokens=280, video_tokens=140, video_fps=1.0, video_max_frames=None) equals SentenceTransformer("google/embeddinggemma-2").encode(inputs, prompt_name=..., truncate_dim=..., normalize_embeddings=...) for the same inputs:

argument values default
inputs list of strings, dicts with text / image / video / audio keys (lists allowed, key order = sequence order), or {"content": [{"type": ...}, ...]}; images as PIL / HxWx3 uint8 / encoded bytes; audio as {"array", "sampling_rate"} or WAV bytes; video as {"frames", "fps", "duration"} required
prompt_name / prompt one of the 20 task names of config_sentence_transformers.json (m.tok.prompts) / free text none
dimensions 768, 512, 256, 128 768
normalize re-normalize after truncation True
output sentence (pooled), tokens (list of [n, 768]) sentence
max_length, truncate <= EMBGEMMA_MAX_SEQ_LEN; right / error max, right
image_tokens, video_tokens 70, 140, 280 (560, 1120 if enabled) 280, 140
video_fps, video_max_frames > 0; 1 .. 32 1.0, 32

from_pretrained(model_dir=None, config=None, device=None): weights from model_dir / EMBGEMMA_WEIGHTS_DIR / the Hugging Face snapshot at the pinned revision; every other server-start setting comes from the EMBGEMMA_* environment (tt_embgemma.config.resolve_config, the table in SERVING.md). EMBGEMMA_TEXT_ENGINE=cpu runs the float32 clean-room reference without a device (slow; tests).

Lower level: m.prepare(parse_input(x, prompt), m.options(...)) does the host preprocessing of one input; m.run(prepared_list) runs the device stages and returns the token-embedding sums, the token counts and (optionally) the token embeddings; tt_embgemma.pipeline.finish does the mean / MRL / normalization.