clsa-code / tests /test_training.py
fahadhafeezofficial's picture
Upload folder using huggingface_hub
fd5366a verified
Raw History Blame Contribute Delete
2.26 kB
# Copyright (c) 2026 CLSA contributors
# SPDX-License-Identifier: MIT
"""Trained artefacts: presence, shapes, calibration contract.
Skipped (not failed) when the artefacts have not been produced yet: a missing
artefact is a missing run, and the suite must say so rather than pass silently.
"""
from __future__ import annotations
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
import numpy as np # noqa: E402
import pytest # noqa: E402
from clsa.transcoder.dataset import DERIVED # noqa: E402
from clsa.util import load_config # noqa: E402
@pytest.fixture(scope="module")
def artefacts():
name = load_config("transcoder")["backbones"]["primary"]
d = DERIVED / name.replace("/", "__")
if not (d / "artifacts.json").exists():
pytest.skip(f"no trained artefacts at {d}; run `make train`")
import json
return d, json.loads((d / "artifacts.json").read_text())
def test_artefact_files_exist(artefacts):
d, _ = artefacts
for f in ("sae.pt", "model.pt", "probe_dense.joblib", "probe_sparse.joblib"):
assert (d / f).exists(), f"missing {f}"
def test_thresholds_are_probabilities(artefacts):
_, blob = artefacts
thr = blob["thresholds"]
assert thr, "no thresholds recorded"
for k, v in thr.items():
assert 0.0 <= float(v) <= 1.0, f"threshold {k}={v} out of range"
def test_admission_report_has_three_arms(artefacts):
_, blob = artefacts
adm = blob.get("admission", {})
assert adm, "no admission report"
assert set(adm) >= {"shared", "probe_only", "random"} or any(
"shared" in str(k) for k in adm
), f"unexpected admission keys: {list(adm)}"
def test_probe_scores_are_bounded():
from clsa.world_model.train import load_model, probe_predict
name = load_config("transcoder")["backbones"]["primary"]
try:
_wm, _rh, _blob, ds = load_model(name)
except FileNotFoundError:
pytest.skip("no trained world model")
import joblib
p = joblib.load(DERIVED / name.replace("/", "__") / "probe_dense.joblib")
X = np.concatenate([ds["dense"], ds["action"]], axis=1)[:50]
s = probe_predict(p, X)
assert s.shape == (50,)
assert np.all((s >= 0) & (s <= 1))