Files
OBLITERATUS/tests/test_candidate_edits.py
Joseph MaglyandClaude Opus 5.5 8848556105 feat(experiments): persistent candidate builder and verifier
Start milestone 4 (F16): turn a confirmed temporary intervention into
the smallest persistent representation that expresses it, as a separate
artifact, and verify the saved candidate itself after a fresh load.

- Builds only for registry keys computed as persistent-pilot eligible
  (fixture models only with a test-only allowance marking outputs
  software-contract). The reference model is never written to; its
  digests are checked before and after.
- Forms: a removable hook-free activation adapter (all operations), a
  LoRA adapter through the existing PEFT exporter (all-token ablation on
  a plain linear site), or a separate weight-edited copy (projection or
  bias shift). Auto picks the feasible form with the fewest stored
  elements.
- A write-once, content-addressed manifest records parent identity and
  fingerprints, recipe and direction binding, every edited parameter
  with before/after digests, compatibility checks, artifact digests and
  build-time tolerances.
- Verification reloads the candidate from disk and compares reference,
  temporary hook and candidate arms on independent rows: the edit must
  match the registered recipe, effects and outputs must agree with the
  temporary intervention, the useful-effect threshold and every
  collateral endpoint must hold. Failures are permanent and never
  selected.
- `obliteratus experiments candidate build|verify|show`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:26:10 -05:00

192 lines
9.0 KiB
Python

"""Persistent edit representations against the intervention hook's arithmetic.
A wrapped module (activation adapter) must reproduce ``apply_operation`` for every
operation, units and token scope; LoRA and weight edits of a plain linear site must
reproduce the all-token projection or translation. Compatibility checks refuse the
forms that cannot express an operation.
"""
from __future__ import annotations
import json
from types import SimpleNamespace
import pytest
import torch
from torch import nn
from obliteratus.experiments import candidate_edits as edits
from obliteratus.experiments import candidate_io as cio
from obliteratus.experiments.intervention_ops import apply_operation
pytestmark = pytest.mark.cpu
WIDTH = 6
class Tupled(nn.Module):
def __init__(self):
super().__init__()
self.inner = nn.Linear(WIDTH, WIDTH)
def forward(self, hidden):
return self.inner(hidden), "cache"
def _model(layer):
torch.manual_seed(0)
model = nn.Sequential(layer).eval()
with torch.no_grad():
for parameter in model.parameters():
parameter.normal_()
return model
def _op(kind="steer", scope="all_tokens", units="raw", coefficient=2.5):
return {"kind": kind, "token_scope": scope, "units": units,
"decision_coefficient": coefficient}
def _hooked(model, vector, operation, hidden):
with apply_operation(model, model[0], vector, operation["decision_coefficient"], operation):
out = model(hidden)
return out[0] if isinstance(out, tuple) else out
def _adapted(model, vector, operation, hidden, tmp_path):
config = edits.adapter_config("0", operation, operation["decision_coefficient"])
edits.save_activation_adapter(tmp_path / "adapter", vector, config)
original = model[0]
wrapper = edits.attach_activation_adapter(model, tmp_path / "adapter")
assert model[0] is wrapper and not model[0]._forward_hooks
out = model(hidden)
edits.detach_adapter(model, "0")
assert model[0] is original
return out[0] if isinstance(out, tuple) else out
@pytest.mark.parametrize("operation,rank", [
(_op(), 1), (_op(scope="last_token"), 1), (_op(units="residual_norm"), 1),
(_op("ablate", units="projection_fraction", coefficient=0.75), 1),
(_op("ablate", "last_token", "projection_fraction", 1.0), 2),
(_op("ablate", units="projection_fraction", coefficient=1.0), 2)])
def test_activation_adapter_matches_the_hook(operation, rank, tmp_path):
model = _model(Tupled() if rank == 2 else nn.Linear(WIDTH, WIDTH))
vector = torch.randn(WIDTH) * 3 if rank == 1 else torch.linalg.qr(
torch.randn(WIDTH, 2))[0].T.contiguous()
hidden = torch.randn(2, 4, WIDTH)
expected = _hooked(model, vector, operation, hidden)
found = _adapted(model, vector, operation, hidden, tmp_path)
assert torch.equal(found, expected)
assert not torch.equal(found, model(hidden)[0] if rank == 2 else model(hidden))
def test_adapter_refuses_unknown_files_and_shapes(tmp_path):
model = _model(nn.Linear(WIDTH, WIDTH))
(tmp_path / "bad").mkdir()
(tmp_path / "bad" / edits.ADAPTER_CONFIG).write_text(json.dumps({"schema": "other"}))
with pytest.raises(edits.CandidateRefused, match="not an activation-edit adapter"):
edits.attach_activation_adapter(model, tmp_path / "bad")
with pytest.raises(edits.CandidateRefused, match="carries no activation adapter"):
edits.detach_adapter(model, "0")
with pytest.raises(edits.CandidateRefused, match="no module"):
edits.detach_adapter(model, "missing")
wrapper = edits.ActivationEditAdapter(lambda x: x.sum(), torch.ones(WIDTH),
{"coefficient": 1.0})
with pytest.raises(edits.CandidateRefused, match="rank-2 or rank-3"):
wrapper(torch.ones(2, WIDTH))
zero = edits.edit_hidden(torch.ones(1, 2, WIDTH), torch.zeros(WIDTH),
{"coefficient": 1.0, "kind": "steer", "units": "raw",
"token_scope": "all_tokens"})
assert torch.equal(zero, torch.ones(1, 2, WIDTH))
@pytest.mark.parametrize("operation,rank", [
(_op("ablate", units="projection_fraction", coefficient=1.0), 2),
(_op("ablate", units="projection_fraction", coefficient=0.5), 1),
(_op(), 1)])
def test_weight_edit_and_lora_match_the_all_token_hook(operation, rank, tmp_path):
layer = nn.Linear(WIDTH, WIDTH)
model = _model(layer)
vector = torch.randn(WIDTH) if rank == 1 else torch.linalg.qr(
torch.randn(WIDTH, 2))[0].T.contiguous()
hidden = torch.randn(2, 3, WIDTH)
expected = _hooked(model, vector, operation, hidden)
coefficient = operation["decision_coefficient"]
assert all(c["passed"] for c in edits.compatibility("weight_edit", operation, layer, WIDTH))
tensors = edits.weight_edit(layer, operation, vector, coefficient)
edited = _model(nn.Linear(WIDTH, WIDTH))
edited.load_state_dict(model.state_dict())
changed = edits.apply_weight_edit(edited, "0", tensors)
assert changed == (["0.bias"] if operation["kind"] == "steer" else ["0.weight", "0.bias"])
torch.testing.assert_close(edited(hidden), expected, atol=1e-5, rtol=1e-5)
if operation["kind"] == "ablate":
# LoRA leaves the bias alone: refused while the bias has a component in the span.
assert not edits.bias_check(layer, vector)["passed"]
with torch.no_grad():
rows = edits._rows(vector).float()
layer.bias -= rows.T @ (rows @ layer.bias)
assert edits.bias_check(layer, vector)["passed"]
expected = _hooked(model, vector, operation, hidden)
pairs = edits.lora_pairs("0", layer, vector, coefficient)
edits.save_lora(tmp_path / "lora", pairs, "local/tiny", None)
adapters = edits.attach_lora(model, tmp_path / "lora")
torch.testing.assert_close(model(hidden), expected, atol=1e-5, rtol=1e-5)
adapters.remove()
assert edits.elements("lora", layer, vector) == rank * 2 * WIDTH
assert edits.elements("activation_adapter", layer, vector) == rank * WIDTH
assert edits.elements("weight_edit", layer, vector) == WIDTH * WIDTH + WIDTH
def test_compatibility_refuses_unexpressible_forms():
class Shifted(nn.Linear):
def forward(self, hidden):
return super().forward(hidden.cumsum(1))
linear, plain = Shifted(WIDTH, WIDTH), nn.Linear(WIDTH, WIDTH, bias=False)
def failed(name, operation, module=linear):
return {c["check"] for c in edits.compatibility(name, operation, module, WIDTH)
if not c["passed"]}
projection = _op("ablate", units="projection_fraction", coefficient=1.0)
assert failed("lora", projection) == {"plain_linear_forward"}
assert failed("lora", _op()) == {"plain_linear_forward", "projection_operation"}
assert failed("weight_edit", _op(scope="last_token")) == {"all_token_scope"}
assert failed("weight_edit", _op(units="residual_norm")) == {"expressible_in_weights"}
assert failed("weight_edit", _op(), plain) == {"bias_present"}
assert failed("weight_edit", projection, Tupled()) == {"site_module_is_output_linear"}
assert failed("activation_adapter", _op(scope="last_token"), Tupled()) == set()
assert edits.bias_check(plain, torch.ones(WIDTH))["passed"]
with pytest.raises(edits.CandidateRefused, match="Unknown representation"):
edits.compatibility("merged", projection, plain, WIDTH)
def test_transformers_io_round_trip_and_write_once(tmp_path):
from tokenizers import Tokenizer, models, pre_tokenizers
from transformers import GPT2Config, GPT2LMHeadModel, PreTrainedTokenizerFast
words = {"<unk>": 0, "a": 1, "b": 2}
core = Tokenizer(models.WordLevel(words, unk_token="<unk>"))
core.pre_tokenizer = pre_tokenizers.Whitespace()
tokenizer = PreTrainedTokenizerFast(tokenizer_object=core, unk_token="<unk>")
torch.manual_seed(0)
model = GPT2LMHeadModel(GPT2Config(vocab_size=3, n_positions=8, n_embd=8, n_layer=1,
n_head=2)).eval()
io = cio.TransformersIO()
io.save(model, tokenizer, tmp_path / "saved")
loaded = io.load(tmp_path / "saved", {"dtype": "float32"})
ids = torch.tensor([[1, 2, 1]])
assert torch.equal(loaded.model(ids).logits, model(ids).logits)
assert loaded.tokenizer.get_vocab() == tokenizer.get_vocab()
files = cio.tree_digests(tmp_path / "saved")
assert "model.safetensors" in files and cio.tree_size(tmp_path / "saved") > 0
single = cio.tree_digests(tmp_path / "saved" / "config.json")
assert list(single) == ["config.json"]
with pytest.raises(FileNotFoundError):
cio.tree_digests(tmp_path / "absent")
assert cio.tree_sha256(files) != cio.tree_sha256({**files, "x": "0"})
assert cio.overlaps(tmp_path, tmp_path / "saved") and not cio.overlaps(
tmp_path / "saved", tmp_path / "other")
staging = cio.staging_dir(tmp_path / "store")
with pytest.raises(FileExistsError, match="written once"):
cio.promote(staging, tmp_path / "saved")
assert cio.promote(staging, tmp_path / "store" / "one").is_dir()
assert SimpleNamespace(io=io).io.name == "transformers"