Files
OBLITERATUS/tests/test_verify_edit_random_control.py
Joseph Magly 8b39cbfc4d feat(verify-edit): calibrate KL with matched random-direction edits
Stock-vs-stock calibration measures numerical noise: on an RTX 4090 it
suggested KL limits about 100x stricter than the defaults (Llama-3.1-8B
0.0012/0.0087) while a refusal-removing edit measured 1.55/0.37.

verify-edit --calibrate-random --stock S --edited E reads the reference
edit's metadata (final strong layers, effective per-layer ranks,
projection target and EXCISE settings, layer weights, KL-restored
groups), applies K seeded random orthonormal subspaces of the same rank
to the same writers on freshly loaded stock weights (never saving a
checkpoint), and measures first-token and next-token KL on the tune
partition's harmless prompts. The evidence records each trial, the
distribution and suggested limits (multiplier times the chosen
statistic); suggestions are recorded, not adopted.

KL-optimized references are matched by their final static state: their
strong layers already exclude restored layers, and restored global
writers (including tied input embeddings) are left unprojected; the
control never runs KL optimization itself. Edit metadata now records
the projection settings and layer excise weights the control needs, so
older checkpoints must be regenerated to serve as references.

Fixes #560
2026-11-19 15:28:06 -05:00

362 lines
16 KiB
Python

"""CPU contracts for matched random-direction verification calibration."""
import argparse
import hashlib
import json
from pathlib import Path
import pytest
import torch
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.verify_edit import cli, measure
from obliteratus.verify_edit.heldout import prompts_sha256
from obliteratus.verify_edit.spec import resolve_spec
from tests.fixtures.tiny_arch_models import tiny_llama
from tests.fixtures.tiny_offline_model import _fixture_tokenizer
pytestmark = pytest.mark.cpu
def test_random_control_parser_recognizes_mode_and_settings():
parser = argparse.ArgumentParser()
cli.add_parser(parser.add_subparsers())
args = parser.parse_args(["verify-edit", "--calibrate-random", "--random-trials", "3",
"--random-quantile", "0.7", "--random-kl-multiplier", "1.5"])
assert args.calibrate_random
assert (args.random_trials, args.random_quantile, args.random_kl_multiplier) == (3, 0.7, 1.5)
@pytest.mark.parametrize("rank", [1, 2, 4])
def test_random_subspaces_are_orthonormal_and_seeded(rank):
from obliteratus.verify_edit.random_edit import random_subspaces
state = torch.random.get_rng_state()
first = random_subspaces({2: rank, 3: rank}, 64, seed=40)
repeat = random_subspaces({3: rank, 2: rank}, 64, seed=40)
next_trial = random_subspaces({2: rank, 3: rank}, 64, seed=41)
for layer, basis in first.items():
assert basis.shape == (rank, 64)
torch.testing.assert_close(basis @ basis.T, torch.eye(rank))
assert torch.equal(basis, repeat[layer])
assert not torch.allclose(basis, next_trial[layer])
assert not torch.allclose(first[2], first[3])
assert torch.equal(torch.random.get_rng_state(), state)
@pytest.fixture
def checkpoints(tmp_path):
stock, edited = tmp_path / "stock", tmp_path / "edited"
model = tiny_llama(layers=3)
model.save_pretrained(stock)
_fixture_tokenizer().save_pretrained(stock)
edited.mkdir()
# Serialize with the same producer used by a reference checkpoint export.
pipeline = AbliterationPipeline(
str(stock), method="advanced", layers=[0, 2], norm_preserve=True,
regularization=0.2, projection_target="ffn", project_biases=False,
project_embeddings=True, embed_regularization=0.65, fold_post_norms=False,
float_layer_interpolation=True, layer_adaptive_strength=True,
)
pipeline._strong_layers = [2, 0]
pipeline.concept_subspaces = {0: torch.eye(16)[:1], 2: torch.eye(16)[:2]}
pipeline._layer_excise_weights = {0: 0.6, 2: 1.0}
pipeline._float_layer_weights = {0: 1.0, 2: 0.7}
pipeline._layer_selection_diagnostics = {"method": "pinned"}
pipeline._effective_refinement_passes = 1
metadata = edited / "abliteration_metadata.json"
metadata.write_text(json.dumps(pipeline._build_metadata()), encoding="utf-8")
return stock, edited, metadata
def _calibration_inputs():
heldout = {"harmless": ["hello world", "safe test", "ignored"], "identity": {"partition": "tune"}}
spec = resolve_spec({"generation": {"seed": 7}, "endpoints": {"kl": {"prompts": 2}}})
return heldout, spec
def _project(weight, basis, regularization, *, residual_output):
if residual_output:
projected = weight - (1 - regularization) * (basis.T @ basis @ weight)
else:
projected = weight - (1 - regularization) * (weight @ basis.T @ basis)
return projected * min(weight.norm() / projected.norm(), 1.10)
def test_random_control_edits_only_matched_writers_and_reloads_stock(
checkpoints, tmp_path, monkeypatch,
):
from obliteratus.verify_edit.random_control import calibrate_random_control
from obliteratus.verify_edit.random_edit import load_reference_edit, random_subspaces
stock, edited, _ = checkpoints
heldout, spec = _calibration_inputs()
original_files = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}
pristine = measure.ModelRunner(str(stock), revision=None, device="cpu", dtype="float32")
initial = {name: value.clone() for name, value in pristine.model.state_dict().items()}
pristine.close()
loaded, closed, observed = [], [], []
class Runner(measure.ModelRunner):
def close(self):
if len(loaded) > 1:
observed.append({name: value.clone() for name, value in self.model.state_dict().items()})
super().close()
closed.append(self)
def factory():
runner = Runner(str(stock), revision=None, device="cpu", dtype="float32")
assert loaded == closed
for name, value in runner.model.state_dict().items():
torch.testing.assert_close(value, initial[name], rtol=0, atol=0)
loaded.append(runner)
return runner
def unwanted(*args, **kwargs):
pytest.fail("random calibration must only run in-memory EXCISE and harmless-prompt KL")
for stage in ("_probe", "_distill", "_rebirth"):
monkeypatch.setattr(AbliterationPipeline, stage, unwanted)
monkeypatch.setattr(Runner, "generate", unwanted)
reference = load_reference_edit(edited)
result = calibrate_random_control(factory, reference, heldout, spec, trials=2, batch_size=2)
assert len(loaded) == len(closed) == 3
assert all(trial["first_token_kl_mean"] > 0 for trial in result["kl"]["trials"])
for state, seed in zip(observed, (7, 8), strict=True):
bases = random_subspaces({2: 2, 0: 1}, 16, seed=seed)
targets = {
"model.embed_tokens.weight": (bases[0], 0.65, False),
"lm_head.weight": (bases[2], 0.0, False),
}
for layer, regularization in ((0, 0.248), (2, 0.272)):
for writer in ("down_proj", "up_proj", "gate_proj"):
targets[f"model.layers.{layer}.mlp.{writer}.weight"] = (
bases[layer], regularization, writer == "down_proj",
)
for name, value in state.items():
expected = initial[name]
if name in targets:
basis, regularization, residual_output = targets[name]
expected = _project(expected, basis, regularization, residual_output=residual_output)
torch.testing.assert_close(value, expected, msg=lambda error: f"{name}\n{error}")
assert not torch.equal(observed[0]["lm_head.weight"], observed[1]["lm_head.weight"])
assert {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()} == original_files
@pytest.mark.parametrize("restore_head, tied", [(False, False), (True, False), (True, True)])
def test_random_control_matches_final_kl_rollback_state(checkpoints, monkeypatch, restore_head, tied):
from obliteratus.verify_edit.random_control import calibrate_random_control
from obliteratus.verify_edit.random_edit import load_reference_edit, random_subspaces
stock, edited, metadata = checkpoints
record = json.loads(metadata.read_text())
record["method_config"].update(
use_kl_optimization=True, rdo_refinement=True, winsorize_activations=True,
direction_method="svd", use_whitened_svd=True,
)
restored = ["1", "lm_head.weight"] if restore_head else ["1"]
record["kl_initial_rollback_groups"] = ["1", "lm_head.weight"]
record["kl_restored_groups"] = restored
record["effective_subspace_ranks"]["1"] = 1
metadata.write_text(json.dumps(record))
if tied:
model = tiny_llama(layers=3)
model.config.tie_word_embeddings = True
model.tie_weights()
model.save_pretrained(stock)
initial, observed = {}, {}
class Runner(measure.ModelRunner):
def close(self):
state = {name: value.clone() for name, value in self.model.state_dict().items()}
(observed if initial else initial).update(state)
super().close()
def unwanted(*args, **kwargs):
pytest.fail("random EXCISE must not optimize KL or capture rollback snapshots")
for stage in ("_kl_optimize_corrections", "_snapshot_layer_state"):
monkeypatch.setattr(AbliterationPipeline, stage, unwanted)
def factory():
runner = Runner(str(stock), revision=None, device="cpu", dtype="float32")
assert (runner.model.lm_head.weight is runner.model.model.embed_tokens.weight) is tied
return runner
reference = load_reference_edit(edited)
heldout, spec = _calibration_inputs()
result = calibrate_random_control(factory, reference, heldout, spec, trials=1, batch_size=2)
assert result["reference_edit"]["kl_restored_groups"] == restored
assert result["reference_edit"]["strong_layers"] == [2, 0]
bases = random_subspaces({2: 2, 0: 1}, 16, seed=7)
targets = {} if tied else {"model.embed_tokens.weight": (bases[0], 0.65, False)}
if not restore_head:
targets["lm_head.weight"] = (bases[2], 0.0, False)
for layer, regularization in ((0, 0.248), (2, 0.272)):
targets[f"model.layers.{layer}.mlp.down_proj.weight"] = (bases[layer], regularization, True)
for name, (basis, regularization, residual_output) in targets.items():
assert not torch.equal(observed[name], initial[name])
torch.testing.assert_close(
observed[name], _project(initial[name], basis, regularization, residual_output=residual_output),
)
for name, value in initial.items():
if (name.startswith("model.layers.1.") or (restore_head and name == "lm_head.weight")
or (tied and name == "model.embed_tokens.weight")):
assert observed[name].numpy().tobytes() == value.numpy().tobytes()
@pytest.mark.parametrize("problem, message", [
("missing_ranks", "effective_subspace_ranks"),
("missing_embedding", "project_embeddings"),
("missing_weights", "layer_excise_weights"),
("bad_rank", "rank"),
("bad_selection", "selected_layers"),
("inconsistent_kl", "kl_restored_groups.*layer 2.*strong_layers"),
("missing_kl_groups", "missing kl_restored_groups"),
])
def test_reference_metadata_rejects_unmatchable_edits(checkpoints, problem, message):
from obliteratus.verify_edit.random_edit import load_reference_edit
_, edited, metadata = checkpoints
record = json.loads(metadata.read_text())
config = record["method_config"]
if problem == "missing_ranks":
record.pop("effective_subspace_ranks")
elif problem == "missing_embedding":
config.pop("project_embeddings")
elif problem == "missing_weights":
record.pop("layer_excise_weights")
elif problem == "bad_rank":
record["effective_subspace_ranks"]["2"] = True
elif problem == "bad_selection":
record["layer_selection"]["selected_layers"] = [2]
elif problem == "inconsistent_kl":
config["use_kl_optimization"] = True
record["kl_restored_groups"] = ["2"]
elif problem == "missing_kl_groups":
config["use_kl_optimization"] = True
record.pop("kl_restored_groups")
metadata.write_text(json.dumps(record))
with pytest.raises(ValueError, match=message):
load_reference_edit(edited)
def test_random_kl_statistics_and_evidence(checkpoints, tmp_path, monkeypatch):
from obliteratus.verify_edit import random_control
from obliteratus.verify_edit.random_edit import load_reference_edit
_, edited, metadata = checkpoints
heldout, spec = _calibration_inputs()
values = iter([(1.0, 4.0), (2.0, 2.0), (5.0, 1.0)])
class Runner:
def prompt_logits(self, prompt, limit):
assert prompt != "ignored"
return [1, 2], torch.zeros(2, 2)
def close(self):
pass
def compare(runner, reference, prompts, limit, batch_size):
assert len(reference) == len(prompts) == 2
assert batch_size == 1
first, next_ = next(values)
return {"first_token_kl_mean": first, "next_token_kl_mean": next_, "batch_size": batch_size}
seeds = []
monkeypatch.setattr(random_control, "_compare_kl", compare)
monkeypatch.setattr(random_control, "apply_random_edit", lambda model, edit, seed: seeds.append(seed))
output = tmp_path / "evidence.json"
result = random_control.calibrate_random_control(
Runner, load_reference_edit(edited), heldout, spec, trials=3, batch_size=1,
quantile=0.75, kl_multiplier=2.0, output=output,
)
assert json.loads(output.read_text()) == result
assert result["schema"] == "obliteratus.verify-edit.random-control/v1"
assert result["reference_edit"]["metadata_sha256"] == hashlib.sha256(metadata.read_bytes()).hexdigest()
assert result["reference_edit"]["effective_subspace_ranks"] == {"2": 2, "0": 1}
assert result["prompts_sha256"]["kl"] == prompts_sha256(heldout["harmless"][:2])
assert result["dataset"]["partition"] == "tune"
assert "obliteratus_git_commit" in result["provenance"]
assert [trial["seed"] for trial in result["kl"]["trials"]] == seeds == [7, 8, 9]
assert result["kl"]["statistics"] == {
"first_token": {"mean": 8 / 3, "max": 5.0, "quantile": 3.5},
"next_token": {"mean": 7 / 3, "max": 4.0, "quantile": 3.0},
}
assert result["suggested_thresholds"]["kl"] == {
"max_first_token_kl": 7.0, "max_next_token_kl": 6.0,
}
@pytest.mark.parametrize("args", [
["--calibrate-random", "--edited", "edited", "--output", "out"],
["--calibrate-random", "--stock", "stock", "--output", "out"],
["--calibrate-random", "--stock", "stock", "--edited", "edited"],
["--calibrate-random", "--calibrate"],
["--calibrate-random", "--write-extraction-pairs", "out"],
["--calibrate", "--stock", "stock", "--output", "out", "--random-trials", "2"],
["--calibrate-random", "--stock", "stock", "--edited", "edited", "--output", "out",
"--calibration-repeats", "2"],
])
def test_random_cli_rejects_invalid_invocations(args, tmp_path, monkeypatch):
from obliteratus.cli import main
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(measure, "ModelRunner", lambda *args, **kw: pytest.fail("opened a model"))
with pytest.raises(SystemExit) as result:
main(["verify-edit", *args])
assert result.value.code == 2
assert not Path("out").exists()
@pytest.mark.parametrize("flag, value", [
("--random-trials", "0"), ("--random-quantile", "nan"), ("--random-kl-multiplier", "inf"),
])
def test_random_cli_rejects_invalid_values(checkpoints, tmp_path, monkeypatch, capsys, flag, value):
from obliteratus.cli import main
stock, edited, _ = checkpoints
output = tmp_path / "out.json"
monkeypatch.setattr(measure, "ModelRunner", lambda *args, **kw: pytest.fail("opened a model"))
with pytest.raises(SystemExit) as result:
main(["verify-edit", "--calibrate-random", "--stock", str(stock), "--edited", str(edited),
"--output", str(output), flag, value])
assert result.value.code == 2
assert flag[2:] in capsys.readouterr().out
assert not output.exists()
def test_random_cli_uses_stock_runner_and_tuning_partition(checkpoints, tmp_path, monkeypatch):
from obliteratus.cli import main
from obliteratus.verify_edit import random_control
from obliteratus.verify_edit.heldout import _builtin_split
stock, edited, _ = checkpoints
output = tmp_path / "out.json"
spec = tmp_path / "spec.json"
spec.write_text(json.dumps({"heldout": {"pairs": 1}}))
calls = []
def calibrate(factory, reference, heldout, spec, **options):
factory()
assert reference.layers == [2, 0]
assert heldout["identity"]["partition"] == "tune"
assert heldout["harmless"] == [_builtin_split().tune[0][1]]
assert options == {"trials": 3, "quantile": 0.7, "kl_multiplier": 1.5, "batch_size": 2}
return {"schema": "obliteratus.verify-edit.random-control/v1"}
monkeypatch.setattr(measure, "ModelRunner", lambda path, **options: calls.append((path, options)))
monkeypatch.setattr(random_control, "calibrate_random_control", calibrate)
assert main(["verify-edit", "--calibrate-random", "--stock", str(stock), "--edited", str(edited),
"--spec", str(spec), "--output", str(output), "--device", "cpu",
"--dtype", "float32", "--random-trials", "3", "--random-quantile", "0.7",
"--random-kl-multiplier", "1.5", "--batch-size", "2"]) is None
assert calls == [(str(stock), {"revision": None, "device": "cpu", "dtype": "float32",
"trust_remote_code": False, "local_only": True})]
result = json.loads(output.read_text())
assert result["stock"]["inventory_sha256"]
assert result["spec_file"]["sha256"] == hashlib.sha256(spec.read_bytes()).hexdigest()