"""Transfer dossiers: own fit/test data, within-model baselines, tested/untested conditions.""" import copy import hashlib import json import pytest import torch from safetensors.torch import save_file from obliteratus.analysis import transfer_stats as stats from obliteratus.analysis.moe_observation import expert_coverage, record_routing from obliteratus.analysis.transfer_dossier import ( TransferRefused, build_transfer_dossier, conditions, write_transfer_dossier, ) from obliteratus.arch_adapters import bind, residual_readout from obliteratus.cli import main from tests.fixtures.tiny_arch_models import tiny_gpt2, tiny_qwen2_moe pytestmark = [pytest.mark.cpu, pytest.mark.usefixtures("tiny_model_runtime")] def synthetic(seed=0, n_align=24, n_test=400, nuisance=6.0, scramble=False, mix=0.0): """Target = source @ M + small noise; the concept lies on source axis 0. ``nuisance`` inflates class-independent variance (hurts a small-budget within-model fit); ``scramble`` breaks the alignment pairing; ``mix`` adds noise to the aligned source activations. """ g = torch.Generator().manual_seed(seed) d_source, d_target = 12, 16 m = torch.randn(d_source, d_target, generator=g) / d_source ** 0.5 concept = torch.zeros(d_source) concept[0] = 1 scale = torch.ones(d_source) scale[1:4] = nuisance def rows(n, labeled): y = (torch.arange(n) % 2).float() h = torch.randn(n, d_source, generator=g) * scale if labeled: h = h + y[:, None] * 1.2 * concept return h, h @ m + 0.02 * torch.randn(n, d_target, generator=g), y a_src, a_tgt, _ = rows(n_align, False) if scramble: a_tgt = a_tgt[torch.randperm(n_align, generator=g)] if mix: a_src = a_src + mix * torch.randn(a_src.shape, generator=g) _, b_tgt, b_y = rows(n_align, True) _, t_tgt, t_y = rows(n_test, True) return {"source_direction": concept, "alignment_source": a_src, "alignment_target": a_tgt, "baseline_fit": b_tgt, "baseline_labels": b_y, "test": t_tgt, "test_labels": t_y} def semantics(site): return {"site": site, "role": "residual_stream"} def make_spec(n_align=24, n_test=400, **settings): return { "schema": "obliteratus.transfer-spec", "schema_version": 1, "name": "toy", "source": {"model_id": "a", "family": "dense_decoder", "domain": "chat", "site": "layer.1", "semantics": semantics("layer.1"), "fit_rows": ["s1", "s2"]}, "target": {"model_id": "b", "family": "sparse_moe_decoder", "domain": "code", "site": "layer.0", "semantics": semantics("layer.0"), "experts_tested": [0, 1], "expert_coverage": {"0": {"tested": True}, "1": {"tested": True}, "2": {"tested": False}}}, "data": {"alignment": [f"a{i}" for i in range(n_align)], "baseline_fit": [f"b{i}" for i in range(n_align)], "test": [f"t{i}" for i in range(n_test)]}, "conditions": {"families": ["sparse_moe_decoder", "dense_decoder"], "domains": ["code", "chat", "math"], "layers": [0, 1], "experts": [0, 1, 2, 3]}, "settings": {"bootstrap": 300, **settings}, } def test_transfer_gain_only_when_it_beats_every_baseline_with_uncertainty(): dossier = build_transfer_dossier(make_spec(), synthetic()) assert dossier["schema"] == "obliteratus.transfer-dossier" and dossier["schema_version"] == 1 assert dossier["comparisons"]["within_model"]["beats"] assert dossier["comparisons"]["random"]["beats"] assert dossier["claim"]["transfer_gain"] is True assert set(dossier["claim"]["kinds"]) == {"cross_family", "cross_domain"} assert dossier["alignment"]["in_sample_r2"] > 0.99 json.dumps(dossier, allow_nan=False) @pytest.mark.parametrize("case", [{"scramble": True, "nuisance": 1.0}, {"mix": 1.0, "nuisance": 1.0}]) def test_transfer_that_does_not_beat_the_within_model_baseline_claims_no_gain(case): dossier = build_transfer_dossier(make_spec(), synthetic(**case)) within = dossier["comparisons"]["within_model"] assert within["beats"] is False and within["interval"][0] <= 0 assert dossier["claim"]["transfer_gain"] is False assert dossier["claim"]["statement"].startswith("No transfer gain claimed") assert "within_model" in dossier["claim"]["statement"] if "mix" in case: # higher point estimate, but the interval includes zero assert dossier["scores"]["transfer"]["auc"] > dossier["scores"]["within_model"]["auc"] def test_tested_and_untested_conditions_are_listed(): claim = build_transfer_dossier(make_spec(), synthetic())["claim"] assert claim["tested_conditions"] == {"families": ["sparse_moe_decoder"], "domains": ["code"], "layers": [0], "experts": [0, 1]} assert claim["untested_conditions"] == {"families": ["dense_decoder"], "domains": ["chat", "math"], "layers": [1], "experts": [2, 3]} assert "untested conditions carry no claim" in claim["scope"] @pytest.mark.parametrize("mutate, message", [ (lambda s: s["data"].update(test=s["data"]["test"][:-1] + ["a3"]), "alignment rows overlap"), (lambda s: s["data"].update(test=s["data"]["test"][:-1] + ["b0"]), "baseline_fit rows overlap"), (lambda s: s["source"].update(fit_rows=["t0"]), "source_fit rows overlap"), (lambda s: s["data"].update( alignment={"ids": s["data"]["alignment"], "content_sha256": [f"h{i}" for i in range(24)]}, test={"ids": s["data"]["test"], "content_sha256": ["h5"] + [f"u{i}" for i in range(399)]}), "1 content digests"), (lambda s: s["data"].update(baseline_fit=s["data"]["baseline_fit"][:-1]), "budget"), (lambda s: s["data"].update(test=["t0", "t0"]), "unique ids"), (lambda s: s["data"].update(test={"ids": ["t0"], "content_sha256": []}), "parallel"), (lambda s: s["target"].update(modality="audio"), "target_definitions and scoring_tasks"), (lambda s: s["target"].pop("semantics"), "residual_stream tensor semantics"), (lambda s: s["source"].update(semantics={"site": "layer.1", "role": "router"}), "semantics"), (lambda s: s["target"].update(site="mlp.0"), "not a residual site"), (lambda s: s["target"].pop("domain"), "target.domain is required"), (lambda s: s.pop("conditions"), "Declare the claim's conditions"), (lambda s: s["conditions"].update(tasks=[]), "Declare the claim's conditions"), (lambda s: s["settings"].update(alpha=1), "Unknown settings"), ]) def test_protocol_violations_are_refused_before_computing(mutate, message): spec = make_spec() mutate(spec) with pytest.raises(TransferRefused, match=message): build_transfer_dossier(spec, synthetic()) def test_new_modality_needs_its_own_targets_and_scoring_tasks(): spec = make_spec() spec["target"]["modality"] = "audio" spec["modalities"] = {"audio": {"target_definitions": ["spoken refusal"], "scoring_tasks": ["asr-then-refusal-judge"]}} dossier = build_transfer_dossier(spec, synthetic()) assert "cross_modality" in dossier["claim"]["kinds"] def test_tensor_refusals(): tensors = synthetic() for name, value, message in ( ("test", tensors["test"][:-1], "one row per test"), ("baseline_fit", torch.zeros(24, 8), "disagree on width"), ("test_labels", tensors["test_labels"] * 2, "binary labels"), ("baseline_labels", torch.ones(24), "both classes")): broken = {**tensors, name: value} with pytest.raises(ValueError, match=message): build_transfer_dossier(make_spec(), broken) with pytest.raises(TransferRefused, match="Missing tensors"): build_transfer_dossier(make_spec(), {k: v for k, v in tensors.items() if k != "test"}) small = synthetic(n_test=8) with pytest.raises(TransferRefused, match="per class"): build_transfer_dossier(make_spec(n_test=8), small) with pytest.raises(ValueError, match="at least 100"): build_transfer_dossier(make_spec(bootstrap=10), tensors) with pytest.raises(ValueError, match="finite and nonzero"): build_transfer_dossier(make_spec(), {**tensors, "source_direction": torch.zeros(12)}) with pytest.raises(ValueError, match="Source direction width"): build_transfer_dossier(make_spec(), {**tensors, "source_direction": torch.ones(5)}) def test_rank_auc_matches_pairwise_definition_with_ties(): torch.manual_seed(3) pos = torch.randint(0, 4, (9, 3)).double() neg = torch.randint(0, 4, (7, 3)).double() pairwise = ((pos[:, None] > neg[None]).double() + 0.5 * (pos[:, None] == neg[None]).double()).mean((0, 1)) assert torch.allclose(stats.auc(pos, neg), pairwise) with pytest.raises(ValueError, match="paired"): stats.fit_alignment(torch.zeros(3, 2), torch.zeros(4, 2), 1e-3) with pytest.raises(ValueError, match="finite"): stats.fit_alignment(torch.full((3, 2), float("nan")), torch.zeros(3, 2), 1e-3) def test_real_tiny_models_dense_source_to_moe_target_end_to_end(tmp_path): source, target = bind(tiny_gpt2()), bind(tiny_qwen2_moe()) g = torch.Generator().manual_seed(5) def batch(n, labeled): ids = torch.randint(5, 11, (n, 6), generator=g) y = (torch.arange(n) % 2) if labeled: ids[:, 2] = torch.where(y.bool(), 3, 4) return [{"input_ids": ids}], y.float() align, _ = batch(24, False) fit, fit_y = batch(24, True) test, test_y = batch(40, True) source_fit, source_y = batch(40, True) source_acts = residual_readout(source, source_fit, 1, "mean") direction = source_acts[source_y.bool()].mean(0) - source_acts[~source_y.bool()].mean(0) tensors = {"source_direction": direction, "alignment_source": residual_readout(source, align, 1, "mean"), "alignment_target": residual_readout(target, align, 0, "mean"), "baseline_fit": residual_readout(target, fit, 0, "mean"), "baseline_labels": fit_y, "test": residual_readout(target, test, 0, "mean"), "test_labels": test_y} covered = expert_coverage(record_routing(target, test))["layers"]["0"]["experts"] spec = make_spec(n_test=40, bootstrap=200) spec["source"]["semantics"] = source.site(1) spec["target"]["semantics"] = target.site(0) spec["target"]["experts_tested"] = [int(e) for e, v in covered.items() if v["tested"]] spec["target"]["expert_coverage"] = covered dossier = build_transfer_dossier(spec, tensors) assert dossier["target"]["semantics"]["meaning"].startswith("layer. is the decoder") assert dossier["claim"]["transfer_gain"] == all(c["beats"] for c in dossier["comparisons"].values()) path = write_transfer_dossier(dossier, tmp_path) with pytest.raises(FileExistsError): write_transfer_dossier(dossier, tmp_path) assert json.loads(path.read_text())["name"] == "toy" with pytest.raises(TransferRefused, match="plain file-name"): write_transfer_dossier({**dossier, "name": "../x"}, tmp_path) def test_transfer_cli_run_and_refusals(tmp_path, capsys): tensors = synthetic() save_file({k: v.contiguous() for k, v in tensors.items()}, str(tmp_path / "t.safetensors")) digest = hashlib.sha256((tmp_path / "t.safetensors").read_bytes()).hexdigest() spec = make_spec() spec["tensors"] = {"path": "t.safetensors", "sha256": digest} (tmp_path / "spec.json").write_text(json.dumps(spec)) main(["experiments", "transfer", "run", str(tmp_path / "spec.json"), "--out", str(tmp_path / "out")]) result = json.loads(capsys.readouterr().out) assert result["transfer_gain"] is True and result["location"].endswith("transfer-toy.json") stored = json.loads((tmp_path / "out" / "transfer-toy.json").read_text()) assert stored["tensor_file_sha256"] == digest for broken, message in (({**spec, "tensors": {**spec["tensors"], "sha256": "0" * 64}}, "digest"), ({**spec, "tensors": {"path": "missing.safetensors"}}, "missing"), ({**spec, "schema_version": 2}, "transfer-spec version 1"), (spec, "exists")): (tmp_path / "bad.json").write_text(json.dumps(copy.deepcopy(broken))) with pytest.raises(SystemExit) as refused: main(["experiments", "transfer", "run", str(tmp_path / "bad.json"), "--out", str(tmp_path / "out")]) assert refused.value.code == 4 and message in capsys.readouterr().err def test_thinly_covered_expert_cannot_be_listed_as_tested(): spec = make_spec() spec["target"]["experts_tested"] = [0, 2] with pytest.raises(TransferRefused, match="below the coverage floor"): conditions(spec, 0) spec["target"].pop("expert_coverage") with pytest.raises(TransferRefused, match="needs the target's expert_coverage"): conditions(spec, 0)