mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Maintainer decisions for epic #223 (2026-10-15): - Q1: `leace` is a deprecated alias of the Fisher LDA direction. It keeps its numerical behaviour, warns once per public resolution with a DeprecationWarning naming `fisher_lda` and `leace_exact`, and will be removed in a future minor release. - Q4: a collateral FAIL blocks both the model card and any Hub push of modified weights or adapters on every path (pipeline, export_run, both CLIs, Concept Lab, the Hub UI and tourney uploads), checked before any network call. `--allow-collateral-fail REASON` (and the matching API argument and UI field) overrides the block and stamps the FAIL and reason into the card, checkpoint metadata and provenance. Local save and export are never blocked. - Q5: research-tier packs no longer carry publication or local-export restrictions; the tier is provenance only and Q4 applies to every pack. - Steering defaults are removal-first: `steer` defaults to alpha -1 and the default dose ladder is non-positive. Positive doses stay available explicitly; `project` keeps its positive removal strength. - Q3 (gate modes) and Q7 (dense only, follow-up #310) are unchanged and documented. Refs #223 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
195 lines
7.2 KiB
Python
195 lines
7.2 KiB
Python
"""Wording and numeric-invariance tests for the `leace` direction method.
|
|
|
|
The `leace` option computes a Fisher LDA direction (S_w^{-1} delta,
|
|
LEACE-inspired), not faithful LEACE (Belrose et al.). These tests pin the
|
|
user-facing wording (no "optimal"/closed-form-LEACE claims) and pin the
|
|
numerics (wording-only change must not move the direction tensors).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from io import StringIO
|
|
from unittest.mock import patch
|
|
import warnings
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.abliterate import METHODS, AbliterationPipeline
|
|
from obliteratus.analysis.leace import LEACEExtractor
|
|
from obliteratus.informed_pipeline import InformedAbliterationPipeline
|
|
|
|
|
|
def _activations():
|
|
harmful = [
|
|
torch.tensor([2.0, 0.0, 1.0]),
|
|
torch.tensor([3.0, 1.0, 0.0]),
|
|
torch.tensor([4.0, -1.0, 1.0]),
|
|
]
|
|
harmless = [
|
|
torch.tensor([-2.0, 0.0, 1.0]),
|
|
torch.tensor([-3.0, 1.0, 0.0]),
|
|
torch.tensor([-4.0, -1.0, 1.0]),
|
|
]
|
|
return harmful, harmless
|
|
|
|
|
|
def _means(harmful, harmless):
|
|
return {
|
|
0: torch.stack(harmful).mean(0, keepdim=True),
|
|
}, {
|
|
0: torch.stack(harmless).mean(0, keepdim=True),
|
|
}
|
|
|
|
|
|
def _assert_fisher_lda_wording(logs: list[str]):
|
|
text = "\n".join(logs)
|
|
assert "Fisher" in text
|
|
assert "LDA" in text
|
|
assert "optimal" not in text.lower()
|
|
assert "provably" not in text.lower()
|
|
|
|
|
|
def test_leace_distill_log_names_fisher_lda_not_optimal():
|
|
"""Main pipeline `leace` logs must say Fisher LDA, never optimal."""
|
|
logs: list[str] = []
|
|
with pytest.warns(DeprecationWarning, match="future minor release") as caught:
|
|
pipeline = AbliterationPipeline(
|
|
model_name="test",
|
|
direction_method="leace",
|
|
n_directions=1,
|
|
on_log=logs.append,
|
|
)
|
|
assert len(caught) == 1
|
|
assert caught[0].filename == __file__
|
|
assert pipeline.direction_method == "leace"
|
|
harmful, harmless = _activations()
|
|
harmful_means, harmless_means = _means(harmful, harmless)
|
|
pipeline._harmful_means = harmful_means
|
|
pipeline._harmless_means = harmless_means
|
|
pipeline._harmful_acts = {0: harmful}
|
|
pipeline._harmless_acts = {0: harmless}
|
|
|
|
pipeline._distill()
|
|
|
|
assert logs, "expected direction_method='leace' to emit log lines"
|
|
_assert_fisher_lda_wording(logs)
|
|
assert any("lda_erasure_loss" in line for line in logs)
|
|
|
|
|
|
def test_leace_informed_distill_log_names_fisher_lda_not_optimal(tmp_path, monkeypatch):
|
|
"""Informed pipeline `leace` logs must say Fisher LDA, never optimal."""
|
|
logs: list[str] = []
|
|
pipeline = InformedAbliterationPipeline(
|
|
model_name="fixture/model",
|
|
output_dir=str(tmp_path / "output"),
|
|
on_log=logs.append,
|
|
)
|
|
pipeline.direction_method = "leace"
|
|
pipeline.n_directions = 1
|
|
harmful, harmless = _activations()
|
|
harmful_means, harmless_means = _means(harmful, harmless)
|
|
pipeline._harmful_means = harmful_means
|
|
pipeline._harmless_means = harmless_means
|
|
pipeline._harmful_acts = {0: harmful}
|
|
pipeline._harmless_acts = {0: harmless}
|
|
monkeypatch.setattr(pipeline, "_select_layers_knee", lambda _ranked: [0])
|
|
monkeypatch.setattr(pipeline, "_emit", lambda *_args, **_kwargs: None)
|
|
|
|
with pytest.warns(DeprecationWarning, match="fisher_lda") as caught:
|
|
pipeline._distill_informed()
|
|
assert len(caught) == 1
|
|
|
|
assert logs, "expected direction_method='leace' to emit log lines"
|
|
_assert_fisher_lda_wording(logs)
|
|
assert any("lda_erasure_loss" in line for line in logs)
|
|
|
|
|
|
def test_informed_preset_description_names_fisher_lda_not_optimal():
|
|
description = METHODS["informed"]["description"]
|
|
assert "Fisher LDA" in description
|
|
assert "optimal" not in description.lower()
|
|
|
|
|
|
def test_obliterate_help_names_fisher_lda_not_optimal():
|
|
from obliteratus.cli import main
|
|
|
|
buf = StringIO()
|
|
with pytest.raises(SystemExit) as exc_info, patch("sys.stdout", buf):
|
|
main(["obliterate", "--help"])
|
|
assert exc_info.value.code == 0
|
|
text = buf.getvalue()
|
|
assert "leace" in text
|
|
assert "Fisher LDA" in text
|
|
assert "optimal" not in text.lower()
|
|
|
|
|
|
def test_leace_direction_matches_independent_fisher_lda():
|
|
"""Direction tensors must equal S_w^{-1} delta computed independently."""
|
|
harmful, harmless = _activations()
|
|
result = LEACEExtractor().extract(harmful, harmless, layer_idx=0)
|
|
|
|
H = torch.stack(harmful).float()
|
|
B = torch.stack(harmless).float()
|
|
mu_h = H.mean(dim=0)
|
|
mu_b = B.mean(dim=0)
|
|
delta = mu_h - mu_b
|
|
S_h = ((H - mu_h).T @ (H - mu_h)) / (H.shape[0] - 1)
|
|
S_b = ((B - mu_b).T @ (B - mu_b)) / (B.shape[0] - 1)
|
|
S_w = (S_h + S_b) / 2.0 + 1e-4 * torch.eye(H.shape[1])
|
|
expected = torch.linalg.solve(S_w, delta)
|
|
expected = expected / expected.norm()
|
|
|
|
assert torch.allclose(result.direction, expected, atol=1e-6)
|
|
# Golden values pin the pre-change numerics for this fixed input.
|
|
assert result.direction.tolist() == pytest.approx([1.0, 0.0, 0.0], abs=1e-6)
|
|
assert result.generalized_eigenvalue == pytest.approx(35.9964, abs=1e-3)
|
|
assert result.erasure_loss == pytest.approx(11.6, abs=1e-3)
|
|
assert result.mean_diff_norm == pytest.approx(6.0)
|
|
|
|
|
|
def test_alias_resolution_warns_once_and_canonical_methods_do_not():
|
|
from obliteratus.analysis.direction_methods import effective_direction_method
|
|
|
|
with pytest.warns(DeprecationWarning) as caught:
|
|
assert effective_direction_method("leace") == "fisher_lda"
|
|
assert len(caught) == 1
|
|
assert caught[0].filename == __file__
|
|
message = str(caught[0].message)
|
|
assert "Fisher LDA direction (fisher_lda)" in message
|
|
assert "exact erasure is leace_exact" in message
|
|
assert "future minor release" in message
|
|
with warnings.catch_warnings(record=True) as repeated:
|
|
warnings.simplefilter("always")
|
|
assert effective_direction_method("leace", warn_alias=False) == "fisher_lda"
|
|
assert effective_direction_method("fisher_lda") == "fisher_lda"
|
|
assert effective_direction_method("leace_exact") == "leace_exact"
|
|
assert not repeated
|
|
|
|
|
|
def test_pipeline_alias_warns_once_across_repeated_internal_resolution():
|
|
with warnings.catch_warnings(record=True) as caught:
|
|
warnings.simplefilter("always", DeprecationWarning)
|
|
pipeline = AbliterationPipeline(model_name="test", direction_method="leace")
|
|
for _ in range(3):
|
|
assert pipeline.direction_method_effective == "fisher_lda"
|
|
pipeline._make_direction_extractor()
|
|
pipeline._build_metadata()
|
|
alias_warnings = [item for item in caught if issubclass(item.category, DeprecationWarning)]
|
|
assert len(alias_warnings) == 1
|
|
|
|
|
|
def test_explicit_alias_warns_when_concept_recipe_uses_canonical_method():
|
|
from obliteratus.concepts import ConceptTarget, ExtractionRecipe
|
|
|
|
target = ConceptTarget(
|
|
id="user/demo", positive=["positive"], controls={"neutral": ["neutral"]},
|
|
recipe=ExtractionRecipe(method="fisher_lda"),
|
|
)
|
|
with pytest.warns(DeprecationWarning, match="fisher_lda") as caught:
|
|
pipeline = AbliterationPipeline(
|
|
model_name="test", method="basic", concept=target, direction_method="leace",
|
|
)
|
|
assert len(caught) == 1
|
|
assert pipeline.direction_method_effective == "fisher_lda"
|