mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 02:53:54 +01:00
534 lines
27 KiB
Python
534 lines
27 KiB
Python
"""Rank-k, affine, and legacy residual intervention contracts."""
|
|
|
|
from copy import deepcopy
|
|
from dataclasses import replace
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
from transformers import AutoModelForCausalLM, Gemma2Config, OPTConfig, OPTForCausalLM
|
|
|
|
from obliteratus.concepts import ConceptDirection, apply_concept_intervention
|
|
from obliteratus.concepts.geometry import ErasureGeometry, ErasureHookManager, project_writer, tensor_hash
|
|
from obliteratus.models.offload_surgery import LogicalParameterTransaction, resolve_logical_parameter
|
|
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
|
|
from tests.test_concept_intervention import (
|
|
_decoder_residuals, _dense_residual_direction, _tiny_residual_model, logits, residual,
|
|
)
|
|
|
|
pytestmark = [pytest.mark.cpu, pytest.mark.integration, pytest.mark.usefixtures("tiny_model_runtime")]
|
|
|
|
|
|
@pytest.fixture
|
|
def affine_model(tmp_path):
|
|
path = build_tiny_offline_model(tmp_path / "base")
|
|
return AutoModelForCausalLM.from_pretrained(path, local_files_only=True).eval()
|
|
|
|
|
|
def direction(rank=2):
|
|
result = _dense_residual_direction()
|
|
result.directions = {0: torch.eye(16)[0]}
|
|
result.subspaces = {0: torch.eye(16)[:rank]}
|
|
result.intervention_layers = [0]
|
|
result.reference_means = {0: torch.arange(16, dtype=torch.float32) / 16 + 0.25}
|
|
result.reference_provenance = {"source": "fixture control population", "count": 12}
|
|
return result
|
|
|
|
|
|
@pytest.mark.parametrize("architecture", ["llama", "gemma2"])
|
|
@pytest.mark.parametrize("mode", ["orthogonalize", "lora"])
|
|
def test_rank_two_removes_every_basis_direction_at_every_block(architecture, mode):
|
|
model = _tiny_residual_model(architecture)
|
|
artifact = direction()
|
|
pristine = logits(model)
|
|
with apply_concept_intervention(
|
|
model, artifact, mode, layers="all", subspace=True, project_embeddings=True,
|
|
) as run:
|
|
for hidden in _decoder_residuals(model):
|
|
assert hidden[..., :2].abs().max() < 2e-6
|
|
assert all(value["rank"] == 2 for value in run.parameters["geometry"].values())
|
|
if mode == "lora":
|
|
assert all(a.shape[0] == b.shape[1] == 2 for b, a in run.manager.adapters.values())
|
|
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_rank_two_hook_projects_selected_positions_and_removes_exactly(affine_model, positions, reference):
|
|
artifact = direction()
|
|
before = residual(affine_model)
|
|
expected = before.clone()
|
|
target = expected if positions == "all_tokens" else expected[:, -1:]
|
|
target[..., :2] = 0 if reference == "zero" else artifact.reference_means[0][:2]
|
|
with apply_concept_intervention(
|
|
affine_model, artifact, "project", subspace=True, reference=reference, positions=positions,
|
|
) as run:
|
|
torch.testing.assert_close(residual(affine_model), expected)
|
|
if reference == "mean":
|
|
metadata = run.parameters["geometry"]["0"]
|
|
assert metadata["mean_sha256"] == tensor_hash(artifact.reference_means[0])
|
|
assert metadata["mean_provenance"] == artifact.reference_provenance
|
|
run.alpha = 0
|
|
assert torch.equal(residual(affine_model), before)
|
|
assert torch.equal(residual(affine_model), before)
|
|
|
|
|
|
@pytest.mark.parametrize("mode,reference", [
|
|
("project", "zero"), ("orthogonalize", "zero"), ("lora", "zero"),
|
|
("project", "mean"), ("orthogonalize", "mean"),
|
|
])
|
|
@pytest.mark.parametrize("rule", [{"rule": "explicit", "layers": [0]},
|
|
{"rule": "norm_ratio", "target": 0.6}])
|
|
@pytest.mark.parametrize("subspace", [False, True])
|
|
@pytest.mark.parametrize("preserve", [False, True])
|
|
def test_loaded_transfer_uses_extraction_geometry_and_destination_mean(
|
|
affine_model, tmp_path, mode, reference, rule, subspace, preserve,
|
|
):
|
|
affine_model.transformer.h.append(deepcopy(affine_model.transformer.h[0]))
|
|
affine_model.config.n_layer = 2
|
|
expected_model = deepcopy(affine_model)
|
|
artifact = direction()
|
|
artifact.directions = {0: torch.eye(16)[2], 1: torch.eye(16)[0]}
|
|
artifact.subspaces = {0: torch.eye(16)[2:4], 1: torch.eye(16)[:2]}
|
|
artifact.extraction_layers = [1]
|
|
artifact.extraction_layer_source = "pinned"
|
|
artifact.recipe = replace(artifact.recipe, method="leace_exact", n_directions=2,
|
|
extraction_layer={"offline": 1}, intervention_layer=rule)
|
|
artifact.reference_means[1] = artifact.reference_means[0] + 2
|
|
artifact.projector_left = {layer: basis.T.clone() for layer, basis in artifact.subspaces.items()}
|
|
artifact.projector_right = {layer: factor.clone() for layer, factor in artifact.projector_left.items()}
|
|
artifact.projector_right[1][2, 0] = 0.5
|
|
artifact.projector_right[1][3, 1] = -0.25
|
|
path = tmp_path / "transfer.safetensors"
|
|
artifact.save(path)
|
|
loaded = ConceptDirection.load(path)
|
|
expected = replace(
|
|
artifact, directions={0: artifact.directions[1]}, subspaces={0: artifact.subspaces[1]},
|
|
extraction_layers=[0], recipe=replace(artifact.recipe, intervention_layer={"rule": "same"}),
|
|
projector_left={0: artifact.projector_left[1]}, projector_right={0: artifact.projector_right[1]},
|
|
)
|
|
before = logits(affine_model)
|
|
protection = [{0: torch.eye(16)[0] + torch.eye(16)[4]}] if preserve else None
|
|
with apply_concept_intervention(
|
|
affine_model, loaded, mode, subspace=subspace, reference=reference, preserve=protection,
|
|
) as run, apply_concept_intervention(
|
|
expected_model, expected, mode, subspace=subspace, reference=reference, preserve=protection,
|
|
):
|
|
assert run.layers == [0]
|
|
assert not torch.equal(logits(affine_model), before)
|
|
torch.testing.assert_close(logits(affine_model), logits(expected_model))
|
|
for name, tensor in affine_model.state_dict().items():
|
|
torch.testing.assert_close(tensor, expected_model.state_dict()[name])
|
|
if reference == "mean":
|
|
assert run.parameters["geometry"]["0"]["mean_sha256"] == tensor_hash(artifact.reference_means[0])
|
|
expected_type = "protected_leace_oblique" if preserve else "leace_oblique"
|
|
assert run.parameters["geometry"]["0"]["projector_type"] == expected_type
|
|
if preserve:
|
|
assert run.parameters["preserve"]["0"]["rank"] == (2 if subspace else 1)
|
|
|
|
|
|
def test_affine_leace_hook_uses_saved_oblique_factors(affine_model):
|
|
artifact = direction(rank=1)
|
|
artifact.recipe = replace(artifact.recipe, method="leace_exact")
|
|
left, right = torch.eye(16)[:, :1], torch.eye(16)[:, :1].clone()
|
|
right[1, 0] = 0.5
|
|
artifact.projector_left, artifact.projector_right = {0: left}, {0: right}
|
|
before = residual(affine_model)
|
|
expected = before - ((before - artifact.reference_means[0]) @ right) @ left.T
|
|
with apply_concept_intervention(affine_model, artifact, "project", reference="mean") as run:
|
|
torch.testing.assert_close(residual(affine_model), expected)
|
|
assert run.parameters["geometry"]["0"]["projector_type"] == "leace_oblique"
|
|
with apply_concept_intervention(affine_model, artifact, "project", reference="zero"):
|
|
expected_zero = before.clone()
|
|
expected_zero[..., 0] = 0
|
|
assert torch.equal(residual(affine_model), expected_zero)
|
|
|
|
|
|
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32, torch.float64])
|
|
@pytest.mark.parametrize("factor_dtype,scale", [(torch.float32, 1e6), (torch.float64, 1e50)])
|
|
@pytest.mark.parametrize("autocast", [False, True])
|
|
def test_leace_projection_promotes_unbalanced_factors(dtype, factor_dtype, scale, autocast):
|
|
basis = torch.eye(3, dtype=factor_dtype)[:, :1]
|
|
geometry = ErasureGeometry(basis / scale, basis * scale, projector_type="leace_oblique")
|
|
value = torch.tensor([[2., 3., 4.]], dtype=dtype)
|
|
with torch.autocast("cpu", dtype=torch.bfloat16, enabled=autocast):
|
|
actual = geometry.project(value)
|
|
expected_dtype = torch.float64 if torch.float64 in (dtype, factor_dtype) else torch.float32
|
|
assert actual.dtype == expected_dtype
|
|
assert torch.isfinite(actual).all()
|
|
torch.testing.assert_close(actual, torch.tensor([[2., 0., 0.]], dtype=expected_dtype))
|
|
|
|
|
|
@pytest.mark.parametrize("autocast", [False, True])
|
|
@pytest.mark.parametrize("fraction", [0.5, 1.0])
|
|
@pytest.mark.parametrize("norm_preserve", [False, True])
|
|
def test_leace_writer_keeps_promoted_precision_under_autocast(autocast, fraction, norm_preserve):
|
|
basis = torch.eye(3)[:, :1]
|
|
geometry = ErasureGeometry(basis * 1e-6, basis * 1e6, projector_type="leace_oblique",
|
|
protected_basis=torch.eye(3)[:, 2:])
|
|
weight = torch.tensor([[2., 3., 4.], [5., 6., 7.]], dtype=torch.float16)
|
|
before = weight.clone()
|
|
target = (SimpleNamespace(tensor=weight), weight, basis[:, 0], False)
|
|
with torch.autocast("cpu", dtype=torch.bfloat16, enabled=autocast):
|
|
actual = project_writer(
|
|
"writer", target, None, geometry, norm_preserve=norm_preserve,
|
|
regularization=0.0, chunk_bytes=16, projection_row_fraction=fraction,
|
|
)
|
|
expected = before.float()
|
|
expected[0 if fraction == 1.0 else 1:, 0] = 0
|
|
if norm_preserve:
|
|
ratio = min((before.float()[:, :2].square().sum()
|
|
/ expected[:, :2].square().sum()).sqrt().item(), 1.10)
|
|
expected[:, :2] *= ratio
|
|
torch.testing.assert_close(actual, expected.to(weight.dtype))
|
|
assert torch.equal(actual[:, 2], before[:, 2])
|
|
assert torch.equal(weight, before)
|
|
|
|
|
|
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16])
|
|
@pytest.mark.parametrize("position", ["all", "last"])
|
|
@pytest.mark.parametrize("tuple_output", [False, True])
|
|
def test_leace_hook_preserves_output_contract_in_low_precision(dtype, position, tuple_output):
|
|
basis = torch.eye(3)[:, :1]
|
|
mean = torch.tensor([0.5, 0.25, -0.5])
|
|
geometry = ErasureGeometry(basis * 1e-6, basis * 1e6, mean, "leace_oblique")
|
|
config = SimpleNamespace(alpha=-0.5, per_layer_alpha={}, position=position)
|
|
hook = ErasureHookManager({0: geometry})._make_hook(config, 0)
|
|
hidden = torch.tensor([[[2., 3., 4.], [5., 6., 7.]]], dtype=dtype)
|
|
before = hidden.clone()
|
|
expected = hidden.clone()
|
|
selected = expected if position == "all" else expected[:, -1:]
|
|
selected[..., 0] = (selected[..., 0] + mean[0]) / 2
|
|
cache = object()
|
|
output = (hidden, cache) if tuple_output else hidden
|
|
with torch.autocast("cpu", dtype=torch.bfloat16):
|
|
actual = hook(None, (), output)
|
|
if tuple_output:
|
|
assert actual[1] is cache
|
|
actual = actual[0]
|
|
assert actual.dtype == dtype
|
|
assert torch.isfinite(actual).all()
|
|
torch.testing.assert_close(actual, expected)
|
|
assert torch.equal(hidden, before)
|
|
config.alpha = 0
|
|
assert hook(None, (), output) is output
|
|
|
|
|
|
@pytest.mark.parametrize("mean,expected", [(70000., 65000.), (-60000., 0.)])
|
|
def test_affine_hook_promotes_centering_and_subtraction(mean, expected):
|
|
basis = torch.eye(2)[:, :1]
|
|
geometry = ErasureGeometry(basis, basis, torch.tensor([mean, 0.]))
|
|
config = SimpleNamespace(alpha=0.5, per_layer_alpha={}, position="all")
|
|
hook = ErasureHookManager({0: geometry})._make_hook(config, 0)
|
|
hidden = torch.tensor([[[60000., 1.]]], dtype=torch.float16)
|
|
actual = hook(None, (), hidden)
|
|
torch.testing.assert_close(actual, torch.tensor([[[expected, 1.]]], dtype=hidden.dtype))
|
|
|
|
|
|
@pytest.mark.parametrize("architecture", ["llama", "gemma2"])
|
|
@pytest.mark.parametrize("chunked", [False, True])
|
|
@pytest.mark.parametrize("offloaded", [False, True])
|
|
def test_rank_one_zero_origin_keeps_legacy_writer_bits(monkeypatch, architecture, chunked, offloaded):
|
|
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
|
|
|
|
legacy = _tiny_residual_model(architecture)
|
|
selected = deepcopy(legacy)
|
|
artifact = _dense_residual_direction()
|
|
artifact.subspaces = {0: artifact.directions[0].unsqueeze(0)}
|
|
if chunked:
|
|
monkeypatch.setattr("obliteratus.concepts.intervention._PROJECTION_CHUNK_BYTES", 128)
|
|
for model in (legacy, selected):
|
|
if offloaded:
|
|
for layer in model.model.layers:
|
|
for writer in (layer.self_attn.o_proj, layer.mlp.down_proj):
|
|
add_hook_to_module(writer, AlignDevicesHook(execution_device="cpu", offload=True))
|
|
apply_concept_intervention(
|
|
model, artifact, "orthogonalize", layers="all", subspace=model is selected,
|
|
reference="zero", regularization=0.25,
|
|
)
|
|
for first, second in zip(legacy.model.layers, selected.model.layers):
|
|
for a, b in ((first.self_attn.o_proj, second.self_attn.o_proj),
|
|
(first.mlp.down_proj, second.mlp.down_proj)):
|
|
actual = resolve_logical_parameter(a, search_roots=(legacy,)).tensor
|
|
expected = resolve_logical_parameter(b, search_roots=(selected,)).tensor
|
|
assert torch.equal(actual, expected)
|
|
|
|
|
|
@pytest.mark.parametrize("offloaded", [False, True])
|
|
def test_permanent_affine_updates_native_biases_once_per_block(affine_model, monkeypatch, offloaded):
|
|
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
|
|
|
|
artifact = direction()
|
|
writers = [affine_model.transformer.h[0].attn.c_proj, affine_model.transformer.h[0].mlp.c_proj]
|
|
with torch.no_grad():
|
|
for index, writer in enumerate(writers):
|
|
writer.bias.copy_(torch.arange(16) / 32 + index)
|
|
original = [(writer.weight.detach().clone(), writer.bias.detach().clone()) for writer in writers]
|
|
keys = set(affine_model.state_dict())
|
|
if offloaded:
|
|
for writer in writers:
|
|
add_hook_to_module(writer, AlignDevicesHook(execution_device="cpu", offload=True))
|
|
monkeypatch.setattr("obliteratus.concepts.intervention._PROJECTION_CHUNK_BYTES", 128)
|
|
run = apply_concept_intervention(
|
|
affine_model, artifact, "orthogonalize", subspace=True, reference="mean", regularization=0.25,
|
|
)
|
|
for index, (writer, (weight, bias)) in enumerate(zip(writers, original)):
|
|
weight[:, :2] *= 0.25
|
|
bias[:2] *= 0.25
|
|
if index == len(writers) - 1:
|
|
bias[:2] += 0.75 * artifact.reference_means[0][:2]
|
|
torch.testing.assert_close(resolve_logical_parameter(writer, search_roots=(affine_model,)).tensor, weight)
|
|
torch.testing.assert_close(resolve_logical_parameter(writer, "bias", search_roots=(affine_model,)).tensor, bias)
|
|
assert set(affine_model.state_dict()) == keys
|
|
assert run.parameters["affine_bias"]["checkpoint"] == "existing_native_biases"
|
|
assert run.parameters["affine_bias"]["incoming_residual"] == "unchanged"
|
|
assert len(run.parameters["affine_bias"]["parameters"]) == 2
|
|
|
|
|
|
@pytest.mark.parametrize("architecture,match", [("llama", "native bias"), ("gemma2", "post-norm")])
|
|
def test_unsupported_permanent_affine_fails_without_mutation_and_hooks_still_work(architecture, match):
|
|
model = _tiny_residual_model(architecture)
|
|
original = {name: value.clone() for name, value in model.state_dict().items()}
|
|
with pytest.raises(ValueError, match=match):
|
|
apply_concept_intervention(model, direction(), "orthogonalize", subspace=True, reference="mean")
|
|
assert all(torch.equal(value, original[name]) for name, value in model.state_dict().items())
|
|
with apply_concept_intervention(model, direction(), "project", subspace=True, reference="mean"):
|
|
hidden = _decoder_residuals(model)[0]
|
|
torch.testing.assert_close(hidden[..., :2], direction().reference_means[0][:2].expand_as(hidden[..., :2]))
|
|
|
|
|
|
@pytest.mark.parametrize("fold_post_norms", [False, True])
|
|
@pytest.mark.parametrize("protected_rank", [0, 1, 2])
|
|
def test_affine_post_norm_guard_is_independent_of_folding(fold_post_norms, protected_rank):
|
|
config = Gemma2Config(
|
|
vocab_size=32, hidden_size=16, intermediate_size=32, num_hidden_layers=1,
|
|
num_attention_heads=4, num_key_value_heads=2, head_dim=4,
|
|
max_position_embeddings=32, attention_bias=True,
|
|
)
|
|
model = AutoModelForCausalLM.from_config(config).cpu().eval()
|
|
before = {name: value.clone() for name, value in model.state_dict().items()}
|
|
options = dict(
|
|
subspace=True, reference="mean", projection_target="attention",
|
|
fold_post_norms=fold_post_norms,
|
|
preserve=[torch.eye(16)[:protected_rank]] if protected_rank else None,
|
|
)
|
|
if protected_rank < 2:
|
|
with pytest.raises(ValueError, match="post-norm.*inference-time"):
|
|
apply_concept_intervention(model, direction(), "orthogonalize", **options)
|
|
else:
|
|
run = apply_concept_intervention(model, direction(), "orthogonalize", **options)
|
|
assert run.parameters["preserve"]["0"]["rank"] == 0
|
|
assert run.parameters["affine_bias"]["parameters"] == []
|
|
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.fixture
|
|
def opt_model():
|
|
config = OPTConfig(
|
|
vocab_size=32, hidden_size=16, ffn_dim=32, num_hidden_layers=2,
|
|
num_attention_heads=4, max_position_embeddings=32, dropout=0.0,
|
|
attention_dropout=0.0, do_layer_norm_before=False,
|
|
pad_token_id=0, bos_token_id=1, eos_token_id=2,
|
|
)
|
|
config._attn_implementation = "eager"
|
|
with torch.random.fork_rng(devices=[]):
|
|
torch.manual_seed(197)
|
|
return OPTForCausalLM(config).cpu().eval()
|
|
|
|
|
|
@pytest.mark.parametrize("projection_target", ["output", "attention", "ffn"])
|
|
def test_post_normalized_opt_rejects_affine_surgery_but_supports_hooks(opt_model, projection_target):
|
|
original = {name: value.clone() for name, value in opt_model.state_dict().items()}
|
|
with pytest.raises(ValueError, match="post-norm.*inference-time"):
|
|
apply_concept_intervention(
|
|
opt_model, direction(), "orthogonalize", layers="all", subspace=True,
|
|
reference="mean", projection_target=projection_target,
|
|
)
|
|
assert all(torch.equal(value, original[name]) for name, value in opt_model.state_dict().items())
|
|
assert all(not module._forward_hooks for module in opt_model.modules())
|
|
with apply_concept_intervention(opt_model, direction(), "project", subspace=True, reference="mean"):
|
|
readings = []
|
|
|
|
def capture(module, args, output):
|
|
readings.append(output[0] if isinstance(output, tuple) else output)
|
|
|
|
handle = opt_model.model.decoder.layers[0].register_forward_hook(capture)
|
|
try:
|
|
logits(opt_model)
|
|
finally:
|
|
handle.remove()
|
|
hidden = readings[0][..., :2]
|
|
torch.testing.assert_close(hidden, direction().reference_means[0][:2].expand_as(hidden))
|
|
|
|
|
|
def test_opt_affine_guard_checks_only_selected_block_layouts(opt_model):
|
|
opt_model.config.do_layer_norm_before = True
|
|
opt_model.model.decoder.layers[0].do_layer_norm_before = True
|
|
original = {name: value.clone() for name, value in opt_model.state_dict().items()}
|
|
with pytest.raises(ValueError, match="post-norm"):
|
|
apply_concept_intervention(
|
|
opt_model, direction(), "orthogonalize", layers="all", subspace=True, reference="mean",
|
|
)
|
|
assert all(torch.equal(value, original[name]) for name, value in opt_model.state_dict().items())
|
|
run = apply_concept_intervention(
|
|
opt_model, direction(), "orthogonalize", layers=[0], subspace=True, reference="mean",
|
|
)
|
|
layer = opt_model.model.decoder.layers[0]
|
|
torch.testing.assert_close(layer.fc2.bias[:2], direction().reference_means[0][:2])
|
|
assert torch.count_nonzero(layer.fc2.weight[:2]) == 0
|
|
assert run.parameters["affine_bias"]["offset_parameters"] == {"0": "model.decoder.layers.0.fc2.bias"}
|
|
assert all(torch.equal(value, original[name]) for name, value in opt_model.state_dict().items()
|
|
if name.startswith("model.decoder.layers.1."))
|
|
|
|
|
|
@pytest.mark.parametrize("protected_rank", [1, 2])
|
|
def test_opt_affine_guard_allows_only_exhausted_protected_edits(opt_model, protected_rank):
|
|
original = {name: value.clone() for name, value in opt_model.state_dict().items()}
|
|
options = dict(layers="all", subspace=True, reference="mean",
|
|
preserve=[torch.eye(16)[:protected_rank]])
|
|
if protected_rank == 1:
|
|
with pytest.raises(ValueError, match="post-norm.*inference-time"):
|
|
apply_concept_intervention(opt_model, direction(), "orthogonalize", **options)
|
|
else:
|
|
run = apply_concept_intervention(opt_model, direction(), "orthogonalize", **options)
|
|
assert all(value["rank"] == 0 for value in run.parameters["preserve"].values())
|
|
assert run.parameters["affine_bias"]["parameters"] == []
|
|
assert all(torch.equal(value, original[name]) for name, value in opt_model.state_dict().items())
|
|
|
|
|
|
@pytest.mark.parametrize("offloaded", [False, True])
|
|
def test_late_affine_bias_failure_rolls_back_weights_and_biases(affine_model, monkeypatch, offloaded):
|
|
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
|
|
|
|
writers = [affine_model.transformer.h[0].attn.c_proj, affine_model.transformer.h[0].mlp.c_proj]
|
|
if offloaded:
|
|
for writer in writers:
|
|
add_hook_to_module(writer, AlignDevicesHook(execution_device="cpu", offload=True))
|
|
originals = {(i, name): resolve_logical_parameter(writer, name, search_roots=(affine_model,)).tensor.clone()
|
|
for i, writer in enumerate(writers) for name in ("weight", "bias")}
|
|
original_commit = LogicalParameterTransaction.commit
|
|
committed = []
|
|
|
|
def commit(transaction, value):
|
|
if transaction.module is writers[-1] and transaction.parameter_name == "bias":
|
|
raise RuntimeError("last affine bias failed")
|
|
original_commit(transaction, value)
|
|
committed.append(transaction)
|
|
|
|
monkeypatch.setattr(LogicalParameterTransaction, "commit", commit)
|
|
with pytest.raises(RuntimeError, match="last affine bias failed"):
|
|
apply_concept_intervention(affine_model, direction(), "orthogonalize", subspace=True, reference="mean")
|
|
assert len(committed) == 3
|
|
for (index, name), expected in originals.items():
|
|
actual = resolve_logical_parameter(writers[index], name, search_roots=(affine_model,)).tensor
|
|
assert torch.equal(actual, expected)
|
|
assert not getattr(affine_model, "_obliteratus_surgery_invalid", False)
|
|
|
|
|
|
@pytest.mark.parametrize("changes,match", [
|
|
({"reference_means": {}}, "reference mean"),
|
|
({"projector_left": {}, "projector_right": {}}, "projector factors"),
|
|
])
|
|
def test_missing_affine_geometry_fails_before_hooks(affine_model, changes, match):
|
|
artifact = direction(rank=1)
|
|
artifact.recipe = replace(artifact.recipe, method="leace_exact")
|
|
artifact.projector_left = artifact.projector_right = {0: torch.eye(16)[:, :1]}
|
|
for name, value in changes.items():
|
|
setattr(artifact, name, value)
|
|
with pytest.raises(ValueError, match=match):
|
|
apply_concept_intervention(affine_model, artifact, "project", reference="mean")
|
|
assert all(not module._forward_hooks for module in affine_model.modules())
|
|
|
|
|
|
def test_operator_supplied_corpus_mean_requires_provenance(affine_model):
|
|
artifact = direction()
|
|
supplied = {0: torch.full((16,), 0.75)}
|
|
with pytest.raises(ValueError, match="provenance"):
|
|
apply_concept_intervention(affine_model, artifact, "project", reference="mean", reference_means=supplied)
|
|
with apply_concept_intervention(
|
|
affine_model, artifact, "project", subspace=True, reference="mean",
|
|
reference_means=supplied, reference_provenance={"source": "operator corpus", "sha256": "abc"},
|
|
) as run:
|
|
torch.testing.assert_close(residual(affine_model)[..., :2], torch.full((2, 3, 2), 0.75))
|
|
assert run.parameters["geometry"]["0"]["mean_sha256"] == tensor_hash(supplied[0])
|
|
|
|
|
|
@pytest.mark.parametrize("layout,folded,norm_preserve,budget", [
|
|
("linear", False, False, 1), ("linear", True, True, 96),
|
|
("experts", True, False, 96), ("experts", False, True, 4096),
|
|
("strided", True, True, 1), ("conv1d", False, False, 4096),
|
|
])
|
|
def test_rank_two_chunked_writer_matches_dense_formula(layout, folded, norm_preserve, budget):
|
|
count = 56 if layout in {"experts", "strided"} else 28
|
|
shape = (2, 4, 7) if layout == "experts" else (4, 7) if layout == "linear" else (7, count // 7)
|
|
weight = torch.arange(count, dtype=torch.float64).reshape(shape) / 32
|
|
if layout == "strided":
|
|
weight = weight[:, ::2]
|
|
before = weight.clone()
|
|
transpose = layout in {"linear", "experts"}
|
|
work = weight.transpose(-1, -2) if transpose else weight
|
|
basis = torch.tensor([[1., 0.], [1., 0.], [0., 1.], [0., 1.]], dtype=torch.float64) / 2 ** 0.5
|
|
geometry = ErasureGeometry(basis, basis)
|
|
scale = torch.tensor([0.5, 2., -1., 1.5], dtype=torch.float64) if folded else None
|
|
target = (SimpleNamespace(tensor=weight), work, basis[:, 0], transpose)
|
|
expected = work.clone()
|
|
if folded:
|
|
expected *= scale
|
|
before_projection = expected.clone()
|
|
expected -= 0.75 * ((expected @ basis) @ basis.T)
|
|
if norm_preserve:
|
|
before_matrices = before_projection if layout == "experts" else [before_projection]
|
|
after_matrices = expected if layout == "experts" else [expected]
|
|
for previous, projected in zip(before_matrices, after_matrices):
|
|
projected *= min((previous.square().sum() / projected.square().sum()).sqrt().item(), 1.10)
|
|
if folded:
|
|
expected /= scale
|
|
result = project_writer(
|
|
"writer", target, scale, geometry, norm_preserve=norm_preserve,
|
|
regularization=0.25, chunk_bytes=budget,
|
|
)
|
|
assert result.dtype == weight.dtype and result.shape == weight.shape
|
|
if layout != "strided":
|
|
assert result.stride() == weight.stride()
|
|
expected = expected.transpose(-1, -2) if transpose else expected
|
|
torch.testing.assert_close(result, expected, atol=1e-12, rtol=1e-12)
|
|
assert torch.equal(weight, before)
|
|
|
|
|
|
def test_dependent_subspace_and_affine_steering_fail_before_hooks(affine_model):
|
|
artifact = direction()
|
|
artifact.subspaces[0][1] = artifact.subspaces[0][0]
|
|
with pytest.raises(ValueError, match="independent rows"):
|
|
apply_concept_intervention(affine_model, artifact, "project", subspace=True)
|
|
with pytest.raises(ValueError, match="Subspace and affine"):
|
|
apply_concept_intervention(affine_model, direction(), "steer", subspace=True)
|
|
assert all(not module._forward_hooks for module in affine_model.modules())
|
|
|
|
|
|
def test_shared_writer_rejects_distinct_projectors_with_same_first_direction():
|
|
model = _tiny_residual_model("llama")
|
|
model.model.layers[1].self_attn.o_proj.weight = model.model.layers[0].self_attn.o_proj.weight
|
|
artifact = direction()
|
|
artifact.directions[1] = artifact.directions[0].clone()
|
|
artifact.subspaces[1] = torch.eye(16)[[0, 2]]
|
|
before = {name: value.clone() for name, value in model.state_dict().items()}
|
|
with pytest.raises(ValueError, match="conflicting layer projectors"):
|
|
apply_concept_intervention(model, artifact, "orthogonalize", subspace=True, layers="all")
|
|
assert all(torch.equal(value, before[name]) for name, value in model.state_dict().items())
|
|
|
|
|
|
def test_expert_only_affine_writer_has_no_reload_safe_constant(affine_model):
|
|
ffn = affine_model.transformer.h[0].mlp
|
|
del ffn.c_proj
|
|
ffn.experts = torch.nn.Module()
|
|
ffn.experts.down_proj = torch.nn.Parameter(torch.ones(2, 24, 16))
|
|
before = {name: value.clone() for name, value in affine_model.state_dict().items()}
|
|
with pytest.raises(ValueError, match="native bias"):
|
|
apply_concept_intervention(
|
|
affine_model, direction(), "orthogonalize", subspace=True,
|
|
reference="mean", projection_target="ffn",
|
|
)
|
|
assert all(torch.equal(value, before[name]) for name, value in affine_model.state_dict().items())
|