mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Nothing produced a single preregistered pass/fail report for an abliteration, and there was no glue to matric-eval (#526). - `obliteratus verify-edit --stock M --edited CKPT --spec SPEC` reports pass/fail/untested per endpoint: held-out refusal with Wilson intervals, collateral over-refusal and coherence, first- and next-token KL on harmless prompts, advisory perplexity, and capability. - Held-out data defaults to the pinned 200-pair final_test partition of the qwen38-v1 split; `--write-extraction-pairs` exports its disjoint train partition for `obliterate --prompt-pairs-file`, and `--extraction-prompts` proves zero overlap (overlap exits 2; unproven disjointness leaves held-out refusal untested). - Margins come from a strict, digest-bound JSON spec (obliteratus.verify-edit.spec/v1) with proposed defaults. - Capability runs matric-eval over vLLM against existing endpoints or servers launched from a command template, one model at a time; since matric-eval exits 0 on benchmark errors, the run summary is parsed and any missing or failed benchmark is untested, never a pass. lm-eval reuses capability-check. - Exit 0 pass, 1 required failure, 3 incomplete (required untested), 2 usage or integrity error; reports carry model, checkpoint, dataset and spec digests and runtime, but no prompt or completion text.
239 lines
11 KiB
Python
239 lines
11 KiB
Python
"""verify-edit end to end on deterministic tiny CPU models with a fake matric-eval."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import textwrap
|
|
|
|
import pytest
|
|
|
|
pytestmark = [pytest.mark.cpu, pytest.mark.usefixtures("tiny_model_runtime")]
|
|
|
|
WORDS = ["<unk>", "<eos>", "<user>", "<assistant>", "I", "cannot", "help", "with", "that",
|
|
"Sure", "here", "is", "the", "answer"]
|
|
REFUSE = ["I", "cannot", "help", "with", "that", "<eos>"]
|
|
COMPLY = ["Sure", "here", "is", "the", "answer", "<eos>"]
|
|
|
|
|
|
def successor_model(directory, *, first, runner_up, gap=0.01):
|
|
"""A one-layer Mistral whose greedy output is a fixed word chain.
|
|
|
|
Attention and MLP outputs are zero, so the residual is the one-hot token
|
|
embedding and ``lm_head`` maps each token to its successor. After the
|
|
``<assistant>`` generation prompt the model emits ``first``; ``runner_up``
|
|
trails it by ``gap`` logits per unit of the normalized embedding.
|
|
"""
|
|
import torch
|
|
from tokenizers import Tokenizer
|
|
from tokenizers.models import WordLevel
|
|
from tokenizers.pre_tokenizers import WhitespaceSplit
|
|
from transformers import MistralConfig, MistralForCausalLM, PreTrainedTokenizerFast
|
|
|
|
vocabulary = {word: index for index, word in enumerate(WORDS)}
|
|
backend = Tokenizer(WordLevel(vocabulary, unk_token="<unk>"))
|
|
backend.pre_tokenizer = WhitespaceSplit()
|
|
tokenizer = PreTrainedTokenizerFast(tokenizer_object=backend, unk_token="<unk>",
|
|
eos_token="<eos>", pad_token="<eos>")
|
|
tokenizer.chat_template = (
|
|
"{% for m in messages %}{{ '<' + m['role'] + '> ' + m['content'] + ' ' }}{% endfor %}"
|
|
"{% if add_generation_prompt %}{{ '<assistant>' }}{% endif %}")
|
|
model = MistralForCausalLM(MistralConfig(
|
|
vocab_size=len(WORDS), hidden_size=16, intermediate_size=32, num_hidden_layers=1,
|
|
num_attention_heads=2, num_key_value_heads=2, max_position_embeddings=1024,
|
|
eos_token_id=1, pad_token_id=1, tie_word_embeddings=False))
|
|
with torch.no_grad():
|
|
for parameter in model.parameters():
|
|
parameter.zero_()
|
|
for module in model.modules():
|
|
if type(module).__name__.endswith("RMSNorm"):
|
|
module.weight.fill_(1.0)
|
|
for index in range(len(WORDS)):
|
|
model.model.embed_tokens.weight[index, index] = 1.0
|
|
head = model.lm_head.weight
|
|
for chain in (REFUSE, COMPLY):
|
|
for before, after in zip(chain, chain[1:]):
|
|
head[vocabulary[after], vocabulary[before]] = 10.0
|
|
head[vocabulary[first[0]], vocabulary["<assistant>"]] = 10.0
|
|
head[vocabulary[runner_up[0]], vocabulary["<assistant>"]] = 10.0 - gap
|
|
model.save_pretrained(directory, safe_serialization=True)
|
|
tokenizer.save_pretrained(directory)
|
|
return directory
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def models(tmp_path_factory):
|
|
root = tmp_path_factory.mktemp("verify-edit-models")
|
|
return {
|
|
"stock": str(successor_model(root / "stock", first=REFUSE, runner_up=COMPLY)),
|
|
"edited": str(successor_model(root / "edited", first=COMPLY, runner_up=REFUSE)),
|
|
"far": str(successor_model(root / "far", first=COMPLY, runner_up=REFUSE, gap=5.0)),
|
|
}
|
|
|
|
|
|
FAKE_MATRIC = textwrap.dedent("""\
|
|
import json, os, sys, pathlib
|
|
args = sys.argv[1:]
|
|
def value(flag):
|
|
return args[args.index(flag) + 1]
|
|
model = value("--model")
|
|
benchmarks = [args[i + 1] for i, a in enumerate(args) if a == "--benchmark"]
|
|
log = os.environ.get("FAKE_MATRIC_LOG")
|
|
if log:
|
|
with open(log, "a") as stream:
|
|
stream.write(json.dumps(args) + "\\n")
|
|
if os.environ.get("FAKE_MATRIC_EXIT"):
|
|
sys.exit(int(os.environ["FAKE_MATRIC_EXIT"]))
|
|
score = json.loads(os.environ["FAKE_MATRIC_SCORES"])[model]
|
|
status = os.environ.get("FAKE_MATRIC_STATUS", "success")
|
|
run = pathlib.Path(value("--output")) / "run-2026-01-01T00-00-00"
|
|
run.mkdir(parents=True)
|
|
result = {"model": "openai/" + model, "status": "success", "provider": "vllm",
|
|
"provenance": {"framework": {"matric_eval": "0.2.0"}},
|
|
"overall_score": score,
|
|
"benchmarks": {b: {"status": status, "score": score, "samples": 5}
|
|
for b in benchmarks}}
|
|
(run / "summary.json").write_text(json.dumps(
|
|
{"tier": value("--tier"), "successful": 1, "failed": 0, "results": [result]}))
|
|
""")
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_matric(tmp_path, monkeypatch):
|
|
script = tmp_path / "bin" / "matric-eval"
|
|
script.parent.mkdir()
|
|
script.write_text(f"#!{sys.executable}\n{FAKE_MATRIC}")
|
|
script.chmod(0o755)
|
|
monkeypatch.setenv("FAKE_MATRIC_SCORES", json.dumps(
|
|
{"obliteratus-stock": 0.70, "obliteratus-edited": 0.69}))
|
|
monkeypatch.setenv("FAKE_MATRIC_LOG", str(tmp_path / "matric.log"))
|
|
return script
|
|
|
|
|
|
def small_spec(tmp_path, **endpoint_overrides):
|
|
endpoints = {"kl": {"prompts": 4}}
|
|
for name, values in endpoint_overrides.items():
|
|
endpoints.setdefault(name, {}).update(values)
|
|
spec = {"schema": "obliteratus.verify-edit.spec/v1",
|
|
"heldout": {"pairs": 24},
|
|
"generation": {"max_new_tokens": 8, "max_input_tokens": 64},
|
|
"endpoints": endpoints}
|
|
path = tmp_path / "spec.json"
|
|
path.write_text(json.dumps(spec))
|
|
return path
|
|
|
|
|
|
def run_cli(arguments):
|
|
from obliteratus.cli import main
|
|
|
|
try:
|
|
main(["verify-edit", *arguments])
|
|
except SystemExit as exc:
|
|
return exc.code
|
|
return 0
|
|
|
|
|
|
def base_arguments(tmp_path, models, edited, spec, fake_matric=None):
|
|
extraction = tmp_path / "extraction.json"
|
|
assert run_cli(["--write-extraction-pairs", str(extraction)]) == 0
|
|
arguments = ["--stock", models["stock"], "--edited", models[edited], "--spec", str(spec),
|
|
"--extraction-prompts", str(extraction), "--output", str(tmp_path / "report.json"),
|
|
"--device", "cpu", "--dtype", "float32", "--batch-size", "8",
|
|
"--stock-endpoint", "http://127.0.0.1:8001",
|
|
"--edited-endpoint", "http://127.0.0.1:8002"]
|
|
if fake_matric is not None:
|
|
arguments += ["--matric-eval-bin", str(fake_matric)]
|
|
return arguments
|
|
|
|
|
|
def read_report(tmp_path):
|
|
return json.loads((tmp_path / "report.json").read_text())
|
|
|
|
|
|
def test_pass_report_records_provenance_and_matric_invocation(tmp_path, models, fake_matric):
|
|
code = run_cli(base_arguments(tmp_path, models, "edited", small_spec(tmp_path), fake_matric))
|
|
report = read_report(tmp_path)
|
|
assert code == 0, report["endpoints"]
|
|
assert report["schema"] == "obliteratus.verify-edit.report/v1"
|
|
assert report["verdict"] == "PASS"
|
|
assert {name: entry["verdict"] for name, entry in report["endpoints"].items()} == {
|
|
"heldout_refusal": "pass", "collateral": "pass", "kl": "pass",
|
|
"perplexity": "pass", "capability": "pass"}
|
|
refusal = report["endpoints"]["heldout_refusal"]["metrics"]
|
|
assert refusal["stock"]["rate"] == 1.0 and refusal["edited"]["rate"] == 0.0
|
|
assert refusal["edited"]["wilson"][1] < 0.2 and refusal["paired"]["stock_only_refused"] == 24
|
|
assert report["endpoints"]["kl"]["metrics"]["first_token_kl_mean"] > 0
|
|
assert report["datasets"]["heldout"]["split_manifest_sha256"].startswith("ba65938b")
|
|
assert report["datasets"]["heldout"]["pairs"] == 24
|
|
assert report["datasets"]["extraction_disjointness"]["status"] == "verified"
|
|
assert len(report["models"]["edited"]["inventory_sha256"]) == 64
|
|
assert "model.safetensors" in report["models"]["stock"]["files"]
|
|
assert len(report["spec"]["sha256"]) == 64
|
|
assert report["runtime"]["wall_seconds"] >= 0 and report["runtime"]["software"]["torch"]
|
|
capability = report["endpoints"]["capability"]["metrics"]
|
|
assert capability["overall_drop"] == pytest.approx(0.01)
|
|
assert capability["scores"]["stock"]["provider_url"] == "http://127.0.0.1:8001"
|
|
calls = [json.loads(line) for line in (tmp_path / "matric.log").read_text().splitlines()]
|
|
assert calls[0][:4] == ["run", "--provider", "vllm", "--provider-url"]
|
|
assert ["--tier", "quick"] == calls[0][calls[0].index("--tier"):calls[0].index("--tier") + 2]
|
|
assert sorted(calls[1][i + 1] for i, a in enumerate(calls[1]) if a == "--benchmark") == [
|
|
"arc", "gsm8k", "ifeval", "mmlu"]
|
|
text = (tmp_path / "report.json").read_text()
|
|
assert "Sure here" not in text and "I cannot help" not in text
|
|
|
|
|
|
def test_unchanged_model_fails_refusal_and_exits_one(tmp_path, models, fake_matric):
|
|
arguments = base_arguments(tmp_path, models, "stock", small_spec(tmp_path), fake_matric)
|
|
assert run_cli(arguments) == 1
|
|
report = read_report(tmp_path)
|
|
assert report["verdict"] == "FAIL"
|
|
assert report["endpoints"]["heldout_refusal"]["verdict"] == "fail"
|
|
assert report["endpoints"]["kl"]["metrics"]["first_token_kl_mean"] == pytest.approx(0, abs=1e-6)
|
|
|
|
|
|
def test_large_kl_fails_required_endpoint(tmp_path, models, fake_matric):
|
|
arguments = base_arguments(tmp_path, models, "far", small_spec(tmp_path), fake_matric)
|
|
assert run_cli(arguments) == 1
|
|
kl = read_report(tmp_path)["endpoints"]["kl"]
|
|
assert kl["verdict"] == "fail" and "first-token KL" in kl["reason"]
|
|
|
|
|
|
def test_unavailable_backend_is_untested_and_incomplete(tmp_path, models):
|
|
arguments = base_arguments(tmp_path, models, "edited", small_spec(tmp_path))
|
|
arguments += ["--matric-eval-bin", str(tmp_path / "missing-matric-eval")]
|
|
assert run_cli(arguments) == 3
|
|
report = read_report(tmp_path)
|
|
assert report["verdict"] == "INCOMPLETE"
|
|
capability = report["endpoints"]["capability"]
|
|
assert capability["verdict"] == "untested" and "not found" in capability["reason"]
|
|
|
|
|
|
def test_optional_capability_and_unverified_disjointness(tmp_path, models):
|
|
spec = small_spec(tmp_path, capability={"required": False, "backend": "none"})
|
|
arguments = base_arguments(tmp_path, models, "edited", spec)
|
|
index = arguments.index("--extraction-prompts")
|
|
del arguments[index:index + 2]
|
|
assert run_cli(arguments) == 3
|
|
refusal = read_report(tmp_path)["endpoints"]["heldout_refusal"]
|
|
assert refusal["verdict"] == "untested" and "disjointness" in refusal["reason"]
|
|
|
|
|
|
def test_usage_errors_exit_two(tmp_path, models):
|
|
assert run_cli(["--stock", models["stock"]]) == 2
|
|
spec = small_spec(tmp_path)
|
|
assert run_cli(["--stock", models["stock"], "--edited", models["edited"], "--spec", str(spec),
|
|
"--spec-sha256", "0" * 64, "--output", str(tmp_path / "r.json")]) == 2
|
|
overlap = tmp_path / "overlap.json"
|
|
from obliteratus.verify_edit.heldout import load_heldout
|
|
from obliteratus.verify_edit.spec import DEFAULT_SPEC
|
|
heldout = load_heldout(DEFAULT_SPEC["heldout"], None)
|
|
overlap.write_text(json.dumps({"harmful": heldout["harmful"][:5],
|
|
"harmless": heldout["harmless"][:5]}))
|
|
assert run_cli(["--stock", models["stock"], "--edited", models["edited"], "--spec", str(spec),
|
|
"--extraction-prompts", str(overlap), "--output", str(tmp_path / "r.json"),
|
|
"--device", "cpu", "--batch-size", "0"]) == 2
|
|
assert run_cli(["--stock", models["stock"], "--edited", models["edited"], "--spec", str(spec),
|
|
"--extraction-prompts", str(overlap), "--output", str(tmp_path / "r.json"),
|
|
"--device", "cpu"]) == 2
|
|
assert not (tmp_path / "r.json").exists()
|