Files
OBLITERATUS/tests/test_layer_selection_scoring.py
Joseph Magly be6093dcb8 fix(abliterate): scale-aware layer selection with recorded diagnostics
Automatic layer selection ranked layers by raw activation-strength
scores, and the small-model cap kept the strongest N of the knee and
COSMIC union, so COSMIC's mid-network candidates could all be dropped.
On Qwen2.5-1.5B the default selected only layers 23-27.

- Rank candidates by strength normalized to each layer's activation
  scale (selection is invariant to per-layer rescaling); raw strengths,
  used for KL rollback ordering, and directions are unchanged.
- When the small-model cap applies, reserve a quota for COSMIC-only
  candidates instead of truncating purely by strength.
- Share one selection routine between both distillation paths.
- Record selection diagnostics (raw and normalized scores, knee and
  COSMIC candidates, caps applied, final layers) and effective direction
  counts and per-layer ranks in metadata; n_directions stays the
  requested value.

Fixes #533
2026-11-19 11:19:20 -05:00

118 lines
5.2 KiB
Python

"""CPU contracts for scale-aware refusal layer selection and its metadata."""
import json
from types import SimpleNamespace
import pytest
import torch
from obliteratus.abliterate import AbliterationPipeline
from tests.test_abliterate import _make_tiny_handle, _make_varied_tokenizer
pytestmark = [pytest.mark.cpu, pytest.mark.usefixtures("tiny_model_runtime")]
def _synthetic_distillation(method, stage, *, rescale=False, safeguards=True):
pipeline = AbliterationPipeline(
"synthetic", method=method, layer_selection="knee_cosmic",
small_model_safeguards=safeguards, layer_adaptive_strength=False,
)
model = SimpleNamespace(config=None, parameters=lambda: ())
pipeline.handle = SimpleNamespace(model=model, hidden_size=64, total_params=1_500_000_000)
pipeline._on_log = lambda message: None
pipeline._on_stage = lambda result: None
for idx in range(28):
amplitude = 1.25 ** idx
if rescale:
amplitude *= (0.67 + 0.14 * (idx % 7)) * 2.0 ** ((idx * 19) % 21 - 10)
# Represent the rescaled inputs with enough precision to isolate selection.
harmful = torch.zeros(4, 1, 64, dtype=torch.float64)
harmless = torch.zeros_like(harmful)
harmless[:, :, 0] = 1
harmful[:, :, 0] = 1.0 + (0.8 + 0.02 * idx if idx >= 21 else 0.005 * (idx + 1))
if idx in (8, 12, 16):
harmful[:, :, 1] = {8: 0.035, 12: 0.045, 16: 0.055}[idx]
harmful[:, 0, 2] = torch.tensor([-0.02, 0.02, -0.01, 0.01], dtype=harmful.dtype)
harmful *= amplitude
harmless *= amplitude
pipeline._harmful_acts[idx] = list(harmful)
pipeline._harmless_acts[idx] = list(harmless)
pipeline._harmful_means[idx] = harmful.mean(dim=0)
pipeline._harmless_means[idx] = harmless.mean(dim=0)
getattr(pipeline, stage)()
return pipeline
@pytest.mark.parametrize("method", ["basic", "advanced"])
@pytest.mark.parametrize("stage", ["_distill", "_distill_inner"])
def test_automatic_selection_is_invariant_to_layer_activation_scale(method, stage):
original = _synthetic_distillation(method, stage)
rescaled = _synthetic_distillation(method, stage, rescale=True)
assert rescaled._strong_layers == original._strong_layers
assert rescaled._refusal_strengths != original._refusal_strengths
original_scores = original._build_metadata()["layer_selection"]["normalized_scores"]
rescaled_scores = rescaled._build_metadata()["layer_selection"]["normalized_scores"]
assert rescaled_scores == pytest.approx(original_scores)
for idx in range(28):
torch.testing.assert_close(
rescaled.refusal_subspaces[idx].abs(), original.refusal_subspaces[idx].abs(),
)
@pytest.mark.parametrize("stage", ["_distill", "_distill_inner"])
def test_small_model_cap_reserves_cosmic_only_candidates(stage):
capped = _synthetic_distillation("advanced", stage)
assert capped._select_layers_cosmic(28) == [16, 12, 8]
assert len(capped._strong_layers) == 5
assert len({8, 12, 16}.intersection(capped._strong_layers)) >= 2
diagnostics = capped._build_metadata()["layer_selection"]
assert set(diagnostics["knee_candidates"]).isdisjoint({8, 12, 16})
assert diagnostics["cosmic_candidates"] == [16, 12, 8]
assert diagnostics["caps_applied"]["small_model_layers"] == 5
assert diagnostics["caps_applied"]["cosmic_only_quota"] == 2
uncapped = _synthetic_distillation("advanced", stage, safeguards=False)
assert {8, 12, 16}.issubset(uncapped._strong_layers)
assert len(uncapped._strong_layers) > 5
assert "small_model_layers" not in uncapped._build_metadata()["layer_selection"]["caps_applied"]
def test_metadata_records_selection_and_effective_ranks():
pipeline = AbliterationPipeline(
"tiny", method="advanced", n_directions=4,
harmful_prompts=[f"bad {i}" for i in range(5)],
harmless_prompts=[f"good {i}" for i in range(5)],
)
pipeline.handle = _make_tiny_handle()
pipeline._on_log = lambda message: None
pipeline._on_stage = lambda result: None
_make_varied_tokenizer(pipeline.handle)
pipeline._probe()
pipeline._distill()
metadata = json.loads(json.dumps(pipeline._build_metadata(), allow_nan=False))
selection = metadata["layer_selection"]
assert set(selection["raw_scores"]) == set(selection["normalized_scores"]) == {"0", "1", "2", "3"}
assert selection["raw_scores"] != selection["normalized_scores"]
assert selection["knee_candidates"]
assert selection["cosmic_candidates"]
assert selection["caps_applied"]["small_model_directions"] == 2
assert selection["selected_layers"] == metadata["strong_layers"] == pipeline._strong_layers
assert metadata["method_config"]["n_directions"] == 4
assert metadata["method_config"]["effective_n_directions"] == 2
assert metadata["effective_subspace_ranks"] == {str(idx): 2 for idx in range(4)}
def test_metadata_uses_actual_subspace_rank():
pipeline = AbliterationPipeline("tiny", method="basic")
pipeline.refusal_subspaces[0] = torch.tensor([[1.0, 0.0], [1.0, 0.0]])
pipeline.refusal_subspaces[1] = torch.zeros(1, 2)
metadata = pipeline._build_metadata()
assert metadata["method_config"]["effective_n_directions"] == 1
assert metadata["effective_subspace_ranks"] == {"0": 1, "1": 0}