Files
OBLITERATUS/tests/test_batched_nulls.py
Joseph MaglyandClaude Opus 5.5 9e65c2385e feat(analysis): batched null kernels and reduced-rank sketches
Add optimization O05: batched random-direction kernels that reproduce
the reference loops, plus validated low-rank sketches with an explicit
error budget. The reference loops stay the default.

- Batched gate, discovery and subspace nulls keep draw order, generator
  state, norm matching, pooling, orientation folding and percentile
  semantics; identical rows are projected once so exact ties survive.
  Declared tolerance 1e-12 with identical decisions; observed 0.0.
- select_rank keeps both residual variance and class-contrast loss
  within budget, measured exactly, and reports the variance-only rank;
  it returns inconclusive when no rank qualifies.
- The sketch gate null is approximate only when the spectrum test
  passes, an exact audit stays within 0.005 AUC and no threshold falls
  inside the band; otherwise it recomputes exactly. It is refused in
  enforce mode. The observed AUC is never approximated.
- A benchmark module reports wall time, peak memory, subspace angles,
  null errors and decision disagreement (timings host-specific).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:29:07 -05:00

187 lines
9.0 KiB
Python

"""Batched null kernels agree with the reference loops (issue #480, roadmap O05)."""
from __future__ import annotations
import json
import pytest
import torch
from obliteratus.analysis import batched_lda as bl
from obliteratus.analysis import batched_nulls as bn
from obliteratus.analysis import kernel_fixtures as kf
from obliteratus.analysis.candidate_discovery import (
_random_direction_null, _splits, discover_candidates,
)
from obliteratus.analysis.subspace_report import build_subspace_report
from obliteratus.concepts.extraction import projection_auc
from obliteratus.concepts.gate import _auc, validate_direction
from tests.test_concept_gate import fixture as gate_test_fixture
from tests.test_labeled_activations import PlantedModel, build_pack, capture, load
pytestmark = pytest.mark.cpu
POOLINGS = ("mean_of_means", "pooled")
def _gen(seed=0):
return torch.Generator().manual_seed(seed)
def test_tie_aware_auc_is_bitwise_equal_to_projection_auc_including_ties():
g = _gen(3)
positive = torch.randint(-2, 3, (17, 9), generator=g).double()
control = torch.randint(-2, 3, (23, 9), generator=g).double()
batched = bn.tie_aware_auc(positive, control)
for column in range(9):
assert batched[column].item() == projection_auc(positive[:, column], control[:, column])
with pytest.raises(ValueError, match="nonempty finite"):
bn.tie_aware_auc(positive[:0], control)
@pytest.mark.parametrize("kind", ["planted", "null", "random", "sign_flipped"])
def test_gate_batched_kernel_matches_reference_report(kind):
direction, cache = gate_test_fixture(kind)
reference = validate_direction(direction, cache, mode="report", random_directions=300)
batched = validate_direction(direction, cache, mode="report", random_directions=300,
null_kernel="batched")
assert "null_kernel" not in reference and "random_null" not in reference["metrics"]["0"]
assert batched["null_kernel"] == "batched"
ref_row, new_row = reference["metrics"]["0"], batched["metrics"]["0"]
agreement = bn.compare_nulls(ref_row["random_aucs"], new_row["random_aucs"])
assert agreement["within_tolerance"], agreement
assert new_row["random_percentile"] == ref_row["random_percentile"]
assert new_row["orientation"] == ref_row["orientation"]
assert new_row["random_null"]["status"] == "exact"
assert batched["verdict"] == reference["verdict"]
assert batched["reasons"] == reference["reasons"]
json.dumps(batched)
def test_reference_copy_reproduces_gate_loop_and_rng_consumption():
direction, cache = gate_test_fixture("planted")
report = validate_direction(direction, cache, mode="report", random_directions=40)
positive = cache.positive[0].double()
controls = {k: v[0].double() for k, v in cache.controls.items()}
vector = direction.directions[0].double() * report["metrics"]["0"]["orientation"]
ref_gen, new_gen = _gen(report["seed"]), _gen(report["seed"])
ref = bn.reference_gate_random_aucs(positive, controls, vector, 40, ref_gen, "mean_of_means")
new = bn.gate_random_aucs(positive, controls, vector, 40, new_gen, "mean_of_means",
chunk_size=7)
assert ref == report["metrics"]["0"]["random_aucs"]
assert bn.compare_nulls(ref, new)["within_tolerance"]
assert torch.equal(torch.randn(5, generator=ref_gen), torch.randn(5, generator=new_gen))
@pytest.mark.parametrize("pooling", POOLINGS)
@pytest.mark.parametrize("kind", ["ties", "low_rank_signal", "tail_signal"])
def test_ties_orientation_and_chunking_agree_with_reference(kind, pooling):
positive, controls, vector = kf.gate_fixture(kind, n=40, d=48)
observed = _auc(positive, controls, vector, pooling)
ref = bn.reference_gate_random_aucs(positive, controls, vector, 120, _gen(), pooling)
for chunk in (1, 13, 512):
new = bn.gate_random_aucs(positive, controls, vector, 120, _gen(), pooling,
chunk_size=chunk)
record = bn.compare_nulls(ref, new, observed, (95.0, 99.0))
assert record["within_tolerance"] and record["decision_disagreements"] == 0, record
assert min(new) >= 0.5 # folded orientation
flipped = bn.gate_random_aucs(positive, controls, -vector, 120, _gen(), pooling)
assert flipped == bn.gate_random_aucs(positive, controls, vector, 120, _gen(), pooling)
def test_duplicate_rows_keep_exact_ties_across_classes():
rows = torch.randn(6, 33, generator=_gen(5), dtype=torch.float64)
positive, controls = rows[[0, 1, 2, 0]], {"a": rows[[0, 3, 4, 5, 1]]}
draws = bn.gate_draws(rows[0], 50, _gen())
scores = bn._Rows(positive, controls).project(draws)
assert torch.equal(scores[0], scores[3]) and torch.equal(scores[0], scores[4])
assert torch.equal(scores[1], scores[8])
def test_kernel_and_comparison_refusals():
with pytest.raises(ValueError, match="null_kernel"):
bn.check_kernel("fast")
with pytest.raises(ValueError, match="chunk_size"):
bn.check_kernel("batched", 0)
with pytest.raises(ValueError, match="same length"):
bn.compare_nulls([0.5], [0.5, 0.6])
assert bn.compare_nulls([], [])["max_abs_error"] == 0.0
assert bn.gate_draws(torch.ones(3, dtype=torch.float64), 0, _gen()).shape == (0, 3)
direction, cache = gate_test_fixture("planted")
with pytest.raises(ValueError, match="null_kernel"):
validate_direction(direction, cache, mode="report", null_kernel="fast")
with pytest.raises(ValueError, match="refused in enforce mode"):
validate_direction(direction, cache, mode="enforce", null_kernel="sketch")
def test_discovery_chunked_null_matches_reference_product():
x, y, groups = kf.discovery_fixture()
splits = _splits(groups, y, 5, 0)
ref = _random_direction_null(x, y, splits, {"n_random": 90}, 0)
for chunk in (1, 32, 500):
new = bn.discovery_random_aucs(x, y, splits, 90, 0, chunk_size=chunk)
assert bn.compare_nulls(ref, new)["within_tolerance"]
assert bn.discovery_random_aucs(x, y, splits, 0, 0).numel() == 0
def _assert_close(left, right):
if isinstance(right, dict):
assert left.keys() == right.keys()
for key in right:
_assert_close(left[key], right[key])
elif isinstance(right, (list, tuple)):
assert len(left) == len(right)
for a, b in zip(left, right):
_assert_close(a, b)
elif isinstance(right, float):
assert left == pytest.approx(right, abs=bn.AUC_TOLERANCE)
else:
assert left == right
def test_discover_candidates_batched_null_is_opt_in_and_equivalent(tmp_path):
pack = build_pack(tmp_path / "pack")
planted = torch.zeros(4, 3)
planted[0, 2] = 4.0
model = PlantedModel(planted=planted)
capture(model, pack, tmp_path / "run", roles=["fit", "select"])
labeled = load(model, pack, tmp_path / "run")
options = {"n_permutations": 3, "n_bootstrap": 3, "n_random": 40}
reference = discover_candidates(labeled, "layer.0", **options)
batched = discover_candidates(labeled, "layer.0", null_kernel="batched", **options)
assert "null_kernel" not in reference and batched.pop("null_kernel") == "batched"
_assert_close(batched, reference)
with pytest.raises(ValueError, match="null_kernel"):
discover_candidates(labeled, "layer.0", null_kernel="fast", **options)
@pytest.mark.parametrize("grouped", [False, True])
def test_subspace_batched_nulls_reproduce_reference_report(grouped):
x, codes, basis = kf.subspace_fixture(n=96, d=14)
groups = torch.arange(96) // 2 if grouped else None
kwargs = {"groups": groups, "n_null": 24, "n_bootstrap": 3}
reference = build_subspace_report(x, codes, basis, **kwargs)
batched = build_subspace_report(x, codes, basis, null_kernel="batched", **kwargs)
assert batched["parameters"].pop("null_kernel") == "batched"
assert "null_kernel" not in reference["parameters"]
ref_null, new_null = reference.pop("subspace_null"), batched.pop("subspace_null")
assert bn.compare_nulls(ref_null["scores"], new_null["scores"])["within_tolerance"]
for ref, new in zip(reference.pop("per_rank"), batched.pop("per_rank")):
assert new["passes"] == ref["passes"]
assert new["increment_threshold"] == pytest.approx(ref["increment_threshold"], abs=1e-12)
assert batched == reference
with pytest.raises(ValueError, match="null_kernel"):
build_subspace_report(x, codes, basis, null_kernel="sketch")
def test_batched_lda_scores_match_scalar_readout_with_absent_class():
from obliteratus.analysis import subspace_stats as st
g = _gen(9)
x = torch.randn(5, 30, 3, generator=g, dtype=torch.float64)
codes = torch.tensor([0, 1] * 15)
codes[-1] = 2 # class 2 appears only in the test rows
train, test = torch.arange(20), torch.arange(20, 30)
batched = bl.batched_lda_scores(x[:, train], codes[train], x[:, test], codes[test], 3, 1e-6)
for j in range(5):
expected = st.lda_score(x[j, train], codes[train], x[j, test], codes[test], 3, 1e-6)
assert batched[j].item() == pytest.approx(expected, abs=1e-12)