"""Tests for model discovery: the Hub search client and its measurements. Nothing here touches the network. Every function that would make a request is monkeypatched, and the safetensors fixtures are packed in the test — the same approach ``tests/test_semantic.py`` uses for the projection tests. The dtype cases matter: the code-tuned sibling this project settled on, ``minishlab/potion-code-16M-v2``, is stored as **F16** (63 457 x 256), while the general ``potion-base-8M`` is F32. Reading an F16 file with the F32 path would silently return garbage rows, so the reader is pinned here. """ import json import os import struct import tempfile import unittest import urllib.parse from unittest import mock from hdc import hub from hdc.semantic import StaticEmbeddings RAW_ONE = { "id": "minishlab/potion-base-8M", "downloads": 447_769, "likes": 12, "library_name": "model2vec", "pipeline_tag": "feature-extraction", "gated": False, "tags": ["model2vec", "license:mit", "feature-extraction"], "siblings": [{"rfilename": "model.safetensors"}, {"rfilename": "tokenizer.json"}], "sha": "abc123", } RAW_TWO = { "id": "jinaai/jina-embeddings-v2-base-code", "downloads": 156_469, "likes": 40, "library_name": "sentence-transformers", "pipeline_tag": "feature-extraction", "gated": True, "tags": ["license:apache-2.0", "sentence-transformers"], "siblings": [{"rfilename": "pytorch_model.bin"}], "sha": "def456", } DTYPE_BYTES = {"F32": 4, "F16": 2, "BF16": 2, "I8": 1} def header(tensors: dict) -> dict: """A parsed safetensors header keyed the way ``hub.safetensors`` returns it. ``bytes`` is derived from shape x dtype, because that is exactly what the real header's ``data_offsets`` gives without reading the tensor buffer. """ return {"repo": "x", "tensors": tensors, "bytes": sum(hub._shape_product(t["shape"]) * DTYPE_BYTES.get(t["dtype"], 4) for t in tensors.values()), "params": sum(hub._shape_product(t["shape"]) for t in tensors.values())} def write_static(directory: str, dtype: str, rows: list[list[float]], vocab: dict | None = None) -> str: """Pack a tiny safetensors file in ``dtype`` plus a tokenizer, no libraries.""" flat = [value for row in rows for value in row] if dtype == "F16": payload = struct.pack("<%de" % len(flat), *flat) elif dtype == "BF16": # bf16 is the top half of the f32 word halves = [struct.unpack("> 16 for value in flat] payload = struct.pack("<%dH" % len(flat), *halves) else: payload = struct.pack("<%df" % len(flat), *flat) meta = json.dumps({ "embeddings": {"dtype": dtype, "shape": [len(rows), len(rows[0])], "data_offsets": [0, len(payload)]} }).encode("utf-8") meta += b" " * ((8 - len(meta) % 8) % 8) with open(os.path.join(directory, "model.safetensors"), "wb") as fh: fh.write(struct.pack(" dict: parsed = urllib.parse.urlparse(self.calls[-1]) return urllib.parse.parse_qs(parsed.query) def test_text_query_keeps_the_task_filter(self): with mock.patch.object(hub, "_get_json", self.fake_get_json): records = hub.search("code embeddings") self.assertEqual(len(records), 2) self.assertEqual(records[0]["id"], RAW_ONE["id"]) query = self.query() self.assertEqual(query["search"], ["code embeddings"]) self.assertEqual(query["filter"], ["feature-extraction"]) def test_author_query_drops_the_filter(self): # measured API quirk: author + filter returns zero rows, silently with mock.patch.object(hub, "_get_json", self.fake_get_json): hub.search(author="minishlab") query = self.query() self.assertNotIn("filter", query) self.assertEqual(query["author"], ["minishlab"]) def test_non_list_payload_is_an_empty_result(self): with mock.patch.object(hub, "_get_json", lambda url, timeout=45: {"error": "nope"}): self.assertEqual(hub.search("anything"), []) class TestCandidatePool(unittest.TestCase): def test_queries_are_de_duplicated_and_ordered_by_downloads(self): shared = hub.normalise(dict(RAW_ONE, id="a/shared", downloads=5)) more = hub.normalise(dict(RAW_ONE, id="b/bigger", downloads=9)) least = hub.normalise(dict(RAW_ONE, id="c/small", downloads=1)) def fake_search(text=None, **kwargs): return [shared, more, least, more] # the same repo from two queries with mock.patch.object(hub, "search", fake_search): pool = hub.candidate_pool(limit=10) self.assertEqual([row["id"] for row in pool], ["b/bigger", "a/shared", "c/small"]) def test_limit_is_applied(self): rows = [hub.normalise(dict(RAW_ONE, id="r/%d" % i, downloads=i)) for i in range(5)] with mock.patch.object(hub, "search", lambda *a, **k: rows): self.assertEqual(len(hub.candidate_pool(limit=2)), 2) class TestBakeoff(unittest.TestCase): def test_offline_uses_the_cache_and_touches_no_network(self): entry = {"id": "minishlab/potion-code-16M-v2", "sha": "s", "downloads": 7, "components": {"runtime_free": 1.0}, "total": 1.0, "static": True, "rows": 63457, "dim": 256, "bytes": 32_489_984, "coverage": None, "license": "mit"} with mock.patch.object(hub, "load_cache", lambda: {"minishlab/potion-code-16M-v2@s": entry}), \ mock.patch.object(hub, "save_cache", lambda cache: None), \ mock.patch.object(hub, "candidate_pool", lambda **kwargs: []): result = hub.bakeoff(offline=True) self.assertEqual(result["pool"], 1) self.assertEqual(result["graded"][0]["id"], "minishlab/potion-code-16M-v2") self.assertTrue(result["graded"][0]["cached"]) self.assertEqual(hub.best(result)["total"], 1.0) class TestHeaderRange(unittest.TestCase): def test_two_range_requests_give_shape_size_and_params(self): meta = json.dumps({ "embeddings": {"dtype": "F16", "shape": [2000, 256], "data_offsets": [0, 1_024_000]}, }).encode("utf-8") seen: list[dict] = [] def fake_request(url, headers=None, timeout=45): seen.append(headers or {}) if (headers or {}).get("Range") == "bytes=0-7": return struct.pack(" StaticEmbeddings: tmp = tempfile.TemporaryDirectory() self.addCleanup(tmp.cleanup) return StaticEmbeddings(write_static(tmp.name, dtype, self.ROWS)) def test_float16_rows_decode_exactly(self): model = self.build("F16") self.assertEqual(model.dtype, "F16") self.assertEqual(model.itemsize, 2) self.assertEqual(model.row(0), (1.0, 0.5, -1.5, 0.25)) self.assertEqual(model.row(1), (2.0, 0.0, 1.875, -0.25)) def test_bfloat16_rows_decode_exactly(self): model = self.build("BF16") self.assertEqual(model.dtype, "BF16") self.assertEqual(model.row(0), (1.0, 0.5, -1.5, 0.25)) self.assertEqual(model.row(1), (2.0, 0.0, 1.875, -0.25)) def test_rows_do_not_overlap(self): # the offset is rows * dim * itemsize; a wrong stride returns row 0 twice model = self.build("F16") self.assertNotEqual(model.row(0), model.row(1)) def test_unsupported_dtype_is_rejected(self): tmp = tempfile.TemporaryDirectory() self.addCleanup(tmp.cleanup) write_static(tmp.name, "I8", self.ROWS) with self.assertRaises(ValueError): StaticEmbeddings(tmp.name) class TestCacheLifetime(unittest.TestCase): """The score cache outlives the code that wrote it. An older run of ``python -m hdc hub --offline`` raised ``KeyError`` on a coverage entry that predated a new field, so ``load_cache`` has to drop what it cannot understand instead of handing it to the report. """ def write(self, payload): tmp = tempfile.TemporaryDirectory() self.addCleanup(tmp.cleanup) path = os.path.join(tmp.name, "scores.json") with open(path, "w", encoding="utf-8") as fh: json.dump(payload, fh) return path def test_current_entries_are_kept(self): entry = {"id": "a/b", "coverage": {"any_fraction": 1.0, "whole_fraction": 0.5}} path = self.write({"a/b@sha": entry}) with mock.patch.object(hub, "cache_path", return_value=path): self.assertIn("a/b@sha", hub.load_cache()) def test_stale_entries_are_dropped(self): stale = {"id": "a/b", "coverage": {"coverage": 1.0, "missing": []}} fresh = {"id": "c/d", "coverage": {"any_fraction": 1.0, "whole_fraction": 0.4}} path = self.write({"a/b@old": stale, "c/d@new": fresh}) with mock.patch.object(hub, "cache_path", return_value=path): cache = hub.load_cache() self.assertEqual(sorted(cache), ["c/d@new"]) def test_missing_or_corrupt_cache_is_empty(self): with mock.patch.object(hub, "cache_path", return_value=os.path.join("does", "not", "exist.json")): self.assertEqual(hub.load_cache(), {}) def test_report_survives_a_coverage_without_any_fraction(self): # older cached rows reach report() when offline; it must not raise entry = {"id": "a/b", "total": 0.9, "static": True, "dim": 256, "rows": 10, "bytes": 30_000_000, "license": "mit", "components": {"code": 0.5}, "coverage": {"coverage": 1.0, "missing": [], "tokens": 42}} with mock.patch.object(hub, "bakeoff", return_value={"pool": 1, "graded": [entry]}): self.assertIn("a/b", hub.report()) if __name__ == "__main__": unittest.main()