Files
OBLITERATUS/tests/test_geometry_audit_oracles.py
Joseph MaglyandClaude Opus 5.5 40744bac67 fix(analysis): label geometry measurements and null unsupported values
Extend the measurement-validity treatment from riemannian_manifold.py
to concept_geometry, the LEACE paths, cross_layer and
wasserstein_optimal. Each audited result carries measurement evidence,
every reported field has a catalog entry, and a registry test fails
when either is missing.

- concept_geometry: the cone "solid angle" is a cap around the mean
  axis, not a minimal enclosing cone; relabelled. Placeholder defaults
  (cosine 1.0, specificity 1.0, linear/polyhedral flags, layer indices 0)
  become None when there is too little data. The informed pipeline skips
  unavailable flags; decisions on valid inputs are unchanged.
- cross_layer: persistence, adjacent cosine and angle path length are
  None with fewer than two layers instead of 0.0/1.0; zero or non-finite
  directions are rejected instead of reported as orthogonal.
- leace: the Fisher "erasure loss" is relabelled as variance along the
  direction; exact and multiclass LEACE gain erasure oracles.
- wasserstein_optimal: a layer with no signal reported a cost ratio of 0
  and could be chosen as best_layer; it is now None and excluded. The
  W2 cost is exact for any direction (closed-form oracle), and the
  comment calling it a bound is corrected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:45:45 -05:00

282 lines
15 KiB
Python

"""Analytic oracles for concept-cone, cross-layer and projection-cost geometry."""
import math
from types import SimpleNamespace
import pytest
import torch
from obliteratus.analysis.concept_geometry import ConceptConeAnalyzer
from obliteratus.analysis.cross_layer import CrossLayerAlignmentAnalyzer
from obliteratus.analysis.wasserstein_optimal import WassersteinOptimalExtractor
pytestmark = pytest.mark.cpu
def cone(directions, zero=()):
"""Two prompts per category; harmless rows are zero so each mean difference is planted."""
harmful, harmless, categories = [], [], {}
for k, direction in enumerate(directions):
for _ in range(2):
categories[len(harmful)] = f"c{k}"
harmful.append(torch.zeros_like(direction) if k in zero else direction.clone())
harmless.append(torch.zeros_like(direction))
return ConceptConeAnalyzer(category_map=categories).analyze_layer(harmful, harmless, 3)
def status(result, name):
return result.measurement_evidence[name].status
def circular(alpha):
return [torch.tensor([math.sin(alpha) * math.cos(phi), math.sin(alpha) * math.sin(phi), math.cos(alpha)],
dtype=torch.float64) for phi in (0, 2 * math.pi / 3, 4 * math.pi / 3)]
@pytest.mark.parametrize("alpha", [0.2, 0.7, 1.2])
def test_circular_cone_has_closed_form_cap_rank_and_cosines(alpha):
result = cone(circular(alpha))
c = math.cos(alpha) ** 2 - math.sin(alpha) ** 2 / 2
assert result.cone_solid_angle == pytest.approx(2 * math.pi * (1 - math.cos(alpha)))
assert list(result.pairwise_cosines.values()) == pytest.approx([abs(c)] * 3, abs=1e-6)
assert result.mean_pairwise_cosine == pytest.approx(abs(c))
singular = torch.tensor([1 + 2 * c, 1 - c, 1 - c]).clamp_min(0).sqrt()
singular = singular[singular > 1e-10]
p = singular / singular.sum()
assert result.cone_dimensionality == pytest.approx(math.exp(-(p * p.log()).sum().item()), rel=1e-5)
for direction in result.category_directions:
assert direction.specificity == pytest.approx(1 - abs(c), abs=1e-6)
assert direction.strength == pytest.approx(1)
torch.testing.assert_close(result.general_direction, torch.tensor([0., 0., 1.]))
assert all(e.status == "diagnostic" for e in result.measurement_evidence.values())
def test_octant_cap_proxy_is_not_the_polyhedral_solid_angle():
result = cone(list(torch.eye(3, dtype=torch.float64)))
true_octant = 4 * math.pi / 8
assert result.cone_solid_angle == pytest.approx(2 * math.pi * (1 - 1 / math.sqrt(3)))
assert result.cone_solid_angle > true_octant * 1.6 # circumscribing cap, not the cone
assert result.cone_dimensionality == pytest.approx(3)
assert result.mean_pairwise_cosine == 0
assert result.is_polyhedral is True and result.is_linear is False
assert "Not a minimal enclosing cone" in result.measurement_evidence["cone_solid_angle"].definition
def test_antipodal_axes_are_unsigned_and_have_no_mean_axis():
d = torch.tensor([1., 0., 0.])
result = cone([d, -d])
assert result.pairwise_cosines == {("c0", "c1"): 1.0}
assert result.cone_dimensionality == pytest.approx(1)
assert result.is_linear is True # sign is ignored by construction
assert result.cone_solid_angle is None
assert status(result, "cone_solid_angle") == "unavailable"
assert status(result, "general_direction") == "unavailable"
def test_single_axis_and_zero_strength_categories_never_become_measurements():
one = cone([torch.tensor([0., 1.])])
mixed = cone([torch.tensor([0., 1.]), torch.tensor([1., 0.])], zero={1})
for result in (one, mixed):
assert result.pairwise_cosines == {}
assert result.mean_pairwise_cosine is result.cone_solid_angle is None
assert result.is_linear is result.is_polyhedral is None
assert result.cone_dimensionality == 1.0
for name in ("mean_pairwise_cosine", "cone_solid_angle", "is_linear", "is_polyhedral"):
assert status(result, name) == "unavailable"
assert result.measurement_evidence[name].unavailable_reason
assert all(cd.specificity is None for cd in result.category_directions)
assert mixed.category_count == 2
assert [cd.strength for cd in mixed.category_directions] == [1.0, 0.0]
report = ConceptConeAnalyzer.format_report(mixed)
assert "UNAVAILABLE" in report and "specificity=unavailable" in report
@pytest.mark.parametrize("values", [[torch.ones(3)] * 4, [torch.zeros(3)] * 4])
def test_constant_or_too_few_samples_have_no_geometry(values):
constant = ConceptConeAnalyzer(category_map={i: "a" for i in range(4)}).analyze_layer(values, values)
sparse = ConceptConeAnalyzer(category_map={0: "a", 1: "b"}).analyze_layer(values[:2], values[:2])
for result in (constant, sparse):
assert result.cone_dimensionality is None
assert result.is_polyhedral is None
assert status(result, "general_direction") == "unavailable"
assert sparse.category_directions == []
assert status(sparse, "category_directions") == "unavailable"
def test_multi_layer_aggregates_only_available_ranks():
analyzer = ConceptConeAnalyzer(category_map={0: "a", 1: "a", 2: "b", 3: "b"})
good = [torch.tensor([1., 0.])] * 2 + [torch.tensor([0., 1.])] * 2
flat = [torch.zeros(2)] * 4
zeros = [torch.zeros(2)] * 4
result = analyzer.analyze_all_layers({0: flat, 1: good}, {0: zeros, 1: zeros})
assert result.cone_complexity_by_layer == {0: None, 1: pytest.approx(2)}
assert result.most_polyhedral_layer == result.most_linear_layer == 1
assert result.mean_cone_dimensionality == pytest.approx(2)
empty = analyzer.analyze_all_layers({0: flat}, {0: zeros})
assert empty.most_polyhedral_layer is empty.most_linear_layer is empty.mean_cone_dimensionality is None
assert empty.measurement_evidence["most_polyhedral_layer"].status == "unavailable"
nothing = analyzer.analyze_all_layers({}, {})
assert nothing.per_layer == {} and nothing.mean_cone_dimensionality is None
def test_pipeline_vote_ignores_unavailable_flags(tmp_path, monkeypatch):
from obliteratus.informed_pipeline import InformedAbliterationPipeline
pipeline = InformedAbliterationPipeline(
model_name="fixture/model", output_dir=str(tmp_path), on_log=lambda _m: None,
)
unavailable = SimpleNamespace(
is_polyhedral=None, cone_dimensionality=None, mean_pairwise_cosine=None,
general_direction=torch.tensor([1.0, 0.0]), category_count=1,
category_directions=[SimpleNamespace(category="a", direction=torch.ones(2), specificity=None, strength=1.0)],
)
results = iter([unavailable, unavailable, unavailable])
monkeypatch.setattr("obliteratus.analysis.concept_geometry.ConceptConeAnalyzer",
lambda: SimpleNamespace(analyze_layer=lambda *a, **k: next(results)))
pipeline._harmful_acts = {idx: [torch.ones(1, 2)] for idx in range(4)}
pipeline._harmless_acts = {idx: [torch.zeros(1, 2)] for idx in range(4)}
pipeline._analyze_cone_geometry()
assert pipeline._insights.cone_is_polyhedral is False
assert pipeline._insights.cone_dimensionality == 1.0
assert pipeline._insights.direction_specificity == {}
def unit_angle(degrees):
rad = math.radians(degrees)
return torch.tensor([math.cos(rad), math.sin(rad)], dtype=torch.float64)
def test_cross_layer_angles_match_hand_computed_values_and_ignore_sign():
analyzer = CrossLayerAlignmentAnalyzer(cluster_threshold=0.85)
result = analyzer.analyze({0: unit_angle(0), 4: -unit_angle(30), 9: 3 * unit_angle(90)})
c30, c60 = math.cos(math.pi / 6), math.cos(math.pi / 3)
assert result.total_geodesic_distance == pytest.approx(math.pi / 2, abs=1e-6)
assert result.angular_drift == pytest.approx([0, math.pi / 6, math.pi / 2], abs=1e-6)
assert result.mean_adjacent_cosine == pytest.approx((c30 + c60) / 2)
assert result.direction_persistence_score == pytest.approx((c30 + c60 + 0) / 3)
assert result.clusters == [[0, 4], [9]] and result.cluster_count == 2
assert all(e.status == "diagnostic" for e in result.measurement_evidence.values())
@pytest.mark.parametrize("directions", [{}, {5: torch.tensor([0., 2.])}])
def test_cross_layer_without_pairs_reports_no_measurement(directions):
result = CrossLayerAlignmentAnalyzer().analyze(directions)
for name in ("total_geodesic_distance", "mean_adjacent_cosine", "direction_persistence_score"):
assert getattr(result, name) is None
assert result.measurement_evidence[name].status == "unavailable"
assert "unavailable" in CrossLayerAlignmentAnalyzer.format_report(result) or not directions
@pytest.mark.parametrize("bad", [torch.zeros(3), torch.tensor([float("nan"), 1., 0.]), torch.ones(2, 3)])
def test_cross_layer_rejects_directions_without_an_angle(bad):
with pytest.raises(ValueError, match="finite nonzero"):
CrossLayerAlignmentAnalyzer().analyze({0: torch.ones(3), 1: bad})
def test_angular_drift_plot_labels_unavailable_persistence(tmp_path):
pytest.importorskip("matplotlib")
from obliteratus.analysis.visualization import plot_angular_drift
result = CrossLayerAlignmentAnalyzer().analyze({0: torch.ones(3)})
plot_angular_drift(result, output_path=tmp_path / "drift.png")
assert (tmp_path / "drift.png").exists()
def gaussian_w2_squared(m1, s1, m2, s2):
"""Closed-form W2^2 between Gaussians: |m1-m2|^2 + Bures^2(s1, s2)."""
def sqrtm(a):
values, vectors = torch.linalg.eigh(a)
return (vectors * values.clamp_min(0).sqrt()) @ vectors.T
root = sqrtm(s1)
bures = s1.trace() + s2.trace() - 2 * sqrtm(root @ s2 @ root).trace()
return ((m1 - m2).square().sum() + bures).item()
def moments(rows, eps):
mean = rows.mean(0)
centered = rows - mean
return mean, centered.T @ centered / (len(rows) - 1) + eps * torch.eye(rows.shape[1], dtype=rows.dtype)
def test_gaussian_oracle_matches_commuting_closed_form():
m, s = torch.tensor([1., 2.], dtype=torch.float64), torch.diag(torch.tensor([4., 9.], dtype=torch.float64))
expected = 1 + (2 - 1) ** 2 + (3 - 2) ** 2
assert gaussian_w2_squared(m, s, m + torch.tensor([1., 0.], dtype=torch.float64),
torch.diag(torch.tensor([1., 4.], dtype=torch.float64))) == pytest.approx(expected)
def test_projection_cost_equals_closed_form_gaussian_w2_for_every_direction():
torch.manual_seed(0)
mix = torch.tensor([[2.0, 0.3, 0.0], [0.0, 0.5, 0.2], [0.4, 0.0, 1.0]], dtype=torch.float64)
harmless = torch.randn(40, 3, dtype=torch.float64) @ mix + torch.tensor([1.0, -0.5, 0.2], dtype=torch.float64)
harmful = harmless + torch.tensor([1.5, 1.0, -0.5], dtype=torch.float64)
extractor = WassersteinOptimalExtractor()
result = extractor.extract(list(harmful), list(harmless))
m, sigma = moments(harmless.float().double(), extractor.regularization_eps)
# Off-eigenvector directions too: Bures^2(S, P S P) = r^T S r for any unit r.
for r in (result.direction.double(), torch.nn.functional.normalize(torch.randn(3, dtype=torch.float64), dim=0)):
eigvec_residual = (sigma @ r - (r @ sigma @ r) * r).norm()
assert eigvec_residual > 1e-3
projector = torch.eye(3, dtype=torch.float64) - torch.outer(r, r)
exact = gaussian_w2_squared(m, sigma, projector @ m, projector @ sigma @ projector)
assert (r @ m).item() ** 2 + (r @ sigma @ r).item() == pytest.approx(exact, rel=1e-6)
r = result.direction.double()
projector = torch.eye(3, dtype=torch.float64) - torch.outer(r, r)
exact = gaussian_w2_squared(m, sigma, projector @ m, projector @ sigma @ projector)
assert result.wasserstein_cost == pytest.approx(exact, rel=1e-4)
assert result.bures_component == pytest.approx((r @ sigma @ r).item(), rel=1e-4)
# The empirical projection coupling attains it with population normalization (eps aside).
centered_cost = ((harmless.float().double() @ r) ** 2).mean().item()
n = len(harmless)
assert centered_cost == pytest.approx(
result.mean_shift_component + (result.bures_component - extractor.regularization_eps) * (n - 1) / n,
rel=1e-4)
cost = torch.outer(m, m) + sigma
d = harmful.mean(0) - harmless.mean(0)
best = 1 / (d @ torch.linalg.solve(cost, d)).item()
assert result.cost_effectiveness_ratio == pytest.approx(best, rel=1e-4)
for _ in range(5): # no other direction has a lower ratio
q = torch.randn(3, dtype=torch.float64)
assert (q @ cost @ q / (q @ d) ** 2).item() >= best * (1 - 1e-6)
def test_layers_without_mean_shift_cannot_be_selected_as_best():
harmless = [torch.tensor([1., 0.]), torch.tensor([0., 1.]), torch.tensor([1., 1.])]
shifted = [h + torch.tensor([2., 0.]) for h in harmless]
extractor = WassersteinOptimalExtractor()
result = extractor.extract_all_layers({0: harmless, 1: shifted}, {0: harmless, 1: harmless})
assert result.per_layer[0].cost_effectiveness_ratio is None
assert result.per_layer[0].measurement_evidence["cost_effectiveness_ratio"].status == "unavailable"
assert result.best_layer == 1
assert result.mean_cost_ratio == pytest.approx(result.per_layer[1].cost_effectiveness_ratio)
none = extractor.extract_all_layers({0: harmless}, {0: harmless})
assert none.best_layer is none.mean_cost_ratio is None
assert "unavailable" in WassersteinOptimalExtractor.format_report(none)
empty = extractor.extract_all_layers({}, {})
assert empty.best_layer is None and empty.measurement_evidence["per_layer"].status == "unavailable"
def test_comparison_with_direction_orthogonal_to_shift_has_no_ratio():
harmless = [torch.tensor([1., 0.]), torch.tensor([0., 1.]), torch.tensor([1., 1.])]
harmful = [h + torch.tensor([2., 0.]) for h in harmless]
extractor = WassersteinOptimalExtractor()
result = extractor.extract(harmful, harmless)
comparison = extractor.compare_with_alternatives(
result, harmful, harmless, fisher_direction=torch.tensor([0., 1.]),
dim_direction=torch.tensor([1., 0.]),
)
assert comparison.fisher_cost_ratio is None and comparison.improvement_over_fisher is None
assert comparison.measurement_evidence["improvement_over_fisher"].status == "unavailable"
assert comparison.dim_cost_ratio is not None and comparison.improvement_over_dim is not None
assert comparison.measurement_evidence["dim_cost_ratio"].status == "diagnostic"
def test_cross_layer_accepts_tiny_nonzero_directions_the_pipeline_keeps():
"""The informed pipeline keeps directions with norm > 1e-10; the analyzer must accept them."""
import torch
from obliteratus.analysis.cross_layer import CrossLayerAlignmentAnalyzer
tiny = torch.tensor([1e-9, 0.0, 0.0])
result = CrossLayerAlignmentAnalyzer().analyze({0: tiny, 1: torch.tensor([0.0, 2.0, 0.0])})
assert result.mean_adjacent_cosine == pytest.approx(0.0, abs=1e-6)