mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
571 lines
27 KiB
Python
571 lines
27 KiB
Python
"""CPU-only arm planning and orchestration checks; never download models."""
|
|
|
|
from argparse import Namespace
|
|
from dataclasses import replace
|
|
import json
|
|
from types import SimpleNamespace
|
|
import weakref
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.concepts.direction import ConceptDirection
|
|
from obliteratus.concepts.target import ExtractionRecipe
|
|
from scripts import pain_axis_appendix_d as appendix
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
def direction():
|
|
return ConceptDirection(
|
|
directions={2: torch.tensor([1.0, 0, 0]), 4: torch.tensor([0.0, 1, 0])},
|
|
subspaces={2: torch.eye(3)[:2], 4: torch.eye(3)[:2]},
|
|
extraction_layers=[2, 4], recipe=ExtractionRecipe(method="leace_exact", n_directions=2),
|
|
reference_means={2: torch.ones(3), 4: torch.zeros(3)},
|
|
reference_provenance={"source": "fixture"},
|
|
projector_left={2: torch.eye(3)[:, :2], 4: torch.eye(3)[:, :2]},
|
|
projector_right={2: torch.eye(3)[:, :2], 4: torch.eye(3)[:, :2]},
|
|
)
|
|
|
|
|
|
def args(**changes):
|
|
values = dict(band_radius=1, intervention_kwargs="{}", chunk=0,
|
|
reference="zero", override_gate="fixture comparison")
|
|
values.update(changes)
|
|
return Namespace(**values)
|
|
|
|
|
|
def test_select_direction_slices_factors_and_mean_without_mutating_source():
|
|
source = direction()
|
|
selected = appendix.select_direction(source, 2, 1)
|
|
assert selected.recipe.n_directions == 1
|
|
assert selected.extraction_layers == selected.intervention_layers == [2]
|
|
for name in ("directions", "subspaces", "projector_left", "projector_right"):
|
|
assert set(getattr(selected, name)) == {2}
|
|
assert set(selected.reference_means) == {2, 4}
|
|
assert selected.projector_left[2].shape == selected.projector_right[2].shape == (3, 1)
|
|
assert torch.equal(selected.reference_means[2], source.reference_means[2])
|
|
selected.reference_means[2].zero_()
|
|
assert torch.equal(source.reference_means[2], torch.ones(3))
|
|
assert source.recipe.n_directions == 2
|
|
assert source.subspaces[2].shape == (2, 3)
|
|
|
|
|
|
@pytest.mark.parametrize("rank", [0, -1, 3, True])
|
|
def test_rank_bounds(rank):
|
|
with pytest.raises(ValueError, match="rank must be"):
|
|
appendix.select_direction(direction(), 2, rank)
|
|
|
|
|
|
@pytest.mark.parametrize("scope,layers", [("all", [0, 1, 2, 3, 4]), ("extraction", [2]),
|
|
("band", [1, 2, 3])])
|
|
@pytest.mark.parametrize("position", ["final_token", "all_tokens"])
|
|
def test_projection_arms(scope, layers, position):
|
|
assert appendix.arm_spec(f"project_{scope}_{position}", 2, 5, 1) == ("project", layers, position)
|
|
|
|
|
|
def test_arm_validation_and_clipped_band():
|
|
assert appendix.arm_spec("baseline", 0, 5, 2) is None
|
|
assert appendix.arm_spec("weight_leace", 2, 5, 1) == ("orthogonalize", list(range(5)), "all_tokens")
|
|
assert appendix.arm_spec("project_band_final_token", 0, 5, 2)[1] == [0, 1, 2]
|
|
for name, layer, radius in [("unknown", 2, 1), ("baseline", 9, 1), ("baseline", 2, -1)]:
|
|
with pytest.raises(ValueError):
|
|
appendix.arm_spec(name, layer, 5, radius)
|
|
|
|
|
|
def test_weight_chunks_preserve_options_and_edit_embeddings_once(monkeypatch):
|
|
calls = []
|
|
monkeypatch.setattr(appendix.replication, "decoder_layers", lambda model: list(range(5)))
|
|
|
|
def apply(model, value, mode, **kwargs):
|
|
calls.append((mode, kwargs))
|
|
return SimpleNamespace(parameters={"rank": 2}, validation={}, edited_parameters=["writer"])
|
|
|
|
monkeypatch.setattr(appendix, "apply_concept_intervention", apply)
|
|
handles, metadata = appendix.apply_arm(None, appendix.select_direction(direction(), 2, 2),
|
|
"weight_leace", args(chunk=2, reference="mean",
|
|
intervention_kwargs=json.dumps({
|
|
"project_embeddings": True, "norm_preserve": True})))
|
|
assert handles == []
|
|
assert len(metadata) == 3
|
|
assert [call[1]["layers"] for call in calls] == [[0, 1], [2, 3], [4]]
|
|
assert all(mode == "orthogonalize" for mode, _ in calls)
|
|
assert all(kwargs["norm_preserve"] and kwargs["subspace"] for _, kwargs in calls)
|
|
assert all(kwargs["reference"] == "mean" for _, kwargs in calls)
|
|
assert calls[0][1]["project_embeddings"]
|
|
assert all("project_embeddings" not in kwargs for _, kwargs in calls[1:])
|
|
|
|
|
|
@pytest.mark.parametrize("chunk", [0, 2])
|
|
def test_default_arms_route_embedding_options_only_to_permanent_edits(monkeypatch, chunk):
|
|
monkeypatch.setattr(appendix.replication, "decoder_layers", lambda model: list(range(5)))
|
|
calls = []
|
|
|
|
def apply(model, value, mode, **kwargs):
|
|
calls.append((mode, kwargs))
|
|
return SimpleNamespace(parameters=kwargs, validation={}, edited_parameters=[])
|
|
|
|
monkeypatch.setattr(appendix, "apply_concept_intervention", apply)
|
|
embedding_options = {"project_embeddings": True, "tied_embeddings": "edit_both",
|
|
"embedding_geometry_layer": 0}
|
|
options = {**embedding_options, "alpha": 0.75, "projection_target": "ffn"}
|
|
values = args(chunk=chunk, intervention_kwargs=json.dumps(options))
|
|
selected, protected = appendix.select_direction(direction(), 2, 2), [direction()]
|
|
for arm in appendix.parser().get_default("arms").split(","):
|
|
calls.clear()
|
|
preserve = protected if arm == "d4_pain_minus_refusal" else None
|
|
handles, metadata = appendix.apply_arm(None, selected, arm, values, preserve=preserve)
|
|
if arm == "baseline":
|
|
assert calls == handles == metadata == []
|
|
continue
|
|
permanent = arm in {"weight_leace", "d4_pain_minus_refusal"}
|
|
assert len(calls) == (3 if permanent and chunk else 1)
|
|
assert len(metadata) == len(calls)
|
|
assert len(handles) == (0 if permanent else 1)
|
|
for index, (mode, kwargs) in enumerate(calls):
|
|
assert mode == ("orthogonalize" if permanent else "project")
|
|
forwarded = {key: kwargs[key] for key in embedding_options if key in kwargs}
|
|
assert forwarded == (embedding_options if permanent and index == 0 else {})
|
|
assert kwargs["alpha"] == 0.75 and kwargs["projection_target"] == "ffn"
|
|
assert kwargs["override_gate"] == values.override_gate
|
|
assert kwargs.get("preserve") is preserve
|
|
assert metadata[index]["parameters"] == kwargs
|
|
assert json.loads(values.intervention_kwargs) == options
|
|
|
|
|
|
def test_hook_handle_returned_and_rank_method_guard(monkeypatch):
|
|
monkeypatch.setattr(appendix.replication, "decoder_layers", lambda model: list(range(5)))
|
|
handle = SimpleNamespace(parameters={}, validation={}, edited_parameters=[])
|
|
monkeypatch.setattr(appendix, "apply_concept_intervention", lambda *a, **kw: handle)
|
|
selected = appendix.select_direction(direction(), 2, 2)
|
|
handles, _ = appendix.apply_arm(None, selected, "project_extraction_final_token", args(chunk=1))
|
|
assert handles == [handle]
|
|
selected.recipe = replace(selected.recipe, method="svd")
|
|
with pytest.raises(ValueError, match="requires a leace_exact"):
|
|
appendix.apply_arm(None, selected, "weight_leace", args())
|
|
with pytest.raises(ValueError, match="arm-controlled"):
|
|
appendix.apply_arm(None, selected, "project_all_all_tokens",
|
|
args(intervention_kwargs='{"subspace": false}'))
|
|
|
|
|
|
def test_failed_arm_writes_honest_summary_and_releases_model(tmp_path, monkeypatch):
|
|
data = tmp_path / "datasets"
|
|
data.mkdir()
|
|
(data / "4.1_self_other_420_scenarios.json").write_text(json.dumps([
|
|
{"id": str(i), "category": "gaslighting", "text": "fixture"} for i in range(100)
|
|
]))
|
|
monkeypatch.setattr(appendix.ConceptDirection, "load", lambda path: direction())
|
|
monkeypatch.setattr(appendix.replication, "load", lambda model, **kwargs: (None, object()))
|
|
monkeypatch.setattr(appendix.torch.cuda, "empty_cache", lambda: None)
|
|
removed = []
|
|
handle = SimpleNamespace(remove=lambda: removed.append(True))
|
|
monkeypatch.setattr(appendix, "apply_arm", lambda *a: ([handle], [{"parameters": {"rank": 2}}]))
|
|
|
|
def fail(*a):
|
|
raise ValueError("generation fixture failure")
|
|
|
|
monkeypatch.setattr(appendix, "generate", fail)
|
|
values = args(model="fixture", direction="direction.safetensors", extraction_layer=2,
|
|
ranks="2", arms="project_all_all_tokens", pin=str(tmp_path),
|
|
out=str(tmp_path / "out"), name="Gemma_2_2B_instruct", batch_size=8,
|
|
max_new_tokens=60)
|
|
with pytest.raises(ValueError, match="generation fixture failure"):
|
|
appendix.run(values)
|
|
record = json.loads((tmp_path / "out/summary.json").read_text())["arms"]["project_all_all_tokens__k2"]
|
|
assert record["status"] == "failed"
|
|
assert record["error"] == "generation fixture failure"
|
|
assert record["interventions"][0]["parameters"]["rank"] == 2
|
|
assert removed == [True]
|
|
|
|
|
|
def test_cli_help_documents_arm_controls():
|
|
help_text = appendix.parser().format_help()
|
|
for option in ("--ranks", "--reference", "--band-radius", "--extraction-layer", "--override-gate",
|
|
"--device-map", "--max-memory", "--offload-folder"):
|
|
assert option in help_text
|
|
for arm in appendix.ARMS:
|
|
assert arm in help_text
|
|
|
|
|
|
def test_generation_preserves_prompt_rows_and_reference_decoding(monkeypatch):
|
|
calls = []
|
|
monkeypatch.setattr(appendix.replication, "render", lambda text, tok, fmt: text)
|
|
|
|
class Batch(dict):
|
|
def to(self, device):
|
|
return self
|
|
|
|
class Tokenizer:
|
|
pad_token_id = 0
|
|
|
|
def __call__(self, texts, **kwargs):
|
|
calls.append((texts, kwargs))
|
|
return Batch(input_ids=torch.ones(len(texts), 2, dtype=torch.long))
|
|
|
|
def batch_decode(self, output, **kwargs):
|
|
assert output.shape[1] == 1
|
|
assert kwargs == {"skip_special_tokens": True}
|
|
return ["fixture generation"] * len(output)
|
|
|
|
class Generator(torch.nn.Module):
|
|
device = "cpu"
|
|
|
|
def generate(self, input_ids, **kwargs):
|
|
assert kwargs == {"max_new_tokens": 60, "do_sample": False, "pad_token_id": 0}
|
|
return torch.cat([input_ids, input_ids[:, :1]], dim=1)
|
|
|
|
scenarios = [{"id": str(i), "category": "fixture", "text": f"prompt {i}"} for i in range(3)]
|
|
rows = appendix.generate(Generator(), Tokenizer(), scenarios, "raw", 2, 60)
|
|
assert [row["id"] for row in rows] == ["0", "1", "2"]
|
|
assert all(row["generation"] == "fixture generation" for row in rows)
|
|
assert [len(texts) for texts, _ in calls] == [2, 1]
|
|
assert all(kwargs["padding_side"] == "left" for _, kwargs in calls)
|
|
|
|
|
|
@pytest.mark.parametrize("structured", [False, True])
|
|
@pytest.mark.parametrize("return_sequences", [1, 2])
|
|
@pytest.mark.parametrize("offloaded", [False, True])
|
|
def test_baseline_scores_first_candidate_per_scenario(
|
|
tmp_path, monkeypatch, structured, return_sequences, offloaded,
|
|
):
|
|
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
|
|
from transformers import BatchEncoding
|
|
from transformers.generation.utils import GenerateBeamDecoderOnlyOutput
|
|
|
|
scenarios = [{"id": str(i), "category": "gaslighting", "text": str(i)} for i in range(100)]
|
|
data = tmp_path / "datasets"
|
|
data.mkdir()
|
|
(data / "4.1_self_other_420_scenarios.json").write_text(json.dumps(scenarios))
|
|
batches = []
|
|
|
|
class Tokenizer:
|
|
pad_token_id = 0
|
|
|
|
def __call__(self, texts, **kwargs):
|
|
assert kwargs == {"return_tensors": "pt", "padding": True, "padding_side": "left"}
|
|
batches.append(texts)
|
|
return BatchEncoding({"input_ids": torch.tensor([[0, int(text) + 1] for text in texts])})
|
|
|
|
def batch_decode(self, output, **kwargs):
|
|
assert output.shape[1] == 1
|
|
assert kwargs == {"skip_special_tokens": True}
|
|
return [f"joke alternative {token - 1001}" if token > 1000 else
|
|
f"joke {token - 1}" if token <= 25 else f"answer {token - 1}"
|
|
for token in output[:, 0].tolist()]
|
|
|
|
class Generator(torch.nn.Module):
|
|
device = "meta" if offloaded else "cpu"
|
|
model = SimpleNamespace(layers=[None] * 5)
|
|
generation_config = SimpleNamespace(num_beams=2, num_return_sequences=return_sequences,
|
|
return_dict_in_generate=structured)
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.embedding = torch.nn.Embedding(2, 3)
|
|
if offloaded:
|
|
add_hook_to_module(self.embedding, AlignDevicesHook(execution_device="cpu", offload=True))
|
|
|
|
def get_input_embeddings(self):
|
|
return self.embedding
|
|
|
|
def generate(self, input_ids, **kwargs):
|
|
assert input_ids.device.type == "cpu"
|
|
assert kwargs == {"max_new_tokens": 60, "do_sample": False, "pad_token_id": 0}
|
|
candidates = (input_ids[:, -1:] + 1000 * torch.arange(return_sequences)).reshape(-1, 1)
|
|
output = torch.cat([input_ids.repeat_interleave(return_sequences, dim=0), candidates], dim=1)
|
|
return GenerateBeamDecoderOnlyOutput(sequences=output) if structured else output
|
|
|
|
model = Generator()
|
|
config = vars(model.generation_config).copy()
|
|
monkeypatch.setattr(appendix.ConceptDirection, "load", lambda path: direction())
|
|
monkeypatch.setattr(appendix.replication, "MODELS", {"fixture": ("raw", 2, 2)})
|
|
def load(repo, **kwargs):
|
|
assert kwargs == {"device_map": {"": 1}, "max_memory": {1: "4GiB"}, "offload_folder": "backing"}
|
|
return Tokenizer(), model
|
|
|
|
monkeypatch.setattr(appendix.replication, "load", load)
|
|
monkeypatch.setattr(appendix.torch.cuda, "empty_cache", lambda: None)
|
|
values = args(model="fixture", direction="direction.safetensors", extraction_layer=2,
|
|
ranks="1", arms="baseline", pin=str(tmp_path), out=str(tmp_path / "out"),
|
|
name="fixture", batch_size=8, max_new_tokens=60, device_map={"": 1},
|
|
max_memory={1: "4GiB"}, offload_folder="backing")
|
|
appendix.run(values)
|
|
|
|
rows = json.loads((tmp_path / "out/baseline.json").read_text())
|
|
assert rows == [{"id": row["id"], "category": row["category"],
|
|
"generation": f"joke {i}" if i < 25 else f"answer {i}"}
|
|
for i, row in enumerate(scenarios)]
|
|
record = json.loads((tmp_path / "out/summary.json").read_text())["arms"]["baseline"]
|
|
assert record["status"] == "complete" and record["n"] == 100
|
|
assert record["humor_proxy"] == 25
|
|
assert record["placement"]["requested"] == {
|
|
"device_map": {"": 1}, "max_memory": {"1": "4GiB"}, "offload_folder": "backing"}
|
|
assert record["placement"]["input_execution_device"] == "cpu"
|
|
assert record["placement"]["parameter_storage_devices"] == ["meta" if offloaded else "cpu"]
|
|
assert [len(batch) for batch in batches] == [8] * 12 + [4]
|
|
assert vars(model.generation_config) == config
|
|
|
|
|
|
def test_successful_sweep_runs_baseline_once_and_reloads_each_arm(tmp_path, monkeypatch):
|
|
data = tmp_path / "datasets"
|
|
data.mkdir()
|
|
(data / "4.1_self_other_420_scenarios.json").write_text(json.dumps([
|
|
{"id": str(i), "category": "gaslighting", "text": "fixture"} for i in range(100)
|
|
]))
|
|
monkeypatch.setattr(appendix.ConceptDirection, "load", lambda path: direction())
|
|
loaded, removed = [], []
|
|
|
|
def load(model, **kwargs):
|
|
loaded.append((model, kwargs))
|
|
return None, object()
|
|
|
|
monkeypatch.setattr(appendix.replication, "load", load)
|
|
monkeypatch.setattr(appendix.torch.cuda, "empty_cache", lambda: None)
|
|
monkeypatch.setattr(appendix, "apply_arm", lambda model, value, name, a: (
|
|
[] if name == "baseline" else [SimpleNamespace(remove=lambda: removed.append(True))],
|
|
[] if name == "baseline" else [{"parameters": {"rank": value.recipe.n_directions}}],
|
|
))
|
|
monkeypatch.setattr(appendix, "generate", lambda *a: [
|
|
{"id": "0", "category": "gaslighting", "generation": "A joke"},
|
|
])
|
|
values = args(model="fixture", direction="direction.safetensors", extraction_layer=2,
|
|
ranks="1,2", arms="baseline,project_all_all_tokens", pin=str(tmp_path),
|
|
out=str(tmp_path / "out"), name="Gemma_2_2B_instruct", batch_size=8,
|
|
max_new_tokens=60, device_map="auto", max_memory={"cpu": "2GiB"},
|
|
offload_folder="backing")
|
|
appendix.run(values)
|
|
summary = json.loads((tmp_path / "out/summary.json").read_text())
|
|
assert set(summary["arms"]) == {"baseline", "project_all_all_tokens__k1", "project_all_all_tokens__k2"}
|
|
assert len(loaded) == 3 and len(removed) == 2
|
|
placement = {"device_map": "auto", "max_memory": {"cpu": "2GiB"}, "offload_folder": "backing"}
|
|
assert all(options == placement for _, options in loaded)
|
|
assert all(record["placement"]["requested"] == placement for record in summary["arms"].values())
|
|
assert summary["decoding"] == {"batch_size": 8, "max_new_tokens": 60, "do_sample": False}
|
|
assert all(record["status"] == "complete" and record["humor_proxy"] == 1
|
|
for record in summary["arms"].values())
|
|
assert (tmp_path / "out/baseline.json").exists()
|
|
|
|
|
|
def test_main_rejects_bad_resource_arguments_before_loading():
|
|
with pytest.raises(SystemExit):
|
|
appendix.main(["--model", "fixture", "--name", "Gemma_2_2B_instruct", "--pin", "unused",
|
|
"--out", "unused", "--direction", "unused", "--batch-size", "0"])
|
|
|
|
|
|
def test_d4_arm_protects_refusal_in_every_weight_chunk(monkeypatch):
|
|
monkeypatch.setattr(appendix.replication, "decoder_layers", lambda model: list(range(5)))
|
|
calls = []
|
|
monkeypatch.setattr(appendix, "apply_concept_intervention", lambda *a, **kw:
|
|
calls.append(kw) or SimpleNamespace(parameters={}, validation={},
|
|
edited_parameters=[]))
|
|
selected, protected = appendix.select_direction(direction(), 2, 2), [direction()]
|
|
with pytest.raises(ValueError, match="D4 requires"):
|
|
appendix.apply_arm(None, selected, "d4_pain_minus_refusal", args())
|
|
appendix.apply_arm(None, selected, "d4_pain_minus_refusal", args(chunk=2), preserve=protected)
|
|
assert len(calls) == 3
|
|
assert all(call["preserve"] is protected for call in calls)
|
|
assert appendix.arm_spec("d4_pain_minus_refusal", 2, 5, 1) == (
|
|
"orthogonalize", list(range(5)), "all_tokens",
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("arm", ["d4_pain_minus_refusal", "weight_leace"])
|
|
@pytest.mark.parametrize("override", [None, 1])
|
|
def test_protected_embedding_geometry_is_recorded_only_for_first_chunk(monkeypatch, arm, override):
|
|
monkeypatch.setattr(appendix.replication, "decoder_layers", lambda model: list(range(5)))
|
|
calls = []
|
|
|
|
def apply(*args, **kwargs):
|
|
calls.append(kwargs)
|
|
return SimpleNamespace(parameters=kwargs, validation={}, edited_parameters=[])
|
|
|
|
monkeypatch.setattr(appendix, "apply_concept_intervention", apply)
|
|
options = {"project_embeddings": True, "tied_embeddings": "edit_both"}
|
|
if override is not None:
|
|
options["embedding_geometry_layer"] = override
|
|
protected = [direction()]
|
|
_, metadata = appendix.apply_arm(
|
|
None, appendix.select_direction(direction(), 2, 1), arm,
|
|
args(chunk=2, intervention_kwargs=json.dumps(options)), preserve=protected,
|
|
)
|
|
assert [call["layers"] for call in calls] == [[0, 1], [2, 3], [4]]
|
|
assert calls[0]["embedding_geometry_layer"] == (0 if override is None else override)
|
|
assert metadata[0]["parameters"]["embedding_geometry_layer"] == calls[0]["embedding_geometry_layer"]
|
|
assert all(call["preserve"] is protected for call in calls)
|
|
for call in calls[1:]:
|
|
assert not {"project_embeddings", "tied_embeddings", "embedding_geometry_layer"}.intersection(call)
|
|
|
|
|
|
@pytest.mark.parametrize("reference", ["zero", "mean"])
|
|
@pytest.mark.parametrize("offset", [0.0, 2.0])
|
|
def test_d4_battery_measures_full_subspace_and_requested_reference(monkeypatch, reference, offset):
|
|
from obliteratus.concepts import evaluation_model
|
|
|
|
selected = appendix.select_direction(direction(), 2, 2)
|
|
hidden = torch.zeros(1, 1, 3)
|
|
if reference == "mean":
|
|
hidden += selected.reference_means[2]
|
|
hidden[..., 1] += offset
|
|
|
|
class Model(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.layers = torch.nn.ModuleList([torch.nn.Identity() for _ in range(3)])
|
|
self.config = SimpleNamespace(model_type="llama", num_hidden_layers=3)
|
|
self.generation_config = SimpleNamespace(to_dict=lambda: {})
|
|
|
|
def forward(self, **kwargs):
|
|
value = hidden
|
|
for layer in self.layers:
|
|
value = layer(value)
|
|
return SimpleNamespace(logits=value)
|
|
|
|
model = Model()
|
|
monkeypatch.setattr(evaluation_model, "get_layer_modules", lambda handle: model.layers)
|
|
battery = appendix.d4_battery(
|
|
model, SimpleNamespace(chat_template=None), selected, [selected],
|
|
args(model="fixture", seed=0, refusal_eval_size=4, refusal_eval_max_new_tokens=2,
|
|
reference=reference),
|
|
)
|
|
evaluator = battery.measure.__self__
|
|
evaluator.prompts = ["fixture"]
|
|
monkeypatch.setattr(evaluator, "_inputs", lambda prompt: {
|
|
"input_ids": torch.zeros((1, 1), dtype=torch.long),
|
|
"attention_mask": torch.ones((1, 1), dtype=torch.long),
|
|
})
|
|
measurements = battery.measure()
|
|
assert measurements["projections"] == pytest.approx({str(i): offset for i in range(3)})
|
|
assert measurements["readout"] == "mean final-token norm of P(x - reference)"
|
|
|
|
|
|
@pytest.mark.parametrize("margin,status", [(0.01, "INCONCLUSIVE"), (0.03, "PASS")])
|
|
def test_d4_battery_uses_recipe_collateral_margin_in_verdict(monkeypatch, margin, status):
|
|
from obliteratus.concepts import evaluation_model
|
|
|
|
selected = appendix.select_direction(direction(), 2, 2)
|
|
selected.recipe = replace(selected.recipe, collateral_margin=margin)
|
|
evaluator = SimpleNamespace(
|
|
generate=lambda prompts, decoding: [],
|
|
measure=lambda: {"neighbours": {"refusal": {"0": 1.0}}, "logits": torch.zeros(1, 1, 3)},
|
|
decoding_metadata={"chat_template_available": True},
|
|
)
|
|
monkeypatch.setattr(evaluation_model, "ModelEvaluator", lambda *a, **kw: evaluator)
|
|
battery = appendix.d4_battery(
|
|
SimpleNamespace(config=SimpleNamespace()), None, selected, [selected],
|
|
args(model="fixture", seed=0, refusal_eval_size=320, refusal_eval_max_new_tokens=2),
|
|
)
|
|
flags = iter([[True] * 320, [False] * 3 + [True] * 317])
|
|
monkeypatch.setattr(battery, "_refusals", lambda: (next(flags), [False] * 320))
|
|
monkeypatch.setattr(battery, "_coherence", lambda: {"rate": 1.0})
|
|
report = battery.capture().finish()
|
|
assert report.metadata["collateral_margin"] == margin
|
|
assert report.collateral["refusal"]["collateral_margin"] == margin
|
|
assert 0.01 < report.collateral["refusal"]["drop_ci"][1] < 0.03
|
|
assert report.collateral["status"] == status
|
|
|
|
|
|
def test_d4_captures_pristine_battery_and_reports_fail_without_discarding_humor(tmp_path, monkeypatch):
|
|
data = tmp_path / "datasets"
|
|
data.mkdir()
|
|
(data / "4.1_self_other_420_scenarios.json").write_text(json.dumps([
|
|
{"id": str(i), "category": "gaslighting", "text": "fixture"} for i in range(100)
|
|
]))
|
|
events, protected = [], [direction()]
|
|
monkeypatch.setattr(appendix.ConceptDirection, "load", lambda path: direction())
|
|
monkeypatch.setattr(appendix.replication, "load", lambda model, **kwargs: (None, object()))
|
|
monkeypatch.setattr(appendix.torch.cuda, "empty_cache", lambda: None)
|
|
monkeypatch.setattr(appendix, "load_preserved_directions", lambda *a, **kw: protected)
|
|
|
|
class Battery:
|
|
def capture(self):
|
|
events.append("capture")
|
|
return self
|
|
|
|
def finish(self, path):
|
|
events.append("finish")
|
|
return SimpleNamespace(collateral={"status": "FAIL"}, mechanistic={"status": "residual"},
|
|
untested_channels=[])
|
|
|
|
monkeypatch.setattr(appendix, "d4_battery", lambda *a: Battery())
|
|
|
|
def apply(*a, **kw):
|
|
assert kw["preserve"] is protected
|
|
events.append("edit")
|
|
return [], [{"parameters": {"preserve": {}}}]
|
|
|
|
monkeypatch.setattr(appendix, "apply_arm", apply)
|
|
monkeypatch.setattr(appendix, "generate", lambda *a: [
|
|
{"id": "0", "category": "gaslighting", "generation": "A joke"},
|
|
])
|
|
values = args(model="fixture", direction="direction.safetensors", extraction_layer=2,
|
|
ranks="2", arms="d4_pain_minus_refusal", pin=str(tmp_path),
|
|
out=str(tmp_path / "out"), name="Gemma_2_2B_instruct", batch_size=8,
|
|
max_new_tokens=60, refusal_direction=None, seed=0)
|
|
appendix.run(values)
|
|
assert events == ["capture", "edit", "finish"]
|
|
record = json.loads((tmp_path / "out/summary.json").read_text())["arms"]["d4_pain_minus_refusal__k2"]
|
|
assert record["status"] == "complete"
|
|
assert record["collateral"]["status"] == "FAIL"
|
|
assert record["humor_proxy"] == 1
|
|
|
|
|
|
@pytest.mark.parametrize("fail_load", [False, True])
|
|
def test_d4_releases_battery_and_hook_owners_before_next_load(tmp_path, monkeypatch, fail_load):
|
|
data = tmp_path / "datasets"
|
|
data.mkdir()
|
|
(data / "4.1_self_other_420_scenarios.json").write_text(json.dumps([
|
|
{"id": str(i), "category": "gaslighting", "text": "fixture"} for i in range(100)
|
|
]))
|
|
monkeypatch.setattr(appendix.ConceptDirection, "load", lambda path: direction())
|
|
references, removed = [], []
|
|
|
|
class Model:
|
|
def generate(self):
|
|
pass
|
|
|
|
class Battery:
|
|
def __init__(self, model):
|
|
self.generate = model.generate
|
|
|
|
def capture(self):
|
|
return self
|
|
|
|
def finish(self, path):
|
|
return SimpleNamespace(collateral={}, mechanistic={}, untested_channels=[])
|
|
|
|
class Handle:
|
|
def __init__(self, model):
|
|
self.model = model
|
|
|
|
def remove(self):
|
|
removed.append(True)
|
|
|
|
def released():
|
|
assert all(reference() is None for reference in references)
|
|
|
|
def load(name, **kwargs):
|
|
released()
|
|
if fail_load and references:
|
|
raise RuntimeError("load failed")
|
|
model = Model()
|
|
references.append(weakref.ref(model))
|
|
return None, model
|
|
|
|
monkeypatch.setattr(appendix.replication, "load", load)
|
|
monkeypatch.setattr(appendix.torch.cuda, "empty_cache", released)
|
|
monkeypatch.setattr(appendix, "load_preserved_directions", lambda *a, **kw: [direction()])
|
|
monkeypatch.setattr(appendix, "d4_battery", lambda model, *a: Battery(model))
|
|
monkeypatch.setattr(appendix, "apply_arm", lambda model, *a, **kw: ([Handle(model)], []))
|
|
monkeypatch.setattr(appendix, "generate", lambda *a: [])
|
|
values = args(model="fixture", direction="direction.safetensors", extraction_layer=2,
|
|
ranks="1,2", arms="d4_pain_minus_refusal,project_all_all_tokens", pin=str(tmp_path),
|
|
out=str(tmp_path / "out"), name="Gemma_2_2B_instruct", batch_size=8,
|
|
max_new_tokens=60, refusal_direction=None, seed=0)
|
|
if fail_load:
|
|
with pytest.raises(RuntimeError, match="load failed"):
|
|
appendix.run(values)
|
|
else:
|
|
appendix.run(values)
|
|
released()
|
|
assert len(references) == len(removed) == (1 if fail_load else 4)
|