mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
Extend backend qualification so precision-induced changes in reasoning outcomes are separated from intervention effects (#512). - Precision declarations record weight, activation and KV-cache precision, quantizer and calibration identity; incomplete or inconsistent declarations are refused. - A bounded, preregistered pilot protocol fixes candidate, dataset, decoding, output budget, seed blocks, final-answer parser, margins, drift limit, resource objective and disjoint splits. - The pilot loads four fresh cells on the same rows: the unedited parent and the saved candidate at the CPU float32 reference and at the declared precision. - Per-row outcomes separate final-answer correctness from reasoning and total tokens and termination; truncated or unparsable outputs count as wrong. - Paired bootstraps give precision effects within each artifact, intervention effects within each precision and their interaction. Gates apply per task and pooled, and expose harm hidden by a pooled pass. Shared-prefix KL is reported apart from free-running disagreement. - Token cost never gates; an efficiency claim needs every gate to pass and the preregistered resource to improve. - Support matrix schema 2 carries precision fields and pilot attempts; pilots never add qualified pairs, and schema 1 records still load. - `obliteratus experiments qualify reasoning`; CUDA path in the conditional gate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
325 lines
18 KiB
Python
325 lines
18 KiB
Python
"""Backend qualification of saved persistent candidates (CPU contracts).
|
|
|
|
Reuses the planted fixture candidates of ``tests/test_persistent_candidates.py``:
|
|
each candidate is built, verified after reload, then qualified by loading the
|
|
saved artifact from disk on a declared backend and comparing it with the CPU
|
|
float32 reference execution of the same artifact on independent rows. A
|
|
bfloat16 CPU backend is the lower-precision fixture that perturbs probe logits
|
|
beyond the declared tolerance.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.concepts.pack import ConceptPack
|
|
from obliteratus.experiments import backend_descriptor as bd
|
|
from obliteratus.experiments import candidate_edits as edits
|
|
from obliteratus.experiments.backend_load import realized_checks
|
|
from obliteratus.experiments.backend_matrix import support_matrix
|
|
from obliteratus.experiments.backend_qualify import evaluation_digest, qualify
|
|
from tests.test_persistent_candidates import FixtureIO, build, env, verify # noqa: F401
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
CPU32 = {"device": "cpu", "dtype": "float32", "cache": "cached"}
|
|
BF16 = {"device": "cpu", "dtype": "bfloat16", "cache": "cached"}
|
|
|
|
|
|
class BackendIO(FixtureIO):
|
|
"""Fixture loads that expose a generation config and remember every object built."""
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.models = []
|
|
|
|
def load(self, path, section):
|
|
handle = super().load(path, section)
|
|
handle.model.generation_config = SimpleNamespace(use_cache=True)
|
|
self.models.append(handle.model)
|
|
return handle
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def selected(env): # noqa: F811
|
|
found = {}
|
|
for name, key, representation in (("edit", "ablate", "weight_edit"),
|
|
("adapter", "steer", "activation_adapter"),
|
|
("lora", "ablate", "lora")):
|
|
manifest = build(env, key, representation=representation)
|
|
assert verify(env, key, manifest)["selected"] is True
|
|
found[name] = (env[key][0], manifest)
|
|
return found
|
|
|
|
|
|
def run(env, selected, name, backend, io=None, **options): # noqa: F811
|
|
run_dir, manifest = selected[name]
|
|
return qualify(run_dir, manifest["id"], backend, env["fresh"], model_io=io or BackendIO(),
|
|
pack_resolver=ConceptPack.load, **options)
|
|
|
|
|
|
def checks(record):
|
|
return {c["check"]: c["passed"] for c in record["checks"]}
|
|
|
|
|
|
def test_cpu_reference_qualifies_against_itself_from_fresh_disk_loads(env, selected): # noqa: F811
|
|
io = BackendIO()
|
|
record = run(env, selected, "edit", CPU32, io)
|
|
manifest = selected["edit"][1]
|
|
assert record["status"] == "qualified", record["reason"]
|
|
assert all(checks(record).values()) and record["class"] == "native"
|
|
comparison = record["comparison"]
|
|
assert comparison["max_probe_logit_difference"] == 0.0
|
|
assert comparison["max_probe_activation_difference"] == 0.0
|
|
assert comparison["completion_agreement"] == 1.0 and comparison["probes"] > 0
|
|
assert set(comparison["collateral"].values()) == {"PASS"}
|
|
# The saved edited copy was read from disk twice into new objects; the in-memory
|
|
# model that produced the candidate is never used.
|
|
artifact = str(Path(manifest["location"]) / "artifact")
|
|
assert io.loads == [artifact, artifact]
|
|
assert len({id(m) for m in io.models}) == 2 and env["model"] not in io.models
|
|
assert record["loads"]["backend"] == {
|
|
"loaded": "edited copy", "representation": "weight_edit",
|
|
"artifact_sha256": manifest["artifact"]["sha256"],
|
|
"parent_files_sha256": manifest["parent"]["files_sha256"]}
|
|
assert record["backend"]["tokenizer"]["vocab_sha256"] == \
|
|
manifest["parent"]["tokenizer_vocab_sha256"]
|
|
assert record["independence"]["basis"] == "different dataset revision"
|
|
# Latency and resources are recorded but outside the evaluation digest.
|
|
host = record["host"]["backend"]
|
|
assert host["load_seconds"] >= 0 and host["generate_seconds"] >= 0 and host["rows"] > 0
|
|
again = run(env, selected, "edit", CPU32)
|
|
assert again["attempt"] == 2 and again["evaluation_sha256"] == record["evaluation_sha256"]
|
|
stored = json.loads((Path(record["location"]) / "qualification.json").read_text())
|
|
stored["host"]["backend"]["generate_seconds"] = 1e9
|
|
assert evaluation_digest(stored) == record["evaluation_sha256"]
|
|
stored["comparison"]["completion_agreement"] = 0.5
|
|
assert evaluation_digest(stored) != record["evaluation_sha256"]
|
|
|
|
|
|
def test_adapter_candidates_load_the_parent_and_attach_the_saved_adapter(env, selected): # noqa: F811
|
|
loaded = {}
|
|
for name in ("adapter", "lora"):
|
|
io = loaded[name] = BackendIO()
|
|
record = run(env, selected, name, {**CPU32, "cache": "uncached"}, io)
|
|
assert record["status"] == "qualified", record["reason"]
|
|
assert io.loads == [str(env["source"])] * 2
|
|
assert record["loads"]["backend"]["loaded"] == "parent with saved adapter"
|
|
# The reference runs cached; the declared backend runs uncached.
|
|
assert [m.generation_config.use_cache for m in io.models] == [True, False]
|
|
# The adapter was detached after evaluation: no wrapper remains on the loaded objects.
|
|
assert all(not isinstance(m, edits.ActivationEditAdapter)
|
|
for model in loaded["adapter"].models for m in model.modules())
|
|
|
|
|
|
def test_lower_precision_backend_fails_and_does_not_broaden_the_claim(env, selected): # noqa: F811
|
|
run_dir, manifest = selected["adapter"]
|
|
assert run(env, selected, "adapter", CPU32)["status"] == "qualified"
|
|
record = run(env, selected, "adapter", BF16)
|
|
assert record["status"] == "failed" and record["class"] == "native"
|
|
found = checks(record)
|
|
assert found["realized_dtype"] and found["tokenizer_identity"]
|
|
assert not found["probe_logits"]
|
|
assert record["comparison"]["max_probe_logit_difference"] > record["tolerances"]["logit_atol"]
|
|
# A later attempt under looser tolerances does not clear the regression.
|
|
loose = run(env, selected, "adapter", BF16,
|
|
tolerances_override={"logit_atol": 1e3, "activation_atol": 1e3})
|
|
assert loose["status"] == "qualified" and loose["backend_id"] == record["backend_id"]
|
|
matrix = support_matrix(run_dir, candidate=manifest["id"],
|
|
backends=[CPU32, BF16, {**CPU32, "dtype": "float16"}])
|
|
rows = {r["backend"]["dtype"] + "/" + r["backend"]["cache"]: r for r in matrix["rows"]}
|
|
assert rows["bfloat16/cached"]["status"] == "failed"
|
|
assert "later attempts do not clear it" in rows["bfloat16/cached"]["reason"]
|
|
assert rows["float16/cached"]["status"] == "untested"
|
|
[pair] = [q for q in matrix["qualified_pairs"]
|
|
if q["backend"]["cache"] == "cached"]
|
|
assert pair["backend"]["dtype"] == "float32" and pair["artifact"] == manifest["id"]
|
|
assert all(q["backend"]["dtype"] == "float32" for q in matrix["qualified_pairs"])
|
|
assert matrix["claim"]["blocked"] == [{
|
|
"artifact": manifest["id"], "failed_backends": [record["backend_id"]],
|
|
"note": matrix["claim"]["blocked"][0]["note"]}]
|
|
assert matrix["claim"]["quantized"] == [] and matrix["schema_version"] == 2
|
|
assert set(matrix["statuses"]) == {"qualified", "failed", "unsupported", "untested"}
|
|
|
|
|
|
@pytest.mark.parametrize("backend,reason", [
|
|
({**CPU32, "quantization": "bnb-int8"}, "requires a CUDA device"),
|
|
({**CPU32, "device": "cuda", "quantization": "bnb-nf4"}, "provides no bnb-nf4 load"),
|
|
({**CPU32, "quantization": "gguf-q4_0"}, "has no loader"),
|
|
({**CPU32, "device": "mps"}, "device type 'mps' is not supported"),
|
|
({**CPU32, "dtype": "int8"}, "dtype 'int8' is not supported"),
|
|
({**CPU32, "attention": "sdpa"}, "does not provide attention 'sdpa'"),
|
|
({**CPU32, "cache": "static"}, "is not cached or uncached"),
|
|
])
|
|
def test_unsupported_combinations_are_recorded_and_never_fall_back(env, selected, backend, # noqa: F811
|
|
reason):
|
|
io = BackendIO()
|
|
record = run(env, selected, "edit", backend, io)
|
|
assert record["status"] == "unsupported" and reason in record["reason"]
|
|
assert io.loads == [] and record["comparison"] is None
|
|
assert record["class"] == ("native" if backend.get("quantization", "none") == "none"
|
|
else "quantized")
|
|
matrix = support_matrix(selected["edit"][0], candidate=selected["edit"][1]["id"])
|
|
[row] = [r for r in matrix["rows"] if r["backend_id"] == record["backend_id"]]
|
|
assert row["status"] == "unsupported"
|
|
assert record["backend_id"] not in {q["backend_id"] for q in matrix["qualified_pairs"]}
|
|
|
|
|
|
def test_lora_on_a_quantized_base_and_unenforceable_cache_are_unsupported(env, selected): # noqa: F811
|
|
record = run(env, selected, "lora", {**CPU32, "device": "cuda:0",
|
|
"quantization": "bnb-int8"})
|
|
assert record["status"] == "unsupported" and "LoRA adapters" in record["reason"]
|
|
plain = FixtureIO() # its models expose no generation config
|
|
record = run(env, selected, "edit", {**CPU32, "cache": "uncached"}, plain)
|
|
assert record["status"] == "unsupported" and "cache mode" in record["reason"]
|
|
|
|
|
|
@pytest.mark.skipif(torch.cuda.is_available(), reason="checks the CPU-only host refusal")
|
|
def test_unavailable_cuda_is_refused_without_a_record(env, selected): # noqa: F811
|
|
run_dir, manifest = selected["edit"]
|
|
before = support_matrix(run_dir, candidate=manifest["id"])["rows"]
|
|
with pytest.raises(bd.BackendUnavailable, match="CUDA is not available.*untested"):
|
|
run(env, selected, "edit", {**CPU32, "device": "cuda"})
|
|
matrix = support_matrix(run_dir, candidate=manifest["id"],
|
|
backends=[{**CPU32, "device": "cuda"}])
|
|
assert len(matrix["rows"]) == len(before) + 1
|
|
assert matrix["rows"][-1]["status"] == "untested"
|
|
|
|
|
|
def test_changed_artifact_or_evidence_is_never_qualified(env): # noqa: F811
|
|
manifest = build(env, "steer")
|
|
run_dir = env["steer"][0]
|
|
assert verify(env, "steer", manifest)["selected"] is True
|
|
selected = {"steer": (run_dir, manifest)}
|
|
good = run(env, selected, "steer", CPU32)
|
|
assert good["status"] == "qualified"
|
|
evidence = Path(good["location"]) / "qualification.json"
|
|
text = evidence.read_text()
|
|
evidence.write_text(text.replace('"completion_agreement": 1.0', '"completion_agreement": 0.9'))
|
|
[row] = support_matrix(run_dir, candidate=manifest["id"])["rows"]
|
|
assert row["status"] == "failed" and "no longer reproduces" in row["reason"]
|
|
evidence.write_text(text)
|
|
adapter = Path(manifest["location"]) / "artifact" / edits.ADAPTER_FILE
|
|
original = adapter.read_bytes()
|
|
adapter.write_bytes(original + b" ")
|
|
try:
|
|
io = BackendIO()
|
|
record = run(env, selected, "steer", BF16, io)
|
|
assert record["status"] == "failed" and record["reason"] == "artifact_digests"
|
|
assert io.loads == []
|
|
rows = support_matrix(run_dir, candidate=manifest["id"])["rows"]
|
|
assert {r["status"] for r in rows} == {"failed"}
|
|
assert any("artifact changed" in r["reason"] for r in rows)
|
|
finally:
|
|
adapter.write_bytes(original)
|
|
|
|
|
|
def test_requests_that_cannot_be_qualified_are_refused(env, selected): # noqa: F811
|
|
run_dir, manifest = selected["edit"]
|
|
built = build(env, "ablate", representation="weight_edit",
|
|
tolerances={"agreement": 0.9})
|
|
with pytest.raises(bd.QualificationRefused, match="is built; only a candidate"):
|
|
qualify(run_dir, built["id"], CPU32, env["fresh"], model_io=BackendIO(),
|
|
pack_resolver=ConceptPack.load)
|
|
with pytest.raises(bd.QualificationRefused, match=r"must declare \['dtype', 'cache'\]"):
|
|
run(env, selected, "edit", {"device": "cpu"})
|
|
with pytest.raises(bd.QualificationRefused, match="Unknown tolerances"):
|
|
run(env, selected, "edit", CPU32, tolerances_override={"effect": 1})
|
|
calibrate = {"pack": str(env["pack"].location), "revision": env["pack"].dataset().revision,
|
|
"role": "calibrate"}
|
|
with pytest.raises(bd.QualificationRefused, match="registered confirmation sets"):
|
|
qualify(run_dir, manifest["id"], CPU32, calibrate, model_io=BackendIO(),
|
|
pack_resolver=ConceptPack.load)
|
|
|
|
|
|
def test_descriptor_normalization_and_identity():
|
|
manifest = {"parent": {"tokenizer_vocab_sha256": "abc"}}
|
|
first = bd.normalize({"device": "CUDA", "dtype": "float16", "cache": "cached"}, manifest)
|
|
assert first == {"device": "cuda:0", "dtype": "float16", "quantization": "none",
|
|
"tokenizer": {"source": "artifact", "vocab_sha256": "abc"},
|
|
"cache": "cached", "attention": "default"}
|
|
explicit = bd.normalize({**first, "tokenizer": "sha256:def"}, manifest)
|
|
assert explicit["tokenizer"]["vocab_sha256"] == "def"
|
|
assert bd.backend_id(first) != bd.backend_id(explicit)
|
|
assert bd.backend_id(first) != bd.backend_id({**first, "cache": "uncached"})
|
|
assert bd.backend_class({**first, "quantization": "bnb-int8"}) == "quantized"
|
|
for bad, match in (([], "mapping"), ({**CPU32, "speed": 1}, "Unknown backend fields"),
|
|
({**CPU32, "device": "cuda:x"}, "Invalid CUDA device"),
|
|
({**CPU32, "device": "cpu:1"}, "takes no index"),
|
|
({**CPU32, "tokenizer": {"source": "hub"}}, "stored with the artifact")):
|
|
with pytest.raises(bd.QualificationRefused, match=match):
|
|
bd.normalize(bad)
|
|
model = torch.nn.Linear(2, 2)
|
|
assert all(c["passed"] for c in realized_checks(model, bd.normalize(CPU32)))
|
|
wrong = {c["check"]: c["passed"] for c in realized_checks(
|
|
model, bd.normalize({**CPU32, "attention": "eager", "quantization": "bnb-int8"}))}
|
|
assert wrong == {"realized_device": True, "realized_quantization": False,
|
|
"realized_attention": False}
|
|
if not torch.cuda.is_available():
|
|
assert bd.availability_problem(bd.normalize({**CPU32, "device": "cuda:3"}))
|
|
|
|
|
|
def test_cli_qualify_run_and_matrix(env, selected, monkeypatch, capsys): # noqa: F811
|
|
from obliteratus.cli import main
|
|
monkeypatch.setattr("obliteratus.experiments.backend_qualify.TransformersBackendIO",
|
|
BackendIO)
|
|
monkeypatch.setattr("obliteratus.experiments.inputs.default_pack_resolver",
|
|
ConceptPack.load)
|
|
run_dir, manifest = selected["lora"]
|
|
fresh = env["fresh"]
|
|
main(["experiments", "qualify", "run", manifest["id"], "--run", str(run_dir), "--pack",
|
|
fresh["pack"], "--revision", fresh["revision"], "--role", "select", "--device", "cpu",
|
|
"--dtype", "float32", "--cache", "cached", "--logit-atol", "1e-3"])
|
|
result = json.loads(capsys.readouterr().out)
|
|
assert result["status"] == "qualified" and result["class"] == "native"
|
|
main(["experiments", "qualify", "run", manifest["id"], "--run", str(run_dir), "--pack",
|
|
fresh["pack"], "--revision", fresh["revision"], "--role", "select", "--device", "cpu",
|
|
"--dtype", "float32", "--cache", "cached", "--quantization", "bnb-int8",
|
|
"--tokenizer-sha256", manifest["parent"]["tokenizer_vocab_sha256"]])
|
|
assert json.loads(capsys.readouterr().out)["status"] == "unsupported"
|
|
main(["experiments", "qualify", "matrix", "--run", str(run_dir), "--candidate",
|
|
manifest["id"], "--backend", json.dumps({**CPU32, "dtype": "float16"})])
|
|
matrix = json.loads(capsys.readouterr().out)
|
|
statuses = {r["backend"]["dtype"] + "/" + r["backend"]["quantization"] + "/" +
|
|
r["backend"]["cache"]: r["status"] for r in matrix["rows"]}
|
|
assert statuses["float32/none/cached"] == "qualified"
|
|
assert statuses["float32/bnb-int8/cached"] == "unsupported"
|
|
assert statuses["float16/none/cached"] == "untested"
|
|
with pytest.raises(SystemExit) as refused:
|
|
main(["experiments", "qualify", "run", "sha256:" + "0" * 64, "--run", str(run_dir),
|
|
"--pack", fresh["pack"], "--revision", fresh["revision"], "--role", "select",
|
|
"--device", "cpu", "--dtype", "float32", "--cache", "cached"])
|
|
assert refused.value.code == 4 and "No readable candidate" in capsys.readouterr().err
|
|
|
|
|
|
@pytest.mark.parametrize("attention", ["default", "eager", "sdpa"])
|
|
def test_transformers_loader_realizes_the_declared_attention_and_dtype(tmp_path, attention):
|
|
from obliteratus.experiments.backend_load import TransformersBackendIO
|
|
from obliteratus.qualification_runtime import create_fixture
|
|
create_fixture(tmp_path, layers=1, seed=0)
|
|
backend = bd.normalize({**CPU32, "dtype": "bfloat16", "attention": attention})
|
|
io = TransformersBackendIO()
|
|
assert bd.support_problems(backend, "lora", io) == []
|
|
handle = io.load(tmp_path, {"dtype": "bfloat16", "attention": attention})
|
|
found = {c["check"]: c["passed"] for c in realized_checks(handle.model, backend)}
|
|
assert all(found.values()) and ("realized_attention" in found) == (attention != "default")
|
|
assert handle.model.generation_config.use_cache is True
|
|
assert bd.support_problems({**backend, "attention": "flash_attention_2"}, "lora", io)
|
|
|
|
|
|
def test_loader_refuses_changed_parent_or_artifact_files(selected):
|
|
from obliteratus.experiments.backend_load import load_artifact
|
|
_, manifest = selected["adapter"]
|
|
path, backend, io = Path(manifest["location"]), bd.normalize(CPU32, manifest), BackendIO()
|
|
parent = {**manifest, "parent": {**manifest["parent"], "files": {}}}
|
|
with pytest.raises(bd.QualificationRefused, match="parent model files changed"):
|
|
load_artifact(parent, path, backend, io, {})
|
|
artifact = {**manifest, "artifact": {**manifest["artifact"], "files": {}}}
|
|
with pytest.raises(bd.QualificationRefused, match="differ from the manifest"):
|
|
load_artifact(artifact, path, backend, io, {})
|
|
assert io.loads == []
|