Download code/tt_embgemma/server/fake_device.py from changh95/embeddinggemma-2: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/changh95/embeddinggemma-2/resolve/main/code/tt_embgemma/server/fake_device.py
- Command line
-
hf download hf://changh95/embeddinggemma-2/code/tt_embgemma/server/fake_device.py
-
curl -L -o fake_device.py https://huggingface.co/changh95/embeddinggemma-2/resolve/main/code/tt_embgemma/server/fake_device.py
2.78 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """A fake device engine for host tests (no ttnn, no torch, no weights): deterministic pseudo rows with the engine | |
| interface (``vision`` / ``audio`` / ``text``), and ``FakeModel`` = the real ``EmbeddingGemma2`` host side (tokenizer, | |
| front ends, MRL / normalize) on top of it, so the HTTP layer, input parsing, batching and post-processing are tested | |
| without a chip.""" | |
| from __future__ import annotations | |
| import threading | |
| from typing import List, Sequence | |
| import numpy as np | |
| from .. import model_spec as S | |
| def _rows(seed: int, n: int, d: int) -> np.ndarray: | |
| rng = np.random.default_rng(seed & 0x7FFFFFFF) | |
| return rng.standard_normal((n, d)).astype(np.float32) | |
| class FakeTable: | |
| def rows(self, ids): | |
| ids = np.asarray(ids, dtype=np.int64) | |
| return np.stack([_rows(int(t) * 1009 + 17, 1, S.HIDDEN)[0] for t in ids]) if len(ids) else \ | |
| np.zeros((0, S.HIDDEN), np.float32) | |
| class FakeEngine: | |
| name = "fake" | |
| def __init__(self, max_batch: int = 32): | |
| self.max_batch = max_batch | |
| self.calls: List[tuple] = [] | |
| self.lock = threading.Lock() | |
| self._proj = _rows(99, S.HIDDEN, S.EMB_DIM) / np.sqrt(S.HIDDEN) | |
| def vision(self, patches: Sequence) -> List[np.ndarray]: | |
| with self.lock: | |
| self.calls.append(("vision", len(patches))) | |
| return [_rows(int(p.pixels.sum() * 1000) + p.n_patches, p.n_tokens, S.HIDDEN) for p in patches] | |
| def audio(self, clips: Sequence) -> List[np.ndarray]: | |
| from ..preprocess.audio import n_tokens | |
| with self.lock: | |
| self.calls.append(("audio", len(clips))) | |
| return [_rows(int(abs(f).sum() * 100) + len(m), n_tokens(m), S.HIDDEN) for f, m in clips] | |
| def text(self, xs: Sequence[np.ndarray], want_tokens: bool = False): | |
| with self.lock: | |
| self.calls.append(("text", len(xs))) | |
| toks = [np.tanh(x) @ self._proj for x in xs] | |
| sums = np.stack([t.astype(np.float64).sum(0) for t in toks]).astype(np.float32) | |
| return sums, (toks if want_tokens else None) | |
| def describe(self): | |
| return {"engine": "fake"} | |
| def close(self): | |
| pass | |
| def FakeModel(model_dir: str, config=None): | |
| """``EmbeddingGemma2`` with the real tokenizer and front ends, a fake table and a fake engine.""" | |
| from .. import config as C | |
| from ..api import EmbeddingGemma2 | |
| from ..preprocess.text import Tokenizer | |
| cfg = config or C.ServeConfig(text_engine="cpu") | |
| m = EmbeddingGemma2(cfg, Tokenizer(model_dir), FakeTable(), FakeEngine(cfg.max_batch), None, | |
| {"engine": "fake", "device": {"dispatch": "none", "grid": "0x0"}}) | |
| m.closed = False | |
| def close(): | |
| m.closed = True | |
| m.close = close | |
| return m | |