mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
355 lines
15 KiB
Python
355 lines
15 KiB
Python
"""CV-10 ordered repair, strict acceptance, rollback."""
|
|
|
|
from contextlib import contextmanager
|
|
import json
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
from obliteratus.concepts import EvaluationReport, RepairConfig, run_repair_ladder
|
|
from obliteratus.concepts.repair import REPAIR_STEPS, rejection_reasons
|
|
from obliteratus.concepts.statistics import paired_refusal_statistics
|
|
from tests.test_concept_extraction_pipeline import small_target
|
|
|
|
pytestmark = [
|
|
pytest.mark.cpu,
|
|
pytest.mark.integration,
|
|
pytest.mark.usefixtures("tiny_model_runtime"),
|
|
]
|
|
|
|
|
|
def report(coherence=0.4, projection=0.0, kl=0.1):
|
|
return EvaluationReport(
|
|
mechanistic={"before": {"0": 1.0}, "after": {"0": projection}},
|
|
collateral={
|
|
"status": "PASS", "first_token_kl": kl,
|
|
"coherence": {"before": {"rate": 1.0, "n": 10},
|
|
"after": {"rate": coherence, "n": 10}},
|
|
"refusal": {"pristine": {"rate": 0.8}, "intervened": {"rate": 0.8}},
|
|
"neighbour_projections": {"before": {"other": {"0": 1}},
|
|
"after": {"other": {"0": 0.9}}},
|
|
},
|
|
)
|
|
|
|
|
|
class Candidates:
|
|
def __init__(self):
|
|
self.active, self.committed, self.events = None, None, []
|
|
|
|
def __call__(self, step, config):
|
|
@contextmanager
|
|
def candidate():
|
|
self.events.append(step)
|
|
self.active = step
|
|
try:
|
|
yield SimpleNamespace(
|
|
parameters={}, accept=lambda: setattr(self, "committed", step)
|
|
)
|
|
finally:
|
|
self.active = self.committed
|
|
yield candidate()
|
|
|
|
|
|
def test_order_reject_reintroduced_direction_and_accept(tmp_path):
|
|
candidates = Candidates()
|
|
readings = iter([report(1, 0.5), report(1, 0.01), report(1, kl=0.8), report(1)])
|
|
repaired, final = run_repair_ladder(report(), lambda: next(readings), candidates,
|
|
output_dir=tmp_path)
|
|
assert candidates.events == list(REPAIR_STEPS[:4])
|
|
assert repaired.accepted_step == "rdo_refinement"
|
|
assert candidates.active == "rdo_refinement"
|
|
assert "target projection reintroduced" in repaired.attempts[0]["reasons"][0]
|
|
assert final.collateral["coherence"]["after"]["rate"] == 1
|
|
assert json.loads((tmp_path / "repair.json").read_text())["repair"] == "complete"
|
|
|
|
|
|
def test_incomplete_restores_original_and_records_best_tradeoff():
|
|
candidates = Candidates()
|
|
original = report()
|
|
repaired, final = run_repair_ladder(original, lambda: report(0.8), candidates)
|
|
assert candidates.events == list(REPAIR_STEPS)
|
|
assert candidates.active is None
|
|
assert repaired.repair == "incomplete" and "repair: incomplete" in repaired.reason
|
|
assert repaired.best_tradeoff == "norm_preservation"
|
|
assert final is original
|
|
assert all(not row["accepted"] for row in repaired.attempts)
|
|
|
|
|
|
def test_repair_rejects_loss_of_paired_collateral_pass():
|
|
pristine = [True] * 160 + [False] * 160
|
|
repaired_flags = [False] * 40 + [True] * 160 + [False] * 120
|
|
original, candidate = report(), report(1)
|
|
for item, flags in ((original, pristine), (candidate, repaired_flags)):
|
|
refusal = paired_refusal_statistics(pristine, flags)
|
|
item.collateral.update(status=refusal["status"], refusal=refusal)
|
|
assert original.collateral["status"] == "PASS"
|
|
assert candidate.collateral["status"] == "INCONCLUSIVE"
|
|
assert original.collateral["refusal"]["drop"] == candidate.collateral["refusal"]["drop"] == 0
|
|
candidates = Candidates()
|
|
repaired, final = run_repair_ladder(original, lambda: candidate, candidates)
|
|
assert repaired.repair == "incomplete" and repaired.accepted_step is None
|
|
assert candidates.active is None and candidates.committed is None
|
|
assert final is original
|
|
assert all("collateral verdict worsened" in row["reasons"] for row in repaired.attempts)
|
|
|
|
|
|
@pytest.mark.parametrize("status", ["INCONCLUSIVE", "UNDERPOWERED", "INCOMPLETE",
|
|
"skipped", "not tested", "FAIL", None])
|
|
def test_established_collateral_pass_must_be_retained(status):
|
|
candidate = report(1)
|
|
candidate.collateral["status"] = status
|
|
assert "collateral verdict worsened" in rejection_reasons(candidate, report(), RepairConfig())
|
|
|
|
|
|
@pytest.mark.parametrize("value", [float("nan"), float("inf"), -float("inf")])
|
|
@pytest.mark.parametrize("metric", ["projection", "kl", "coherence", "refusal",
|
|
"neighbour", "behavioural"])
|
|
@pytest.mark.parametrize("accept_later", [False, True])
|
|
def test_nonfinite_rejected_ladder_continues_and_report_saved(
|
|
tmp_path, value, metric, accept_later,
|
|
):
|
|
invalid = report(1)
|
|
if metric == "projection":
|
|
invalid.mechanistic["after"]["0"] = value
|
|
elif metric == "kl":
|
|
invalid.collateral["first_token_kl"] = value
|
|
elif metric == "coherence":
|
|
invalid.collateral["coherence"]["after"]["rate"] = value
|
|
elif metric == "refusal":
|
|
invalid.collateral["refusal"]["intervened"]["rate"] = value
|
|
elif metric == "neighbour":
|
|
invalid.collateral["neighbour_projections"]["after"]["other"]["0"] = value
|
|
else:
|
|
invalid.behavioural = {"scores": [value]}
|
|
candidates = Candidates()
|
|
readings = iter([invalid] + [report(1) if accept_later else invalid] * 4)
|
|
original = report()
|
|
repaired, final = run_repair_ladder(original, lambda: next(readings), candidates,
|
|
output_dir=tmp_path)
|
|
saved = json.loads((tmp_path / "repair.json").read_text(),
|
|
parse_constant=lambda token: pytest.fail(f"Non-finite JSON: {token}"))
|
|
assert saved["attempts"] == repaired.attempts
|
|
first = repaired.attempts[0]
|
|
assert not first["accepted"]
|
|
assert any(reason.startswith("non-finite measurement: evaluation.")
|
|
for reason in first["reasons"])
|
|
assert "null" in json.dumps(first["evaluation"], allow_nan=False)
|
|
if accept_later:
|
|
assert candidates.events == list(REPAIR_STEPS[:2])
|
|
assert repaired.accepted_step == "retained_fraction"
|
|
assert candidates.active == "retained_fraction"
|
|
assert final is not original
|
|
else:
|
|
assert candidates.events == list(REPAIR_STEPS)
|
|
assert candidates.active is None and final is original
|
|
assert saved["repair"] == "incomplete"
|
|
assert "repair: incomplete" in saved["reason"]
|
|
|
|
|
|
def test_nonfinite_original_saved_when_candidates_unavailable(tmp_path):
|
|
original = report(projection=float("nan"))
|
|
repaired, final = run_repair_ladder(original, Mock(), lambda step, config: (),
|
|
output_dir=tmp_path)
|
|
saved = json.loads((tmp_path / "repair.json").read_text())
|
|
assert saved["repair"] == repaired.repair == "incomplete"
|
|
assert final is original
|
|
assert all(row["evaluation"]["mechanistic"]["after"]["0"] is None
|
|
for row in saved["attempts"])
|
|
|
|
|
|
@pytest.mark.parametrize("accept_rollback", [False, True])
|
|
def test_multi_candidate_kl_rollback_order_and_stop(accept_rollback):
|
|
active, committed, events = None, None, []
|
|
|
|
@contextmanager
|
|
def trial(step, fraction):
|
|
nonlocal active, committed
|
|
assert active is None # Every rejected trial has rolled back before the next.
|
|
active = (step, fraction)
|
|
events.append(active)
|
|
|
|
def accept():
|
|
nonlocal committed
|
|
committed = active
|
|
|
|
try:
|
|
yield SimpleNamespace(parameters={"retained_fraction": fraction}, accept=accept)
|
|
finally:
|
|
active = committed
|
|
|
|
def candidates(step, config):
|
|
fractions = config.rollback_fractions if step == "kl_budget_rollback" else (None,)
|
|
for fraction in fractions:
|
|
yield trial(step, fraction)
|
|
|
|
def evaluate():
|
|
if active == ("kl_budget_rollback", 0.25):
|
|
return report(1, kl=0.8)
|
|
if active == ("kl_budget_rollback", 0.5):
|
|
return report(1, projection=0.2)
|
|
if active == ("kl_budget_rollback", 0.75) and accept_rollback:
|
|
return report(1)
|
|
return report(0.8)
|
|
|
|
repaired, _ = run_repair_ladder(report(), evaluate, candidates)
|
|
expected = [(step, None) for step in REPAIR_STEPS[:2]]
|
|
expected += [("kl_budget_rollback", f) for f in (0.25, 0.5, 0.75)]
|
|
if not accept_rollback:
|
|
expected += [("kl_budget_rollback", 1.0)]
|
|
expected += [(step, None) for step in REPAIR_STEPS[3:]]
|
|
assert events == expected
|
|
assert [(row["step"], row["parameters"]["retained_fraction"])
|
|
for row in repaired.attempts] == expected
|
|
assert "KL outside budget or unmeasured" in repaired.attempts[2]["reasons"]
|
|
assert "target projection reintroduced or unmeasured" in repaired.attempts[3]["reasons"]
|
|
assert repaired.accepted_step == ("kl_budget_rollback" if accept_rollback else None)
|
|
assert active == (("kl_budget_rollback", 0.75) if accept_rollback else None)
|
|
|
|
|
|
@pytest.mark.parametrize("missing", [False, True])
|
|
def test_non_degraded_or_unmeasured_is_noop(missing):
|
|
original = report(1)
|
|
if missing:
|
|
original.collateral = {"status": "skipped"}
|
|
candidates = Mock(side_effect=AssertionError("must not attempt repair"))
|
|
result, final = run_repair_ladder(original, Mock(), candidates)
|
|
assert result.repair == ("not tested" if missing else "not needed")
|
|
assert final is original and not result.attempts
|
|
|
|
|
|
@pytest.mark.parametrize("metric", ["kl", "neighbour", "refusal", "missing", "nan", "layer"])
|
|
def test_collateral_and_projection_fail_closed(metric):
|
|
candidate = report(1)
|
|
if metric == "kl":
|
|
candidate.collateral["first_token_kl"] = 0.2
|
|
elif metric == "neighbour":
|
|
candidate.collateral["neighbour_projections"]["after"]["other"]["0"] = 1.2
|
|
elif metric == "refusal":
|
|
candidate.collateral["refusal"]["intervened"]["rate"] = 0.7
|
|
elif metric == "missing":
|
|
candidate.collateral.pop("refusal")
|
|
elif metric == "nan":
|
|
candidate.mechanistic["after"]["0"] = float("nan")
|
|
else:
|
|
candidate.mechanistic["after"] = {}
|
|
assert rejection_reasons(candidate, report(), RepairConfig())
|
|
|
|
|
|
def test_failure_rolls_back_and_unavailable_steps_recorded():
|
|
candidates = Candidates()
|
|
with pytest.raises(RuntimeError, match="measurement"):
|
|
run_repair_ladder(report(), Mock(side_effect=RuntimeError("measurement")), candidates)
|
|
assert candidates.active is None
|
|
result, _ = run_repair_ladder(report(), Mock(), lambda step, config: ())
|
|
assert [row["step"] for row in result.attempts] == list(REPAIR_STEPS)
|
|
assert result.repair == "incomplete"
|
|
|
|
|
|
@pytest.mark.parametrize("kwargs", [
|
|
{"coherence_budget": -1}, {"kl_budget": float("nan")}, {"retained_fraction": 2},
|
|
{"rollback_fractions": ()}, {"lora_rank": 0}, {"rdo_steps": False},
|
|
])
|
|
def test_bad_config(kwargs):
|
|
with pytest.raises(ValueError):
|
|
RepairConfig(**kwargs)
|
|
|
|
|
|
def test_incomplete_export_requires_choice(tmp_path):
|
|
pipeline = AbliterationPipeline("offline", concept=small_target(), output_dir=str(tmp_path))
|
|
pipeline._repair_report, _ = run_repair_ladder(report(), Mock(), lambda step, config: ())
|
|
with pytest.raises(ValueError, match="repair_export_choice"):
|
|
pipeline._rebirth()
|
|
pipeline._repair_export_choice = "original"
|
|
pipeline._gather_state_dict = Mock(side_effect=RuntimeError("export reached"))
|
|
with pytest.raises(RuntimeError, match="export reached"):
|
|
pipeline._rebirth()
|
|
assert pipeline._build_metadata()["concept"]["repair"]["repair"] == "incomplete"
|
|
|
|
|
|
@pytest.mark.parametrize("flag, expected", [("--repair", True), ("--skip-repair", False)])
|
|
def test_cli_repair_options(tmp_path, monkeypatch, flag, expected):
|
|
from obliteratus import cli
|
|
|
|
factory = Mock(return_value=Mock(run=Mock(return_value=tmp_path)))
|
|
monkeypatch.setattr("obliteratus.abliterate.AbliterationPipeline", factory)
|
|
monkeypatch.setattr("sys.argv", ["obliteratus", "obliterate", "offline", flag,
|
|
"--repair-export-choice", "original"])
|
|
cli.main()
|
|
assert factory.call_args.kwargs["repair"] is expected
|
|
assert factory.call_args.kwargs["repair_export_choice"] == "original"
|
|
|
|
|
|
@pytest.mark.parametrize("flag", ["--repair", "--skip-repair"])
|
|
def test_remote_rejects_repair(monkeypatch, flag):
|
|
from obliteratus import cli
|
|
|
|
runner = Mock()
|
|
monkeypatch.setattr(cli, "_make_remote_runner", runner)
|
|
monkeypatch.setattr(
|
|
"sys.argv", ["obliteratus", "obliterate", "offline", "--remote", "gpu", flag]
|
|
)
|
|
with pytest.raises(SystemExit) as exc:
|
|
cli.main()
|
|
assert exc.value.code == 2
|
|
runner.assert_not_called()
|
|
|
|
|
|
def test_repair_rejects_more_invalid_output_even_below_pristine():
|
|
def with_invalid(item, intervened):
|
|
item.collateral["invalid_output"] = {
|
|
"pristine": {"rate": 0.5}, "intervened": {"rate": intervened},
|
|
}
|
|
return item
|
|
|
|
original = with_invalid(report(), 0.1)
|
|
assert "collateral worse than before step or unmeasured" in rejection_reasons(
|
|
with_invalid(report(1), 0.4), original, RepairConfig()
|
|
)
|
|
assert rejection_reasons(with_invalid(report(1), 0.1), original, RepairConfig()) == []
|
|
|
|
|
|
@pytest.mark.parametrize("status", ["PASS", "FAIL", "skipped", "INCOMPLETE", "future-status"])
|
|
def test_protected_repair_runs_on_collateral_failure_and_requires_pass(status):
|
|
original = report(1)
|
|
original.collateral["status"] = "FAIL"
|
|
measured = report(1, projection=0.4)
|
|
measured.collateral["status"] = status
|
|
candidates = Candidates()
|
|
result, final = run_repair_ladder(
|
|
original, lambda: measured, candidates, protected_direction=True,
|
|
)
|
|
assert candidates.events == ["protected_direction"]
|
|
assert result.accepted_step == ("protected_direction" if status == "PASS" else None)
|
|
assert final is (measured if status == "PASS" else original)
|
|
assert measured.metadata["protected_direction_tradeoff"]["original_target"]["after"] == {"0": 0.4}
|
|
if status != "PASS":
|
|
assert "protected-direction collateral battery did not PASS" in result.attempts[0]["reasons"]
|
|
|
|
|
|
def test_protected_repair_rejects_missing_target_and_nonfinite_evidence():
|
|
original = report(1)
|
|
original.collateral["status"] = "FAIL"
|
|
measured = report(1)
|
|
measured.mechanistic["after"] = {}
|
|
candidates = Candidates()
|
|
result, final = run_repair_ladder(
|
|
original, lambda: measured, candidates, protected_direction=True,
|
|
)
|
|
assert result.accepted_step is None and final is original
|
|
assert "target projection unmeasured" in result.attempts[0]["reasons"]
|
|
|
|
|
|
def test_protected_repair_does_not_accept_legacy_step_with_failed_battery():
|
|
original = report()
|
|
original.collateral["status"] = "FAIL"
|
|
measured = report(1)
|
|
measured.collateral["status"] = "FAIL"
|
|
candidates = Candidates()
|
|
result, final = run_repair_ladder(
|
|
original, lambda: measured, candidates, protected_direction=True,
|
|
)
|
|
assert candidates.events == [*REPAIR_STEPS, "protected_direction"]
|
|
assert result.accepted_step is None and final is original
|