feat(ml): fuse two embedding models and protect the ML service with a token
This commit is contained in:
1 parent
bc00d6901f
commit
b24e5ff906
63 files changed
+2287
-276
No files matched your search
@@ -0,0 +1,54 @@
|
||||
"""Parity of the real Qwen3-Embedding model with the research vectors.
|
||||
|
||||
Marked slow: it needs the pinned weights in ML_QWEN3_MODEL_DIR (fetch them with
|
||||
`make -C ml model-fetch`) and runs only with `make -C ml test-model`. The
|
||||
files are verified against the checked-in lock file. ML_TEST_DEVICE=cuda runs
|
||||
it on the GPU in float32.
|
||||
"""
|
||||
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from lincodex_ml.inference.embedder import InputKind
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
GOLDEN = ROOT / "tests" / "data" / "qwen3_golden.json"
|
||||
LOCK = ROOT / "model" / "qwen3-embedding-0.6b.lock.json"
|
||||
# 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_QWEN3_MODEL_DIR", "")
|
||||
if not raw:
|
||||
pytest.skip("ML_QWEN3_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.qwen3 import Qwen3Embedder
|
||||
from lincodex_ml.inference.torch_support import configure_torch, prepare_device
|
||||
|
||||
configure_torch(4)
|
||||
device = prepare_device(os.environ.get("ML_TEST_DEVICE", "cpu"), 0)
|
||||
embedder = Qwen3Embedder(model_dir, LOCK, max_tokens=1024, verify_weights=True, device=device)
|
||||
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
|
||||
Reference in new issue
Block a user