File size: 2,583 Bytes
5c33f00 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | # SPDX-License-Identifier: Apache-2.0
"""Host tests of the front ends against the official fp32 goldens (G0): token ids, image pixels / positions,
log-mel features / masks, video frame sampling; skipped when the goldens are not on this machine."""
import os
import numpy as np
import pytest
from tt_embgemma.tests.goldens import MODEL, golden_available, golden_module, load
pytestmark = pytest.mark.skipif(not (golden_available() and MODEL), reason="goldens (EMBGEMMA_GOLDEN) or weights not available")
@pytest.fixture(scope="module")
def tok():
from tt_embgemma.preprocess.text import Tokenizer
return Tokenizer(MODEL)
def test_text_ids(tok):
from tt_embgemma import pipeline as P
for tag in ("text_fp32_hooks", "retrieval_fp32"):
g, gm = load(tag)
for c in gm["cases"]:
pn = c["prompt_name"]
pr = P.prepare(tok, P.Item([("text", c["text"])], tok.prompts[pn] if pn else ""), P.Options())
assert pr.seq.ids == g[f"{c['key']}.ids"].tolist(), c["key"]
def test_image_pixels(tok):
from tt_embgemma.preprocess.image import preprocess_image
G = golden_module("gen_golden")
if G is None:
pytest.skip("EMBGEMMA_GOLDEN_SRC (research/embgemma/golden) not set: the golden images are loaded by its scripts")
g, gm = load("image_fp32_hooks")
for c in gm["cases"][:6]:
p = preprocess_image(G.load_image(c["path"]), c["max_soft_tokens"])
gp = g[f"{c['key']}.pixel_values"][0][: p.n_patches]
assert np.array_equal(p.pixels, gp), c["key"]
assert np.array_equal(p.positions, g[f"{c['key']}.image_position_ids"][0][: p.n_patches])
def test_log_mel():
from tt_embgemma.preprocess.audio import log_mel
g, gm = load("audio_fp32_hooks")
for c in gm["cases"]:
k = c["key"]
f, m = log_mel(g[f"{k}.waveform"])
assert f.shape == g[f"{k}.input_features"][0].shape, k
assert np.array_equal(m, g[f"{k}.input_features_mask"][0].astype(bool)), k
assert np.abs(f - g[f"{k}.input_features"][0]).max() < 1e-5, k
def test_placeholder_rules(tok):
from tt_embgemma import model_spec as S
seq = tok.build([("text", "a <|image|> b"), ("image", 0)], "", {("image", 0): 3})
assert seq.ids.count(S.IMAGE_TOKEN_ID) == 3 and seq.slots[0].count == 3
seq2 = tok.build([("image", 0), ("text", "x")], "", {("image", 0): 2})
assert seq2.ids[1] == S.BOI_ID and seq2.ids[1 + 3] == S.EOI_ID
with pytest.raises(ValueError):
tok.build([("text", "<|image|> <|image|>"), ("image", 0)], "", {("image", 0): 2})
|