mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
686 lines
34 KiB
Python
686 lines
34 KiB
Python
"""Repair candidates on the tiny local GPT-2, including bit-exact adapter removal."""
|
|
|
|
from copy import deepcopy
|
|
from dataclasses import asdict, replace
|
|
import json
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
from obliteratus.concepts import ConceptDirection, RepairConfig, apply_concept_intervention
|
|
from obliteratus.concepts.evaluation import EvaluationBattery, evaluate_intervention
|
|
from obliteratus.concepts.evaluation_model import ModelEvaluator
|
|
from obliteratus.concepts.repair import REPAIR_STEPS, run_repair_ladder
|
|
from obliteratus.concepts.repair_model import WeightRepair
|
|
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
|
|
from tests.test_concept_extraction_pipeline import small_target
|
|
from tests.test_concept_intervention import _tiny_residual_model
|
|
from tests.test_concept_repair import report
|
|
|
|
pytestmark = [
|
|
pytest.mark.cpu,
|
|
pytest.mark.integration,
|
|
pytest.mark.usefixtures("tiny_model_runtime"),
|
|
]
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def fixture_completions_are_valid(monkeypatch):
|
|
# The tiny fixture vocabulary mostly decodes to empty completions, which the
|
|
# battery correctly excludes as invalid (#309). These tests cover plumbing,
|
|
# so treat fixture text as valid; validity itself is covered in
|
|
# test_concept_evaluation.py.
|
|
monkeypatch.setattr("obliteratus.concepts.evaluation._is_invalid_output", lambda text: False)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def base(tmp_path_factory):
|
|
threads = torch.get_num_threads()
|
|
torch.set_num_threads(1)
|
|
path = build_tiny_offline_model(tmp_path_factory.mktemp("repair") / "model")
|
|
yield AutoModelForCausalLM.from_pretrained(path, local_files_only=True).eval()
|
|
torch.set_num_threads(threads)
|
|
|
|
|
|
@pytest.fixture
|
|
def model(base):
|
|
return deepcopy(base)
|
|
|
|
|
|
def direction():
|
|
return ConceptDirection({0: torch.eye(16)[0]}, validation={
|
|
"concept_id": "refusal", "pack_hash": None, "verdict": "pass",
|
|
})
|
|
|
|
|
|
def backend_for(model):
|
|
generator = torch.Generator().manual_seed(233)
|
|
activations = {0: (torch.randn(8, 16, generator=generator),
|
|
torch.randn(8, 16, generator=generator))}
|
|
backend = WeightRepair(model, direction(), activations=activations)
|
|
apply_concept_intervention(model, direction(), "orthogonalize")
|
|
# Destructive orthogonal shrink leaves a component that LoRA can restore.
|
|
with torch.no_grad():
|
|
for weight in backend.weights.values():
|
|
weight.mul_(0.2)
|
|
return backend.bind()
|
|
|
|
|
|
def test_all_candidates_restore_exact_weights_and_logits(model):
|
|
backend = backend_for(model)
|
|
inputs = torch.tensor([[3, 5, 6]])
|
|
logits = model(inputs).logits.detach()
|
|
snapshot = {name: value.clone() for name, value in model.state_dict().items()}
|
|
config = RepairConfig(rdo_steps=3)
|
|
for step in REPAIR_STEPS:
|
|
candidates = list(backend(step, config))
|
|
assert candidates
|
|
for candidate in candidates:
|
|
with candidate:
|
|
assert torch.isfinite(model(inputs).logits).all()
|
|
assert all(torch.equal(value, snapshot[name])
|
|
for name, value in model.state_dict().items())
|
|
assert torch.equal(logits, model(inputs).logits)
|
|
|
|
|
|
def test_lora_is_reversible_and_saves_unmerged_adapter(model, tmp_path):
|
|
backend = backend_for(model)
|
|
inputs = torch.tensor([[3, 5, 6]])
|
|
original = model(inputs).logits.detach()
|
|
snapshot = {name: value.clone() for name, value in model.state_dict().items()}
|
|
candidate = next(backend("lora_repair", RepairConfig()))
|
|
with candidate:
|
|
assert not torch.equal(model(inputs).logits, original)
|
|
candidate.accept()
|
|
assert all(torch.equal(value, snapshot[name]) for name, value in model.state_dict().items())
|
|
backend.adapter.save(tmp_path, base_model_name_or_path="local/edited-base")
|
|
assert (tmp_path / "adapter_model.safetensors").exists()
|
|
backend.adapter.remove()
|
|
assert torch.equal(model(inputs).logits, original)
|
|
|
|
|
|
@pytest.mark.parametrize("geometry", ["rank_one", "subspace", "mean", "leace", "folded"])
|
|
@pytest.mark.parametrize("step", ["lora_repair", "rdo_refinement"])
|
|
def test_repair_preserves_complete_erasure_geometry(model, monkeypatch, geometry, step):
|
|
if geometry == "folded":
|
|
model = _tiny_residual_model("gemma2")
|
|
artifact = direction()
|
|
artifact.subspaces = {0: torch.eye(16)[1:2] if geometry == "rank_one" else torch.eye(16)[:2]}
|
|
reference = "mean" if geometry in {"mean", "leace"} else "zero"
|
|
artifact.reference_means = {0: torch.arange(16, dtype=torch.float32) / 16}
|
|
artifact.reference_provenance = {"source": "fixture controls"}
|
|
if geometry == "leace":
|
|
artifact.recipe = replace(artifact.recipe, method="leace_exact")
|
|
artifact.projector_left = {0: torch.eye(16)[:, :2]}
|
|
artifact.projector_right = {0: torch.eye(16)[:, :2].clone()}
|
|
artifact.projector_right[0][2, 0] = 0.5
|
|
backend = WeightRepair(model, artifact, subspace=True, reference=reference, fold_post_norms=True)
|
|
apply_concept_intervention(model, artifact, "orthogonalize", subspace=True, reference=reference)
|
|
with torch.no_grad():
|
|
for weight in backend.weights.values():
|
|
weight.mul_(0.2)
|
|
backend.bind()
|
|
refined = (torch.eye(16)[0] + torch.eye(16)[2]) / 2 ** 0.5
|
|
monkeypatch.setattr(backend, "_refined_vectors", lambda config: {0: refined})
|
|
candidate = next(backend(step, RepairConfig()))
|
|
protected = artifact.projector_right[0] if geometry == "leace" else artifact.subspaces[0].T
|
|
for name in backend.weights:
|
|
if step == "lora_repair":
|
|
b, a = candidate.adapters[name.removesuffix(".weight")]
|
|
work = (b @ a).T
|
|
else:
|
|
work = backend._work(name, candidate.updates[name])
|
|
if name in backend.post_norm_scales:
|
|
work = work * backend.post_norm_scales[name]
|
|
assert work.norm() > 1e-4
|
|
residual = work @ protected
|
|
torch.testing.assert_close(residual, torch.zeros_like(residual), atol=2e-7, rtol=0)
|
|
snapshot = {name: value.clone() for name, value in model.state_dict().items()}
|
|
with candidate:
|
|
assert all(torch.equal(value, model.state_dict()[name])
|
|
for name, value in snapshot.items() if not name.endswith("weight"))
|
|
assert all(torch.equal(value, model.state_dict()[name]) for name, value in snapshot.items())
|
|
|
|
|
|
def test_exception_restores_candidate(model):
|
|
backend = backend_for(model)
|
|
with pytest.raises(RuntimeError, match="measure"):
|
|
run_repair_ladder(report(), Mock(side_effect=RuntimeError("measure")), backend)
|
|
assert all(torch.equal(backend.weights[n], v) for n, v in backend.edited.items())
|
|
|
|
|
|
def test_accepted_candidate_changes_real_projection_and_coherence(model, monkeypatch, tmp_path):
|
|
# Keep the incoming residual orthogonal to the target so writer removal is measurable.
|
|
with torch.no_grad():
|
|
model.transformer.wte.weight[:, 0].zero_()
|
|
model.transformer.wpe.weight[:, 0].zero_()
|
|
backend = backend_for(model)
|
|
inputs = torch.tensor([[7]]) # hello
|
|
|
|
def hidden():
|
|
with torch.inference_mode():
|
|
return model(inputs, output_hidden_states=True).hidden_states[-1][0, -1].clone()
|
|
|
|
edited_hidden = hidden()
|
|
with next(backend("norm_preservation", RepairConfig())):
|
|
repaired_hidden = hidden()
|
|
with torch.no_grad():
|
|
for name, weight in backend.weights.items():
|
|
weight.copy_(backend.pristine[name])
|
|
pristine_hidden = hidden()
|
|
|
|
# Calibrate one fixture-vocabulary anchor: pristine and repaired predict "world",
|
|
# while the damaged model emits padding. All readings below come from real forwards.
|
|
differences = torch.stack([pristine_hidden - edited_hidden, repaired_hidden - edited_hidden])
|
|
separator = torch.linalg.lstsq(differences, torch.ones(2)).solution
|
|
head = torch.nn.Linear(16, 11)
|
|
with torch.no_grad():
|
|
head.weight.zero_()
|
|
head.bias.zero_()
|
|
head.weight[8].copy_(separator * 0.01)
|
|
head.bias[8] = (-separator @ edited_hidden - 0.5) * 0.01
|
|
model.lm_head = head
|
|
monkeypatch.setattr("obliteratus.abliterate._COHERENCE_ANCHORS", {"hello": (("world",),)})
|
|
tokenizer = AutoTokenizer.from_pretrained(model.config._name_or_path, local_files_only=True)
|
|
evaluator = ModelEvaluator(model, tokenizer, direction())
|
|
battery = EvaluationBattery(evaluator.generate, measure=evaluator.measure,
|
|
refusal_size=4, max_new_tokens=1).capture()
|
|
with torch.no_grad():
|
|
for name, weight in backend.weights.items():
|
|
weight.copy_(backend.edited[name])
|
|
original = battery.finish()
|
|
original_logits = model(inputs).logits.detach().clone()
|
|
repaired, final = run_repair_ladder(original, battery.finish, backend, output_dir=tmp_path)
|
|
assert repaired.accepted_step == "norm_preservation"
|
|
assert original.collateral["coherence"]["before"]["rate"] == 1
|
|
assert original.collateral["coherence"]["after"]["rate"] == 0
|
|
assert final.collateral["coherence"]["after"]["rate"] == 1
|
|
assert final.mechanistic["before"]["0"] > 1e-8
|
|
assert final.mechanistic["after"]["0"] <= 1e-8
|
|
assert not torch.equal(model(inputs).logits, original_logits)
|
|
assert all(torch.equal(weight, backend.edited[name] * 2)
|
|
for name, weight in backend.weights.items())
|
|
assert battery.finish().collateral["coherence"] == final.collateral["coherence"]
|
|
|
|
|
|
def test_same_inputs_same_rdo_and_reports(model):
|
|
backend = backend_for(model)
|
|
config = RepairConfig(rdo_steps=3)
|
|
first = next(backend("rdo_refinement", config)).updates
|
|
second = next(backend("rdo_refinement", config)).updates
|
|
assert all(torch.equal(value, second[name]) for name, value in first.items())
|
|
a, _ = run_repair_ladder(report(), lambda: report(0.8), backend, config=config)
|
|
b, _ = run_repair_ladder(report(), lambda: report(0.8), backend, config=config)
|
|
assert asdict(a) == asdict(b)
|
|
|
|
|
|
@pytest.mark.parametrize("subspace", [False, True])
|
|
def test_evaluated_api_accepts_and_cleans_repair_adapter(model, tmp_path, subspace):
|
|
# Force traversal through every step using controlled battery measurements.
|
|
reads = [report()] + [report(0.4)] * 6 + [report(1)]
|
|
battery = SimpleNamespace(capture=Mock(), finish=Mock(side_effect=reads))
|
|
artifact = direction()
|
|
artifact.subspaces = {0: torch.eye(16)[:2]}
|
|
with evaluate_intervention(model, artifact, "orthogonalize", battery,
|
|
repair=True, subspace=subspace, output_dir=tmp_path) as run:
|
|
assert run.repair.accepted_step == "lora_repair"
|
|
adapter = run.repair_adapter
|
|
assert adapter._hooks
|
|
for b, a in adapter.adapters.values():
|
|
assert (b @ a)[:2 if subspace else 1].abs().max() < 1e-8
|
|
logits = model(torch.tensor([[3, 5, 6]])).logits.detach().clone()
|
|
hooks = list(adapter._hooks)
|
|
with pytest.raises(ValueError, match="orthogonalize is permanent"):
|
|
run.remove()
|
|
assert adapter._hooks == hooks
|
|
assert torch.equal(model(torch.tensor([[3, 5, 6]])).logits, logits)
|
|
assert not adapter._hooks
|
|
assert (tmp_path / "repair.json").exists()
|
|
|
|
|
|
def test_pipeline_capture_repair_and_metadata(model, tmp_path):
|
|
pipeline = AbliterationPipeline("offline", concept=small_target(), output_dir=str(tmp_path))
|
|
pipeline.handle = SimpleNamespace(model=model)
|
|
pipeline._concept_direction = direction()
|
|
pipeline._evaluation_battery = SimpleNamespace(finish=lambda: report(1))
|
|
pipeline._capture_concept_repair()
|
|
assert pipeline._repair_backend is not None
|
|
apply_concept_intervention(model, direction(), "orthogonalize")
|
|
pipeline._evaluation_report = report()
|
|
pipeline._repair_concept_coherence()
|
|
assert pipeline.repair_report.accepted_step == "norm_preservation"
|
|
assert pipeline._build_metadata()["concept"]["repair"]["repair"] == "complete"
|
|
assert (tmp_path / "repair.json").exists()
|
|
|
|
|
|
def test_checkpoint_preserves_reports_and_separate_adapter(model, tmp_path):
|
|
import json
|
|
|
|
backend = backend_for(model)
|
|
with next(backend("lora_repair", RepairConfig())) as candidate:
|
|
candidate.accept()
|
|
pipeline = AbliterationPipeline("offline", concept=small_target(), output_dir=str(tmp_path))
|
|
pipeline._repair_backend = backend
|
|
pipeline._evaluation_report = report(1)
|
|
pipeline._repair_report, _ = run_repair_ladder(report(1), Mock(), backend)
|
|
pipeline.handle = SimpleNamespace(
|
|
model=model, tokenizer=SimpleNamespace(save_pretrained=lambda directory: None),
|
|
)
|
|
pipeline._write_local_checkpoint(tmp_path, json.dumps(pipeline._build_metadata()),
|
|
model.state_dict())
|
|
assert (tmp_path / "repair_adapter" / "adapter_model.safetensors").is_file()
|
|
assert (tmp_path / "repair.json").is_file()
|
|
assert (tmp_path / "evaluation.json").is_file()
|
|
metadata = json.loads((tmp_path / "abliteration_metadata.json").read_text())
|
|
assert metadata["concept"]["repair_adapter"]["merged"] is False
|
|
assert pipeline.repair_adapter is backend.adapter
|
|
pipeline.cleanup_failed_run()
|
|
assert not backend.adapter._hooks
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["orthogonalize", "project"])
|
|
def test_incomplete_repair_survives_auto_save_and_blocks_export(model, tmp_path, mode):
|
|
from obliteratus.concepts.export import export_run
|
|
from obliteratus.concepts.runs import load_run
|
|
|
|
battery = SimpleNamespace(capture=Mock(), finish=Mock(side_effect=lambda: report()))
|
|
source = tmp_path / "run"
|
|
with evaluate_intervention(model, direction(), mode, battery,
|
|
repair=True, output_dir=source) as run:
|
|
record = load_run(source)
|
|
assert record["repair"]["status"] == "incomplete"
|
|
assert record["repair"]["steps"] == run.repair.attempts
|
|
saved_repair = json.loads((source / "repair.json").read_text())
|
|
assert record["repair"]["config"] == saved_repair["config"]
|
|
assert record["evaluation"]["metadata"]["repair"] == saved_repair
|
|
assert record["evaluation"] == json.loads(
|
|
(source / "evaluation.json").read_text()
|
|
)
|
|
assert (source / "repair.json").is_file()
|
|
with pytest.raises(ValueError, match="accept-incomplete-repair"):
|
|
export_run(source, tmp_path / "blocked", kind="report")
|
|
exported = export_run(source, tmp_path / "export", kind="report",
|
|
repair_choice="keep original intervention")
|
|
assert exported["run"]["repair"] == record["repair"]
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["orthogonalize", "project", "lora"])
|
|
@pytest.mark.parametrize("subspace", [False, True])
|
|
def test_protected_candidate_replays_pristine_and_rolls_back(model, mode, subspace):
|
|
from obliteratus.concepts.repair_model import ProtectedDirectionRepair
|
|
|
|
target = direction()
|
|
target.directions[0] = (torch.eye(16)[0] + torch.eye(16)[1]) / 2**0.5
|
|
target.subspaces[0] = torch.stack([target.directions[0], torch.eye(16)[2]])
|
|
preserve = [torch.eye(16)[0]]
|
|
pristine = deepcopy(model)
|
|
expected = deepcopy(model)
|
|
expected_run = apply_concept_intervention(expected, target, mode, subspace=subspace,
|
|
preserve=preserve)
|
|
backend = ProtectedDirectionRepair(model, target, mode, preserve, subspace=subspace)
|
|
run = apply_concept_intervention(model, target, mode, subspace=subspace)
|
|
backend.bind(run)
|
|
inputs = torch.tensor([[3, 5, 6]])
|
|
edited_logits = model(inputs).logits.detach()
|
|
edited_weights = {name: value.clone() for name, value in model.state_dict().items()}
|
|
candidate = next(backend("protected_direction", RepairConfig()))
|
|
with candidate:
|
|
assert torch.equal(model(inputs).logits, expected(inputs).logits)
|
|
assert candidate.parameters["intervention"] == expected_run.parameters
|
|
assert torch.equal(model(inputs).logits, edited_logits)
|
|
assert all(torch.equal(value, edited_weights[name]) for name, value in model.state_dict().items())
|
|
with next(backend("protected_direction", RepairConfig())) as candidate:
|
|
candidate.accept()
|
|
assert run.parameters == expected_run.parameters
|
|
assert torch.equal(model(inputs).logits, expected(inputs).logits)
|
|
if mode != "orthogonalize":
|
|
run.remove()
|
|
expected_run.remove()
|
|
assert torch.equal(model(inputs).logits, pristine(inputs).logits)
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["orthogonalize", "lora"])
|
|
def test_protected_repair_replays_embedding_geometry_layer(mode):
|
|
from obliteratus.concepts.repair_model import ProtectedDirectionRepair
|
|
|
|
model = _tiny_residual_model("gemma2", tied=True)
|
|
expected = deepcopy(model)
|
|
target = direction()
|
|
target.directions = {
|
|
0: (torch.eye(16)[0] + torch.eye(16)[1]) / 2**0.5,
|
|
1: (torch.eye(16)[0] + torch.eye(16)[2]) / 2**0.5,
|
|
}
|
|
preserve = [{0: torch.eye(16)[0], 1: torch.eye(16)[2]}]
|
|
options = {"layers": "all", "project_embeddings": True,
|
|
"tied_embeddings": "edit_both", "embedding_geometry_layer": 1}
|
|
expected_run = apply_concept_intervention(expected, target, mode, preserve=preserve, **options)
|
|
backend = ProtectedDirectionRepair(model, target, mode, preserve, **options)
|
|
run = apply_concept_intervention(model, target, mode, **options)
|
|
backend.bind(run)
|
|
inputs = torch.tensor([[3, 5, 6]])
|
|
edited = model(inputs).logits.detach().clone()
|
|
try:
|
|
with next(backend("protected_direction", RepairConfig())) as candidate:
|
|
assert candidate.parameters["intervention"]["embedding_geometry_layer"] == 1
|
|
torch.testing.assert_close(model(inputs).logits, expected(inputs).logits)
|
|
assert run.parameters["embedding_geometry_layer"] == 1
|
|
torch.testing.assert_close(model(inputs).logits, edited)
|
|
with next(backend("protected_direction", RepairConfig())) as candidate:
|
|
candidate.accept()
|
|
assert run.parameters == expected_run.parameters
|
|
torch.testing.assert_close(model(inputs).logits, expected(inputs).logits)
|
|
finally:
|
|
if mode == "orthogonalize":
|
|
for result in (run, expected_run):
|
|
with pytest.raises(ValueError, match="orthogonalize is permanent"):
|
|
result.remove()
|
|
else:
|
|
run.remove()
|
|
expected_run.remove()
|
|
|
|
|
|
@pytest.mark.parametrize("fail", [False, True])
|
|
def test_protected_affine_candidate_restores_native_biases(model, fail):
|
|
from obliteratus.concepts.repair_model import ProtectedDirectionRepair
|
|
|
|
target = direction()
|
|
target.directions[0] = (torch.eye(16)[0] + torch.eye(16)[1]) / 2**0.5
|
|
target.subspaces[0] = torch.stack([target.directions[0], torch.eye(16)[2]])
|
|
target.reference_means = {0: torch.ones(16)}
|
|
target.reference_provenance = {"source": "test controls"}
|
|
kwargs = {"subspace": True, "reference": "mean"}
|
|
backend = ProtectedDirectionRepair(model, target, "orthogonalize", [torch.eye(16)[0]],
|
|
**kwargs)
|
|
run = apply_concept_intervention(model, target, "orthogonalize", **kwargs)
|
|
backend.bind(run)
|
|
assert any(name.endswith(".bias") for name in backend.pristine)
|
|
snapshot = {name: value.clone() for name, value in model.state_dict().items()}
|
|
candidate = next(backend("protected_direction", RepairConfig()))
|
|
if fail:
|
|
with pytest.raises(RuntimeError, match="measurement"):
|
|
with candidate:
|
|
raise RuntimeError("measurement failed")
|
|
else:
|
|
with candidate:
|
|
assert any(not torch.equal(model.state_dict()[name], snapshot[name])
|
|
for name in backend.edited if name.endswith(".bias"))
|
|
assert all(torch.equal(value, snapshot[name]) for name, value in model.state_dict().items())
|
|
|
|
|
|
@pytest.mark.parametrize("geometry", ["rank_one", "subspace", "mean"])
|
|
@pytest.mark.parametrize("fraction", [0.5, 1.0])
|
|
def test_evaluate_protected_repair_accepts_tradeoff_and_updates_run(model, tmp_path, geometry, fraction):
|
|
target = direction()
|
|
target.directions[0] = (torch.eye(16)[0] + torch.eye(16)[1]) / 2**0.5
|
|
target.subspaces = {0: torch.stack([target.directions[0], torch.eye(16)[2]])}
|
|
target.reference_means = {0: torch.ones(16)}
|
|
target.reference_provenance = {"source": "fixture controls"}
|
|
original, measured = report(1), report(1, projection=0.5)
|
|
original.collateral["status"] = "FAIL"
|
|
battery = SimpleNamespace(capture=Mock(), finish=Mock(side_effect=[original, measured]))
|
|
run = evaluate_intervention(
|
|
model, target, "orthogonalize", battery, repair=True,
|
|
repair_preserve=[torch.eye(16)[0]], output_dir=tmp_path,
|
|
subspace=geometry == "subspace", reference="mean" if geometry == "mean" else "zero",
|
|
projection_row_fraction=fraction,
|
|
)
|
|
assert run.repair.accepted_step == "protected_direction"
|
|
assert run.evaluation.mechanistic["after"] == {"0": 0.5}
|
|
assert run.evaluation.metadata["protected_direction_tradeoff"]["collateral_status"] == "PASS"
|
|
assert run.parameters == run.repair.attempts[0]["parameters"]["intervention"]
|
|
assert run.parameters.get("projection_row_fraction", 1.0) == fraction
|
|
assert run.parameters["geometry"]["0"]["rank"] == (2 if geometry == "subspace" else 1)
|
|
saved = json.loads((tmp_path / "repair.json").read_text())
|
|
assert saved["accepted_step"] == "protected_direction"
|
|
|
|
|
|
@pytest.mark.parametrize("repair_only", [False, True])
|
|
@pytest.mark.parametrize("accept", [False, True])
|
|
def test_pipeline_protected_repair_replays_selective_projection(model, repair_only, accept, monkeypatch):
|
|
monkeypatch.setattr("obliteratus.concepts.evaluation._admit_evaluation_phase", lambda *a: None)
|
|
target = direction()
|
|
target.directions[0] = (torch.eye(16)[0] + torch.eye(16)[1]) / 2**0.5
|
|
pipeline = AbliterationPipeline(
|
|
"offline", method="basic", repair=True, projection_row_fraction=0.1,
|
|
projection_target="attention", norm_preserve=False, regularization=0.0,
|
|
)
|
|
pipeline.handle = SimpleNamespace(model=model)
|
|
pipeline._concept_direction = target
|
|
pipeline._strong_layers = [0]
|
|
protected = [torch.eye(16)[0]]
|
|
pipeline._preserved_directions = [] if repair_only else protected
|
|
pipeline._repair_preserved_directions = protected if repair_only else []
|
|
pipeline._evaluation_battery = SimpleNamespace()
|
|
pristine = model.transformer.h[0].attn.c_proj.weight.detach().clone()
|
|
expected = pristine.clone()
|
|
indices = pristine[:, 1].abs().argsort(descending=True)[:2]
|
|
expected[indices, 1] = 0
|
|
pipeline._capture_concept_repair()
|
|
backend = pipeline._protected_repair_backend
|
|
assert backend.kwargs["projection_row_fraction"] == 0.1
|
|
if repair_only:
|
|
run = apply_concept_intervention(model, target, "orthogonalize",
|
|
**pipeline._preserved_intervention_options())
|
|
else:
|
|
pipeline._excise()
|
|
run = pipeline._preserved_intervention
|
|
torch.testing.assert_close(model.transformer.h[0].attn.c_proj.weight, expected)
|
|
backend.bind(run)
|
|
edited = {name: value.clone() for name, value in model.state_dict().items()}
|
|
with next(backend("protected_direction", RepairConfig())) as candidate:
|
|
torch.testing.assert_close(model.transformer.h[0].attn.c_proj.weight, expected)
|
|
assert candidate.parameters["intervention"]["projection_row_fraction"] == 0.1
|
|
if accept:
|
|
candidate.accept()
|
|
if accept:
|
|
torch.testing.assert_close(model.transformer.h[0].attn.c_proj.weight, expected)
|
|
assert backend.accepted_run.parameters["projection_row_fraction"] == 0.1
|
|
else:
|
|
assert all(torch.equal(value, edited[name]) for name, value in model.state_dict().items())
|
|
|
|
|
|
@pytest.mark.parametrize("options", [
|
|
{"preserve": []}, {"repair_preserve": []}, {"preserve": [], "repair_preserve": []},
|
|
{"preserve": (), "repair_preserve": ()},
|
|
])
|
|
@pytest.mark.parametrize("geometry", [{}, {"subspace": True}, {"reference": "mean"}])
|
|
def test_empty_protection_keeps_legacy_repair_candidates(model, options, geometry):
|
|
target = direction()
|
|
target.subspaces = {0: torch.eye(16)[:2]}
|
|
target.reference_means = {0: torch.ones(16)}
|
|
target.reference_provenance = {"source": "test controls"}
|
|
original = deepcopy(model)
|
|
batteries = [SimpleNamespace(capture=Mock(), finish=Mock(side_effect=report)) for _ in range(2)]
|
|
legacy = evaluate_intervention(original, target, "orthogonalize", batteries[0],
|
|
repair=True, **geometry)
|
|
run = evaluate_intervention(model, target, "orthogonalize", batteries[1],
|
|
repair=True, **geometry, **options)
|
|
assert asdict(run.repair) == asdict(legacy.repair)
|
|
assert run.parameters == legacy.parameters
|
|
assert batteries[0].finish.call_count == batteries[1].finish.call_count > 1
|
|
assert all(torch.equal(value, original.state_dict()[name])
|
|
for name, value in model.state_dict().items())
|
|
|
|
|
|
@pytest.mark.parametrize("fold_post_norms", [False, True])
|
|
def test_empty_protection_forwards_full_geometry_to_weight_repair(model, monkeypatch, fold_post_norms):
|
|
artifact = direction()
|
|
artifact.subspaces = {0: torch.eye(16)[:2]}
|
|
means, provenance = {0: torch.ones(16)}, {"source": "operator controls"}
|
|
capture = Mock(wraps=WeightRepair)
|
|
monkeypatch.setattr("obliteratus.concepts.repair_model.WeightRepair", capture)
|
|
battery = SimpleNamespace(capture=Mock(), finish=Mock(side_effect=[report(), report(1)]))
|
|
run = evaluate_intervention(
|
|
model, artifact, "orthogonalize", battery, repair=True, preserve=[], repair_preserve=[],
|
|
subspace=True, reference="mean", reference_means=means, reference_provenance=provenance,
|
|
fold_post_norms=fold_post_norms,
|
|
)
|
|
capture.assert_called_once_with(
|
|
model, artifact, layers="intervention", projection_target="output", activations=None,
|
|
fold_post_norms=fold_post_norms, subspace=True, reference="mean",
|
|
reference_means=means, reference_provenance=provenance,
|
|
)
|
|
assert run.repair.accepted_step == "norm_preservation"
|
|
assert run.parameters["geometry"]["0"]["rank"] == 2
|
|
assert run.parameters["geometry"]["0"]["mean_provenance"] == provenance
|
|
|
|
|
|
def test_empty_repair_protection_keeps_initial_protection(model):
|
|
snapshot = {name: value.clone() for name, value in model.state_dict().items()}
|
|
battery = SimpleNamespace(capture=Mock(), finish=Mock(side_effect=report))
|
|
run = evaluate_intervention(model, direction(), "orthogonalize", battery, repair=True,
|
|
preserve=[torch.eye(16)[0]], repair_preserve=[])
|
|
assert run.parameters["preserve"]["0"]["rank"] == 0
|
|
assert run.repair.attempts[-1]["step"] == "protected_direction"
|
|
assert all("candidate unavailable" in row["reasons"][0] for row in run.repair.attempts[:-1])
|
|
assert all(torch.equal(value, snapshot[name]) for name, value in model.state_dict().items())
|
|
|
|
|
|
@pytest.mark.parametrize("entrypoint", ["pipeline", "evaluate", "repair_only"])
|
|
@pytest.mark.parametrize("transpose", [False, True])
|
|
def test_fused_writers_keep_surgery_when_protected_repair_is_unavailable(
|
|
model, tmp_path, entrypoint, transpose,
|
|
):
|
|
ffn = model.transformer.h[0].mlp
|
|
del ffn.c_proj
|
|
ffn.experts = torch.nn.Module()
|
|
weight = torch.ones(2, 24, 16)
|
|
ffn.experts.down_proj = torch.nn.Parameter(weight.transpose(-1, -2) if transpose else weight)
|
|
target = direction()
|
|
target.directions[0] = (torch.eye(16)[0] + torch.eye(16)[1]) / 2**0.5
|
|
preserve = [torch.eye(16)[0]]
|
|
battery = SimpleNamespace(capture=Mock(), finish=Mock(side_effect=report))
|
|
if entrypoint == "pipeline":
|
|
pipeline = AbliterationPipeline("offline", method="basic", concept=small_target(),
|
|
output_dir=str(tmp_path),
|
|
override_gate="Fixture tests fused-writer repair availability")
|
|
pipeline.handle = SimpleNamespace(model=model)
|
|
pipeline._concept_direction = target
|
|
pipeline._strong_layers = [0]
|
|
pipeline._preserved_directions = preserve
|
|
pipeline._evaluation_battery = battery
|
|
pipeline._capture_concept_repair()
|
|
assert not pipeline._protected_repair_backend.available
|
|
assert not pipeline._protected_repair_backend.pristine
|
|
pipeline._excise()
|
|
assert pipeline._preserved_intervention.validation["override_gate"] == pipeline._override_gate
|
|
pipeline._evaluation_report = report()
|
|
pipeline._repair_concept_coherence()
|
|
repaired = pipeline.repair_report
|
|
else:
|
|
options = {"repair_preserve" if entrypoint == "repair_only" else "preserve": preserve}
|
|
run = evaluate_intervention(model, target, "orthogonalize", battery,
|
|
repair=True, **options)
|
|
repaired = run.repair
|
|
assert repaired.repair == "incomplete"
|
|
assert [row["step"] for row in repaired.attempts] == [*REPAIR_STEPS, "protected_direction"]
|
|
assert all("candidate unavailable" in row["reasons"][0] for row in repaired.attempts)
|
|
work = ffn.experts.down_proj.transpose(-1, -2) if transpose else ffn.experts.down_proj
|
|
if entrypoint == "repair_only":
|
|
assert work[..., :2].abs().max() < 1e-6
|
|
else:
|
|
assert torch.equal(work[..., 0], torch.ones_like(work[..., 0]))
|
|
assert work[..., 1].abs().max() < 1e-6
|
|
|
|
|
|
def test_invalid_repair_protection_fails_before_initial_mutation(model):
|
|
snapshot = {name: value.clone() for name, value in model.state_dict().items()}
|
|
battery = SimpleNamespace(capture=Mock(), finish=Mock())
|
|
with pytest.raises(ValueError, match="protected"):
|
|
evaluate_intervention(
|
|
model, direction(), "orthogonalize", battery, repair=True,
|
|
repair_preserve=[torch.ones(3)],
|
|
)
|
|
battery.capture.assert_not_called()
|
|
assert all(torch.equal(value, snapshot[name]) for name, value in model.state_dict().items())
|
|
|
|
|
|
@pytest.mark.parametrize("outcome", ["accept", "reject", "error"])
|
|
@pytest.mark.parametrize("tied", [False, True])
|
|
def test_pipeline_protected_replay_restores_all_legacy_edits(model, tmp_path, monkeypatch, outcome, tied):
|
|
from obliteratus.concepts import intervention, repair
|
|
from obliteratus.concepts.repair_model import ProtectedDirectionRepair
|
|
|
|
if not tied:
|
|
model.lm_head.weight = torch.nn.Parameter(model.lm_head.weight.detach().clone())
|
|
model.config.tie_word_embeddings = False
|
|
model.register_buffer("repair_fixture", torch.ones(1), persistent=False)
|
|
target = direction()
|
|
target.directions[0] = (torch.eye(16)[0] + torch.eye(16)[1]) / 2**0.5
|
|
pipeline = AbliterationPipeline("offline", method="basic", concept=small_target(),
|
|
output_dir=str(tmp_path), repair=True,
|
|
override_gate="Fixture tests protected replay of legacy edits")
|
|
pipeline.handle = SimpleNamespace(model=model, architecture="gpt2", config=model.config,
|
|
num_layers=model.config.num_hidden_layers)
|
|
pipeline._concept_direction = target
|
|
pipeline.concept_directions = target.directions
|
|
pipeline._strong_layers = [0]
|
|
pipeline._repair_preserved_directions = [torch.eye(16)[0]]
|
|
pipeline._evaluation_battery = SimpleNamespace(finish=lambda: report(1))
|
|
options = pipeline._preserved_intervention_options()
|
|
expected = deepcopy(model)
|
|
expected_run = apply_concept_intervention(
|
|
expected, target, "orthogonalize", preserve=pipeline._repair_preserved_directions, **options,
|
|
)
|
|
assert expected_run.validation["override_gate"] == pipeline._override_gate
|
|
legacy_bytes = WeightRepair.capture_host_bytes(model, target, layers=[0])
|
|
assert pipeline._repair_capture_host_bytes() == (
|
|
legacy_bytes + ProtectedDirectionRepair.full_snapshot_host_bytes(model)
|
|
)
|
|
pipeline._capture_concept_repair()
|
|
assert pipeline._protected_repair_backend.snapshot_all
|
|
apply_concept_intervention(model, target, "orthogonalize", **options)
|
|
with torch.no_grad():
|
|
model.lm_head.weight.mul_(0.5)
|
|
model.get_input_embeddings().weight.mul_(0.75)
|
|
for name, value in model.named_parameters():
|
|
if name.endswith(".bias"):
|
|
value.add_(0.125)
|
|
model.repair_fixture.zero_()
|
|
pipeline._install_activation_steering(model.transformer.h)
|
|
pipeline._lora_adapters = {"legacy": (torch.ones(1), torch.ones(1))}
|
|
edited = {name: value.clone() for name, value in model.state_dict().items()}
|
|
inputs = torch.tensor([[3, 5, 6]])
|
|
edited_logits = model(inputs).logits.detach().clone()
|
|
pipeline._evaluation_report = report()
|
|
|
|
def ladder(original, evaluate, candidates, **kwargs):
|
|
candidate = next(candidates("protected_direction", RepairConfig()))
|
|
if outcome == "error":
|
|
def fail(*args, **kwargs):
|
|
raise RuntimeError("protected replay failed")
|
|
|
|
monkeypatch.setattr(intervention, "apply_concept_intervention", fail)
|
|
with pytest.raises(RuntimeError, match="protected replay failed"):
|
|
with candidate:
|
|
pytest.fail("candidate entry should fail")
|
|
else:
|
|
with candidate:
|
|
assert not pipeline._steering_hooks
|
|
assert torch.equal(model(inputs).logits, expected(inputs).logits)
|
|
assert torch.equal(model.repair_fixture, expected.repair_fixture)
|
|
assert all(torch.equal(value, expected.state_dict()[name])
|
|
for name, value in model.state_dict().items())
|
|
if outcome == "accept":
|
|
candidate.accept()
|
|
return repair.RepairReport("complete" if outcome == "accept" else "incomplete", {}), report(1)
|
|
|
|
monkeypatch.setattr(repair, "run_repair_ladder", ladder)
|
|
pipeline._repair_concept_coherence()
|
|
if outcome == "accept":
|
|
assert not pipeline._steering_hooks and not pipeline._lora_adapters
|
|
assert pipeline._preserved_intervention.parameters == expected_run.parameters
|
|
assert pipeline._preserved_intervention.validation == expected_run.validation
|
|
assert torch.equal(model(inputs).logits, expected(inputs).logits)
|
|
else:
|
|
assert pipeline._steering_hooks and pipeline._lora_adapters
|
|
assert torch.equal(model(inputs).logits, edited_logits)
|
|
assert model.repair_fixture.item() == 0
|
|
assert all(torch.equal(value, edited[name]) for name, value in model.state_dict().items())
|
|
pipeline.cleanup_failed_run()
|
|
assert pipeline._protected_repair_backend is None
|