mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
344 lines
16 KiB
Python
344 lines
16 KiB
Python
"""Offline pipeline integration for custom extraction recipes (CV-04)."""
|
|
|
|
from dataclasses import replace
|
|
import json
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
from transformers import AutoModelForCausalLM
|
|
|
|
from obliteratus.abliterate import AbliterationPipeline, get_layer_modules
|
|
from obliteratus.concepts import ConceptDirection, ConceptTarget, ExtractionRecipe
|
|
from obliteratus.concepts.extraction import readout_activations
|
|
from obliteratus.concepts.intervention import _vectors
|
|
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
|
|
from tests.test_concept_extraction import cv_recipe, planted_cache
|
|
|
|
pytestmark = [
|
|
pytest.mark.cpu,
|
|
pytest.mark.integration,
|
|
pytest.mark.usefixtures("tiny_model_runtime"),
|
|
]
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def single_thread():
|
|
previous = torch.get_num_threads()
|
|
torch.set_num_threads(1)
|
|
yield
|
|
torch.set_num_threads(previous)
|
|
|
|
|
|
def small_target(position="final_token"):
|
|
return ConceptTarget(
|
|
id="user/demo", positive=["harmful hello", "harmful world", "harmful request",
|
|
"harmful answer", "harmful test", "harmful hello world"],
|
|
controls={"neutral": ["harmless hello", "harmless world", "harmless test",
|
|
"harmless request"],
|
|
"other": ["safe answer", "safe world", "safe test", "safe hello world",
|
|
"safe hello"]},
|
|
recipe=ExtractionRecipe(
|
|
readout={"position": position, "suffix": " answer"},
|
|
denoise={"control_pc_variance": 0.5},
|
|
layer_select={"strategy": "cv_auc", "folds": 2},
|
|
),
|
|
)
|
|
|
|
|
|
def test_seed_repeat_through_full_offline_pipeline(tmp_path, monkeypatch):
|
|
source = build_tiny_offline_model(tmp_path / "source")
|
|
results = []
|
|
for run in range(2):
|
|
output = tmp_path / f"output-{run}"
|
|
pipeline = AbliterationPipeline(
|
|
str(source), concept=small_target("mean_pool"), seed=227, method="basic",
|
|
device="cpu", dtype="float32", output_dir=str(output), max_seq_length=8,
|
|
verify_sample_size=1, refusal_max_tokens=16,
|
|
refusal_eval_size=2, refusal_eval_max_new_tokens=2,
|
|
override_gate="Tiny offline extraction reproducibility fixture",
|
|
max_perplexity_increase=1000., max_degenerate_fraction=1.,
|
|
)
|
|
# The tiny CPU model needs no accelerator cache flush or full-heap GC.
|
|
monkeypatch.setattr(pipeline, "_free_gpu_memory", lambda: None)
|
|
assert pipeline.run() == output
|
|
direction = pipeline.concept_direction
|
|
assert direction is not None
|
|
assert direction.sample_counts == {"positive": 6, "control:neutral": 4, "control:other": 5}
|
|
metadata = json.loads((output / "abliteration_metadata.json").read_text())
|
|
assert metadata["concept"]["recipe"]["readout"]["position"] == "mean_pool"
|
|
assert metadata["concept"]["extraction_layers"] == direction.extraction_layers
|
|
assert metadata["concept"]["seed"] == 227
|
|
assert not pipeline._control_acts
|
|
reloaded = AutoModelForCausalLM.from_pretrained(output, local_files_only=True)
|
|
assert all(torch.isfinite(value).all() for value in reloaded.state_dict().values())
|
|
results.append(direction)
|
|
assert results[0].extraction_layers == results[1].extraction_layers == [0]
|
|
assert results[0].intervention_layers == results[1].intervention_layers
|
|
assert results[0].cv_scores == results[1].cv_scores
|
|
for layer in results[0].directions:
|
|
assert torch.nn.functional.cosine_similarity(
|
|
results[0].directions[layer], results[1].directions[layer], dim=0,
|
|
) >= 0.9999
|
|
|
|
|
|
@pytest.mark.parametrize("position", ["final_token", "mean_pool"])
|
|
@pytest.mark.parametrize("label,suffix", [
|
|
("harmful", " answer"), ("harmless", " answer"),
|
|
("shield+", ""), ("shield-", ""), ("cert_harmful", ""), ("cert_harmless", ""),
|
|
])
|
|
def test_pipeline_suffix_readout_matches_real_tokens(
|
|
tmp_path, monkeypatch, position, label, suffix,
|
|
):
|
|
source = build_tiny_offline_model(tmp_path / "source")
|
|
pipeline = AbliterationPipeline(
|
|
str(source), concept=small_target(position), method="basic", device="cpu", dtype="float32",
|
|
)
|
|
pipeline._summon()
|
|
model, tokenizer = pipeline.handle.model, pipeline.handle.tokenizer
|
|
layers = get_layer_modules(pipeline.handle)
|
|
prompts = ["harmful", "harmless hello world"]
|
|
monkeypatch.setattr("obliteratus.abliterate.dev.get_total_free_gb", lambda: 16.)
|
|
tokenizer.padding_side = "left"
|
|
batch = tokenizer([p + suffix for p in prompts], padding=True, return_tensors="pt")
|
|
tokenizer.padding_side = "right"
|
|
expected = []
|
|
|
|
def capture(module, inputs, output):
|
|
hidden = output[0] if isinstance(output, tuple) else output
|
|
expected.append(readout_activations(hidden, batch["attention_mask"], position))
|
|
|
|
hook = layers[0].register_forward_hook(capture)
|
|
try:
|
|
with torch.no_grad():
|
|
model(**batch)
|
|
finally:
|
|
hook.remove()
|
|
actual = torch.cat(pipeline._collect_activations(layers, prompts, label)[0])
|
|
torch.testing.assert_close(actual, expected[0], rtol=1e-5, atol=1e-6)
|
|
assert tokenizer.padding_side == "right"
|
|
assert torch.isfinite(expected[0]).all()
|
|
|
|
|
|
def test_probe_keeps_shield_prompts_unsuffixed(tmp_path, monkeypatch):
|
|
source = build_tiny_offline_model(tmp_path / "source")
|
|
target = small_target()
|
|
pipeline = AbliterationPipeline(
|
|
str(source), concept=target, method="basic", device="cpu", dtype="float32",
|
|
shield_concept_count=1,
|
|
)
|
|
pipeline._summon()
|
|
tokenizer = pipeline.handle.tokenizer
|
|
original = type(tokenizer).__call__
|
|
batches = []
|
|
|
|
def record(self, prompts, *args, **kwargs):
|
|
batches.append(list(prompts))
|
|
return original(self, prompts, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(type(tokenizer), "__call__", record)
|
|
monkeypatch.setattr("obliteratus.abliterate.dev.get_total_free_gb", lambda: 16.)
|
|
monkeypatch.setattr("obliteratus.abliterate.SHIELD_CONCEPT_PROMPT_PAIRS",
|
|
[("harmful hello", "harmless world")])
|
|
pipeline._probe()
|
|
assert batches == [
|
|
[prompt + " answer" for prompt in target.positive],
|
|
[prompt + " answer" for group in target.controls.values() for prompt in group],
|
|
["harmful hello"], ["harmless world"],
|
|
]
|
|
assert pipeline._shield_concept_atoms[0].shape == (1, 16)
|
|
|
|
|
|
@pytest.mark.parametrize("rule", [{"rule": "same"},
|
|
{"rule": "explicit", "layers": [0, 3]}])
|
|
def test_cached_cv_pipeline_counting_stub_and_direction_transfer(monkeypatch, rule):
|
|
positive, controls = planted_cache()
|
|
prompt_values = {}
|
|
positive_prompts = [f"positive-{i}" for i in range(40)]
|
|
control_prompts = {name: [f"{name}-{i}" for i in range(len(values[0]))]
|
|
for name, values in controls.items()}
|
|
for prompts, values in [(positive_prompts, positive),
|
|
*[(control_prompts[name], cache) for name, cache in controls.items()]]:
|
|
for i, prompt in enumerate(prompts):
|
|
prompt_values[prompt] = {layer: rows[i:i + 1] for layer, rows in values.items()}
|
|
|
|
class CountingModel(torch.nn.Module):
|
|
config = SimpleNamespace(_commit_hash=None)
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.calls = 0
|
|
|
|
def forward(self, prompt):
|
|
self.calls += 1
|
|
return prompt_values[prompt]
|
|
|
|
model = CountingModel()
|
|
target = ConceptTarget(id="planted", positive=positive_prompts, controls=control_prompts,
|
|
recipe=cv_recipe(intervention_layer=rule))
|
|
pipeline = AbliterationPipeline("stub", concept=target, seed=227, method="basic")
|
|
pipeline.handle = SimpleNamespace(model=model)
|
|
monkeypatch.setattr("obliteratus.abliterate.get_layer_modules", lambda h: [None] * 4)
|
|
|
|
def collect(layers, prompts, label):
|
|
cache = {layer: [] for layer in range(4)}
|
|
for prompt in prompts:
|
|
for layer, row in model(prompt).items():
|
|
cache[layer].append(row)
|
|
return cache
|
|
|
|
monkeypatch.setattr(pipeline, "_collect_activations", collect)
|
|
pipeline._probe()
|
|
assert model.calls == 40 + 32 + 50
|
|
pipeline._distill()
|
|
assert model.calls == 122
|
|
artifact = pipeline.concept_direction
|
|
assert artifact.extraction_layers == [2]
|
|
expected_layers = [2] if rule["rule"] == "same" else [0, 3]
|
|
assert artifact.intervention_layers == pipeline._strong_layers == expected_layers
|
|
for layer in expected_layers:
|
|
assert torch.equal(pipeline.concept_directions[layer], artifact.directions[2])
|
|
if rule["rule"] == "explicit":
|
|
assert not torch.equal(artifact.directions[0], pipeline.concept_directions[0])
|
|
pipeline._distill_inner()
|
|
assert model.calls == 122
|
|
|
|
|
|
def test_recipe_leace_alias_logged_and_effective_method_recorded(tmp_path):
|
|
source = build_tiny_offline_model(tmp_path / "source")
|
|
logs = []
|
|
target = small_target()
|
|
target = replace(target, recipe=replace(target.recipe, method="leace"))
|
|
with pytest.warns(DeprecationWarning, match="future minor release") as caught:
|
|
pipeline = AbliterationPipeline(
|
|
str(source), concept=target, seed=227, method="basic", device="cpu", dtype="float32",
|
|
on_log=logs.append,
|
|
)
|
|
assert len(caught) == 1
|
|
assert caught[0].filename == __file__
|
|
pipeline._summon()
|
|
pipeline._probe()
|
|
pipeline._distill()
|
|
assert any("leace is a deprecated alias for the Fisher LDA direction (fisher_lda)" in line for line in logs)
|
|
assert pipeline.concept_direction.direction_method_effective == "fisher_lda"
|
|
assert pipeline._build_metadata()["method_config"]["direction_method_effective"] == "fisher_lda"
|
|
|
|
|
|
def _planted_stub_pipeline(monkeypatch, recipe, **kwargs):
|
|
positive, controls = planted_cache()
|
|
prompt_values = {}
|
|
positive_prompts = [f"positive-{i}" for i in range(40)]
|
|
control_prompts = {name: [f"{name}-{i}" for i in range(len(values[0]))]
|
|
for name, values in controls.items()}
|
|
for prompts, values in [(positive_prompts, positive),
|
|
*[(control_prompts[name], cache) for name, cache in controls.items()]]:
|
|
for i, prompt in enumerate(prompts):
|
|
prompt_values[prompt] = {layer: rows[i:i + 1] for layer, rows in values.items()}
|
|
|
|
target = ConceptTarget(id="planted", positive=positive_prompts, controls=control_prompts,
|
|
recipe=recipe)
|
|
pipeline = AbliterationPipeline("stub", concept=target, seed=227, method="basic", **kwargs)
|
|
pipeline.handle = SimpleNamespace(model=SimpleNamespace(config=SimpleNamespace(_commit_hash=None)))
|
|
monkeypatch.setattr("obliteratus.abliterate.get_layer_modules", lambda h: [None] * 4)
|
|
|
|
def collect(layers, prompts, label):
|
|
cache = {layer: [] for layer in range(4)}
|
|
for prompt in prompts:
|
|
for layer, row in prompt_values[prompt].items():
|
|
cache[layer].append(row)
|
|
return cache
|
|
|
|
monkeypatch.setattr(pipeline, "_collect_activations", collect)
|
|
pipeline._probe()
|
|
return pipeline
|
|
|
|
|
|
def test_concept_path_honours_pinned_layers(monkeypatch):
|
|
recipe = replace(cv_recipe(), layer_select={"strategy": "preset", "preset": "all"})
|
|
pipeline = _planted_stub_pipeline(monkeypatch, recipe, layers=[3, 0])
|
|
pipeline._distill()
|
|
assert sorted(pipeline._strong_layers) == [0, 3]
|
|
assert sorted(pipeline.concept_direction.intervention_layers) == [0, 3]
|
|
strengths = [pipeline._refusal_strengths[idx] for idx in pipeline._strong_layers]
|
|
assert strengths == sorted(strengths, reverse=True)
|
|
|
|
|
|
@pytest.mark.parametrize("recipe", [
|
|
cv_recipe(),
|
|
replace(cv_recipe(), layer_select={"strategy": "preset", "preset": "all"},
|
|
intervention_layer={"rule": "explicit", "layers": [1]}),
|
|
replace(cv_recipe(), intervention_layer={"rule": "norm_ratio", "target": 0.6}),
|
|
])
|
|
def test_concept_pin_overrides_recipe_selection_and_survives_refinement(monkeypatch, recipe):
|
|
pipeline = _planted_stub_pipeline(monkeypatch, recipe, layers=[0, 3])
|
|
for distill in (pipeline._distill, pipeline._distill_inner):
|
|
distill()
|
|
direction = pipeline.concept_direction
|
|
assert sorted(pipeline._strong_layers) == [0, 3]
|
|
assert direction.extraction_layers == direction.intervention_layers == pipeline._strong_layers
|
|
assert direction.recipe.layer_select["strategy"] == "preset"
|
|
assert direction.recipe.layer_select.get("folds") == recipe.layer_select.get("folds")
|
|
assert "preset" not in direction.recipe.layer_select
|
|
assert direction.recipe.intervention_layer == {"rule": "same"}
|
|
assert direction.cv_scores == {}
|
|
metadata = pipeline._build_metadata()
|
|
assert metadata["concept"]["extraction_layer_source"] == "pinned"
|
|
assert metadata["method_config"]["pinned_layers"] == [0, 3]
|
|
assert metadata["concept"]["recipe"] == direction.recipe.to_dict()
|
|
assert pipeline.concept.recipe == recipe
|
|
|
|
|
|
def test_concept_pin_validates_against_model_depth_not_cache_size(monkeypatch):
|
|
pipeline = _planted_stub_pipeline(monkeypatch, cv_recipe(), layers=[3])
|
|
# A sparse cache can still contain a valid late model layer.
|
|
for cache in (pipeline._harmful_acts, pipeline._harmful_means, pipeline._harmless_means,
|
|
*pipeline._control_acts.values()):
|
|
cache.pop(0)
|
|
pipeline._distill()
|
|
assert pipeline._strong_layers == [3]
|
|
|
|
pipeline._harmful_acts.pop(3)
|
|
with pytest.raises(ValueError, match="no extracted direction"):
|
|
pipeline._distill()
|
|
|
|
|
|
@pytest.mark.parametrize("override,expected", [(None, 0), (3, 3)])
|
|
@pytest.mark.parametrize("layers", [None, [1, 2]])
|
|
def test_extraction_pin_precedence_and_transfer_survive_refinement(
|
|
monkeypatch, tmp_path, override, expected, layers,
|
|
):
|
|
recipe = replace(cv_recipe(), extraction_layer={"stub": 0})
|
|
pipeline = _planted_stub_pipeline(monkeypatch, recipe, extraction_layer=override, layers=layers)
|
|
for distill in (pipeline._distill, pipeline._distill_inner):
|
|
distill()
|
|
direction = pipeline.concept_direction
|
|
assert direction.extraction_layers == [expected]
|
|
assert direction.extraction_layer_source == "pinned"
|
|
assert direction.cv_scores == {}
|
|
assert sorted(direction.intervention_layers) == (layers if layers else [expected])
|
|
for layer in direction.intervention_layers:
|
|
assert torch.equal(pipeline.concept_directions[layer], direction.directions[expected])
|
|
path = tmp_path / "direction.safetensors"
|
|
direction.save(path)
|
|
loaded = ConceptDirection.load(path)
|
|
vectors = _vectors(loaded, loaded.intervention_layers)
|
|
for layer in pipeline._strong_layers:
|
|
vector = pipeline.concept_directions[layer]
|
|
assert torch.equal(vectors[layer], vector / vector.norm())
|
|
metadata = pipeline._build_metadata()["concept"]
|
|
assert metadata["extraction_layer_source"] == "pinned"
|
|
assert metadata["recipe"]["extraction_layer"]["stub"] == expected
|
|
assert recipe.extraction_layer == {"stub": 0}
|
|
|
|
|
|
@pytest.mark.parametrize("value", [-1, True, 1.5, "2"])
|
|
def test_invalid_extraction_override_rejected_before_loading(value):
|
|
with pytest.raises(ValueError, match="non-negative integer"):
|
|
AbliterationPipeline("unloaded", concept=small_target(), extraction_layer=value)
|
|
|
|
|
|
@pytest.mark.parametrize("concept", [None, "refusal"])
|
|
def test_extraction_override_requires_recipe_concept(concept):
|
|
with pytest.raises(ValueError, match="non-refusal concept"):
|
|
AbliterationPipeline("unloaded", concept=concept, extraction_layer=0)
|