Files
OBLITERATUS/tests/test_leace_exact.py

207 lines
9.5 KiB
Python

"""Offline numerical oracles for the full affine LEACE eraser."""
from __future__ import annotations
import pytest
import torch
from obliteratus.analysis.leace_exact import ExactLEACEExtractor, fit_leace_eraser
def _gaussian_classes(n_per_class, generator):
# Correlated, anisotropic shared covariance; delta is not an eigenvector.
factor = torch.tensor([
[3.0, 0.0, 0.0], [1.5, 0.7, 0.0], [0.2, 0.4, 0.3],
], dtype=torch.float64)
delta = torch.tensor([1.0, -1.5, 0.5], dtype=torch.float64)
mean = torch.tensor([4.0, -2.0, 1.0], dtype=torch.float64)
labels = torch.cat([torch.ones(n_per_class), torch.zeros(n_per_class)]).double()
noise = torch.randn(2 * n_per_class, 3, generator=generator, dtype=torch.float64) @ factor.T
return noise + mean + (labels - 0.5).unsqueeze(1) * delta, labels
def _probe_accuracy(train, labels, test, test_labels):
# Include an intercept; SVD least squares handles the erased null direction.
design = torch.cat([train, torch.ones(train.shape[0], 1, dtype=train.dtype)], dim=1)
test_design = torch.cat([test, torch.ones(test.shape[0], 1, dtype=test.dtype)], dim=1)
precision = max(design.shape) * torch.finfo(design.dtype).eps
weights = torch.linalg.lstsq(
design, labels.unsqueeze(1), rcond=precision, driver="gelsd",
).solution
scores = (test_design @ weights).squeeze(1)
# The erased optimum is the constant 0.5 predictor. Resolve ties at the
# solver's numerical precision, scaled for cancellation in each score;
# rounding noise must not become a confidently classified label.
tie_width = precision * ((test_design.abs() @ weights.abs()).squeeze(1) + 0.5)
tied = (scores - 0.5).abs() <= tie_width
correct = ((scores >= 0.5) == test_labels.bool()).double()
# Expected accuracy of an unbiased tie-break, without random sampling.
return torch.where(tied, 0.5, correct).mean().item()
@pytest.fixture(params=[1, 4, 8], ids=lambda count: f"threads-{count}")
def probe_threads(request):
previous = torch.get_num_threads()
torch.set_num_threads(request.param)
try:
yield
finally:
torch.set_num_threads(previous)
def test_exact_eraser_removes_linear_probe_signal_and_preserves_mean(probe_threads):
generator = torch.Generator().manual_seed(226)
X, labels = _gaussian_classes(6000, generator)
held_out, held_out_labels = _gaussian_classes(8000, generator)
eraser = fit_leace_eraser(X, labels)
erased = eraser.apply(X)
assert _probe_accuracy(X, labels, held_out, held_out_labels) > 0.9
accuracy = _probe_accuracy(erased, labels, eraser.apply(held_out), held_out_labels)
assert abs(accuracy - 0.5) < 0.05
assert (erased[labels == 1].mean(0) - erased[labels == 0].mean(0)).norm() < 1e-10
torch.testing.assert_close(erased.mean(0), X.mean(0), atol=1e-12, rtol=0)
torch.testing.assert_close(
eraser.projection @ eraser.projection, eraser.projection, atol=1e-12, rtol=0,
)
torch.testing.assert_close(eraser.apply(erased), erased, atol=1e-12, rtol=0)
# Anisotropy makes the exact projection oblique.
assert not torch.allclose(eraser.projection, eraser.projection.T)
assert torch.linalg.matrix_rank(torch.eye(3) - eraser.projection) == 1
@pytest.mark.parametrize("retained_signal", [1.0, 0.5, 1e-4])
def test_probe_rejects_unmodified_and_partially_erased_signal(probe_threads, retained_signal):
generator = torch.Generator().manual_seed(226)
X, labels = _gaussian_classes(6000, generator)
held_out, held_out_labels = _gaussian_classes(8000, generator)
eraser = fit_leace_eraser(X, labels)
erased, erased_held_out = eraser.apply(X), eraser.apply(held_out)
partial = erased + retained_signal * (X - erased)
partial_held_out = erased_held_out + retained_signal * (held_out - erased_held_out)
accuracy = _probe_accuracy(partial, labels, partial_held_out, held_out_labels)
assert accuracy > 0.9
assert abs(accuracy - 0.5) >= 0.05 # Must fail the erasure acceptance criterion.
@pytest.mark.parametrize("held_out_label", [0, 1])
def test_probe_ties_do_not_favor_either_label(probe_threads, held_out_label):
train = torch.zeros(16, 3, dtype=torch.float64)
labels = (torch.arange(16) % 2).double()
held_out = torch.zeros(24, 3, dtype=torch.float64)
held_out_labels = torch.full((24,), held_out_label, dtype=torch.float64)
assert _probe_accuracy(train, labels, held_out, held_out_labels) == 0.5
def test_exact_eraser_matches_binary_closed_form_and_beats_random_erasers():
generator = torch.Generator().manual_seed(227)
X, labels = _gaussian_classes(1000, generator)
eraser = fit_leace_eraser(X, labels)
delta = X[labels == 1].mean(0) - X[labels == 0].mean(0)
centered = X - X.mean(0)
covariance = centered.T @ centered / X.shape[0]
normal = torch.linalg.solve(covariance, delta)
expected = torch.eye(3) - torch.outer(delta, normal) / (normal @ delta)
torch.testing.assert_close(eraser.projection, expected, atol=1e-12, rtol=0)
loss = (X - eraser.apply(X)).square().sum(1).mean()
for _ in range(5):
normal = torch.randn(3, generator=generator, dtype=X.dtype)
normal = normal / (normal @ delta)
alternative = torch.eye(3) - torch.outer(delta, normal)
alternate_erased = centered @ alternative.T + X.mean(0)
# Each alternative also erases the concept and is idempotent.
mean_difference = (
alternate_erased[labels == 1].mean(0) - alternate_erased[labels == 0].mean(0)
)
assert mean_difference.norm() < 1e-10
torch.testing.assert_close(alternative @ alternative, alternative)
alternate_loss = (X - alternate_erased).square().sum(1).mean()
assert loss <= alternate_loss + 1e-10
assert loss < 0.95 * alternate_loss
def test_rank_deficient_covariance_and_one_hot_labels():
generator = torch.Generator().manual_seed(228)
# d > n with a duplicated feature and a constant feature.
X = torch.randn(8, 20, generator=generator, dtype=torch.float64)
X[:, -1] = 5
X[:, -2] = X[:, 0]
labels = torch.arange(8) % 2
eraser = fit_leace_eraser(X, labels)
one_hot = fit_leace_eraser(X, torch.nn.functional.one_hot(labels))
torch.testing.assert_close(eraser.projection, one_hot.projection)
erased = eraser.apply(X)
assert torch.isfinite(erased).all()
torch.testing.assert_close(eraser.projection @ eraser.projection, eraser.projection)
assert (erased[labels == 1].mean(0) - erased[labels == 0].mean(0)).norm() < 1e-10
torch.testing.assert_close(erased[:, -1], X[:, -1])
def test_multiclass_eraser_and_float32_output():
generator = torch.Generator().manual_seed(229)
labels = torch.arange(90) % 3
X = torch.randn(90, 4, generator=generator) + torch.nn.functional.one_hot(labels, 4)
eraser = fit_leace_eraser(X, labels)
erased = eraser.apply(X)
assert erased.dtype == torch.float32
means = torch.stack([erased[labels == label].mean(0) for label in range(3)])
torch.testing.assert_close(means, erased.mean(0).expand_as(means), atol=1e-6, rtol=0)
torch.testing.assert_close(eraser.projection @ eraser.projection, eraser.projection)
def test_binary_extractor_returns_original_coordinate_direction_and_full_eraser():
generator = torch.Generator().manual_seed(230)
X, labels = _gaussian_classes(100, generator)
harmful, harmless = X[labels == 1], X[labels == 0]
result = ExactLEACEExtractor().extract(
list(harmful.unsqueeze(1)), list(harmless.unsqueeze(1)), layer_idx=7,
)
delta = harmful.mean(0) - harmless.mean(0)
torch.testing.assert_close(result.direction, delta / delta.norm())
assert result.layer_idx == 7
assert result.direction.norm().item() == pytest.approx(1)
loss = (X - result.eraser.apply(X)).square().sum(1).mean().item()
assert result.erasure_loss == pytest.approx(loss)
centered = X - X.mean(0)
covariance = centered.T @ centered / X.shape[0]
separation = (delta @ torch.linalg.solve(covariance, delta)).item()
assert result.generalized_eigenvalue == pytest.approx(separation)
orthogonal = torch.eye(3) - torch.outer(result.direction, result.direction)
assert not torch.allclose(orthogonal, result.eraser.projection)
def test_zero_covariance_and_absent_concept_return_identity():
X = torch.ones(4, 3)
eraser = fit_leace_eraser(X, torch.tensor([0, 0, 1, 1]))
assert torch.equal(eraser.projection, torch.eye(3))
result = ExactLEACEExtractor().extract(list(X[:2]), list(X[2:]))
assert torch.equal(result.direction, torch.zeros(3))
assert result.erasure_loss == 0
assert result.generalized_eigenvalue == 0
X = torch.arange(12).reshape(4, 3).float()
eraser = fit_leace_eraser(X, torch.zeros(4))
assert torch.equal(eraser.projection, torch.eye(3))
@pytest.mark.cpu
@pytest.mark.parametrize("n_harmful, n_harmless", [(0, 0), (0, 1), (1, 0)])
def test_binary_extractor_rejects_empty_activation_lists(n_harmful, n_harmless):
harmful = [torch.ones(3) for _ in range(n_harmful)]
harmless = [torch.zeros(3) for _ in range(n_harmless)]
with pytest.raises(ValueError, match="activation lists must both be nonempty"):
ExactLEACEExtractor().extract(harmful, harmless)
@pytest.mark.parametrize("X, labels, floor", [
(torch.ones(3), torch.ones(3), 1e-7),
(torch.ones(3, 2), torch.ones(2), 1e-7),
(torch.ones(3, 2), torch.ones(3, 0), 1e-7),
(torch.ones(3, 2), torch.ones(3), 0),
(torch.full((3, 2), float("nan")), torch.ones(3), 1e-7),
])
def test_invalid_fit_inputs_fail_clearly(X, labels, floor):
with pytest.raises(ValueError):
fit_leace_eraser(X, labels, floor)