Download code/tt_minilm/tests/test_host_api.py from changh95/all-MiniLM-L6-v2: direct link, hf CLI and curl.
- Browser
- Download file 2.65 kB
-
https://huggingface.co/changh95/all-MiniLM-L6-v2/resolve/main/code/tt_minilm/tests/test_host_api.py
- Command line
-
hf download hf://changh95/all-MiniLM-L6-v2/code/tt_minilm/tests/test_host_api.py
-
curl -L -o test_host_api.py https://huggingface.co/changh95/all-MiniLM-L6-v2/resolve/main/code/tt_minilm/tests/test_host_api.py
2.65 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """Host test of the Python API on the CPU engine (no device): encode == the official model.encode outputs, options, | |
| batching split, errors.""" | |
| import os | |
| import numpy as np | |
| import pytest | |
| from tt_minilm.tests.gates import model_dir # noqa: E402 | |
| MODEL_DIR = model_dir() | |
| pytestmark = pytest.mark.skipif(not os.path.isfile(os.path.join(MODEL_DIR, "model.safetensors")), | |
| reason="weights not available") | |
| def model(): | |
| from tt_minilm import config as C | |
| from tt_minilm.api import MiniLM | |
| cfg = C.resolve_config({"MINILM_ENGINE": "cpu", "MINILM_MAX_BATCH": "3", "MINILM_CPU_THREADS": "2"}) | |
| with MiniLM.from_pretrained(weights_dir=MODEL_DIR, config=cfg, warmup=False) as m: | |
| yield m | |
| def test_encode_matches_official(model): | |
| import json | |
| from pathlib import Path | |
| ref = json.loads((Path(__file__).resolve().parents[1] / "samples" / "smoke_reference.json").read_text()) | |
| e = model.encode(ref["texts"]) | |
| w = np.asarray(ref["embeddings"], dtype=np.float32) | |
| cos = (e * w).sum(1) / np.linalg.norm(w, axis=1) | |
| assert e.shape == (6, 384) and cos.min() > 0.999999 | |
| assert np.allclose(np.linalg.norm(e, axis=1), 1, atol=1e-5) | |
| one = model.encode(ref["texts"][0]) | |
| assert one.shape == (384,) and np.allclose(one, e[0], atol=1e-6) | |
| def test_options(model): | |
| t = ["a short one", "a somewhat longer sentence about tokenizers"] | |
| raw = model.encode(t, normalize=False) | |
| assert not np.allclose(np.linalg.norm(raw, axis=1), 1) | |
| toks = model.encode(t, pooling="none", normalize=False) | |
| ids = model.tokenize(t) | |
| assert [x.shape for x in toks] == [(len(i), 384) for i in ids] | |
| assert np.allclose(raw[0], toks[0].mean(0), atol=1e-5) | |
| assert np.allclose(model.encode(t, pooling="cls", normalize=False)[1], toks[1][0], atol=1e-6) | |
| assert np.allclose(model.encode(t, pooling="max", normalize=False)[1], toks[1].max(0), atol=1e-6) | |
| timing = {} | |
| model.encode(t * 4, timing=timing) # 8 texts, max batch 3: 3 device batches | |
| assert timing["tokens"] == [len(i) for i in ids] * 4 | |
| def test_errors(model): | |
| from tt_minilm.api import InputError | |
| with pytest.raises(InputError): | |
| model.encode("x", pooling="avg") | |
| with pytest.raises(InputError): | |
| model.encode("x", max_length=1) | |
| with pytest.raises(InputError): | |
| model.encode("word " * 400, truncate="error") | |
| with pytest.raises(InputError): | |
| model.encode(["ok", 3]) | |
| assert model.encode([]).shape == (0, 384) | |
| info = model.info | |
| assert info["engine"] == "cpu" and info["dim"] == 384 | |