mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
Remove whole per-layer subspaces in project, orthogonalize and lora modes; add mean-centred orthogonal and LEACE erasure at inference time and, where a native bias exists, as a permanent edit with recorded reference provenance. k=1 zero-origin paths are unchanged. Adds the Pain Axis Appendix D script. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
200 lines
8.7 KiB
Python
200 lines
8.7 KiB
Python
"""Calibration regressions for fixed vectors, lexical CV and reproducibility."""
|
|
|
|
from dataclasses import replace
|
|
import json
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.concepts import ConceptDirection, ExtractionRecipe
|
|
from obliteratus.concepts.extraction import extract_directions
|
|
from obliteratus.concepts.gate import (
|
|
ActivationCache, effective_thresholds, validate_direction, write_validation,
|
|
)
|
|
from tests.test_concept_gate import fixture, single_thread # noqa: F401
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
@pytest.mark.parametrize("rank", [1, 2])
|
|
@pytest.mark.parametrize("pooling", ["mean_of_means", "concat"])
|
|
def test_leace_auxiliary_calibration_is_scalar_without_changing_target_rank(
|
|
monkeypatch, rank, pooling,
|
|
):
|
|
from obliteratus.concepts import gate
|
|
|
|
generator = torch.Generator().manual_seed(335)
|
|
positive = {0: torch.randn(16, 4, generator=generator) + torch.tensor([4., 0., 0., 0.])}
|
|
controls = {
|
|
"neutral": {0: torch.randn(16, 4, generator=generator)},
|
|
"semantic": {0: torch.randn(12, 4, generator=generator) + torch.tensor([0., 4., 0., 0.])},
|
|
}
|
|
recipe = ExtractionRecipe(
|
|
method="leace_exact", n_directions=rank, pool_controls=pooling,
|
|
layer_select={"strategy": "cv_auc", "folds": 2},
|
|
)
|
|
direction = extract_directions(positive, controls, recipe, seed=335, concept_id="user/test")
|
|
original = direction.subspaces[0].clone()
|
|
cache = ActivationCache(
|
|
positive, controls,
|
|
neighbours={"nearby": {0: torch.randn(16, 4, generator=generator)}},
|
|
)
|
|
calls = []
|
|
extract = gate.extract_layer
|
|
|
|
def capture(positive, controls, recipe):
|
|
calls.append((recipe.method, recipe.n_directions, len(controls)))
|
|
return extract(positive, controls, recipe)
|
|
|
|
monkeypatch.setattr(gate, "extract_layer", capture)
|
|
report = validate_direction(direction, cache, random_directions=2)
|
|
assert calls == [("leace_exact", 1, 1), ("leace_exact", 1, 1), ("leace_exact", 1, 2)]
|
|
assert report["cv_recipe"]["n_directions"] == rank
|
|
assert direction.recipe.n_directions == rank
|
|
assert torch.equal(direction.subspaces[0], original)
|
|
row = report["metrics"]["0"]
|
|
assert all(value["control_vs_remaining_auc"] is not None
|
|
for value in row["semantic_controls"].values())
|
|
assert -1 <= row["neighbours"]["nearby"]["cosine"] <= 1
|
|
if rank == 2:
|
|
assert report["auxiliary_calibration"] == {
|
|
"method": "leace_exact", "n_directions": 1, "source_n_directions": 2,
|
|
"scope": "semantic_controls_and_neighbours",
|
|
}
|
|
assert any("do not validate every component" in note for note in report["notes"])
|
|
else:
|
|
assert "auxiliary_calibration" not in report
|
|
|
|
|
|
def test_saved_random_vector_is_scored_separately_from_recipe(tmp_path):
|
|
direction, cache = fixture("random")
|
|
path = tmp_path / "random.safetensors"
|
|
direction.save(path)
|
|
restored = ConceptDirection.load(path)
|
|
report = validate_direction(restored, cache)
|
|
write_validation(report, tmp_path / "validation.json")
|
|
row = json.loads((tmp_path / "validation.json").read_text())["metrics"]["0"]
|
|
assert row["heldout_auc"] == row["supplied_vector_heldout_auc"] < 0.75
|
|
assert row["fold_aucs"] == row["supplied_vector_fold_aucs"]
|
|
assert row["recipe_refit_cv_auc"] > 0.85
|
|
assert len(row["recipe_refit_fold_aucs"]) == 5
|
|
assert report["random_directions"] == 1000
|
|
assert len(row["random_aucs"]) == 1000
|
|
assert report["verdict"] == "fail"
|
|
torch.testing.assert_close(restored.directions[0], direction.directions[0])
|
|
|
|
|
|
def test_fixed_vector_sign_is_learned_only_on_training_rows():
|
|
direction, cache = fixture(count=32)
|
|
direction.recipe = replace(direction.recipe, layer_select={"strategy": "preset", "folds": 2})
|
|
generator = torch.Generator().manual_seed(direction.seed)
|
|
first, second = torch.randperm(32, generator=generator).tensor_split(2)
|
|
cache.positive[0].zero_()
|
|
cache.controls["neutral"][0].zero_()
|
|
cache.positive[0][first, 0] = 1
|
|
cache.positive[0][second, 0] = -1
|
|
report = validate_direction(direction, cache)
|
|
# Each training half has the opposite sign to its held-out half.
|
|
assert report["metrics"]["0"]["supplied_vector_fold_aucs"] == [0, 0]
|
|
|
|
|
|
def test_sign_orientation_preserves_supplied_tensors_and_projection_scores():
|
|
direction, cache = fixture("neighbour")
|
|
thresholds = {"max_neighbour_abs_cos": 0.95, "warn_neighbour_abs_cos": 0.92}
|
|
expected = validate_direction(
|
|
direction, cache, reference=direction.directions, thresholds=thresholds,
|
|
)
|
|
direction.directions[0] = -direction.directions[0]
|
|
original = direction.directions[0].clone()
|
|
actual = validate_direction(
|
|
direction, cache, reference={0: original}, thresholds=thresholds,
|
|
)
|
|
row = actual["metrics"]["0"]
|
|
assert row["orientation"] == -1
|
|
assert row["reference_cosine"] == expected["metrics"]["0"]["reference_cosine"] == 1
|
|
assert actual["verdict"] == expected["verdict"] == "pass"
|
|
assert expected["metrics"]["0"]["neighbours"]["injury"]["cosine"] == pytest.approx(0.9)
|
|
assert row["neighbours"]["injury"]["cosine"] == pytest.approx(-0.9)
|
|
row["orientation"] = 1
|
|
row["neighbours"]["injury"]["cosine"] *= -1
|
|
assert actual == expected
|
|
assert torch.equal(direction.directions[0], original)
|
|
|
|
|
|
def test_lexical_gap_override_and_core_tightening():
|
|
direction, cache = fixture("suffix")
|
|
report = validate_direction(direction, cache, thresholds={"min_lexical_auc_gap": 0})
|
|
assert report["verdict"] == "warn" # The token scan still warns.
|
|
assert not any(r["check"] == "lexical_confound" for r in report["reasons"])
|
|
assert effective_thresholds({"min_lexical_auc_gap": 0.1}, tier="core")[
|
|
"min_lexical_auc_gap"
|
|
] == 0.1
|
|
with pytest.raises(ValueError, match="only tighten"):
|
|
effective_thresholds({"min_lexical_auc_gap": 0.01}, tier="core")
|
|
|
|
|
|
@pytest.mark.parametrize("pooling", ["mean_of_means", "concat"])
|
|
def test_lexical_cv_detects_character_cues_and_is_exactly_reproducible(pooling):
|
|
direction, cache = fixture()
|
|
direction.recipe = replace(direction.recipe, pool_controls=pooling)
|
|
cache.controls["other"] = {0: cache.controls["neutral"][0][:40]}
|
|
cache.texts = (
|
|
[f"topic aaa{i}" for i in range(64)],
|
|
{"neutral": [f"topic bbb{i}" for i in range(64)],
|
|
"other": [f"topic bbb{i}" for i in range(40)]},
|
|
)
|
|
# No full word identifies a class across rows; character trigrams do.
|
|
state = torch.random.get_rng_state().clone()
|
|
report = validate_direction(direction, cache)
|
|
assert torch.equal(torch.random.get_rng_state(), state)
|
|
cache.controls = dict(reversed(list(cache.controls.items())))
|
|
cache.texts = (cache.texts[0], dict(reversed(list(cache.texts[1].items()))))
|
|
assert validate_direction(direction, cache) == report
|
|
assert report["lexical_baseline"]["fold_aucs"] == [1] * 5
|
|
assert any(r["check"] == "lexical_confound" and r["severity"] == "fail"
|
|
for r in report["reasons"])
|
|
|
|
|
|
def test_lexical_vocabulary_does_not_learn_heldout_only_tokens():
|
|
direction, cache = fixture()
|
|
# Single, unique characters have no trigrams or words shared across folds.
|
|
cache.texts = ([chr(0x4E00 + i) for i in range(64)],
|
|
{"neutral": [chr(0x4F00 + i) for i in range(64)]})
|
|
report = validate_direction(direction, cache)
|
|
assert report["lexical_baseline"]["fold_aucs"] == [0.5] * 5
|
|
assert report["verdict"] == "pass"
|
|
|
|
|
|
@pytest.mark.parametrize("texts", [
|
|
(["topic"], {"neutral": ["topic"] * 64}),
|
|
(["topic"] * 64, {"unknown": ["topic"] * 64}),
|
|
([1] * 64, {"neutral": ["topic"] * 64}),
|
|
])
|
|
def test_prompt_cache_must_match_activation_rows(texts):
|
|
direction, cache = fixture()
|
|
cache.texts = texts
|
|
with pytest.raises(ValueError, match="Prompt texts must match"):
|
|
validate_direction(direction, cache)
|
|
|
|
|
|
def test_missing_texts_are_explicitly_unavailable():
|
|
direction, cache = fixture()
|
|
cache.texts = None
|
|
report = validate_direction(direction, cache)
|
|
assert report["lexical_baseline"]["heldout_auc"] is None
|
|
assert report["lexical_baseline"]["fold_aucs"] == []
|
|
assert report["verdict"] == "warn"
|
|
assert report["reasons"][0]["check"] == "lexical_confound"
|
|
assert report["reasons"][0]["message"] == "Text-only lexical baseline unavailable"
|
|
|
|
|
|
def test_texts_with_insufficient_folds_have_specific_warning():
|
|
direction, cache = fixture(count=1)
|
|
report = validate_direction(direction, cache)
|
|
assert report["lexical_baseline"]["heldout_auc"] is None
|
|
assert report["lexical_baseline"]["fold_aucs"] == []
|
|
assert report["verdict"] == "fail"
|
|
assert [r["message"] for r in report["reasons"] if r["check"] == "lexical_confound"] == [
|
|
"Insufficient folds for text-only lexical baseline",
|
|
]
|