"""Parity of the real model with the research vectors. Marked slow: it needs the pinned weights in ML_MODEL_DIR (fetch them with `make -C ml model-fetch`) and runs only with `make -C ml test-model`. """ import json import math import os from pathlib import Path import pytest from lincodex_ml.inference.embedder import InputKind GOLDEN = Path(__file__).resolve().parent / "data" / "giga_golden.json" # CPU float32 against the research CUDA float32 run; measured differences are # below 1e-6 per component. MAX_COMPONENT_DIFFERENCE = 1e-4 pytestmark = pytest.mark.slow @pytest.fixture(scope="module") def model_dir() -> Path: raw = os.environ.get("ML_MODEL_DIR", "") if not raw: pytest.skip("ML_MODEL_DIR is not set") return Path(raw) def test_real_model_reproduces_the_research_vectors_deterministically(model_dir: Path) -> None: from lincodex_ml.inference.giga import GigaEmbedder, configure_torch configure_torch(4) embedder = GigaEmbedder(model_dir, max_tokens=1024, verify_weights=True) golden = json.loads(GOLDEN.read_text(encoding="utf-8")) assert embedder.info.model_id == golden["model"] assert embedder.info.revision == golden["revision"] assert embedder.info.dimension == 1024 for case in golden["cases"]: kind = InputKind(case["kind"]) first = embedder.encode_batch(kind, [case["text"]])[0] second = embedder.encode_batch(kind, [case["text"]])[0] assert first == second assert math.isclose(math.sqrt(sum(value * value for value in first)), 1.0, abs_tol=1e-5) difference = max(abs(a - b) for a, b in zip(first, case["vector"], strict=True)) assert difference < MAX_COMPONENT_DIFFERENCE