mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
# Conflicts: # obliteratus/concepts/direction.py # tests/test_concept_intervention.py
423 lines
20 KiB
Python
423 lines
20 KiB
Python
"""CV-04 tensor recipe contracts, including held-out selection and persistence."""
|
|
|
|
from contextlib import nullcontext
|
|
from dataclasses import replace
|
|
import warnings
|
|
import json
|
|
|
|
import pytest
|
|
from safetensors import safe_open
|
|
import torch
|
|
|
|
from obliteratus.concepts import ConceptDirection, ExtractionRecipe
|
|
from obliteratus.concepts import extraction
|
|
from obliteratus.concepts.extraction import (
|
|
control_principal_components,
|
|
extract_directions,
|
|
extract_layer,
|
|
pooled_control_mean,
|
|
projection_auc,
|
|
readout_activations,
|
|
)
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def single_thread():
|
|
previous = torch.get_num_threads()
|
|
torch.set_num_threads(1)
|
|
yield
|
|
torch.set_num_threads(previous)
|
|
|
|
|
|
def planted_cache():
|
|
generator = torch.Generator().manual_seed(227)
|
|
positive, controls = {}, {"neutral": {}, "other": {}}
|
|
for layer in range(4):
|
|
positive[layer] = torch.randn(40, 16, generator=generator) * 0.05
|
|
for name, count, offset in (("neutral", 32, 8), ("other", 50, -8)):
|
|
values = torch.randn(count, 16, generator=generator) * 0.05
|
|
values[:, 0] = torch.linspace(-4, 4, count) + offset
|
|
controls[name][layer] = values
|
|
positive[layer][:, 0] += 3
|
|
positive[2][:, 1] += 4
|
|
return positive, controls
|
|
|
|
|
|
def cv_recipe(**changes):
|
|
return ExtractionRecipe(
|
|
denoise={"control_pc_variance": 0.8},
|
|
layer_select={"strategy": "cv_auc", "folds": 4}, **changes,
|
|
)
|
|
|
|
|
|
def test_planted_direction_cv_and_seed_repeat():
|
|
positive, controls = planted_cache()
|
|
first = extract_directions(positive, controls, cv_recipe(), seed=227)
|
|
second = extract_directions(positive, controls, cv_recipe(), seed=227)
|
|
assert first.extraction_layers == second.extraction_layers == [2]
|
|
assert first.cv_scores == second.cv_scores
|
|
assert first.directions[2][1] >= 0.95
|
|
for layer in positive:
|
|
assert torch.nn.functional.cosine_similarity(
|
|
first.directions[layer], second.directions[layer], dim=0,
|
|
) >= 0.9999
|
|
assert first.sample_counts == {"positive": 40, "control:neutral": 32, "control:other": 50}
|
|
assert first.intervention_layers == [2]
|
|
assert first.extraction_layer_source == "selected"
|
|
|
|
|
|
@pytest.mark.parametrize("layer", [0, 3])
|
|
@pytest.mark.parametrize("strategy", ["cv_auc", "preset"])
|
|
def test_model_pin_bypasses_cv_and_round_trips(layer, strategy, monkeypatch, tmp_path):
|
|
positive, controls = planted_cache()
|
|
recipe = replace(cv_recipe(), extraction_layer={"offline": layer},
|
|
layer_select={"strategy": strategy, "folds": 100})
|
|
|
|
def no_cv(*args, **kwargs):
|
|
pytest.fail("A pinned extraction must not split or score CV folds")
|
|
|
|
monkeypatch.setattr(extraction, "sample_folds", no_cv)
|
|
monkeypatch.setattr(extraction, "projection_auc", no_cv)
|
|
direction = extract_directions(positive, controls, recipe, model_id="offline", preset_layers=[])
|
|
assert direction.extraction_layers == direction.intervention_layers == [layer]
|
|
assert direction.extraction_layer_source == "pinned"
|
|
assert direction.cv_scores == {}
|
|
expected = extract_layer(positive[layer], {n: c[layer] for n, c in controls.items()}, recipe)
|
|
assert torch.equal(direction.directions[layer], expected.subspace[0])
|
|
path = tmp_path / "pinned.safetensors"
|
|
direction.save(path)
|
|
restored = ConceptDirection.load(path)
|
|
assert restored.extraction_layer_source == "pinned"
|
|
assert restored.extraction_layers == [layer]
|
|
assert restored.recipe == recipe
|
|
|
|
|
|
def test_model_pin_exact_match_fallback_and_missing_cache():
|
|
positive, controls = planted_cache()
|
|
recipe = replace(cv_recipe(), extraction_layer={"canonical/model": 3})
|
|
for model in ("mirror/model", "canonical/model-copy", "", "/local/model"):
|
|
direction = extract_directions(positive, controls, recipe, model_id=model, seed=227)
|
|
assert direction.extraction_layers == [2]
|
|
assert direction.extraction_layer_source == "selected"
|
|
assert direction.cv_scores
|
|
with pytest.raises(ValueError, match="Pinned extraction layer 8.*outside cached"):
|
|
extract_directions(positive, controls, replace(recipe, extraction_layer={"model": 8}),
|
|
model_id="model")
|
|
|
|
|
|
@pytest.mark.parametrize("rule,expected", [
|
|
({"rule": "same"}, [0]),
|
|
({"rule": "norm_ratio", "target": 0.6}, [1]),
|
|
({"rule": "explicit", "layers": [2]}, [2]),
|
|
])
|
|
def test_pinned_extraction_retains_independent_intervention_rule(rule, expected):
|
|
positive = {i: torch.tensor([[x, 4.]]).repeat(8, 1)
|
|
for i, x in enumerate((3., 4., 12.))}
|
|
controls = {"neutral": {i: rows - torch.tensor([0., 4.]) for i, rows in positive.items()}}
|
|
direction = extract_directions(positive, controls, ExtractionRecipe(
|
|
extraction_layer={"model": 0}, intervention_layer=rule,
|
|
layer_select={"strategy": "cv_auc"},
|
|
), model_id="model")
|
|
assert direction.extraction_layers == [0]
|
|
assert direction.intervention_layers == expected
|
|
|
|
|
|
def test_grouped_cv_selects_transfer_signal_instead_of_item_memorization():
|
|
from obliteratus.concepts import ConceptTarget
|
|
|
|
# Repeated perspectives share an item-specific feature at layer 0.
|
|
# It predicts held-out samples of known items, but cannot transfer to new items.
|
|
groups = [f"item-{i}" for i in range(4) for _ in range(16)]
|
|
items = torch.eye(4).repeat_interleave(16, dim=0)
|
|
positive = {0: items, 1: torch.tensor([[2.], [0.]]).repeat(32, 4)}
|
|
controls = {"neutral": {0: -items, 1: torch.tensor([[1.], [-1.]]).repeat(32, 4)}}
|
|
recipe = ExtractionRecipe(layer_select={"strategy": "cv_auc", "folds": 2})
|
|
sample = extract_directions(positive, controls, recipe, seed=331)
|
|
grouped_recipe = replace(recipe, layer_select={**recipe.layer_select, "group_by": "item"})
|
|
target = ConceptTarget(
|
|
"user/grouped", ["positive"] * 64, {"neutral": ["control"] * 64}, recipe=grouped_recipe,
|
|
records={name: [{"text": text, "item": group, "person": i % 2}
|
|
for i, group in enumerate(groups)]
|
|
for name, text in (("positive", "positive"), ("control:neutral", "control"))},
|
|
)
|
|
grouped = extract_directions(positive, controls, grouped_recipe, seed=331,
|
|
sample_groups=target.sample_groups())
|
|
repeated = extract_directions(positive, controls, grouped_recipe, seed=331,
|
|
sample_groups=target.sample_groups())
|
|
assert sample.extraction_layers == [0]
|
|
assert grouped.extraction_layers == [1]
|
|
assert grouped.cv_scores == repeated.cv_scores
|
|
assert grouped.cv_scores[0] == [0.5, 0.5]
|
|
assert grouped.cv_scores[1] == [0.75, 0.75]
|
|
assert target.entry_point is None
|
|
with pytest.raises(ValueError, match="sample groups"):
|
|
extract_directions(positive, controls, grouped_recipe)
|
|
missing = replace(target, records={})
|
|
with pytest.raises(ValueError, match="group_by requires"):
|
|
missing.sample_groups()
|
|
|
|
|
|
def test_grouping_fields_must_agree_and_records_affect_identity():
|
|
from obliteratus.concepts import ConceptTarget
|
|
from obliteratus.concepts.entry_points import EntryPoint
|
|
|
|
recipe = ExtractionRecipe(layer_select={"strategy": "cv_auc", "group_by": "item"},
|
|
entry_point=EntryPoint(group_by="perspective"))
|
|
with pytest.raises(ValueError, match="must agree"):
|
|
extraction.validate_recipe(recipe)
|
|
extraction.validate_recipe(replace(recipe, entry_point=EntryPoint(group_by="item")))
|
|
target = ConceptTarget("grouped", ["positive"], {"neutral": ["control"]},
|
|
recipe=replace(recipe, entry_point=None))
|
|
annotated = replace(target, records={"positive": [{"text": "positive", "item": "first"}]})
|
|
assert target.activation_identity() != annotated.activation_identity()
|
|
|
|
|
|
def test_unequal_controls_use_mean_of_means_without_pairing():
|
|
positive = torch.tensor([[6., 9.]]).repeat(40, 1)
|
|
controls = {
|
|
"small": torch.tensor([[2., 0.]]).repeat(32, 1),
|
|
"large": torch.tensor([[10., 0.]]).repeat(50, 1),
|
|
}
|
|
assert torch.equal(pooled_control_mean(controls), torch.tensor([6., 0.]))
|
|
result = extract_layer(positive, controls, ExtractionRecipe())
|
|
assert torch.equal(result.subspace, torch.tensor([[0., 1.]]))
|
|
concat = extract_layer(positive, controls, ExtractionRecipe(pool_controls="concat"))
|
|
assert concat.subspace[0, 0].abs() > 0.09
|
|
# Duplicating samples in one named set cannot change its contribution.
|
|
duplicated = {**controls, "small": controls["small"].repeat(3, 1)}
|
|
assert torch.equal(
|
|
result.subspace, extract_layer(positive, duplicated, ExtractionRecipe()).subspace,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("method,n_directions", [
|
|
("diff_means", 1), ("svd", 3), ("fisher_lda", 1), ("leace", 1), ("leace_exact", 1),
|
|
])
|
|
def test_denoising_removes_control_pcs_for_every_method(method, n_directions):
|
|
positive, controls = planted_cache()
|
|
groups = {name: cache[2] for name, cache in controls.items()}
|
|
with pytest.warns(DeprecationWarning) if method == "leace" else nullcontext():
|
|
result = extract_layer(
|
|
positive[2], groups, cv_recipe(method=method, n_directions=n_directions),
|
|
)
|
|
pcs = control_principal_components(groups, 0.8)
|
|
assert len(pcs) == 1
|
|
assert (result.subspace @ pcs.T).abs().max() < 1e-5
|
|
torch.testing.assert_close(result.subspace.norm(dim=1), torch.ones(len(result.subspace)))
|
|
# Fisher (including its leace alias) whitens the signal, so recovery is
|
|
# measured by class separation rather than cosine with the planted axis.
|
|
for group in groups.values():
|
|
assert projection_auc(positive[2] @ result.subspace[0], group @ result.subspace[0]) >= 0.99
|
|
if method not in {"fisher_lda", "leace"}:
|
|
assert result.subspace[0, 1] >= 0.95
|
|
|
|
|
|
def test_control_pc_variance_selects_minimal_prefix_and_ignores_null_space():
|
|
controls = {"one": torch.tensor([[3., 0., 0.], [-3., 0., 0.],
|
|
[0., 1., 0.], [0., -1., 0.]])}
|
|
assert len(control_principal_components(controls, 0)) == 0
|
|
assert len(control_principal_components(controls, 0.8)) == 1
|
|
assert len(control_principal_components(controls, 0.95)) == 2
|
|
assert len(control_principal_components(controls, 1)) == 2
|
|
assert len(control_principal_components({"constant": torch.ones(8, 3)}, 1)) == 0
|
|
|
|
|
|
@pytest.mark.parametrize("fraction", [True, False, -0.1, 1.1, float("nan"), "0.8", None])
|
|
def test_control_pc_variance_rejects_invalid_values(fraction):
|
|
with pytest.raises(ValueError, match="variance_fraction must be in"):
|
|
control_principal_components({"neutral": torch.ones(4, 3)}, fraction)
|
|
|
|
|
|
@pytest.mark.parametrize("method", ["diff_means", "svd", "fisher_lda", "leace_exact"])
|
|
def test_zero_signal_stays_finite_and_does_not_invent_directions(method):
|
|
values = torch.zeros(6, 4)
|
|
result = extract_layer(values, {"neutral": values}, ExtractionRecipe(method=method))
|
|
assert torch.equal(result.subspace, torch.zeros(1, 4))
|
|
|
|
|
|
@pytest.mark.parametrize("positive,control,expected", [
|
|
([2., 3.], [0., 1.], 1.), ([0., 1.], [2., 3.], 0.),
|
|
([1., 1.], [1., 1., 1.], 0.5), ([0., 1., 2.], [1., 2.], 1 / 3),
|
|
])
|
|
def test_rank_auc_handles_ties_and_orientation(positive, control, expected):
|
|
assert projection_auc(torch.tensor(positive), torch.tensor(control)) == pytest.approx(expected)
|
|
|
|
|
|
def test_cv_denoising_is_fit_only_on_training_folds(monkeypatch):
|
|
positive, controls = planted_cache()
|
|
sizes = []
|
|
original = extraction.control_principal_components
|
|
|
|
def record(groups, *args):
|
|
sizes.append(tuple(len(group) for group in groups.values()))
|
|
return original(groups, *args)
|
|
|
|
monkeypatch.setattr(extraction, "control_principal_components", record)
|
|
extract_directions(positive, controls, cv_recipe(), seed=227)
|
|
assert sizes.count((32, 50)) == 4 # One final fit per layer.
|
|
assert sizes.count((24, 37)) == 8
|
|
assert sizes.count((24, 38)) == 8
|
|
|
|
|
|
@pytest.mark.parametrize("rule,expected", [
|
|
({"rule": "same"}, [0]),
|
|
({"rule": "norm_ratio", "target": 0.6}, [1]),
|
|
({"rule": "explicit", "layers": [2, 1]}, [2, 1]),
|
|
])
|
|
def test_intervention_layer_rule_is_separate_from_extraction(rule, expected):
|
|
positive = {0: torch.tensor([[3., 4.]]).repeat(8, 1),
|
|
1: torch.tensor([[4., 4.]]).repeat(8, 1),
|
|
2: torch.tensor([[12., 4.]]).repeat(8, 1)}
|
|
controls = {"neutral": {layer: values - torch.tensor([0., 4.])
|
|
for layer, values in positive.items()}}
|
|
direction = extract_directions(
|
|
positive, controls, ExtractionRecipe(intervention_layer=rule), preset_layers=[0],
|
|
)
|
|
assert direction.extraction_layers == [0]
|
|
assert direction.intervention_layers == expected
|
|
|
|
|
|
@pytest.mark.parametrize("position", ["final_token", "mean_pool"])
|
|
@pytest.mark.parametrize("side", ["left", "right"])
|
|
def test_readout_ignores_padding(position, side):
|
|
if side == "left":
|
|
hidden = torch.tensor([[[99.], [2.], [4.]], [[1.], [3.], [5.]]])
|
|
mask = torch.tensor([[0, 1, 1], [1, 1, 1]])
|
|
else:
|
|
hidden = torch.tensor([[[2.], [4.], [99.]], [[1.], [3.], [5.]]])
|
|
mask = torch.tensor([[1, 1, 0], [1, 1, 1]])
|
|
expected = (torch.tensor([[4.], [5.]]) if position == "final_token"
|
|
else torch.tensor([[3.], [3.]]))
|
|
assert torch.equal(readout_activations(hidden, mask, position), expected)
|
|
|
|
|
|
def test_complete_direction_round_trip_without_torch_serialization(tmp_path, monkeypatch):
|
|
def reject_pickle(*args, **kwargs):
|
|
pytest.fail("torch pickle serialization was called")
|
|
|
|
monkeypatch.setattr(torch, "load", reject_pickle)
|
|
monkeypatch.setattr(torch, "save", reject_pickle)
|
|
positive, controls = planted_cache()
|
|
direction = extract_directions(
|
|
positive, controls, cv_recipe(intervention_layer={"rule": "explicit", "layers": [1]}),
|
|
seed=227, model_id="offline", model_revision="abc", pack_hash="hash", concept_id="demo",
|
|
)
|
|
path = tmp_path / "direction.safetensors"
|
|
direction.save(path)
|
|
assert {file.suffix for file in tmp_path.iterdir()} == {".safetensors", ".json"}
|
|
with safe_open(path, framework="pt", device="cpu") as tensors:
|
|
assert set(tensors.keys()) == {
|
|
f"{kind}.{layer}" for kind in ("directions", "subspaces", "reference_means")
|
|
for layer in positive
|
|
}
|
|
restored = ConceptDirection.load(path)
|
|
for name in direction.__dataclass_fields__:
|
|
expected, actual = getattr(direction, name), getattr(restored, name)
|
|
if name in {"directions", "subspaces", "reference_means", "projector_left", "projector_right"}:
|
|
assert expected.keys() == actual.keys()
|
|
for layer in expected:
|
|
assert torch.equal(expected[layer], actual[layer])
|
|
else:
|
|
assert expected == actual
|
|
metadata = json.loads(path.with_suffix(".json").read_text())
|
|
assert metadata["direction_method_effective"] == "diff_means"
|
|
|
|
|
|
def test_load_accepts_pre_cv04_schema_one_metadata(tmp_path):
|
|
path = tmp_path / "direction.safetensors"
|
|
ConceptDirection({0: torch.ones(4)}).save(path)
|
|
metadata = json.loads(path.with_suffix(".json").read_text())
|
|
for key in ("cv_scores", "sample_counts", "direction_method_effective", "extraction_layer_source"):
|
|
del metadata[key]
|
|
metadata["recipe"]["pool_controls"] = "concat"
|
|
path.with_suffix(".json").write_text(json.dumps(metadata))
|
|
direction = ConceptDirection.load(path)
|
|
assert direction.cv_scores == direction.sample_counts == {}
|
|
assert direction.recipe.pool_controls == "concat"
|
|
assert direction.extraction_layer_source == "selected"
|
|
|
|
|
|
@pytest.mark.parametrize("changes,match", [
|
|
({"method": "som"}, "not available"), ({"method": "unknown"}, "Unknown direction"),
|
|
({"n_directions": 0}, "n_directions"), ({"n_directions": 2}, "requires n_directions=1"),
|
|
({"pool_controls": "paired"}, "pool_controls"),
|
|
({"readout": {"position": "first_token"}}, "readout"),
|
|
({"readout": {"suffix": 2}}, "readout"),
|
|
({"denoise": {"control_pc_variance": 1.1}}, "control_pc_variance"),
|
|
({"denoise": {"control_pc_variance": True}}, "control_pc_variance"),
|
|
({"denoise": {"control_pc_variance": False}}, "control_pc_variance"),
|
|
({"layer_select": {"strategy": "cv_auc", "folds": 1}}, "folds"),
|
|
({"layer_select": {"strategy": "unknown"}}, "strategy"),
|
|
({"layer_select": {"strategy": "cv_auc", "group_by": " "}}, "group_by"),
|
|
({"extraction_layer": {"model": -1}}, "extraction_layer"),
|
|
({"extraction_layer": {"model": True}}, "extraction_layer"),
|
|
({"extraction_layer": {"model": 1.5}}, "extraction_layer"),
|
|
({"extraction_layer": {"": 1}}, "extraction_layer"),
|
|
({"extraction_layer": {3: 1}}, "extraction_layer"),
|
|
({"extraction_layer": [1]}, "extraction_layer"),
|
|
({"layer_select": {"strategy": "preset", "preset": "bogus"}}, "selection preset"),
|
|
({"intervention_layer": {"rule": "unknown"}}, "rule"),
|
|
({"intervention_layer": {"rule": "norm_ratio", "target": float("nan")}}, "target"),
|
|
({"intervention_layer": {"rule": "explicit", "layers": [0, 0]}}, "distinct"),
|
|
])
|
|
def test_invalid_recipe_rejected_before_tensor_math(changes, match):
|
|
with pytest.raises(ValueError, match=match):
|
|
extract_layer(torch.ones(4, 3), {"neutral": torch.zeros(6, 3)},
|
|
replace(ExtractionRecipe(), **changes))
|
|
|
|
|
|
@pytest.mark.parametrize("preset", [
|
|
"all", "all_except_first", "middle60", "top_k", "knee", "knee_cosmic",
|
|
])
|
|
def test_recipe_accepts_supported_layer_selection_presets(preset):
|
|
extraction.validate_recipe(ExtractionRecipe(
|
|
layer_select={"strategy": "preset", "preset": preset},
|
|
))
|
|
|
|
|
|
def test_cv_rejects_empty_folds_and_uncached_intervention_layers():
|
|
positive = {0: torch.ones(4, 3)}
|
|
controls = {"neutral": {0: torch.zeros(6, 3)}}
|
|
with pytest.raises(ValueError, match="size of any prompt set"):
|
|
extract_directions(positive, controls, replace(cv_recipe(),
|
|
layer_select={"strategy": "cv_auc", "folds": 5}))
|
|
with pytest.raises(ValueError, match="outside cached"):
|
|
extract_directions(positive, controls, ExtractionRecipe(
|
|
intervention_layer={"rule": "explicit", "layers": [2]},
|
|
))
|
|
|
|
|
|
def test_leace_alias_warns_once_for_multilayer_cv_and_preserves_directions(tmp_path):
|
|
positive, controls = planted_cache()
|
|
with warnings.catch_warnings(record=True) as caught:
|
|
warnings.simplefilter("always", DeprecationWarning)
|
|
alias = extract_directions(positive, controls, cv_recipe(method="leace"), seed=227)
|
|
alias.save(tmp_path / "alias.safetensors")
|
|
assert alias.direction_method_effective == "fisher_lda"
|
|
assert len(caught) == 1
|
|
assert caught[0].category is DeprecationWarning
|
|
assert caught[0].filename == __file__
|
|
canonical = extract_directions(positive, controls, cv_recipe(method="fisher_lda"), seed=227)
|
|
assert alias.cv_scores == canonical.cv_scores
|
|
assert alias.extraction_layers == canonical.extraction_layers
|
|
for layer in positive:
|
|
assert torch.equal(alias.directions[layer], canonical.directions[layer])
|
|
with pytest.warns(DeprecationWarning) as loaded_warnings:
|
|
restored = ConceptDirection.load(tmp_path / "alias.safetensors")
|
|
assert len(loaded_warnings) == 1
|
|
assert restored.recipe.method == "leace"
|
|
assert restored.direction_method_effective == "fisher_lda"
|
|
|
|
|
|
def test_leace_layer_alias_warns_once_and_preserves_direction():
|
|
positive, controls = planted_cache()
|
|
groups = {name: cache[2] for name, cache in controls.items()}
|
|
with pytest.warns(DeprecationWarning) as caught:
|
|
alias = extract_layer(positive[2], groups, cv_recipe(method="leace"))
|
|
assert len(caught) == 1
|
|
assert caught[0].filename == __file__
|
|
canonical = extract_layer(positive[2], groups, cv_recipe(method="fisher_lda"))
|
|
assert torch.equal(alias.subspace, canonical.subspace)
|