"""Matched-precision reasoning outcome controls (CPU contracts). Pure fixtures exercise the outcome endpoints (failed and truncated outputs), precision declarations (missing fields) and protocol preregistration; the paired analysis has its own synthetic-cell tests in ``tests/test_precision_effects.py``. The pilot runs the planted persistent candidates of ``tests/test_persistent_candidates.py`` on the CPU float32 reference and a CPU bfloat16 declared precision; the fixture loader plants extra "reasoning" tokens in bfloat16 so that a precision-induced length change is visible. Software contracts only, never pretrained behaviour. """ from __future__ import annotations import json from pathlib import Path import pytest import torch from obliteratus.concepts.pack import ConceptPack from obliteratus.experiments import backend_descriptor as bd from obliteratus.experiments import precision_spec as ps from obliteratus.experiments import reasoning_outcomes as ro from obliteratus.experiments.backend_matrix import support_matrix from obliteratus.experiments.precision_pilot import evaluation_digest, run_pilot from tests.test_backend_qualification import CPU32, BackendIO, run, selected # noqa: F401 from tests.test_persistent_candidates import env # noqa: F401 pytestmark = pytest.mark.cpu BF16 = {**CPU32, "dtype": "bfloat16"} NATIVE_BF16 = {"backend": BF16, "weights": "bfloat16", "activations": "bfloat16", "kv_cache": "bfloat16", "quantizer": None, "calibration": {"method": "none"}} LAST = {"id": "last-word", "version": 1} PREREG = {"accuracy_margin": 0.05, "drift_limit": 0.5, "resource_objective": "total_tokens", "bootstrap": {"samples": 200, "seed": 0, "confidence": 0.95}} SAMPLE, EOS = 3, 2 def words(ids): vocab = {SAMPLE: "sample", 10: "yes", 11: "no", 12: "42", 13: "answer:"} return " ".join(vocab[i] for i in ids) # --- outcome endpoints --------------------------------------------------------------------- def test_outcome_rows_separate_final_correctness_from_length_and_termination(): row = lambda ids, eos, budget=8, parser=LAST, target="yes": ro.outcome_row( # noqa: E731 task="t", example_id="e", seed_block=0, prompt_tokens=3, ids=ids, ended_with_eos=eos, budget=budget, decode=words, target=target, parser=parser) plain = row([SAMPLE, SAMPLE, 10], True) assert plain["final_correct"] and plain["termination"] == "eos" assert plain["generated_tokens"] == 4 and plain["reasoning_tokens"] == 2 # More tokens and still a wrong final answer; the correct string earlier is diagnostic. wrong = row([10, SAMPLE, SAMPLE, 11], True) assert not wrong["final_correct"] and wrong["intermediate_correct"] assert wrong["final_answer"] == "no" and wrong["reasoning_tokens"] == 3 # A truncated output has no final answer even when its text ends in the target. cut = row([SAMPLE, 10, SAMPLE, 10], False, budget=4) assert cut["truncated"] and cut["termination"] == "budget" and not cut["final_correct"] assert not cut["answer_found"] and cut["reasoning_tokens"] == 4 # A failed output (nothing parsable) is incorrect and not truncated. empty = row([], True) assert not empty["answer_found"] and not empty["final_correct"] and not empty["truncated"] early = row([10], False, budget=8) assert early["termination"] == "other" and early["final_correct"] marker = row([SAMPLE, 13, 12], True, parser={"id": "answer-marker", "version": 1}, target=["42", "forty-two"]) assert marker["final_answer"] == "42" and marker["final_correct"] assert not row([SAMPLE], True, parser={"id": "answer-marker", "version": 1})["answer_found"] number = row([12, SAMPLE], True, parser={"id": "last-number", "version": 1}, target="42.0") assert number["final_correct"] and number["intermediate_correct"] is False assert not ro.score_completion("x", "abc", {"id": "last-number", "version": 1}, stop="eos")[2] assert not ro.score_completion("7", "seven", {"id": "last-number", "version": 1}, stop="eos")[2] assert ro.validate_rows([plain, wrong, cut]) == [plain, wrong, cut] for bad, match in (({**plain, "termination": "stall"}, "termination"), ({**cut, "final_correct": True}, "never correct"), ({k: v for k, v in plain.items() if k != "truncated"}, "lacks")): with pytest.raises(ValueError, match=match): ro.validate_rows([bad]) for parser in ({"id": "regex"}, {"id": "last-word", "version": 2}): with pytest.raises(ValueError, match="parser"): ro.parser_identity(parser) with pytest.raises(ValueError, match="nonempty answer"): ro.reference_answers([" "]) # --- precision declarations and protocol ---------------------------------------------------- def test_precision_declarations_require_every_field_and_match_the_descriptor(): found = ps.normalize_precision(NATIVE_BF16) assert found["backend"]["dtype"] == "bfloat16" and found["id"].startswith("sha256:") reference = ps.reference_precision() assert reference["weights"] == reference["kv_cache"] == "float32" assert reference["id"] != found["id"] nf4 = {"backend": {**CPU32, "device": "cuda", "dtype": "bfloat16", "quantization": "bnb-nf4"}, "weights": "nf4", "activations": "bfloat16", "kv_cache": "bfloat16", "quantizer": {"name": "bitsandbytes", "version": "0.45"}, "calibration": {"method": "none"}} assert ps.normalize_precision(nf4)["weights"] == "nf4" assert ps.derived_precision(bd.normalize(nf4["backend"]))["weights"] == "nf4" gptq = {**nf4, "backend": {**nf4["backend"], "quantization": "gptq-4bit"}, "weights": "int4", "calibration": {"method": "gptq", "data_id": "c4-sample", "rows_sha256": "abc", "sequence_length": 2048, "samples": 128, "seed": 0}} assert ps.normalize_precision(gptq)["calibration"]["method"] == "gptq" assert ps.derived_precision(bd.normalize(gptq["backend"]))["calibration"] is None for bad, match in ( ({k: v for k, v in NATIVE_BF16.items() if k != "kv_cache"}, r"\['kv_cache'\]"), ({**NATIVE_BF16, "kv_cache": "float8"}, "kv_cache 'float8'"), ({**NATIVE_BF16, "quantizer": {"name": "x"}}, "no quantizer"), ({**NATIVE_BF16, "calibration": {}}, "declares a method"), ({**NATIVE_BF16, "calibration": {"method": "none", "seed": 1}}, "carries no"), ({**nf4, "quantizer": {"name": "bitsandbytes"}}, "name and version"), ({**nf4, "weights": "int8"}, "weights 'int8'"), ({**nf4, "calibration": gptq["calibration"]}, "calibration-free"), ({**gptq, "calibration": {"method": "gptq"}}, "must declare"), ({**NATIVE_BF16, "mode": "fast"}, "Unknown precision fields"), ("bf16", "is a mapping")): with pytest.raises(ps.PilotRefused, match=match): ps.normalize_precision(bad) def protocol(candidate, fresh, **changes): dataset = dict(fresh) found = {"candidate": candidate, "dataset": dataset, "precision": NATIVE_BF16, "decoding": {"strategy": "greedy", "max_new_tokens": 4}, "seed_blocks": [0, 1], "max_examples": 6, "parser": LAST, "preregistration": {**PREREG, "splits": { "calibration": None, "selection": {**dataset, "role": "calibrate"}, "test": dataset}}} for key, value in changes.items(): if key in PREREG or key == "splits": found["preregistration"] = {**found["preregistration"], key: value} else: found[key] = value return found def test_protocol_is_preregistered_bounded_and_keeps_default_decoding(): fresh = {"pack": "p", "revision": "r", "role": "select"} found = ps.normalize_protocol(protocol("sha256:c", fresh)) assert found["prompt"] == {"chat_template": False, "system": None} assert found["protocol_sha256"] != ps.normalize_protocol( protocol("sha256:c", fresh, seed_blocks=[0]))["protocol_sha256"] assert ps.task_margins(found, ["a", "b"]) == {"a": 0.05, "b": 0.05} split = ps.normalize_protocol(protocol("sha256:c", fresh, accuracy_margin={"a": 0.01})) with pytest.raises(ps.PilotRefused, match=r"no preregistered accuracy margin.*\['b'\]"): ps.task_margins(split, ["a", "b"]) sample = {"strategy": "sample", "max_new_tokens": 8, "temperature": 0.6, "top_p": 0.95, "top_k": 20} assert ps.normalize_protocol(protocol("c", fresh, decoding=sample))["decoding"] == sample for changes, match in ( ({"decoding": {"strategy": "greedy", "max_new_tokens": 4, "hesitation_penalty": 1.0}}, "Unknown decoding fields"), ({"decoding": {"strategy": "beam", "max_new_tokens": 4}}, "greedy or sample"), ({"decoding": {"strategy": "greedy", "max_new_tokens": 10 ** 6}}, "max_new_tokens"), ({"seed_blocks": [0, 0]}, "distinct"), ({"seed_blocks": list(range(9))}, "1..8"), ({"max_examples": 10 ** 4}, "bounded pilot"), ({"parser": {"id": "regex", "version": 1}}, "Unknown final-answer parser"), ({"resource_objective": "shorter"}, "resource_objective"), ({"drift_limit": 2}, "drift_limit"), ({"bootstrap": {"samples": 10, "seed": 0, "confidence": 0.95}}, ">= 100"), ({"splits": {"calibration": fresh, "selection": None, "test": fresh}}, "distinct"), ({"splits": {"calibration": None, "selection": None, "test": {**fresh, "role": "test"}}}, "preregistered test split"), ({"splits": {"calibration": "c4", "selection": None, "test": fresh}}, "dataset identity"), ({"prompt": {"chat_template": True}}, "system")): with pytest.raises(ps.PilotRefused, match=match): ps.normalize_protocol(protocol("c", fresh, **changes)) with pytest.raises(ps.PilotRefused, match="accuracy margin of 'a'"): ps.task_margins(ps.normalize_protocol(protocol("c", fresh, accuracy_margin={"a": 1.5})), ["a"]) with pytest.raises(ps.PilotRefused, match="maps each task"): ps.task_margins(ps.normalize_protocol(protocol("c", fresh, accuracy_margin={})), ["a"]) with pytest.raises(ps.PilotRefused, match="must declare"): ps.normalize_protocol({"candidate": "c"}) # --- pilot on the planted candidate --------------------------------------------------------- class ReasoningIO(BackendIO): """Planted generation: ``filler[dtype]`` extra tokens, then the argmax answer and eos.""" def __init__(self, filler=None, fail_dtype=None): super().__init__() self.filler, self.fail_dtype = filler or {}, fail_dtype def load(self, path, section): if section.get("dtype") == self.fail_dtype: raise RuntimeError("planted load failure") handle = super().load(path, section) model, extra = handle.model, self.filler.get(section.get("dtype"), 0) def generate(input_ids, attention_mask=None, max_new_tokens=1, **_options): answer = model(input_ids).logits[:, -1].argmax(-1, keepdim=True) new = torch.cat([torch.full((1, extra), SAMPLE), answer.cpu(), torch.full((1, 1), EOS)], 1).to(input_ids.device) return torch.cat([input_ids, new], 1)[:, :input_ids.shape[1] + max_new_tokens] model.generate = generate return handle def pilot(env, selected, name="adapter", io=None, **changes): # noqa: F811 run_dir, manifest = selected[name] return run_pilot(run_dir, protocol(manifest["id"], env["fresh"], **changes), model_io=io or ReasoningIO(), pack_resolver=ConceptPack.load) def test_pilot_pairs_noop_and_intervention_within_each_precision(env, selected): # noqa: F811 run_dir, manifest = selected["adapter"] io = ReasoningIO({"bfloat16": 1}) record = pilot(env, selected, io=io) assert record["status"] == "measured", record["reason"] # Four fresh disk loads (plus the tokenizer load): parent, parent, parent+adapter twice. assert io.loads == [str(env["source"])] * 5 cells_ = record["cells"] assert {c["load"]["loaded"] for c in cells_.values()} == {"unedited parent", "parent with saved adapter"} assert cells_["declared/intervention"]["precision"]["weights"] == "bfloat16" analysis = record["analysis"] pooled = analysis["costs"]["pooled"] assert pooled["declared/noop"]["mean_generated_tokens"] == \ pooled["reference/noop"]["mean_generated_tokens"] + 1 assert pooled["declared/noop"]["mean_reasoning_tokens"] == 1 assert pooled["reference/noop"]["truncation_rate"] == 0 # The steering intervention changes answers within each precision; precision does not. assert analysis["effects"]["pooled"]["intervention:reference"]["estimate"] != 0 assert analysis["effects"]["pooled"]["precision:noop"]["estimate"] == 0 assert set(analysis["effects"]) == {*record["margins"], "pooled"} kl = analysis["shared_prefix_kl"]["intervention:declared_vs_reference"] assert kl["positions"] > 0 and kl["mean"] >= 0 and kl["infinite_positions"] == 0 assert kl["numeric"].startswith("float64") rows = cells_["reference/noop"]["rows"] assert {r["seed_block"] for r in rows} == {0, 1} assert len(rows) == 2 * len({(r["task"], r["example_id"]) for r in rows}) assert all(sum(r["task"] == t for r in rows) <= 2 * 6 for t in record["margins"]) # More tokens at the same accuracy is not an efficiency claim. assert record["efficiency"]["claim"] is False stored = json.loads((Path(record["location"]) / "reasoning.json").read_text()) assert evaluation_digest(stored) == record["evaluation_sha256"] stored["host"]["declared/noop"]["seconds_per_row"] = 1e9 stored["efficiency"]["claim"] = True assert evaluation_digest(stored) == record["evaluation_sha256"] stored["cells"]["declared/noop"]["rows"][0]["final_correct"] ^= True assert evaluation_digest(stored) != record["evaluation_sha256"] matrix = support_matrix(run_dir, candidate=manifest["id"]) assert matrix["schema_version"] == 2 [row] = [r for r in matrix["reasoning"] if r["attempt"] == record["attempt"]] assert row["status"] == "measured" and set(row["cells"].values()) == {"measured"} assert row["decision"] == analysis["decision"] and row["evidence"]["valid"] assert all(q["backend"]["dtype"] == "float32" for q in matrix["qualified_pairs"]) def test_truncation_under_the_declared_precision_fails_the_pilot(env, selected): # noqa: F811 record = pilot(env, selected, io=ReasoningIO({"bfloat16": 6})) analysis = record["analysis"] declared = analysis["costs"]["pooled"]["declared/intervention"] assert declared["truncation_rate"] == 1 and declared["accuracy"] == 0 assert analysis["decision"] == "FAIL" assert any(h["gate"].startswith("precision:") for h in analysis["harmful_cells"]) assert record["efficiency"]["claim"] is False def test_failed_cells_are_recorded_and_the_attempt_is_kept(env, selected): # noqa: F811 run_dir, manifest = selected["adapter"] record = pilot(env, selected, io=ReasoningIO(fail_dtype="bfloat16")) assert record["status"] == "incomplete" and "planted load failure" in record["reason"] assert record["cells"]["declared/noop"]["status"] == "failed" assert record["analysis"]["decision"] == "INCOMPLETE" assert record["analysis"]["shared_prefix_kl"]["noop:declared_vs_reference"] is None [row] = [r for r in support_matrix(run_dir, candidate=manifest["id"])["reasoning"] if r["attempt"] == record["attempt"]] assert row["cells"]["declared/noop"] == "failed" and row["status"] == "incomplete" def test_unsupported_precisions_are_recorded_without_loading(env, selected): # noqa: F811 run_dir, manifest = selected["adapter"] io = ReasoningIO() gptq = {"backend": {**BF16, "quantization": "gptq-4bit"}, "weights": "int4", "activations": "bfloat16", "kv_cache": "bfloat16", "quantizer": {"name": "gptq", "version": "1"}, "calibration": {"method": "gptq", "data_id": "d", "rows_sha256": "r", "sequence_length": 512, "samples": 8, "seed": 0}} record = pilot(env, selected, io=io, precision=gptq) assert record["status"] == "unsupported" and "has no loader" in record["reason"] assert io.loads == [] and record["cells"] is None and record["class"] == "quantized" rows = support_matrix(run_dir, candidate=manifest["id"])["reasoning"] [row] = [r for r in rows if r["attempt"] == record["attempt"]] assert set(row["cells"].values()) == {"untested"} and row["decision"] is None class NoCacheIO(ReasoningIO): def load(self, path, section): handle = super().load(path, section) if section.get("dtype") == "bfloat16": del handle.model.generation_config return handle cacheless = pilot(env, selected, io=NoCacheIO()) assert cacheless["status"] == "unsupported" and "cache mode" in cacheless["reason"] if not torch.cuda.is_available(): before = len(support_matrix(run_dir, candidate=manifest["id"])["reasoning"]) cuda = {**NATIVE_BF16, "backend": {**BF16, "device": "cuda"}} with pytest.raises(bd.BackendUnavailable, match="nothing recorded"): pilot(env, selected, precision=cuda) assert len(support_matrix(run_dir, candidate=manifest["id"])["reasoning"]) == before def test_pilot_refusals_and_tampered_evidence(env, selected): # noqa: F811 run_dir, manifest = selected["edit"] with pytest.raises(ps.PilotRefused, match="no preregistered accuracy margin"): pilot(env, selected, "edit", accuracy_margin={"keep": 0.05}) record = pilot(env, selected, "edit", io=ReasoningIO()) assert record["status"] == "measured" assert record["cells"]["reference/intervention"]["load"]["loaded"] == "edited copy" path = Path(record["location"]) / "reasoning.json" path.write_text(path.read_text().replace('"status": "measured"', '"status": "measurex"', 1)) [row] = [r for r in support_matrix(run_dir, candidate=manifest["id"])["reasoning"] if r["attempt"] == record["attempt"]] assert row["status"] == "failed" and "no longer reproduces" in row["reason"] from obliteratus.experiments.precision_pilot import read_pilots path.write_text("{broken") assert all(r["attempt"] != record["attempt"] for r in read_pilots(run_dir)) def test_old_qualification_records_stay_readable(env, selected): # noqa: F811 from obliteratus.experiments.backend_qualify import evaluation_digest as digest run_dir, manifest = selected["lora"] record = run(env, selected, "lora", CPU32) assert record["schema_version"] == 2 and record["precision"]["weights"] == "float32" path = Path(record["location"]) / "qualification.json" old = json.loads(path.read_text()) old.pop("precision") old["schema_version"] = 1 old["evaluation_sha256"] = digest(old) path.write_text(json.dumps(old)) matrix = support_matrix(run_dir, candidate=manifest["id"], backends=[BF16]) [row] = [r for r in matrix["rows"] if r["backend_id"] == record["backend_id"]] assert row["status"] == "qualified" and row["precision"] is None assert row["record_schema_version"] == 1 [untested] = [r for r in matrix["rows"] if r["status"] == "untested"] assert untested["precision"]["weights"] == "bfloat16" def test_parent_load_refuses_changed_parent_files(selected): # noqa: F811 from obliteratus.experiments.backend_load import load_parent _, manifest = selected["edit"] changed = {**manifest, "parent": {**manifest["parent"], "files": {}}} with pytest.raises(bd.QualificationRefused, match="parent model files changed"): load_parent(changed, bd.normalize(CPU32, manifest), BackendIO(), {}) def test_cli_qualify_reasoning(env, selected, monkeypatch, capsys, tmp_path): # noqa: F811 from obliteratus.cli import main monkeypatch.setattr("obliteratus.experiments.precision_pilot.TransformersBackendIO", ReasoningIO) monkeypatch.setattr("obliteratus.experiments.inputs.default_pack_resolver", ConceptPack.load) run_dir, manifest = selected["lora"] spec = tmp_path / "pilot.json" spec.write_text(json.dumps(protocol(manifest["id"], env["fresh"]))) main(["experiments", "qualify", "reasoning", str(spec), "--run", str(run_dir)]) result = json.loads(capsys.readouterr().out) assert result["status"] == "measured" and result["decision"] in ( "PASS", "FAIL", "INCONCLUSIVE") spec.write_text(json.dumps(protocol(manifest["id"], env["fresh"], max_examples=0))) with pytest.raises(SystemExit) as refused: main(["experiments", "qualify", "reasoning", str(spec), "--run", str(run_dir)]) assert refused.value.code == 4 and "bounded pilot" in capsys.readouterr().err def test_pilot_samples_by_seed_block_and_refuses_unselected_or_changed_artifacts( env, selected): # noqa: F811 from obliteratus.experiments import candidate_edits as edits from tests.test_persistent_candidates import build sample = {"strategy": "sample", "max_new_tokens": 4, "temperature": 0.7, "top_p": 0.9, "top_k": 5} record = pilot(env, selected, decoding=sample) assert record["status"] == "measured" assert record["cells"]["declared/noop"]["context"]["decoding"] == sample run_dir, manifest = selected["adapter"] built = build(env, "steer", tolerances={"agreement": 0.8}) with pytest.raises(bd.QualificationRefused, match="only a candidate whose every"): run_pilot(env["steer"][0], protocol(built["id"], env["fresh"]), model_io=ReasoningIO(), pack_resolver=ConceptPack.load) adapter = Path(manifest["location"]) / "artifact" / edits.ADAPTER_FILE original = adapter.read_bytes() adapter.write_bytes(original + b" ") try: io = ReasoningIO() changed = pilot(env, selected, io=io) assert changed["status"] == "failed" and changed["reason"].startswith("artifact_digests") assert io.loads == [] and changed["cells"] is None finally: adapter.write_bytes(original)