Files
OBLITERATUS/tests/test_direction_methods.py
T
Joseph MaglyandClaude Opus 5.5 2fe5d902a0 feat(directions): add fisher_lda id, leace alias and exact LEACE
Add a direction-method registry that lists only available methods
(diff_means, svd, fisher_lda, leace, leace_exact; som stays unavailable).
The CLI and UI choices are generated from it. Unknown methods now fail at
construction with the available choices, instead of silently falling back.

fisher_lda is the canonical id for the existing Fisher LDA direction. leace
stays a permanent alias with bit-identical numerics, logs a notice pointing
to leace_exact, and saved metadata records both the requested method and
direction_method_effective.

leace_exact implements closed-form LEACE (Belrose et al., arXiv:2306.03819)
with an eigenvalue floor and a pseudo-inverse for rank-deficient covariance.
The full oblique eraser is exposed for tests and future interventions. The
pipeline uses its removal direction with the existing orthogonal
projection, and logs the eraser loss only as a reference value. Tests check
that, on anisotropic synthetic data, a linear probe is at chance after
erasure, class means coincide, and the eraser is idempotent.

Refs #226

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 18:22:01 -04:00

162 lines
6.8 KiB
Python

"""Registry, alias compatibility, and pipeline dispatch contracts."""
from __future__ import annotations
import argparse
import json
import pytest
import torch
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.analysis.direction_methods import (
LEACE_ALIAS_NOTICE,
available_direction_methods,
effective_direction_method,
)
from obliteratus.analysis.leace_exact import ExactLEACEExtractor
from obliteratus.cli import main
from obliteratus.informed_pipeline import InformedAbliterationPipeline
def _activations():
generator = torch.Generator().manual_seed(226)
harmful = torch.randn(12, 4, generator=generator) * torch.tensor([3.0, 0.3, 2.0, 1.0])
harmless = torch.randn(12, 4, generator=generator) * torch.tensor([3.0, 0.3, 2.0, 1.0])
harmful += torch.tensor([1.0, -2.0, 0.5, 1.5])
return list(harmful), list(harmless)
def _legacy_fisher_direction(harmful, harmless):
# Pre-change LEACEExtractor formula, including its float32 operation order.
H = torch.stack(harmful).float()
B = torch.stack(harmless).float()
mu_h, mu_b = H.mean(0), B.mean(0)
H_centered, B_centered = H - mu_h.unsqueeze(0), B - mu_b.unsqueeze(0)
S_h = H_centered.T @ H_centered / max(H.shape[0] - 1, 1)
S_b = B_centered.T @ B_centered / max(B.shape[0] - 1, 1)
S_w = (S_h + S_b) / 2.0
S_w_reg = S_w + 1e-4 * torch.eye(H.shape[1])
direction = torch.linalg.solve(S_w_reg, mu_h - mu_b)
return direction / direction.norm()
def _populate(pipeline, harmful, harmless, monkeypatch):
pipeline._harmful_acts = {0: harmful}
pipeline._harmless_acts = {0: harmless}
pipeline._harmful_means = {0: torch.stack(harmful).mean(0, keepdim=True)}
pipeline._harmless_means = {0: torch.stack(harmless).mean(0, keepdim=True)}
monkeypatch.setattr(pipeline, "_select_layers_knee", lambda _ranked: [0])
@pytest.mark.parametrize("requested", ["leace", "fisher_lda", "leace_exact"])
@pytest.mark.parametrize("path", ["base", "inner", "informed"])
def test_dispatch_and_metadata_record_requested_and_effective_methods(
requested, path, tmp_path, monkeypatch,
):
logs = []
if path == "informed":
pipeline = InformedAbliterationPipeline(model_name="fixture/model", on_log=logs.append)
pipeline.direction_method = requested
else:
pipeline = AbliterationPipeline(
model_name="fixture/model", method="basic",
direction_method=requested, on_log=logs.append,
)
harmful, harmless = _activations()
_populate(pipeline, harmful, harmless, monkeypatch)
expected = (
ExactLEACEExtractor().extract(harmful, harmless).direction
if requested == "leace_exact" else _legacy_fisher_direction(harmful, harmless)
)
{"base": pipeline._distill, "inner": pipeline._distill_inner,
"informed": getattr(pipeline, "_distill_informed", None)}[path]()
# torch.equal pins bit identity, beyond a tolerance/cosine comparison.
assert torch.equal(pipeline.refusal_directions[0], expected)
assert torch.equal(pipeline.refusal_subspaces[0], expected.unsqueeze(0))
config = pipeline._build_metadata()["method_config"]
(tmp_path / "metadata.json").write_text(json.dumps(config))
saved = json.loads((tmp_path / "metadata.json").read_text())
assert saved["direction_method"] == requested
assert saved["direction_method_effective"] == effective_direction_method(requested)
assert (LEACE_ALIAS_NOTICE in logs) == (requested == "leace" and path != "inner")
if requested == "leace_exact":
assert not any("Fisher LDA" in line for line in logs)
if path != "inner":
assert any("orthogonal pipeline projection" in line for line in logs)
assert any(
"exact_leace_eraser_loss "
"(reference; pipeline applies orthogonal projection)=" in line
for line in logs
)
assert not any("full_eraser_loss=" in line for line in logs)
else:
assert any("Fisher LDA direction (LEACE-inspired)" in line for line in logs)
@pytest.mark.parametrize("requested", ["leace", "fisher_lda", "leace_exact"])
def test_informed_save_records_method_used_after_analysis(tmp_path, requested):
from obliteratus.models.loader import load_model
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
source = build_tiny_offline_model(tmp_path / "source")
pipeline = InformedAbliterationPipeline(
model_name=str(source), output_dir=str(tmp_path / "output"), on_log=lambda _message: None,
)
pipeline.handle = load_model(str(source), device="cpu", local_files_only=True)
pipeline._insights.cone_is_polyhedral = True
pipeline._insights.cone_dimensionality = 1.5
pipeline._derive_configuration()
assert pipeline.direction_method == "leace"
assert pipeline.direction_method_effective == "fisher_lda"
# A manual override must be recorded even if the recommendation is stale.
pipeline.direction_method = requested
output = pipeline._rebirth_informed()
metadata = json.loads((output / "abliteration_metadata.json").read_text())
assert metadata["derived_config"]["direction_method"] == (
pipeline._insights.recommended_direction_method
) == "leace"
assert metadata["derived_config"]["method_used"] == requested
assert metadata["derived_config"]["direction_method_effective"] == (
effective_direction_method(requested)
)
def test_all_cli_direction_choices_equal_registry_and_exclude_som(monkeypatch):
captured = []
class ParserCaptured(Exception):
pass
def capture(parser, *_args, **_kwargs):
captured.append(parser)
raise ParserCaptured
monkeypatch.setattr(argparse.ArgumentParser, "parse_args", capture)
with pytest.raises(ParserCaptured):
main([])
subparsers = next(
action for action in captured[0]._actions
if isinstance(action, argparse._SubParsersAction)
)
available = available_direction_methods()
assert available == ("diff_means", "svd", "fisher_lda", "leace", "leace_exact")
commands = []
for command, parser in subparsers.choices.items():
for action in parser._actions:
if action.dest == "direction_method":
commands.append(command)
assert tuple(action.choices) == available
assert "som" not in action.choices
assert set(commands) == {"obliterate", "abliterate", "self-improve"}
@pytest.mark.parametrize("method, message", [
("som", "not available"), ("typo", "Unknown direction method"),
])
def test_unavailable_direction_methods_fail_before_loading(method, message):
with pytest.raises(ValueError, match=message) as exc_info:
AbliterationPipeline(model_name="fixture/model", direction_method=method)
assert ", ".join(available_direction_methods()) in str(exc_info.value)