mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
The REBIRTH save stage only checked that files exist and parse, so a save that dropped or mangled the edit passed silently (#525). - `obliterate --record-reference` records greedy outputs of the in-memory edited model on the pinned obliteratus-verify-v1 prompt set (8 harmful + 8 harmless, 32 tokens) into a sidecar with digests of the files the reload reads. - `obliteratus verify-checkpoint` reloads the checkpoint from local safetensors only, regenerates greedily and compares token ids: exact match, prefix agreement and harmful refusal-rate change against per-reload tolerances (strict for full precision, provisional for bitsandbytes 8/4-bit, overridable and recorded). - Fails with named reasons for no or empty reference, files changed since the reference, tokenizer mismatch, divergence, invalid outputs and, with --stock-model, an edit that was not persisted. A structural-only check can never pass. - Versioned report bound to checkpoint digests, prompt-set digest and runtime; exit 0 pass, 2 input error, 3 verification failed. - CPU tests use a tiny local GPT-2 through the real REBIRTH; a CUDA and bitsandbytes test runs in the conditional gates.
106 lines
4.9 KiB
Python
106 lines
4.9 KiB
Python
"""``obliteratus verify-checkpoint``: behavioural reload verification (issue #525).
|
|
|
|
Exit codes: 0 verification passed; 2 unusable input or runtime (usage error);
|
|
3 verification failed (same code as a failed obliterate quality gate).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
|
|
EXIT_PASS = 0
|
|
EXIT_INPUT_ERROR = 2
|
|
EXIT_VERIFICATION_FAILED = 3
|
|
|
|
|
|
def _fraction(value: str) -> float:
|
|
try:
|
|
number = float(value)
|
|
except ValueError:
|
|
raise argparse.ArgumentTypeError(f"{value!r} is not a number") from None
|
|
if not 0.0 <= number <= 1.0:
|
|
raise argparse.ArgumentTypeError("must be between 0 and 1")
|
|
return number
|
|
|
|
|
|
def add_parser(subparsers) -> None:
|
|
from obliteratus.verify_checkpoint_report import DTYPES, QUANTIZATION_MODES
|
|
|
|
parser = subparsers.add_parser(
|
|
"verify-checkpoint",
|
|
help="Reload a saved edited checkpoint and verify it reproduces the edited behaviour",
|
|
description=(
|
|
"Reload a local safetensors checkpoint, greedily replay the reference prompts "
|
|
"recorded at save time (obliterate --record-reference) or a supplied reference "
|
|
"JSON, and compare outputs. Exit 0 pass, 2 unusable input, 3 verification failed."
|
|
),
|
|
)
|
|
parser.add_argument("checkpoint", type=Path, help="Saved checkpoint directory")
|
|
parser.add_argument("--reference", type=Path, default=None,
|
|
help="Reference JSON (default: CHECKPOINT/verification_reference.json)")
|
|
parser.add_argument("--stock-model", type=Path, default=None,
|
|
help="Optional local stock-model directory for comparison")
|
|
parser.add_argument("--device", default="auto", help="auto, cpu, cuda or cuda:N")
|
|
parser.add_argument("--dtype", choices=DTYPES, default="auto",
|
|
help="Reload dtype (default: the dtype recorded with the reference)")
|
|
parser.add_argument("--quantization", choices=QUANTIZATION_MODES, default="none",
|
|
help="Reload with bitsandbytes 8bit/4bit (CUDA only)")
|
|
parser.add_argument("--min-exact-match", type=_fraction, default=None,
|
|
help="Override the minimum exact-match rate")
|
|
parser.add_argument("--min-prefix-agreement", type=_fraction, default=None,
|
|
help="Override the minimum mean prefix agreement")
|
|
parser.add_argument("--max-refusal-increase", type=_fraction, default=None,
|
|
help="Override the allowed harmful refusal-rate increase")
|
|
parser.add_argument("--report", type=Path, default=None,
|
|
help="Report path (default: CHECKPOINT.verify-report.json beside it)")
|
|
parser.add_argument("--json", action="store_true", help="Print only the report JSON")
|
|
|
|
|
|
def run(args, console=None) -> int:
|
|
from dataclasses import replace
|
|
|
|
from obliteratus.verify_checkpoint import VerificationInputError
|
|
from obliteratus.verify_checkpoint_report import DEFAULT_TOLERANCES, verify_checkpoint
|
|
|
|
def say(message: str) -> None:
|
|
if console is not None and not args.json:
|
|
console.print(message)
|
|
|
|
tolerance = DEFAULT_TOLERANCES[args.quantization]
|
|
overrides = {name: value for name, value in (
|
|
("min_exact_match_rate", args.min_exact_match),
|
|
("min_mean_prefix_agreement", args.min_prefix_agreement),
|
|
("max_refusal_rate_increase", args.max_refusal_increase),
|
|
) if value is not None}
|
|
tolerance = replace(tolerance, **overrides)
|
|
try:
|
|
report = verify_checkpoint(
|
|
args.checkpoint, reference_path=args.reference, stock_model=args.stock_model,
|
|
device=args.device, dtype=args.dtype, quantization=args.quantization,
|
|
tolerance=tolerance,
|
|
)
|
|
except VerificationInputError as error:
|
|
if args.json:
|
|
print(json.dumps({"verdict": "error", "error": str(error)}, sort_keys=True))
|
|
else:
|
|
say(f"[bold red]verify-checkpoint input error[/]: {error}")
|
|
return EXIT_INPUT_ERROR
|
|
checkpoint = Path(args.checkpoint).expanduser().resolve()
|
|
report_path = args.report or checkpoint.with_name(checkpoint.name + ".verify-report.json")
|
|
text = json.dumps(report, indent=2, sort_keys=True) + "\n"
|
|
report_path.write_text(text, encoding="utf-8")
|
|
if args.json:
|
|
print(text, end="")
|
|
else:
|
|
metrics = report.get("metrics") or {}
|
|
if metrics:
|
|
say(f"exact-match rate {metrics['exact_match_rate']:.3f}, mean prefix agreement "
|
|
f"{metrics['mean_prefix_agreement']:.3f} over {metrics['n_prompts']} prompts")
|
|
for reason in report["reasons"]:
|
|
say(f"[yellow]{reason['code']}[/]: {reason['detail']}")
|
|
verdict = "[bold green]PASS[/]" if report["verdict"] == "pass" else "[bold red]FAIL[/]"
|
|
say(f"verify-checkpoint {verdict}; report: {report_path}")
|
|
return EXIT_PASS if report["verdict"] == "pass" else EXIT_VERIFICATION_FAILED
|