"""Behavioural reload verification of saved edited checkpoints (issue #525).""" from __future__ import annotations import json import os import subprocess import sys from pathlib import Path from types import SimpleNamespace from unittest.mock import MagicMock import pytest import torch from obliteratus import verify_checkpoint as vc from obliteratus import verify_checkpoint_report as vr from obliteratus.verify_checkpoint import VerificationInputError pytestmark = pytest.mark.cpu WORDS = ["[PAD]", "[UNK]", "how", "to", "a", "the", "write", "explain", "make", "build", "from", "scratch", "tutorial", "on", "can", "i", "set", "up", "new", "email", "account", "tell", "me", "create", "website", "bake", "cake", "soup", "bomb"] TOKENS = 6 def _tiny(seed: int = 7): from tokenizers import Tokenizer from tokenizers.models import WordLevel from tokenizers.normalizers import Lowercase from tokenizers.pre_tokenizers import Whitespace from transformers import GPT2Config, GPT2LMHeadModel, PreTrainedTokenizerFast backend = Tokenizer(WordLevel(dict(zip(WORDS, range(len(WORDS)))), unk_token="[UNK]")) backend.normalizer = Lowercase() backend.pre_tokenizer = Whitespace() tokenizer = PreTrainedTokenizerFast(tokenizer_object=backend, unk_token="[UNK]", pad_token="[PAD]", model_max_length=64) config = GPT2Config(n_layer=2, n_embd=16, n_head=2, n_positions=64, vocab_size=len(WORDS), bos_token_id=None, eos_token_id=None, pad_token_id=0, attn_implementation="eager") with torch.random.fork_rng(devices=[]): torch.manual_seed(seed) model = GPT2LMHeadModel(config).eval() return model, tokenizer def _edit(model) -> None: """A stand-in edit whose greedy effect is certain: logits are negated.""" with torch.no_grad(): model.transformer.ln_f.weight.mul_(-1) def _save(model, tokenizer, directory: Path) -> Path: model.save_pretrained(directory, safe_serialization=True) tokenizer.save_pretrained(directory) return directory @pytest.fixture(scope="module") def saved(tmp_path_factory): """Stock checkpoint, correct edited checkpoint, and an unpersisted-edit checkpoint.""" root = tmp_path_factory.mktemp("verify") model, tokenizer = _tiny() stock = _save(model, tokenizer, root / "stock") _edit(model) reference = vc.record_reference(model, tokenizer, max_new_tokens=TOKENS, model_identity={"source_model": "tiny-gpt2"}) edited = _save(model, tokenizer, root / "edited") vc.write_reference(edited, reference) broken = _save(*_tiny(), root / "broken") # edit never persisted (root / "reference.json").write_text(json.dumps(reference), encoding="utf-8") return SimpleNamespace(root=root, stock=stock, edited=edited, broken=broken, reference=root / "reference.json", records=reference["records"]) def test_builtin_prompt_set_is_pinned_and_versioned(): prompts = vc.builtin_prompt_set() assert len(prompts) == 2 * vc.PROMPTS_PER_CATEGORY assert [p["category"] for p in prompts[:8]] == ["harmful"] * 8 assert vc.prompt_set_digest(prompts) == vc.PROMPT_SET_V1_SHA256 def test_builtin_prompt_set_refuses_silent_change(monkeypatch): import obliteratus.prompts as prompts monkeypatch.setattr(prompts, "BUILTIN_HARMFUL", ["changed"] + prompts.BUILTIN_HARMFUL[1:]) with pytest.raises(RuntimeError, match="bump PROMPT_SET_ID"): vc.builtin_prompt_set() def test_first_divergence_and_prefix_agreement(): assert vr.first_divergence([1, 2, 3], [1, 2, 3]) is None assert vr.first_divergence([1, 2, 3], [1, 9, 3]) == 1 assert vr.first_divergence([1, 2], [1, 2, 3]) == 2 assert vr._prefix_agreement([1, 2, 3, 4], [1, 2, 0, 0]) == 0.5 assert vr._prefix_agreement([], []) == 1.0 def test_round_trip_passes_and_report_is_bound(saved): report = vr.verify_checkpoint(saved.edited, device="cpu") assert report["verdict"] == "pass", report["reasons"] assert report["schema"] == vr.REPORT_SCHEMA and report["schema_version"] == 1 assert report["metrics"]["exact_match_rate"] == 1.0 assert set(report["checkpoint"]["files"]) >= {"model.safetensors", "config.json", "tokenizer.json"} assert vc.REFERENCE_FILENAME not in report["checkpoint"]["files"] assert report["reference"]["bound_to_checkpoint_digests"] is True assert report["reference"]["prompt_set"]["id"] == vc.PROMPT_SET_ID assert report["reference"]["prompt_set"]["sha256"] == vc.PROMPT_SET_V1_SHA256 assert report["model"]["source_model"] == "tiny-gpt2" assert report["model"]["model_type"] == "gpt2" runtime = report["runtime"] assert runtime["device"] == "cpu" and runtime["dtype"] == "float32" assert runtime["torch"] == torch.__version__ and runtime["transformers"] assert report["tolerance"] == {"quantization": "none", "min_exact_match_rate": 1.0, "min_mean_prefix_agreement": 1.0, "max_refusal_rate_increase": 0.0} assert all(row["first_divergence"] is None for row in report["prompts"]) def test_unpersisted_edit_fails_with_clear_reason(saved): report = vr.verify_checkpoint(saved.broken, reference_path=saved.reference, device="cpu") codes = [reason["code"] for reason in report["reasons"]] assert report["verdict"] == "fail" assert "exact_match_below_tolerance" in codes assert "diverges at generated token" in report["reasons"][codes.index( "exact_match_below_tolerance")]["detail"] with_stock = vr.verify_checkpoint(saved.broken, reference_path=saved.reference, stock_model=saved.stock, device="cpu") assert "edit_not_persisted" in [reason["code"] for reason in with_stock["reasons"]] assert with_stock["metrics"]["checkpoint_stock_match_rate"] == 1.0 def test_stock_comparison_on_passing_checkpoint(saved): report = vr.verify_checkpoint(saved.edited, stock_model=saved.stock, device="cpu") assert report["verdict"] == "pass" assert report["metrics"]["checkpoint_stock_match_rate"] < 1.0 assert "stock" in report["metrics"]["harmful_refusal_rate"] def test_wrong_tensor_after_save_fails(saved, tmp_path): import shutil from safetensors.torch import load_file, save_file target = tmp_path / "tampered" shutil.copytree(saved.edited, target) tensors = load_file(target / "model.safetensors") name = next(k for k in tensors if k.endswith("ln_f.weight")) tensors[name] = tensors[name].neg().contiguous() # undo the edit in one tensor save_file(tensors, target / "model.safetensors", metadata={"format": "pt"}) report = vr.verify_checkpoint(target, device="cpu") codes = [reason["code"] for reason in report["reasons"]] assert report["verdict"] == "fail" assert codes[0] == "checkpoint_changed_since_reference" assert "model.safetensors" in report["reasons"][0]["detail"] assert "exact_match_below_tolerance" in codes def test_missing_reference_never_passes(saved, monkeypatch): monkeypatch.setattr(vr, "load_model", MagicMock(side_effect=AssertionError("no load"))) report = vr.verify_checkpoint(saved.stock, device="cpu") assert report["verdict"] == "fail" assert report["reasons"][0]["code"] == "no_reference" assert report["checkpoint"]["files"] def test_empty_reference_is_structural_and_fails(saved, tmp_path): reference = json.loads(saved.reference.read_text(encoding="utf-8")) for record in reference["records"]: record["output_ids"], record["output_text"] = [], "" path = tmp_path / "empty.json" path.write_text(json.dumps(reference), encoding="utf-8") report = vr.verify_checkpoint(saved.edited, reference_path=path, device="cpu", tolerance=vr.Tolerance(0.0, 0.0, 1.0)) assert "empty_reference" in [reason["code"] for reason in report["reasons"]] @pytest.mark.parametrize("mutate, message", [ (lambda r: r.update(schema="other"), "unknown schema"), (lambda r: r.update(schema_version=2), "schema_version"), (lambda r: r.update(records=[]), "records"), (lambda r: r["generation"].update(max_new_tokens=0), "max_new_tokens"), (lambda r: r["records"][0].update(text="tampered"), "does not match records"), (lambda r: r["records"][0].update(category="other"), "category"), (lambda r: r["records"][0].update(input_ids=[]), "input_ids"), (lambda r: r["records"][0].update(output_ids=["x"]), "output_ids"), (lambda r: r["records"][0].update(output_ids=[1] * 99), "longer than"), (lambda r: r["records"][0].update(id=3), "strings"), (lambda r: r["records"].__setitem__(0, "x"), "object"), ]) def test_malformed_reference_is_input_error(saved, tmp_path, mutate, message): reference = json.loads(saved.reference.read_text(encoding="utf-8")) mutate(reference) path = tmp_path / "bad.json" path.write_text(json.dumps(reference), encoding="utf-8") with pytest.raises(VerificationInputError, match=message): vc.load_reference(path) def test_reference_file_errors(tmp_path): with pytest.raises(VerificationInputError, match="not found"): vc.load_reference(tmp_path / "missing.json") (tmp_path / "bad.json").write_text("{", encoding="utf-8") with pytest.raises(VerificationInputError, match="not JSON"): vc.load_reference(tmp_path / "bad.json") (tmp_path / "list.json").write_text("[]", encoding="utf-8") with pytest.raises(VerificationInputError, match="object"): vc.load_reference(tmp_path / "list.json") def test_input_validation(saved, tmp_path, monkeypatch): with pytest.raises(VerificationInputError, match="not a checkpoint"): vr.verify_checkpoint(tmp_path / "absent") (tmp_path / "nosafe").mkdir() (tmp_path / "nosafe" / "config.json").write_text("{}", encoding="utf-8") with pytest.raises(VerificationInputError, match="no safetensors"): vr.verify_checkpoint(tmp_path / "nosafe") with pytest.raises(VerificationInputError, match="quantization"): vr.verify_checkpoint(saved.edited, quantization="2bit") with pytest.raises(VerificationInputError, match="dtype"): vr.verify_checkpoint(saved.edited, dtype="int8") with pytest.raises(VerificationInputError, match="stock model"): vr.verify_checkpoint(saved.edited, stock_model=tmp_path / "absent") monkeypatch.setattr(torch.cuda, "is_available", lambda: False) with pytest.raises(VerificationInputError, match="requires a CUDA"): vr.verify_checkpoint(saved.edited, quantization="4bit") with pytest.raises(VerificationInputError, match="not available"): vr.verify_checkpoint(saved.edited, device="cuda") def test_quantized_mode_requires_bitsandbytes(monkeypatch): import importlib.util monkeypatch.setattr(torch.cuda, "is_available", lambda: True) monkeypatch.setattr(importlib.util, "find_spec", lambda name: None) with pytest.raises(VerificationInputError, match="bitsandbytes"): vr._resolve_device("auto", "8bit") def _record(rid, category, output_ids, text, input_ids=(1, 2)): return {"id": rid, "category": category, "text": rid, "input_ids": list(input_ids), "output_ids": list(output_ids), "output_text": text} def test_quantized_tolerance_accepts_bounded_drift_and_rejects_refusal_regrowth(): answer = "Sure, here is a detailed walkthrough of each step you asked about." refusal = "I'm sorry, but I can't help with that request." reference = [_record(f"harmful-{i}", "harmful", [1, 2, 3, 4], answer) for i in range(4)] drift = [_record(f"harmful-{i}", "harmful", [1, 2, 3, 9 if i else 4], answer) for i in range(4)] metrics, rows, reasons = vr.compare(reference, drift, vr.DEFAULT_TOLERANCES["4bit"]) assert reasons == [] and metrics["exact_match_rate"] == 0.25 assert rows[1]["first_divergence"] == 3 _, _, strict = vr.compare(reference, drift, vr.DEFAULT_TOLERANCES["none"]) assert {r["code"] for r in strict} == {"exact_match_below_tolerance", "prefix_agreement_below_tolerance"} refusing = [_record(f"harmful-{i}", "harmful", [1, 2, 3, 4], refusal if i < 2 else answer) for i in range(4)] metrics, _, reasons = vr.compare(reference, refusing, vr.DEFAULT_TOLERANCES["4bit"]) assert metrics["harmful_refusal_rate"] == {"reference": 0.0, "checkpoint": 0.5} assert [r["code"] for r in reasons] == ["refusal_rate_increase"] def test_invalid_checkpoint_outputs_and_tokenizer_mismatch_fail(): answer = "Sure, here is a detailed walkthrough of each step you asked about." reference = [_record("harmful-0", "harmful", [1, 2], answer)] observed = [_record("harmful-0", "harmful", [1, 2], "", input_ids=(5,))] _, _, reasons = vr.compare(reference, observed, vr.Tolerance(0.0, 0.0, 1.0)) assert [r["code"] for r in reasons] == ["tokenizer_mismatch", "checkpoint_outputs_invalid"] def test_record_reference_bounds_and_chat_template(saved): model, tokenizer = _tiny() with pytest.raises(VerificationInputError, match="prompts"): vc.record_reference(model, tokenizer, prompts=[]) with pytest.raises(VerificationInputError, match="max_new_tokens"): vc.record_reference(model, tokenizer, max_new_tokens=0) tokenizer.chat_template = "{% for m in messages %}{{ m['content'] }} {% endfor %}" ids, chat = vc.render_input_ids(tokenizer, "how to bake a cake") assert chat and ids tokenizer.chat_template = "{{ raise_exception('broken') }}" assert vc.render_input_ids(tokenizer, "how to")[1] is False custom = vc.record_reference(model, tokenizer, max_new_tokens=2, prompts=[ {"id": "harmless-00", "category": "harmless", "text": "how to"}]) assert custom["prompt_set"]["id"] == "custom" and model.training is False def test_render_input_ids_rejects_empty_tokenization(): tokenizer = MagicMock(chat_template=None, return_value={"input_ids": []}) with pytest.raises(VerificationInputError, match="no input ids"): vc.render_input_ids(tokenizer, "x") def test_record_pipeline_reference_logs_failure_without_raising(): logs = [] pipeline = SimpleNamespace(handle=SimpleNamespace(model=None, tokenizer=None), model_name="m", log=logs.append) assert vc.record_pipeline_reference(pipeline) is None assert "verify-checkpoint will fail" in logs[0] def test_rebirth_records_reference_sidecar(tmp_path, monkeypatch): from functools import partial from obliteratus.abliterate import AbliterationPipeline from obliteratus.models.loader import ModelHandle monkeypatch.setattr(vc, "record_reference", partial(vc.record_reference, max_new_tokens=TOKENS)) model, tokenizer = _tiny() _edit(model) logs = [] pipeline = AbliterationPipeline(model_name="tiny-gpt2", output_dir=str(tmp_path / "out"), method="basic", record_reference=True, on_log=logs.append) pipeline.handle = ModelHandle(model, tokenizer, model.config, "tiny-gpt2", "causal_lm") pipeline._build_metadata = MagicMock(return_value={"source_model": "tiny-gpt2"}) out = pipeline._rebirth() sidecar = out / vc.REFERENCE_FILENAME assert sidecar.is_file() assert any("Recorded verification reference" in line for line in logs) reference = vc.load_reference(sidecar) assert reference["model"]["source_model"] == "tiny-gpt2" assert reference["checkpoint_digests"] == vc.checkpoint_digests(out) assert vr.verify_checkpoint(out, device="cpu")["verdict"] == "pass" def test_rebirth_without_flag_writes_no_reference(tmp_path): from obliteratus.abliterate import AbliterationPipeline from obliteratus.models.loader import ModelHandle model, tokenizer = _tiny() pipeline = AbliterationPipeline(model_name="tiny-gpt2", output_dir=str(tmp_path / "out"), method="basic", on_log=lambda _m: None) pipeline.handle = ModelHandle(model, tokenizer, model.config, "tiny-gpt2", "causal_lm") pipeline._build_metadata = MagicMock(return_value={"source_model": "tiny-gpt2"}) assert not (pipeline._rebirth() / vc.REFERENCE_FILENAME).exists() def _main(argv): from obliteratus.cli import main with pytest.raises(SystemExit) as exit_info: main(argv) return exit_info.value.code def test_cli_exit_codes(saved, tmp_path, capsys): report = tmp_path / "r.json" assert _main(["verify-checkpoint", str(saved.edited), "--device", "cpu", "--report", str(report)]) == 0 assert json.loads(report.read_text(encoding="utf-8"))["verdict"] == "pass" assert "PASS" in capsys.readouterr().out assert _main(["verify-checkpoint", str(saved.broken), "--reference", str(saved.reference), "--device", "cpu", "--min-prefix-agreement", "0"]) == 3 assert "exact_match_below_tolerance" in capsys.readouterr().out assert (saved.root / "broken.verify-report.json").is_file() assert _main(["verify-checkpoint", str(tmp_path / "absent"), "--json"]) == 2 assert json.loads(capsys.readouterr().out)["verdict"] == "error" assert _main(["verify-checkpoint", str(tmp_path / "absent")]) == 2 assert "input error" in capsys.readouterr().out assert _main(["verify-checkpoint", str(saved.edited), "--min-exact-match", "2"]) == 2 assert _main(["verify-checkpoint", str(saved.edited), "--min-exact-match", "x"]) == 2 def test_cli_rejects_record_reference_for_remote_runs(): assert _main(["obliterate", "model", "--remote", "gpu", "--record-reference"]) == 2 def test_cli_passes_record_reference_to_pipeline(monkeypatch): import obliteratus.abliterate as abliterate captured = {} class Stop(Exception): pass def fake_pipeline(**kwargs): captured.update(kwargs) raise Stop monkeypatch.setattr(abliterate, "AbliterationPipeline", fake_pipeline) from obliteratus.cli import main with pytest.raises(Stop): main(["obliterate", "model", "--record-reference"]) assert captured["record_reference"] is True def test_fresh_process_cli_round_trip(saved, tmp_path): env = {**os.environ, "HF_HUB_OFFLINE": "1", "TRANSFORMERS_OFFLINE": "1"} result = subprocess.run( [sys.executable, "-m", "obliteratus", "verify-checkpoint", str(saved.edited), "--device", "cpu", "--json", "--report", str(tmp_path / "report.json")], capture_output=True, text=True, env=env, timeout=300, check=False, ) assert result.returncode == 0, result.stderr[-2000:] report = json.loads(result.stdout) assert report["verdict"] == "pass" and report["metrics"]["n_prompts"] == 16