mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
213 lines
8.3 KiB
Python
213 lines
8.3 KiB
Python
"""Offline regression coverage for intervention hook and load boundaries."""
|
|
|
|
from copy import deepcopy
|
|
import json
|
|
|
|
import pytest
|
|
import torch
|
|
from safetensors.torch import save_file
|
|
from transformers import AutoModelForCausalLM
|
|
|
|
from obliteratus.analysis.steering_vectors import (
|
|
SteeringConfig, SteeringHookManager, SteeringVector,
|
|
)
|
|
from obliteratus.concepts import ConceptDirection, apply_concept_intervention
|
|
from obliteratus.lora_ablation import UnmergedLoraAdapters
|
|
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
|
|
|
|
pytestmark = [
|
|
pytest.mark.cpu,
|
|
pytest.mark.integration,
|
|
pytest.mark.usefixtures("tiny_model_runtime"),
|
|
]
|
|
IDS = torch.tensor([[3, 5, 6], [4, 7, 8]])
|
|
|
|
|
|
@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("hook-regressions") / "model")
|
|
yield AutoModelForCausalLM.from_pretrained(path, local_files_only=True).eval()
|
|
torch.set_num_threads(threads)
|
|
|
|
|
|
@pytest.fixture
|
|
def model(base):
|
|
return deepcopy(base)
|
|
|
|
|
|
@pytest.fixture
|
|
def direction():
|
|
vector = torch.zeros(16)
|
|
vector[0] = 1
|
|
return ConceptDirection({0: vector}, extraction_layers=[0], intervention_layers=[0])
|
|
|
|
|
|
def logits(model):
|
|
with torch.inference_mode():
|
|
return model(IDS, use_cache=False).logits
|
|
|
|
|
|
def fail_registration(*args, **kwargs):
|
|
raise RuntimeError("hook installation failed")
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["steer", "project"])
|
|
@pytest.mark.parametrize("magnitude", [0, 40000])
|
|
def test_opted_in_fp16_hook_normalizes_without_underflow_or_overflow(mode, magnitude):
|
|
vector = torch.full((4,), magnitude, dtype=torch.float16)
|
|
config = SteeringConfig([SteeringVector(vector, 0, "fixture", 1)], [0], mode=mode,
|
|
normalize_fp32=True)
|
|
hook = SteeringHookManager()._make_hook(config, 0)
|
|
hidden = torch.ones(1, 2, 4, dtype=torch.float16)
|
|
actual = hook(None, (), hidden)
|
|
expected = hidden if magnitude == 0 else hidden + 0.5 if mode == "steer" else hidden * 0
|
|
assert actual.dtype == torch.float16
|
|
assert torch.isfinite(actual).all()
|
|
assert torch.equal(actual, expected)
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["steer", "project"])
|
|
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16])
|
|
def test_default_hook_keeps_storage_dtype_normalization_bits(mode, dtype):
|
|
vector = torch.tensor([1, 6], dtype=dtype)
|
|
config = SteeringConfig([SteeringVector(vector, 0, "fixture", 1)], [0], mode=mode)
|
|
hidden = torch.tensor([[[2, 3]]], dtype=dtype)
|
|
unit = vector / vector.norm().clamp(min=1e-10)
|
|
expected = (hidden - (hidden @ unit).unsqueeze(-1) * unit
|
|
if mode == "project" else hidden + unit)
|
|
actual = SteeringHookManager()._make_hook(config, 0)(None, (), hidden)
|
|
assert torch.equal(actual, expected)
|
|
if dtype == torch.bfloat16:
|
|
assert unit[1].item() == 0.984375
|
|
|
|
|
|
def test_manager_mid_install_restores_existing_hooks(model, monkeypatch):
|
|
layers = model.transformer.h
|
|
layers.append(deepcopy(layers[0]))
|
|
existing = layers[0].register_forward_hook(lambda *args: None)
|
|
pristine = logits(model)
|
|
manager = SteeringHookManager()
|
|
config = SteeringConfig([SteeringVector(torch.ones(16), 0, "fixture", 1)], [0, 1])
|
|
monkeypatch.setattr(layers[1], "register_forward_hook", fail_registration)
|
|
with pytest.raises(RuntimeError, match="installation failed"):
|
|
manager.install(model, config)
|
|
assert not manager._hooks
|
|
assert not manager.is_active
|
|
assert list(layers[0]._forward_hooks) == [existing.id]
|
|
assert not layers[1]._forward_hooks
|
|
assert torch.equal(logits(model), pristine)
|
|
existing.remove()
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["steer", "project", "lora"])
|
|
def test_apply_mid_install_is_bit_exact(model, direction, mode, monkeypatch):
|
|
model.transformer.h.append(deepcopy(model.transformer.h[0]))
|
|
direction.directions[1] = direction.directions[0].clone()
|
|
direction.intervention_layers = [0, 1]
|
|
pristine = logits(model)
|
|
if mode == "lora":
|
|
later = model.transformer.h[0].mlp.c_proj
|
|
else:
|
|
later = model.transformer.h[1]
|
|
monkeypatch.setattr(later, "register_forward_hook", fail_registration)
|
|
with pytest.raises(RuntimeError, match="installation failed"):
|
|
apply_concept_intervention(model, direction, mode, override_gate="offline regression")
|
|
assert all(not module._forward_hooks for module in model.modules())
|
|
assert torch.equal(logits(model), pristine)
|
|
|
|
|
|
@pytest.mark.parametrize("first_mode,second_mode", [
|
|
("steer", "project"), ("project", "lora"), ("lora", "steer"),
|
|
])
|
|
@pytest.mark.parametrize("remove_first", [True, False])
|
|
def test_stacked_runs_removed_in_either_order(
|
|
model, direction, first_mode, second_mode, remove_first,
|
|
):
|
|
pristine = logits(model)
|
|
weights = {name: value.clone() for name, value in model.state_dict().items()}
|
|
first = apply_concept_intervention(
|
|
model, direction, first_mode, override_gate="offline regression",
|
|
)
|
|
first_only = logits(model)
|
|
second = apply_concept_intervention(
|
|
model, direction, second_mode, override_gate="offline regression",
|
|
)
|
|
if remove_first:
|
|
first.remove()
|
|
remaining = logits(model)
|
|
second.remove()
|
|
with apply_concept_intervention(
|
|
model, direction, second_mode, override_gate="offline regression",
|
|
):
|
|
assert torch.equal(logits(model), remaining)
|
|
else:
|
|
second.remove()
|
|
assert torch.equal(logits(model), first_only)
|
|
first.remove()
|
|
assert torch.equal(logits(model), pristine)
|
|
assert all(torch.equal(value, weights[name]) for name, value in model.state_dict().items())
|
|
assert all(not module._forward_hooks for module in model.modules())
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["steer", "project", "lora"])
|
|
def test_repeated_install_remove_is_bit_exact(model, direction, mode):
|
|
pristine = logits(model)
|
|
for _ in range(3):
|
|
run = apply_concept_intervention(model, direction, mode, override_gate="offline regression")
|
|
assert not torch.equal(logits(model), pristine)
|
|
run.remove()
|
|
run.remove()
|
|
assert torch.equal(logits(model), pristine)
|
|
assert all(not module._forward_hooks for module in model.modules())
|
|
|
|
|
|
def test_lora_final_token_rejects_2d_input_and_cleans_up(model):
|
|
name = "transformer.h.0.attn.c_proj"
|
|
writer = model.get_submodule(name)
|
|
run = UnmergedLoraAdapters(model, {name: (torch.ones(16, 1), torch.ones(1, 16))},
|
|
positions="final_token")
|
|
with pytest.raises(ValueError, match="final_token.*3D"):
|
|
writer(torch.ones(2, 16))
|
|
assert not run._hooks
|
|
assert not writer._forward_hooks
|
|
|
|
|
|
@pytest.mark.parametrize("positions", ["all_tokens", "final_token"])
|
|
def test_lora_missing_supported_input_is_clear_and_cleans_up(model, positions, monkeypatch):
|
|
name = "transformer.h.0.attn.c_proj"
|
|
writer = model.get_submodule(name)
|
|
original_forward = writer.forward
|
|
monkeypatch.setattr(writer, "forward", lambda hidden_states: original_forward(hidden_states))
|
|
run = UnmergedLoraAdapters(
|
|
model, {name: (torch.ones(16, 1), torch.ones(1, 16))}, positions=positions,
|
|
)
|
|
with pytest.raises(ValueError, match="LoRA.*input"):
|
|
writer(hidden_states=torch.ones(1, 2, 16))
|
|
assert not run._hooks
|
|
assert not writer._forward_hooks
|
|
|
|
|
|
@pytest.mark.parametrize("failure", [
|
|
"missing_sidecar", "missing_tensor", "missing_direction",
|
|
"invalid_json", "invalid_metadata", "invalid_tensor",
|
|
])
|
|
def test_steering_vector_load_errors_are_value_errors(tmp_path, failure):
|
|
path = tmp_path / "direction.safetensors"
|
|
SteeringVector(torch.ones(16), 0, "fixture", 1).save(path)
|
|
if failure == "missing_sidecar":
|
|
path.with_suffix(".json").unlink()
|
|
elif failure == "missing_tensor":
|
|
path.unlink()
|
|
elif failure == "missing_direction":
|
|
save_file({"other": torch.ones(16)}, str(path))
|
|
elif failure == "invalid_json":
|
|
path.with_suffix(".json").write_text("{", encoding="utf-8")
|
|
elif failure == "invalid_metadata":
|
|
path.with_suffix(".json").write_text(json.dumps([]), encoding="utf-8")
|
|
else:
|
|
path.write_bytes(b"invalid safetensors")
|
|
with pytest.raises(ValueError, match="steering vector"):
|
|
SteeringVector.load(path)
|