Files
OBLITERATUS/tests/test_concept_preservation.py

842 lines
42 KiB
Python

"""Protected residual spans across hooks, writers, adapters and replay."""
from copy import deepcopy
from dataclasses import replace
import json
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
from safetensors.torch import load_file, save_file
import torch
from obliteratus.concepts import apply_concept_intervention
from obliteratus.concepts import geometry as geometry_module, intervention as intervention_module
from obliteratus.concepts.export import export_run
from obliteratus.concepts.geometry import (
ErasureGeometry, ErasureHookManager, project_writer, protected_geometry, tensor_hash,
)
from obliteratus.concepts.runs import load_preserved_spans, save_concept_run
from obliteratus.models.offload_surgery import resolve_logical_parameter
from obliteratus.models.post_norms import post_norm_scales
from tests.test_concept_affine_intervention import affine_model as affine_model, direction
from tests.test_concept_intervention import _tiny_residual_model, logits, residual
pytestmark = [pytest.mark.cpu, pytest.mark.integration, pytest.mark.usefixtures("tiny_model_runtime")]
def _target(rank):
artifact = direction(rank)
first = torch.eye(16)[0] + torch.eye(16)[1]
artifact.directions = {0: first / first.norm()}
artifact.subspaces = {0: torch.stack([artifact.directions[0], torch.eye(16)[2]])[:rank]}
return artifact
@pytest.mark.parametrize("mode", ["orthogonalize", "lora"])
def test_embedding_projection_requires_explicit_layer_for_protected_geometry(mode):
model = _tiny_residual_model("gemma2", tied=True)
before = {name: value.clone() for name, value in model.state_dict().items()}
protected = {0: torch.eye(16)[0], 1: torch.eye(16)[1]}
with pytest.raises(ValueError, match="unambiguous subspace.*embedding_geometry_layer"):
apply_concept_intervention(
model, _target(1), mode, layers="all", subspace=True, preserve=[protected],
project_embeddings=True, tied_embeddings="edit_both",
)
assert all(torch.equal(value, before[name]) for name, value in model.state_dict().items())
assert all(not module._forward_hooks for module in model.modules())
@pytest.mark.parametrize("mode", ["orthogonalize", "lora"])
@pytest.mark.parametrize("choice", [None, "edit_both", "untie"])
@pytest.mark.parametrize("layer", [0, 1])
def test_embedding_geometry_layer_removes_residualized_target_and_preserves_span(mode, choice, layer):
model = _tiny_residual_model("gemma2", tied=choice is not None)
protected = {0: torch.eye(16)[0], 1: torch.eye(16)[1]}
embedding, head = model.get_input_embeddings(), model.get_output_embeddings()
before = embedding.weight.detach().clone()
original_head = head.weight.detach().clone()
with apply_concept_intervention(
model, _target(1), mode, layers=[1, 0], subspace=True, preserve=[protected],
project_embeddings=True, tied_embeddings=choice, embedding_geometry_layer=layer,
) as run:
actual, output = embedding.weight.detach(), head.weight.detach()
if mode == "lora":
b, a = run.manager.adapters["model.embed_tokens"]
actual = actual + (b @ a).T
if choice == "edit_both":
b, a = run.manager.adapters["lm_head"]
output = output + b @ a
target = _target(1).directions[0]
target = target - protected[layer] * (target @ protected[layer])
target /= target.norm()
torch.testing.assert_close(actual @ target, torch.zeros(actual.shape[0]), atol=2e-7, rtol=0)
torch.testing.assert_close(actual @ protected[layer], before @ protected[layer])
torch.testing.assert_close(output, actual if choice == "edit_both" else original_head)
assert run.parameters["embedding_geometry_layer"] == layer
assert run.parameters["geometry"]["embedding_geometry_layer"] == layer
for index, block in enumerate(model.model.layers):
writer = block.self_attn.o_proj
work = writer.weight.detach()
if mode == "lora":
b, a = run.manager.adapters[f"model.layers.{index}.self_attn.o_proj"]
work = work + b @ a
scale = post_norm_scales(model, block, 16)["attention"]
assert (work.T * scale)[:, 1 - index].abs().max() < 2e-7
@pytest.mark.parametrize("layer", [-1, 0, 2, True, 1.0, "1"])
def test_invalid_embedding_geometry_layer_fails_before_mutation(layer):
model = _tiny_residual_model("gemma2", tied=True)
before = {name: value.clone() for name, value in model.state_dict().items()}
with pytest.raises(ValueError, match="embedding_geometry_layer must be one of the selected layers"):
apply_concept_intervention(
model, _target(1), "orthogonalize", layers=[1], project_embeddings=True,
tied_embeddings="untie", embedding_geometry_layer=layer,
)
assert all(torch.equal(value, before[name]) for name, value in model.state_dict().items())
assert model.get_input_embeddings().weight is model.get_output_embeddings().weight
def test_saved_embedding_geometry_layer_exports_and_replays(tmp_path, monkeypatch):
model = _tiny_residual_model("gemma2", tied=True)
pristine = deepcopy(model)
artifact = _target(1)
artifact.directions[1] = (torch.eye(16)[0] + torch.eye(16)[2]) / 2**0.5
run = apply_concept_intervention(
model, artifact, "orthogonalize", layers="all",
preserve=[{0: torch.eye(16)[0], 1: torch.eye(16)[2]}],
project_embeddings=True, tied_embeddings="edit_both", embedding_geometry_layer=1,
)
source, output = tmp_path / "run", tmp_path / "bundle"
record = save_concept_run(source, artifact, run, tool_commit="b" * 40)
exported = export_run(source, output, kind="direction")
assert exported["run"] == record
parameters = exported["run"]["intervention"]["parameters"]
assert parameters["embedding_geometry_layer"] == 1
assert parameters["geometry"]["embedding_geometry_layer"] == 1
monkeypatch.chdir(output)
namespace = {"model": pristine}
exec((output / "loader.py").read_text(), namespace)
replayed = namespace["run"].parameters
assert replayed["embedding_geometry_layer"] == 1
assert replayed["geometry"] == run.parameters["geometry"]
for name, value in model.state_dict().items():
torch.testing.assert_close(pristine.state_dict()[name], value)
@pytest.mark.parametrize("architecture", ["llama", "gemma2"])
@pytest.mark.parametrize("mode", ["orthogonalize", "lora"])
@pytest.mark.parametrize("rank", [1, 2])
def test_writer_updates_preserve_protected_output(architecture, mode, rank):
model = _tiny_residual_model(architecture)
artifact = _target(rank)
protected = torch.eye(16)[0]
layer = model.model.layers[0]
scales = post_norm_scales(model, layer, 16)
writers = {"attention": layer.self_attn.o_proj, "ffn": layer.mlp.down_proj}
before = {kind: writer.weight.detach().clone() for kind, writer in writers.items()}
pristine = logits(model)
with apply_concept_intervention(model, artifact, mode, subspace=rank > 1, preserve=[protected]) as run:
for kind, writer in writers.items():
work = writer.weight.detach().T
if mode == "lora":
name = next(name for name, module in model.named_modules() if module is writer)
b, a = run.manager.adapters[name]
work = work + (b @ a).T
scale = scales.get(kind, 1)
actual, original = work * scale, before[kind].T * scale
torch.testing.assert_close(actual @ protected, original @ protected, atol=2e-7, rtol=2e-6)
torch.testing.assert_close(actual[:, 1], torch.zeros_like(actual[:, 1]), atol=2e-7, rtol=0)
if rank == 2:
torch.testing.assert_close(actual[:, 2], torch.zeros_like(actual[:, 2]), atol=2e-7, rtol=0)
metadata = run.parameters["preserve"]["0"]
assert metadata["original_rank"] == metadata["rank"] == rank
assert metadata["protected_rank"] == 1
assert metadata["principal_cosines"] == pytest.approx([2 ** -0.5])
assert metadata["removed_norm_fraction"] == pytest.approx((1 - 0.5 / rank) ** 0.5)
assert metadata["overlap_norm_fraction"] == pytest.approx((0.5 / rank) ** 0.5)
if mode == "lora":
assert torch.equal(logits(model), pristine)
@pytest.mark.parametrize("positions", ["all_tokens", "final_token"])
@pytest.mark.parametrize("reference", ["zero", "mean"])
def test_project_hook_preserves_span_at_selected_positions(affine_model, positions, reference):
artifact = _target(2)
before = residual(affine_model)
expected = before.clone()
selected = expected if positions == "all_tokens" else expected[:, -1:]
selected[..., 1:3] = 0 if reference == "zero" else artifact.reference_means[0][1:3]
with apply_concept_intervention(
affine_model, artifact, "project", subspace=True, preserve=[torch.eye(16)[0]],
positions=positions, reference=reference,
) as run:
torch.testing.assert_close(residual(affine_model), expected)
run.alpha = 0
assert torch.equal(residual(affine_model), before)
assert torch.equal(residual(affine_model), before)
def test_oblique_affine_projection_is_idempotent_and_preserves_both_sides(affine_model):
artifact = _target(1)
artifact.recipe = replace(artifact.recipe, method="leace_exact")
left = artifact.directions[0][:, None]
right = left.clone()
right[2, 0] = 0.5
artifact.projector_left, artifact.projector_right = {0: left}, {0: right}
before = residual(affine_model)
expected = before.clone()
centered = before - artifact.reference_means[0]
expected[..., 1] -= centered[..., 1] + 2 ** -0.5 * centered[..., 2]
with apply_concept_intervention(
affine_model, artifact, "project", reference="mean", preserve=[torch.eye(16)[0]],
) as run:
torch.testing.assert_close(residual(affine_model), expected)
geometry = run.manager.geometries[0]
projection = geometry.project(before)
torch.testing.assert_close(geometry.project(projection), projection)
assert geometry.project(torch.eye(16)[0]).abs().max() < 1e-7
assert projection[..., 0].abs().max() < 1e-7
assert run.parameters["geometry"]["0"]["projector_type"] == "protected_leace_oblique"
@pytest.mark.parametrize("dtype", [torch.float16, torch.float32])
def test_protected_span_orthogonalization_keeps_fp32_under_autocast(dtype):
basis = torch.tensor([[1.], [2.], [3.]], dtype=dtype)
basis = basis.float() / basis.float().norm()
protected = torch.tensor([2., -1., 1.], dtype=dtype)
geometry = ErasureGeometry(basis, basis)
with torch.autocast("cpu", dtype=torch.bfloat16):
_, metadata, changed = protected_geometry({0: geometry}, [protected])
assert changed and metadata["0"]["rank"] == 1
assert geometry.left.dtype == geometry.right.dtype == torch.float32
assert geometry.protected_basis.dtype == torch.float32
torch.testing.assert_close(geometry.left.T @ geometry.protected_basis,
torch.zeros(1, 1), atol=2e-7, rtol=0)
torch.testing.assert_close(geometry.left.T @ geometry.left, torch.ones(1, 1))
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32, torch.float64])
@pytest.mark.parametrize("row_basis", [False, True])
def test_ordinary_protected_rows_match_base_geometry_exactly(monkeypatch, dtype, row_basis):
rescale = Mock(side_effect=AssertionError("Ordinary rows must use the base formula"))
monkeypatch.setattr(geometry_module, "_rescale_protected_rows", rescale)
protected = torch.tensor([[1., 3., 0.], [0.62, -0.73, 2.17]], dtype=dtype)
if not row_basis:
protected = protected[:1]
work = protected.to(torch.float64 if dtype == torch.float64 else torch.float32)
expected_rows = work / work.norm(dim=1, keepdim=True)
svd = torch.linalg.svd
_, singular, vh = svd(expected_rows, full_matrices=False)
tolerance = max(expected_rows.shape) * torch.finfo(work.dtype).eps
expected_basis = vh[singular > tolerance * singular[0]].T.contiguous()
basis = torch.tensor([[1.], [0.], [0.]], dtype=work.dtype)
q = torch.linalg.qr(basis, mode="reduced").Q
overlap = expected_basis.T @ q
residual = q - expected_basis @ overlap
u, s, _ = svd(residual, full_matrices=False)
expected_left = u[:, s > max(residual.shape) * torch.finfo(work.dtype).eps]
captured = []
def capture_rows(value, *args, **kwargs):
if not captured:
captured.append(value.clone())
return svd(value, *args, **kwargs)
monkeypatch.setattr(torch.linalg, "svd", capture_rows)
geometry = ErasureGeometry(basis, basis)
spans, metadata, changed = protected_geometry({0: geometry}, [protected])
assert changed
assert torch.equal(captured[0], expected_rows)
assert torch.equal(spans[0], expected_basis.T)
assert torch.equal(geometry.left, expected_left)
assert torch.equal(geometry.right, expected_left)
assert metadata["0"]["protected_sha256"] == tensor_hash(expected_basis.T)
assert metadata["0"]["principal_cosines"] == torch.linalg.svdvals(overlap).clamp(0, 1).tolist()
probe = torch.tensor([[0.123, -0.73, 2.17]], dtype=work.dtype)
assert torch.equal(geometry.project(probe), (probe @ expected_left) @ expected_left.T)
rescale.assert_not_called()
@pytest.mark.parametrize("dtype", [torch.float32, torch.float64])
@pytest.mark.parametrize("arm", ["refusal", "union"])
def test_d4_protected_orthogonalize_matches_base_exactly(monkeypatch, dtype, arm):
model = _tiny_residual_model("llama")
artifact = direction(1)
artifact.directions = {0: torch.linspace(-0.7, 1.1, 16, dtype=dtype)}
artifact.subspaces = {}
rows = torch.nn.functional.pad(torch.tensor([
[1., 3., 0., 0.62], [0.62, -0.73, 2.17, 0.09], [0.123, 0.62, -1.13, 0.004],
], dtype=dtype), (0, 12))
union = []
for index, row in enumerate(rows):
concept = direction(1)
concept.concept_id = f"fixture/protected-{index}"
concept.directions, concept.subspaces = {0: row}, {}
union.append(concept)
refusal = union[0]
preserve = [refusal] if arm == "refusal" else union
# upstream/main normalizes each source before concatenation, SVD and residualization.
expected_rows = torch.cat([
value / value.norm(dim=1, keepdim=True)
for value in (item.directions[0].reshape(1, -1) for item in preserve)
])
svd = torch.linalg.svd
_, singular, vh = svd(expected_rows, full_matrices=False)
tolerance = max(expected_rows.shape) * torch.finfo(dtype).eps
protected = vh[singular > tolerance * singular[0]].T.contiguous()
vector = artifact.directions[0]
basis = torch.linalg.qr((vector / vector.norm())[:, None], mode="reduced").Q
overlap = protected.T @ basis
residual = basis - protected @ overlap
u, s, _ = svd(residual, full_matrices=False)
expected_left = u[:, s > max(residual.shape) * torch.finfo(dtype).eps]
writers = (model.model.layers[0].self_attn.o_proj, model.model.layers[0].mlp.down_proj)
before = [writer.weight.detach().clone() for writer in writers]
captured_rows, captured_geometry = [], {}
def capture_rows(value, *args, **kwargs):
if not captured_rows:
captured_rows.append(value.clone())
return svd(value, *args, **kwargs)
def capture_geometry(geometries, preserve):
result = protected_geometry(geometries, preserve)
captured_geometry.update(geometries)
return result
rescale = Mock(side_effect=AssertionError("D4 ordinary rows must not be rescaled"))
monkeypatch.setattr(geometry_module, "_rescale_protected_rows", rescale)
monkeypatch.setattr(torch.linalg, "svd", capture_rows)
monkeypatch.setattr(intervention_module, "protected_geometry", capture_geometry)
with apply_concept_intervention(model, artifact, "orthogonalize", preserve=preserve) as run:
assert torch.equal(captured_rows[0], expected_rows)
assert torch.equal(run.preserved_spans[0], protected.T)
geometry = captured_geometry[0]
assert torch.equal(geometry.protected_basis, protected)
assert torch.equal(geometry.left, expected_left)
assert torch.equal(geometry.right, expected_left)
assert run.parameters["preserve"]["0"]["protected_sha256"] == tensor_hash(protected.T)
for writer, original in zip(writers, before):
work = original.T.to(dtype)
expected = work - (work @ expected_left) @ expected_left.T
assert torch.equal(writer.weight, expected.to(original.dtype).T)
rescale.assert_not_called()
@pytest.mark.parametrize("dtype", [torch.float32, torch.float64])
@pytest.mark.filterwarnings("error")
def test_protected_row_norm_uses_vector_kernel_without_warning(monkeypatch, dtype):
deprecated = Mock(side_effect=AssertionError("Row norms must not call frobenius_norm"))
monkeypatch.setattr(torch._VF, "frobenius_norm", deprecated)
value = torch.tensor([[1., 3., 0.62], [0.123, -0.73, 2.17]], dtype=dtype)
assert torch.equal(value.norm(dim=1, keepdim=True),
torch.linalg.vector_norm(value, dim=1, keepdim=True))
deprecated.assert_not_called()
@pytest.mark.parametrize("dtype", [torch.float32, torch.float64])
@pytest.mark.parametrize("value", [0., 1e-9, 5e-9, 1e-8, 2e-8])
def test_finite_tiny_protected_rows_keep_base_rejection(monkeypatch, dtype, value):
rescale = Mock(side_effect=AssertionError("Finite tiny norms must keep the base threshold"))
monkeypatch.setattr(geometry_module, "_rescale_protected_rows", rescale)
protected = torch.tensor([[value, 0.]], dtype=dtype)
basis = torch.tensor([[0.], [1.]], dtype=dtype)
geometry = ErasureGeometry(basis, basis)
if (protected.norm(dim=1) < 1e-8).any():
with pytest.raises(ValueError, match="Protected directions must be nonzero"):
protected_geometry({0: geometry}, [protected])
assert torch.equal(geometry.left, basis)
assert geometry.protected_basis is None
else:
spans, _, changed = protected_geometry({0: geometry}, [protected])
assert not changed
assert torch.equal(spans[0], protected / protected.norm(dim=1, keepdim=True))
rescale.assert_not_called()
@pytest.mark.parametrize("value", [1e30, 1e-30])
def test_protected_row_fallback_keeps_neighboring_base_normalization(monkeypatch, value):
rescale = Mock(wraps=geometry_module._rescale_protected_rows)
monkeypatch.setattr(geometry_module, "_rescale_protected_rows", rescale)
protected = torch.tensor([[1., 3., 0.], [0., 0., value]])
before = protected.clone()
expected_rows = torch.cat([
protected[:1] / protected[:1].norm(dim=1, keepdim=True),
torch.tensor([[0., 0., 1.]]),
])
svd = torch.linalg.svd
captured = []
def capture_rows(value, *args, **kwargs):
if not captured:
captured.append(value.clone())
return svd(value, *args, **kwargs)
monkeypatch.setattr(torch.linalg, "svd", capture_rows)
basis = torch.tensor([[1.], [0.], [0.]])
protected_geometry({0: ErasureGeometry(basis, basis)}, [protected])
rescale.assert_called_once()
assert torch.equal(rescale.call_args.args[0], before[1:])
assert torch.equal(captured[0], expected_rows)
assert torch.equal(protected, before)
@pytest.mark.parametrize("value,dtype", [
(1e30, torch.float32), (1e300, torch.float64),
(1e-30, torch.float32), (1e-300, torch.float64),
])
@pytest.mark.parametrize("row_basis", [False, True])
def test_extreme_protected_rows_retain_their_span_and_identity(monkeypatch, value, dtype, row_basis):
rescale = Mock(wraps=geometry_module._rescale_protected_rows)
monkeypatch.setattr(geometry_module, "_rescale_protected_rows", rescale)
basis = torch.tensor([[1.], [0.]], dtype=dtype)
geometry = ErasureGeometry(basis, basis)
protected = torch.tensor([[value, 0.], [0., 2.]] if row_basis else [value, 0.], dtype=dtype)
before = protected.clone()
spans, metadata, changed = protected_geometry({0: geometry}, [protected])
rescale.assert_called_once()
assert changed and geometry.rank == 0
assert metadata["0"]["protected_rank"] == (2 if row_basis else 1)
assert metadata["0"]["removed_norm_fraction"] == 0
assert torch.isfinite(spans[0]).all()
torch.testing.assert_close(torch.linalg.vector_norm(spans[0], dim=1),
torch.ones(spans[0].shape[0], dtype=dtype))
hidden = torch.tensor([[[3., 4.]]], dtype=dtype)
config = SimpleNamespace(alpha=1., per_layer_alpha={}, position="all")
assert ErasureHookManager({0: geometry})._make_hook(config, 0)(None, (), hidden) is hidden
assert torch.equal(protected, before)
@pytest.mark.parametrize("full_overlap", [False, True])
def test_protected_affine_hook_promotes_centering_and_keeps_zero_rank_identity(full_overlap):
basis = torch.tensor([[1.], [1.], [0.]]) / 2**0.5
geometry = ErasureGeometry(basis, basis, torch.tensor([0., 70000., 0.]))
config = SimpleNamespace(alpha=0.5, per_layer_alpha={}, position="all")
hidden = torch.tensor([[[1., 60000., 3.]]], dtype=torch.float16)
with torch.autocast("cpu", dtype=torch.bfloat16):
protected_geometry({0: geometry}, [basis.T if full_overlap else torch.eye(3)[0]])
actual = ErasureHookManager({0: geometry})._make_hook(config, 0)(None, (), hidden)
if full_overlap:
assert actual is hidden
else:
assert actual.dtype == hidden.dtype
torch.testing.assert_close(actual, torch.tensor([[[1., 65000., 3.]]], dtype=hidden.dtype))
@pytest.mark.parametrize("mode", ["project", "steer", "orthogonalize", "lora"])
@pytest.mark.parametrize("preserve", [None, [], "orthogonal"])
def test_no_or_orthogonal_protection_keeps_legacy_bits(affine_model, mode, preserve):
legacy, protected = deepcopy(affine_model), deepcopy(affine_model)
artifact = direction(1)
options = {
"preserve": [torch.eye(16)[4]] if preserve == "orthogonal" else preserve,
}
first = apply_concept_intervention(legacy, artifact, mode)
second = apply_concept_intervention(protected, artifact, mode, **options)
try:
assert torch.equal(logits(legacy), logits(protected))
assert all(torch.equal(value, protected.state_dict()[name])
for name, value in legacy.state_dict().items())
if preserve != "orthogonal":
assert first.parameters == second.parameters
finally:
if mode != "orthogonalize":
first.remove()
second.remove()
@pytest.mark.parametrize("mode", ["steer", "project"])
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16])
@pytest.mark.parametrize("preserve", [None, [], "orthogonal"])
def test_unchanged_protection_uses_legacy_hook_arithmetic(affine_model, mode, dtype, preserve):
artifact = direction(1)
artifact.directions[0][:2] = torch.tensor([1., 6.])
protection = [torch.eye(16)[4]] if preserve == "orthogonal" else preserve
hidden = torch.ones(1, 2, 16, dtype=dtype)
with apply_concept_intervention(affine_model, artifact, mode, preserve=protection) as run:
vector = run.config.per_layer_vectors[0][0].direction.to(dtype)
unit = vector / vector.norm().clamp(min=1e-10)
expected = (hidden - (hidden @ unit).unsqueeze(-1) * unit if mode == "project"
else hidden - hidden.norm(dim=-1, keepdim=True) * unit)
hook = run.manager._make_hook(run.config, 0)
assert torch.equal(hook(None, (), hidden), expected)
@pytest.mark.parametrize("mode,reference", [
("project", "zero"), ("project", "mean"), ("orthogonalize", "zero"),
("orthogonalize", "mean"), ("lora", "zero"),
])
@pytest.mark.parametrize("preserve", [None, []])
def test_rank_k_and_affine_paths_keep_bits_without_protection(affine_model, mode, reference, preserve):
legacy, selected = deepcopy(affine_model), deepcopy(affine_model)
artifact = direction(2)
first = apply_concept_intervention(legacy, artifact, mode, subspace=True, reference=reference)
second = apply_concept_intervention(
selected, artifact, mode, subspace=True, reference=reference, preserve=preserve,
)
try:
assert torch.equal(logits(legacy), logits(selected))
assert first.parameters == second.parameters
assert all(torch.equal(value, selected.state_dict()[name])
for name, value in legacy.state_dict().items())
finally:
if mode != "orthogonalize":
first.remove()
second.remove()
@pytest.mark.parametrize("mode", ["project", "steer", "orthogonalize", "lora"])
def test_fully_overlapping_target_is_identity_with_zero_remaining_rank(affine_model, mode):
artifact = direction(1)
before = logits(affine_model)
weights = {name: value.clone() for name, value in affine_model.state_dict().items()}
with apply_concept_intervention(affine_model, artifact, mode, preserve=[artifact]) as run:
assert torch.equal(logits(affine_model), before)
assert all(torch.equal(value, weights[name]) for name, value in affine_model.state_dict().items())
metadata = run.parameters["preserve"]["0"]
assert metadata["rank"] == 0
assert metadata["removed_norm_fraction"] == 0
assert metadata["principal_cosines"] == pytest.approx([1])
@pytest.mark.parametrize("autocast", [False, True])
def test_steering_retains_target_orientation_after_residualization(affine_model, autocast):
artifact = _target(1)
before = residual(affine_model)
with torch.autocast("cpu", dtype=torch.bfloat16, enabled=autocast):
run = apply_concept_intervention(
affine_model, artifact, "steer", alpha=0.25, preserve=[torch.eye(16)[0]],
)
with run:
vector = run.config.per_layer_vectors[0][0].direction
torch.testing.assert_close(vector, torch.eye(16)[1], atol=2e-7, rtol=0)
after = residual(affine_model)
torch.testing.assert_close(after[..., 0], before[..., 0])
assert (after[..., 1] > before[..., 1]).all()
torch.testing.assert_close(after[..., 2:], before[..., 2:])
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32])
@pytest.mark.parametrize("positions", ["all_tokens", "final_token"])
def test_fully_overlapping_steering_is_finite_identity(affine_model, dtype, positions):
affine_model.to(dtype=dtype)
artifact = direction(1)
before = logits(affine_model)
with apply_concept_intervention(
affine_model, artifact, "steer", preserve=[artifact], positions=positions,
) as run:
after = logits(affine_model)
assert torch.isfinite(after).all()
assert torch.equal(after, before)
assert run.parameters["preserve"]["0"]["rank"] == 0
@pytest.mark.parametrize("offloaded", [False, True])
@pytest.mark.parametrize("fraction", [0.1, 1.0])
def test_chunked_folded_writer_preserves_span_with_norm_preservation(monkeypatch, offloaded, fraction):
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
model = _tiny_residual_model("gemma2")
layer = model.model.layers[0]
scales = post_norm_scales(model, layer, 16)
writers = {"attention": layer.self_attn.o_proj, "ffn": layer.mlp.down_proj}
before = {kind: writer.weight.detach().clone().T * scales[kind] for kind, writer in writers.items()}
if offloaded:
for writer in writers.values():
add_hook_to_module(writer, AlignDevicesHook(execution_device="cpu", offload=True))
monkeypatch.setattr("obliteratus.concepts.intervention._PROJECTION_CHUNK_BYTES", 128)
apply_concept_intervention(
model, _target(2), "orthogonalize", subspace=True,
preserve=[torch.eye(16)[0]], norm_preserve=True, projection_row_fraction=fraction,
)
for kind, writer in writers.items():
actual = resolve_logical_parameter(writer, search_roots=(model,)).tensor.T * scales[kind]
expected = before[kind].clone()
keep = (len(expected) + 9) // 10 if fraction == 0.1 else len(expected)
for column in (1, 2):
indices = expected[:, column].abs().argsort(descending=True)[:keep]
expected[indices, column] = 0
ratio = min((before[kind][:, 1:].square().sum() / expected[:, 1:].square().sum()).sqrt().item(), 1.10)
expected[:, 1:] *= ratio
torch.testing.assert_close(actual, expected, atol=2e-7, rtol=2e-6)
torch.testing.assert_close(actual[:, 0], before[kind][:, 0])
assert actual[:, 3:].norm() >= before[kind][:, 3:].norm() - 1e-7
@pytest.mark.parametrize("chunk_bytes", [1, 96, 4096])
def test_norm_preserving_writer_only_rescales_unprotected_complement(chunk_bytes):
weight = torch.arange(28, dtype=torch.float64).reshape(7, 4) / 32
protected = torch.eye(4, dtype=torch.float64)[:, :1]
basis = torch.eye(4, dtype=torch.float64)[:, 1:2]
geometry = ErasureGeometry(basis, basis, protected_basis=protected)
before = weight.clone()
expected = before.clone()
expected[:, 1] = 0
ratio = min((before[:, 1:].square().sum() / expected[:, 1:].square().sum()).sqrt().item(), 1.10)
expected[:, 1:] *= ratio
result = project_writer(
"writer", (SimpleNamespace(tensor=weight), weight, basis[:, 0], False),
None, geometry, norm_preserve=True, regularization=0, chunk_bytes=chunk_bytes,
)
torch.testing.assert_close(result, expected, rtol=1e-12, atol=1e-12)
assert torch.equal(result[:, 0], before[:, 0])
assert torch.equal(weight, before)
@pytest.mark.parametrize("chunk_bytes", [1, 96, 4096])
@pytest.mark.parametrize("fraction", [0.1, 0.25, 1.0])
@pytest.mark.parametrize("rank", [1, 2])
@pytest.mark.parametrize("norm_preserve", [False, True])
@pytest.mark.parametrize("layout,folded", [("standard", False), ("transposed", True), ("fused", True)])
def test_selective_writer_uses_global_coefficients_per_direction_and_expert(
chunk_bytes, fraction, rank, norm_preserve, layout, folded,
):
coordinate = torch.arange(1, 11, dtype=torch.float64)
work = torch.stack([coordinate + 10, coordinate, coordinate.flip(0) * 2, coordinate + 20], dim=1)
if layout == "fused":
work = torch.stack([work, work.flip(0) * 2])
scale = torch.tensor([2., 4., 8., 16.], dtype=torch.float64) if folded else None
expected = work.clone()
keep = {0.1: 1, 0.25: 3, 1.0: 10}[fraction]
for original, changed in (zip(work, expected) if layout == "fused" else [(work, expected)]):
for column in range(1, rank + 1):
indices = original[:, column].abs().argsort(descending=True)[:keep]
changed[indices, column] *= 0.2
if norm_preserve:
ratio = min((original[:, 1:].square().sum() / changed[:, 1:].square().sum()).sqrt().item(), 1.10)
changed[:, 1:] *= ratio
work = work if scale is None else work / scale
transpose = layout != "standard"
weight = work.transpose(-1, -2) if transpose else work
before = weight.clone()
basis = torch.eye(4, dtype=torch.float64)[:, 1:rank + 1]
geometry = ErasureGeometry(basis, basis, protected_basis=torch.eye(4, dtype=torch.float64)[:, :1])
result = project_writer(
"writer", (SimpleNamespace(tensor=weight), work, basis[:, 0], transpose),
scale, geometry, norm_preserve=norm_preserve, regularization=0.2,
chunk_bytes=chunk_bytes, projection_row_fraction=fraction,
)
actual = result.transpose(-1, -2) if transpose else result
actual = actual if scale is None else actual * scale
torch.testing.assert_close(actual, expected, rtol=1e-12, atol=1e-12)
assert torch.equal(weight, before)
@pytest.mark.parametrize("architecture", ["llama", "gemma2"])
@pytest.mark.parametrize("mode", ["orthogonalize", "lora"])
@pytest.mark.parametrize("protection", ["overlap", "orthogonal", "full", "none"])
def test_selective_protected_intervention_keeps_unselected_writer_coefficients(architecture, mode, protection):
model = _tiny_residual_model(architecture)
artifact = _target(1)
preserve = {"overlap": [torch.eye(16)[0]], "orthogonal": [torch.eye(16)[3]],
"full": [artifact], "none": None}[protection]
vector = torch.eye(16)[1] if protection == "overlap" else artifact.directions[0]
layer = model.model.layers[0]
scales = post_norm_scales(model, layer, 16)
writers = {"attention": layer.self_attn.o_proj, "ffn": layer.mlp.down_proj}
before = {kind: writer.weight.detach().clone().T * scales.get(kind, 1)
for kind, writer in writers.items()}
with apply_concept_intervention(
model, artifact, mode, preserve=preserve, projection_row_fraction=0.1,
) as run:
assert run.parameters["projection_row_fraction"] == 0.1
for kind, writer in writers.items():
expected = before[kind].clone()
if protection != "full":
coeff = expected @ vector
keep = (len(coeff) + 9) // 10
indices = coeff.abs().argsort(descending=True)[:keep]
expected[indices] -= coeff[indices, None] * vector
actual = writer.weight.detach().T
if mode == "lora":
name = next(name for name, module in model.named_modules() if module is writer)
b, a = run.manager.adapters[name]
actual = actual + (b @ a).T
torch.testing.assert_close(actual * scales.get(kind, 1), expected, atol=2e-7, rtol=2e-6)
@pytest.mark.parametrize("fraction", [0.0, -0.1, 1.01, float("nan"), float("inf")])
def test_invalid_projection_fraction_fails_before_protected_mutation(affine_model, fraction):
before = {name: value.clone() for name, value in affine_model.state_dict().items()}
with pytest.raises(ValueError, match="projection_row_fraction"):
apply_concept_intervention(
affine_model, _target(1), "orthogonalize", preserve=[torch.eye(16)[0]],
projection_row_fraction=fraction,
)
assert all(torch.equal(value, before[name]) for name, value in affine_model.state_dict().items())
@pytest.mark.parametrize("mode", ["project", "steer"])
def test_selective_weight_fraction_is_not_silently_ignored_by_hooks(affine_model, mode):
with pytest.raises(ValueError, match="requires a weight intervention"):
apply_concept_intervention(affine_model, _target(1), mode, projection_row_fraction=0.1)
assert all(not module._forward_hooks for module in affine_model.modules())
def test_dependent_protected_rows_and_multiple_sources_form_one_span(affine_model):
first, second = torch.eye(16)[0], torch.eye(16)[3]
protected = direction(1)
protected.directions = {0: second}
protected.subspaces = {}
with apply_concept_intervention(
affine_model, _target(2), "project", subspace=True,
preserve=[torch.stack([first, 2 * first]), {0: first}, protected],
) as run:
assert run.parameters["preserve"]["0"]["protected_rank"] == 2
assert run.parameters["preserve"]["0"]["rank"] == 2
span = run.preserved_spans[0]
torch.testing.assert_close(span @ span.T, torch.eye(2))
def test_protected_mapping_uses_each_layers_own_span():
vector = torch.tensor([[1.], [1.], [0.]]) / 2 ** 0.5
geometries = {layer: ErasureGeometry(vector.clone(), vector.clone()) for layer in (0, 1)}
spans, metadata, changed = protected_geometry(
geometries, [{0: torch.eye(3)[0], 1: torch.eye(3)[1]}],
)
assert changed and set(spans) == {0, 1}
for layer in (0, 1):
expected = torch.zeros(3)
expected[1 - layer] = 1
torch.testing.assert_close(geometries[layer].project(torch.ones(3)), expected)
assert metadata[str(layer)]["sources"][0]["source_layer"] == layer
@pytest.mark.parametrize("invalid", [
"refusal", [torch.zeros(16)], [torch.ones(15)], [torch.ones(16, dtype=torch.int64)],
[torch.full((16,), float("nan"))], [{1: torch.ones(16)}], [{"0": torch.ones(16)}],
])
def test_invalid_protected_span_fails_before_mutation(affine_model, invalid):
weights = {name: value.clone() for name, value in affine_model.state_dict().items()}
with pytest.raises(ValueError):
apply_concept_intervention(affine_model, _target(2), "orthogonalize", preserve=invalid)
assert all(torch.equal(value, weights[name]) for name, value in affine_model.state_dict().items())
assert all(not module._forward_hooks for module in affine_model.modules())
def test_fold_post_norms_can_be_disabled_explicitly():
model = _tiny_residual_model("gemma2")
writer = model.model.layers[0].self_attn.o_proj
before = writer.weight.clone()
run = apply_concept_intervention(
model, _target(2), "orthogonalize", subspace=True,
preserve=[torch.eye(16)[0]], fold_post_norms=False,
)
expected = before.clone()
expected[1:3] = 0
torch.testing.assert_close(writer.weight, expected)
assert run.parameters["fold_post_norms"] is False
def test_affine_writer_biases_keep_protected_coordinate(affine_model):
writers = [affine_model.transformer.h[0].attn.c_proj, affine_model.transformer.h[0].mlp.c_proj]
with torch.no_grad():
for writer in writers:
writer.bias.copy_(torch.arange(16) / 16 + 0.5)
before = [(writer.weight.clone(), writer.bias.clone()) for writer in writers]
artifact = _target(2)
apply_concept_intervention(
affine_model, artifact, "orthogonalize", subspace=True,
reference="mean", preserve=[torch.eye(16)[0]],
)
for index, (writer, (weight, bias)) in enumerate(zip(writers, before)):
assert torch.equal(writer.weight[:, 0], weight[:, 0])
assert torch.equal(writer.bias[0], bias[0])
expected = artifact.reference_means[0][1:3] if index == 1 else torch.zeros(2)
torch.testing.assert_close(writer.bias[1:3], expected)
def test_saved_protected_span_exports_and_replays(affine_model, tmp_path, monkeypatch):
artifact = _target(2)
source, output = tmp_path / "run", tmp_path / "bundle"
with apply_concept_intervention(
affine_model, artifact, "project", subspace=True, reference="mean",
preserve=[torch.eye(16)[0]],
) as run:
expected = residual(affine_model)
record = save_concept_run(source, artifact, run, tool_commit="b" * 40)
exported = export_run(source, output, kind="direction")
assert exported["run"] == record
assert "preserved.safetensors" in exported["files"]
restored = load_preserved_spans(output, record["intervention"]["parameters"]["preserve"])
assert set(restored[0]) == {0}
monkeypatch.chdir(output)
namespace = {"model": affine_model}
exec((output / "loader.py").read_text(), namespace)
try:
torch.testing.assert_close(residual(affine_model), expected)
finally:
namespace["run"].remove()
save_file({"0": torch.eye(16)[3:4]}, str(output / "preserved.safetensors"))
with pytest.raises(ValueError, match="do not match"):
load_preserved_spans(output, record["intervention"]["parameters"]["preserve"])
def test_saved_selective_protected_writer_replays_fraction(affine_model, tmp_path, monkeypatch):
pristine = deepcopy(affine_model)
artifact = _target(2)
run = apply_concept_intervention(
affine_model, artifact, "orthogonalize", subspace=True,
preserve=[torch.eye(16)[0]], projection_row_fraction=0.1,
)
source, output = tmp_path / "run", tmp_path / "bundle"
record = save_concept_run(source, artifact, run, tool_commit="b" * 40)
assert record["intervention"]["parameters"]["projection_row_fraction"] == 0.1
export_run(source, output, kind="direction")
monkeypatch.chdir(output)
namespace = {"model": pristine}
exec((output / "loader.py").read_text(), namespace)
assert namespace["run"].parameters["projection_row_fraction"] == 0.1
for name, value in affine_model.state_dict().items():
torch.testing.assert_close(pristine.state_dict()[name], value)
def test_saved_span_rejects_symlinks(tmp_path):
actual = tmp_path / "actual.safetensors"
save_file({"0": torch.eye(3)[:1]}, str(actual))
(tmp_path / "preserved.safetensors").symlink_to(actual)
with pytest.raises(ValueError, match="symlink"):
load_preserved_spans(tmp_path, {})
def test_protected_geometry_restricts_oblique_rank_when_dual_span_is_exhausted():
left = torch.tensor([[1.], [1.], [0.]])
right = torch.tensor([[1.], [0.], [0.]])
geometry = ErasureGeometry(left, right, projector_type="leace_oblique")
_, metadata, changed = protected_geometry({0: geometry}, [torch.tensor([1., 0., 0.])])
assert changed and geometry.rank == 0
assert metadata["0"]["rank"] == 0
assert torch.equal(geometry.project(torch.ones(3)), torch.zeros(3))
@pytest.mark.parametrize("protected_rank", [1, 2])
def test_mixed_remaining_ranks_pad_lora_for_peft_export(tmp_path, protected_rank):
model = _tiny_residual_model("llama")
artifact = direction(2)
protected = {0: torch.eye(16)[:protected_rank], 1: torch.eye(16)[3]}
with apply_concept_intervention(
model, artifact, "lora", layers="all", subspace=True, preserve=[protected],
) as run:
assert run.parameters["preserve"]["0"]["rank"] == 2 - protected_rank
assert run.parameters["preserve"]["1"]["rank"] == 2
for name, (b, a) in run.manager.adapters.items():
assert b.shape[1] == a.shape[0] == 2
if name.startswith("model.layers.0."):
assert not torch.count_nonzero(b[:, 2 - protected_rank:])
assert not torch.count_nonzero(a[2 - protected_rank:])
writer = model.get_submodule(name)
updated = writer.weight + b @ a
torch.testing.assert_close(updated[:protected_rank], writer.weight[:protected_rank])
assert updated[protected_rank:2].abs().sum() < 1e-6
output = tmp_path / "adapter"
run.manager.save(output, base_model_name_or_path="fixture/tiny")
config = json.loads((output / "adapter_config.json").read_text())
assert config["r"] == config["lora_alpha"] == 2
tensors = load_file(str(output / "adapter_model.safetensors"))
assert len(tensors) == 2 * len(run.manager.adapters)
for name, value in tensors.items():
assert value.shape[0 if "lora_A" in name else 1] == 2