Files
OBLITERATUS/tests/test_precision_effects.py
Joseph MaglyandClaude Opus 5.5 6c2f79672f feat(experiments): matched-precision reasoning controls for backend qualification
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>
2026-11-16 22:03:40 -05:00

168 lines
9.1 KiB
Python

"""Paired matched-precision analysis on synthetic cells (CPU contracts).
Failed and truncated outputs, missing precision fields, incompatible pairings,
harmful tasks hidden by pooled gains, drift gating and efficiency claims.
"""
from __future__ import annotations
import pytest
from obliteratus.experiments import precision_effects as fx
from obliteratus.experiments import precision_spec as ps
from tests.test_precision_reasoning import BF16, LAST, NATIVE_BF16, PREREG
pytestmark = pytest.mark.cpu
def _rows(outcomes, tokens=4):
"""``outcomes``: {task: [correct flags per example]}; one seed block."""
rows = []
for task, flags in outcomes.items():
for index, ok in enumerate(flags):
text = "yes" if ok else "no"
rows.append({"task": task, "example_id": f"e{index}", "seed_block": 0,
"prompt_tokens": 3, "generated_tokens": tokens,
"reasoning_tokens": tokens - 2, "termination": "eos",
"truncated": False, "answer_found": True, "final_answer": text,
"final_correct": ok, "intermediate_correct": False,
"completion": text})
return rows
def cells(outcomes, **overrides):
reference, declared = ps.reference_precision(), ps.normalize_precision(NATIVE_BF16)
context = {"parent_files_sha256": "p", "tokenizer_vocab_sha256": "v",
"prompts_sha256": "q", "decoding": {"strategy": "greedy", "max_new_tokens": 4},
"seed_blocks": [0], "parser": LAST, "prompt": None}
found = {}
for name in fx.CELLS:
side, arm = name.split("/")
found[name] = {"arm": arm, "precision": reference if side == "reference" else declared,
"artifact_sha256": "parent:p" if arm == "noop" else "a",
"context": dict(context), "status": "measured",
"rows": _rows(outcomes[name])}
found[name].update(overrides.get(name, {}))
return found
def every(flags_by_task):
return {name: flags_by_task for name in fx.CELLS}
def test_harmful_task_hidden_by_a_pooled_gain_fails_the_pilot():
n = 40
base = every({"easy": [False] * n, "hard": [True] * n})
# The declared precision gains a lot on "easy" and loses 25% of "hard": pooled looks fine.
base["declared/intervention"] = {"easy": [True] * n, "hard": [True] * 30 + [False] * 10}
result = fx.analyze(cells(base), {**PREREG, "drift_limit": 1.0},
{"easy": 0.05, "hard": 0.05})
assert result["gates"]["pooled"]["precision:intervention"] == "PASS"
assert result["gates"]["hard"]["precision:intervention"] == "FAIL"
assert result["decision"] == "FAIL"
assert {"task": "hard", "gate": "precision:intervention"} in result["harmful_cells"]
assert "precision:intervention" in result["pooled_hides_harm"]
worst = result["worst_cell"]["precision_lower_bound"]
assert worst["task"] == "hard" and worst["effect"] == "precision:intervention"
effect = result["effects"]["hard"]["precision:intervention"]
assert effect["transitions"]["correct_to_wrong"] == 10 and effect["n_examples"] == n
assert effect["estimate"] == pytest.approx(-0.25) and effect["ci"][1] < 0
assert effect["sensitivity_note"].endswith("not a confidence interval")
# The interaction separates the precision effect on the edit from that on the parent.
assert result["effects"]["hard"]["precision:noop"]["estimate"] == 0
assert result["effects"]["hard"]["interaction"]["estimate"] == pytest.approx(-0.25)
assert result["gates"]["hard"]["interaction"] == "FAIL"
assert result["drift"]["hard"]["intervention:declared_vs_reference"]["final_answer"] == 0.25
def test_identical_cells_pass_and_drift_is_gated_separately():
n = 40
same = every({"a": [True] * 20 + [False] * 20})
result = fx.analyze(cells(same), PREREG, {"a": 0.05})
assert result["decision"] == "PASS" and result["harmful_cells"] == []
assert result["effects"]["a"]["interaction"]["ci"] == [0.0, 0.0]
flipped = dict(same)
flipped["declared/noop"] = {"a": [False] * 20 + [True] * 20}
result = fx.analyze(cells(flipped), {**PREREG, "drift_limit": 0.1}, {"a": 0.05})
assert result["effects"]["a"]["precision:noop"]["estimate"] == 0 # accuracy unchanged
assert result["gates"]["a"]["drift:noop"] == "FAIL" and result["decision"] == "FAIL"
assert result["worst_cell"]["drift"]["value"] == 1.0
assert len(fx.bootstrap([1, 0] * n, samples=100, seed=0, confidence=0.9)[1]) == 2
assert fx.bootstrap([], samples=100, seed=0, confidence=0.9) == (None, None)
small = every({"a": [True, False]})
small["declared/intervention"] = {"a": [False, False]}
assert fx.analyze(cells(small), PREREG, {"a": 0.05})["decision"] == "INCONCLUSIVE"
def test_failed_and_truncated_outputs_are_counted_and_failed_cells_are_incomplete():
base = every({"a": [True] * 10})
data = cells(base)
rows = data["declared/intervention"]["rows"]
for row in rows[:4]: # truncated at the budget: more tokens, no final answer
row.update(generated_tokens=4, termination="budget", truncated=True,
answer_found=False, final_answer=None, final_correct=False,
intermediate_correct=True, completion="yes sample sample sample")
rows[4].update(answer_found=False, final_answer=None, final_correct=False, completion="")
result = fx.analyze(data, {**PREREG, "drift_limit": 1.0}, {"a": 0.05})
cost = result["costs"]["a"]["declared/intervention"]
assert cost["truncation_rate"] == 0.4 and cost["failed_output_rate"] == 0.1
assert cost["intermediate_correct_final_wrong"] == 4 and cost["accuracy"] == 0.5
assert cost["terminations"] == {"eos": 6, "budget": 4, "other": 0}
assert result["decision"] == "FAIL"
broken = cells(base, **{"declared/noop": {"status": "failed", "rows": [],
"reason": "RuntimeError: out of memory"}})
result = fx.analyze(broken, PREREG, {"a": 0.05})
assert result["decision"] == "INCOMPLETE" and result["failed_cells"] == ["declared/noop"]
assert result["effects"]["a"]["precision:noop"] is None
assert result["effects"]["a"]["precision:intervention"] is not None
assert result["gates"]["a"]["interaction"] == "UNTESTED"
assert "declared/noop" not in result["costs"]["a"]
def test_incompatible_pairings_and_missing_precision_fields_are_refused():
base = every({"a": [True, False]})
reference = ps.reference_precision()
partial = {k: v for k, v in ps.normalize_precision(NATIVE_BF16).items() if k != "kv_cache"}
other = ps.normalize_precision({**NATIVE_BF16, "backend": {**BF16, "cache": "uncached"}})
decoding = {"strategy": "greedy", "max_new_tokens": 8}
context = cells(base)["declared/noop"]["context"]
cases = (
({"declared/noop": {"precision": partial}}, r"lacks precision fields \['kv_cache'\]"),
({"declared/noop": {"precision": other}}, "run different precisions"),
({"declared/noop": {"precision": reference},
"declared/intervention": {"precision": reference}}, "equals the reference"),
({"declared/noop": {"artifact_sha256": "parent:other"}}, "noop cells load different"),
({"reference/intervention": {"artifact_sha256": "parent:p"},
"declared/intervention": {"artifact_sha256": "parent:p"}}, "loads the no-op"),
({"declared/noop": {"context": {**context, "decoding": decoding}}}, r"\['decoding'\]"),
({"declared/intervention": {"context": {**context, "parser": {"id": "x"}}}},
r"\['parser'\]"),
({"declared/noop": {"rows": _rows({"a": [True]})}}, "same rows"),
({"declared/noop": {"rows": _rows({"a": [True, True]}) * 2}}, "repeats"))
for overrides, match in cases:
with pytest.raises(fx.PairingRefused, match=match):
fx.check_pairing(cells(base, **overrides))
with pytest.raises(fx.PairingRefused, match="missing cells"):
fx.check_pairing({"reference/noop": cells(base)["reference/noop"]})
def test_efficiency_needs_every_accuracy_gate_and_a_measured_resource_gain():
data = every({"a": [True] * 20})
short = cells(data, **{"declared/intervention": {"rows": _rows({"a": [True] * 20}, 2)}})
result = fx.analyze(short, PREREG, {"a": 0.05})
assert result["decision"] == "PASS"
tokens = fx.efficiency(result, {}, "total_tokens")
assert tokens["claim"] and tokens["declared"] < tokens["reference"]
# Shorter output alone is not a latency pass.
host = {"reference/intervention": {"seconds_per_row": 1.0, "peak_bytes": 5},
"declared/intervention": {"seconds_per_row": 2.0, "peak_bytes": 3}}
latency = fx.efficiency(result, host, "latency")
assert not latency["claim"] and "shorter output alone" in latency["reason"]
assert fx.efficiency(result, host, "peak_memory")["claim"]
harmed = every({"a": [True] * 20})
harmed["declared/intervention"] = {"a": [False] * 20}
worse = fx.analyze(cells(harmed, **{"declared/intervention": {
"rows": _rows({"a": [False] * 20}, 2)}}), PREREG, {"a": 0.05})
found = fx.efficiency(worse, host, "total_tokens")
assert not found["claim"] and "accuracy decision is FAIL" in found["reason"]