Download code/models/autoports/google_embeddinggemma_2/demo/hf_reference.py from tt-hous/embeddinggemma-2: direct link, hf CLI and curl.
- Browser
- Download file 4.03 kB
-
https://huggingface.co/tt-hous/embeddinggemma-2/resolve/main/code/models/autoports/google_embeddinggemma_2/demo/hf_reference.py
- Command line
-
hf download hf://tt-hous/embeddinggemma-2/code/models/autoports/google_embeddinggemma_2/demo/hf_reference.py
-
curl -L -o hf_reference.py https://huggingface.co/tt-hous/embeddinggemma-2/resolve/main/code/models/autoports/google_embeddinggemma_2/demo/hf_reference.py
4.03 kB
| # SPDX-FileCopyrightText: © 2026 Tenstorrent USA, Inc. | |
| # SPDX-License-Identifier: Apache-2.0 | |
| import argparse | |
| import json | |
| import time | |
| from concurrent.futures import Future | |
| from pathlib import Path | |
| import numpy as np | |
| from models.autoports.google_embeddinggemma_2.demo.index import ( | |
| LIBRARY_DIR, | |
| DemoIndex, | |
| audio_input, | |
| load_audio, | |
| uniform_times, | |
| video_duration, | |
| video_frames_at, | |
| video_input, | |
| ) | |
| from models.autoports.google_embeddinggemma_2.server.engine import HF_REVISION | |
| ALL_KINDS = {"image", "video", "audio", "text"} | |
| class HFEngine: | |
| def __init__(self, st): | |
| self.st = st | |
| self.policy_id = "hf-fp32-cpu" | |
| self.prompts = st.prompts | |
| def prepare(self, inputs, input_type=None): | |
| prompt = self.prompts.get(input_type, "") if input_type else "" | |
| return [(x, prompt) for x in inputs] | |
| def submit(self, items, dim): | |
| f = Future() | |
| out = [] | |
| for x, prompt in items: | |
| v = self.st.encode([x], prompt=prompt, convert_to_numpy=True, normalize_embeddings=True)[0] | |
| v = np.asarray(v, dtype=np.float32)[:dim] | |
| out.append(v / np.linalg.norm(v)) | |
| f.set_result(out) | |
| return f | |
| def query_input(q, lib): | |
| kind = q["query_kind"] | |
| if kind == "text": | |
| return q["query"], "text" | |
| p = lib / q["query"] | |
| if kind == "image": | |
| from PIL import Image | |
| return {"image": Image.open(p).convert("RGB")}, "image" | |
| if kind == "audio": | |
| return audio_input(load_audio(p)), "audio" | |
| return video_input(video_frames_at(p, uniform_times(0.0, video_duration(p), 8))), "video" | |
| def hits_for(index, q, vec): | |
| tab, exp = q["tab"], q.get("expect") | |
| if tab == "media": | |
| return index.search(vec, 768, kinds=ALL_KINDS, k=5) | |
| if tab == "moments": | |
| if isinstance(exp, dict): | |
| return index.search(vec, 768, kinds={"video"}, k=3, windows=True, parent=exp["id"]) | |
| return index.search(vec, 768, kinds={"video"}, k=3) | |
| if tab == "audio": | |
| return index.search(vec, 768, kinds={"audio"}, k=5, windows=isinstance(exp, dict)) | |
| if tab == "code": | |
| return index.search(vec, 768, kinds={"code"}, k=5) | |
| if tab == "multilingual": | |
| return index.search(vec, 768, kinds={"text"}, k=5) | |
| return index.search(vec, 768, k=10) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--library", default=str(LIBRARY_DIR)) | |
| ap.add_argument("--cache", required=True) | |
| ap.add_argument("--out", required=True) | |
| args = ap.parse_args() | |
| import torch | |
| from huggingface_hub import snapshot_download | |
| from sentence_transformers import SentenceTransformer | |
| torch.set_grad_enabled(False) | |
| snap = snapshot_download("google/embeddinggemma-2", revision=HF_REVISION, local_files_only=True) | |
| st = SentenceTransformer(snap, model_kwargs={"torch_dtype": torch.float32}, local_files_only=True, device="cpu") | |
| lib = Path(args.library) | |
| index = DemoIndex(engine=HFEngine(st), library_dir=lib, cache_dir=args.cache) | |
| t0 = time.perf_counter() | |
| index.build() | |
| build_s = time.perf_counter() - t0 | |
| queries = json.loads((lib / "queries.json").read_text()) | |
| if isinstance(queries, dict): | |
| queries = queries["queries"] | |
| records = [] | |
| for q in queries: | |
| x, kind = query_input(q, lib) | |
| vec, _ = index.embed_query(x, kind) | |
| hits = hits_for(index, q, vec) | |
| records.append( | |
| { | |
| "tab": q["tab"], | |
| "query": q["query"], | |
| "top": [ | |
| {"id": e.parent or e.id, "score": s, "start_s": e.start_s, "end_s": e.end_s} for e, s in hits[:5] | |
| ], | |
| } | |
| ) | |
| out = {"engine": "HF sentence-transformers fp32 CPU", "build_s": round(build_s, 1), "records": records} | |
| Path(args.out).parent.mkdir(parents=True, exist_ok=True) | |
| Path(args.out).write_text(json.dumps(out, indent=1, ensure_ascii=False) + "\n") | |
| print(args.out) | |
| if __name__ == "__main__": | |
| main() | |