mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
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>
140 lines
6.8 KiB
Python
140 lines
6.8 KiB
Python
"""Reduced-rank analysis: spectrum test, tail-signal fixture and approximate gate nulls (O05)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.analysis import batched_nulls as bn
|
|
from obliteratus.analysis import kernel_fixtures as kf
|
|
from obliteratus.analysis import low_rank as lr
|
|
from obliteratus.analysis.subspace_stats import principal_angles
|
|
from obliteratus.concepts import ExtractionRecipe
|
|
from obliteratus.concepts.extraction import extract_directions
|
|
from obliteratus.concepts.gate import ActivationCache, _auc, validate_direction
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
THRESHOLDS = (95.0, 99.0)
|
|
|
|
|
|
def _rows(kind, **shape):
|
|
positive, controls, vector = kf.gate_fixture(kind, **shape)
|
|
rows = torch.cat([positive, *controls.values()])
|
|
labels = torch.cat([torch.ones(len(positive)), torch.zeros(len(rows) - len(positive))])
|
|
return rows, labels, positive, controls, vector
|
|
|
|
|
|
@pytest.mark.parametrize("method", ["exact", "randomized"])
|
|
def test_low_variance_target_signal_drives_rank_selection(method):
|
|
rows, labels, *_ = _rows("tail_signal")
|
|
report, basis = lr.select_rank(rows, labels, max_rank=12, method=method)
|
|
variance_rank = report["variance_only_rank"]
|
|
assert variance_rank == 4
|
|
assert report["signal_loss_fraction"][variance_rank - 1] > report["budget"]["signal"]
|
|
assert report["selected_rank"] == 5 and basis.shape == (5, rows.shape[1])
|
|
assert report["status"] == ("exact" if method == "exact" else "approximate")
|
|
json.dumps(report)
|
|
|
|
|
|
def test_randomized_basis_matches_exact_subspace_on_a_justified_spectrum():
|
|
rows, labels, *_ = _rows("low_rank_signal")
|
|
exact, exact_basis = lr.select_rank(rows, labels, max_rank=12)
|
|
approx, approx_basis = lr.select_rank(rows, labels, max_rank=12, method="randomized")
|
|
assert exact["selected_rank"] == approx["selected_rank"] == 4
|
|
assert max(principal_angles(exact_basis, approx_basis)) < 1e-2
|
|
assert approx["tail_energy_fraction"][3] <= lr.DEFAULT_BUDGET["energy"]
|
|
|
|
|
|
def test_flat_spectrum_is_inconclusive_and_unlabelled_selection_uses_energy_only():
|
|
rows, labels, *_ = _rows("flat")
|
|
report, basis = lr.select_rank(rows, labels)
|
|
assert report["status"] == "inconclusive" and basis is None and report["reason"]
|
|
rows, *_ = _rows("low_rank_null")
|
|
report, basis = lr.select_rank(rows, max_rank=8)
|
|
assert report["signal_loss_fraction"] is None and report["selected_rank"] == 4
|
|
constant = torch.ones(10, 6, dtype=torch.float64)
|
|
report, _ = lr.select_rank(constant, torch.arange(10) % 2, max_rank=2)
|
|
assert report["selected_rank"] == 1
|
|
|
|
|
|
def test_select_rank_and_budget_refusals():
|
|
rows, labels, *_ = _rows("low_rank_null", n=10, d=8)
|
|
with pytest.raises(ValueError, match="method"):
|
|
lr.select_rank(rows, labels, method="sketch")
|
|
with pytest.raises(ValueError, match="max_rank"):
|
|
lr.select_rank(rows, labels, max_rank=100)
|
|
with pytest.raises(ValueError, match="Unknown"):
|
|
lr.check_budget({"angle": 0.1})
|
|
with pytest.raises(ValueError, match="lie in"):
|
|
lr.check_budget({"auc": 1.5})
|
|
with pytest.raises(ValueError, match="two classes"):
|
|
lr.class_contrast(rows, torch.zeros(len(rows)))
|
|
with pytest.raises(ValueError, match="Unknown gate fixture"):
|
|
kf.gate_fixture("missing")
|
|
|
|
|
|
def _sketch(kind, budget=None, max_rank=None, count=400):
|
|
_, _, positive, controls, vector = _rows(kind)
|
|
observed = _auc(positive, controls, vector, "mean_of_means")
|
|
draws = bn.gate_draws(vector, count, torch.Generator().manual_seed(0))
|
|
reference = bn.reference_gate_random_aucs(positive, controls, vector, count,
|
|
torch.Generator().manual_seed(0), "mean_of_means")
|
|
approx, meta = lr.sketch_gate_random_aucs(positive, controls, vector, draws, "mean_of_means",
|
|
observed, THRESHOLDS, budget=budget,
|
|
max_rank=max_rank)
|
|
return observed, reference, approx, meta
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["low_rank_signal", "ties"])
|
|
def test_sketch_null_is_approximate_only_within_declared_decision_tolerance(kind):
|
|
observed, reference, approx, meta = _sketch(kind)
|
|
assert meta["status"] == "approximate", meta["reason"]
|
|
record = bn.compare_nulls(reference, approx, observed, THRESHOLDS,
|
|
tolerance=lr.DEFAULT_BUDGET["auc"])
|
|
assert record["within_tolerance"] and record["decision_disagreements"] == 0
|
|
low, high = meta["percentile_interval"]
|
|
assert low <= record["reference_percentile"] <= high
|
|
|
|
|
|
def test_sketch_reports_inconclusive_for_tail_loss_audit_error_and_threshold_band():
|
|
_, _, approx, meta = _sketch("tail_signal", max_rank=4)
|
|
assert approx is None and meta["status"] == "inconclusive"
|
|
assert "target-signal" in meta["reason"]
|
|
_, _, _, meta = _sketch("low_rank_signal", budget={"auc": 1e-9})
|
|
assert meta["status"] == "inconclusive" and "audited" in meta["reason"]
|
|
_, _, _, meta = _sketch("low_rank_null")
|
|
assert meta["status"] == "inconclusive" and "band" in meta["reason"]
|
|
|
|
|
|
def test_kernel_dispatch_falls_back_to_exact_values():
|
|
_, _, positive, controls, vector = _rows("flat", n=30, d=40)
|
|
observed = _auc(positive, controls, vector, "mean_of_means")
|
|
exact = bn.gate_random_aucs(positive, controls, vector, 60, torch.Generator().manual_seed(1),
|
|
"mean_of_means")
|
|
values, meta = lr.kernel_random_aucs("sketch", positive, controls, vector, 60,
|
|
torch.Generator().manual_seed(1), "mean_of_means",
|
|
observed, THRESHOLDS)
|
|
assert meta["status"] == "reference_fallback" and values == exact
|
|
values, meta = lr.kernel_random_aucs("batched", positive, controls, vector, 60,
|
|
torch.Generator().manual_seed(1), "mean_of_means",
|
|
observed, THRESHOLDS)
|
|
assert meta["status"] == "exact" and values == exact
|
|
|
|
|
|
def test_gate_sketch_kernel_records_metadata_and_keeps_the_decision():
|
|
positive, controls, _ = kf.gate_fixture("low_rank_signal")
|
|
pos, ctrl = {0: positive.float()}, {k: {0: v.float()} for k, v in controls.items()}
|
|
direction = extract_directions(pos, ctrl, ExtractionRecipe(), seed=3, concept_id="lowrank")
|
|
cache = ActivationCache(pos, ctrl)
|
|
reference = validate_direction(direction, cache, mode="report", random_directions=300)
|
|
sketch = validate_direction(direction, cache, mode="report", random_directions=300,
|
|
null_kernel="sketch")
|
|
meta = sketch["metrics"]["0"]["random_null"]
|
|
assert sketch["null_kernel"] == "sketch"
|
|
assert meta["kernel"] == "sketch" and meta["status"] in {"approximate", "reference_fallback"}
|
|
assert sketch["verdict"] == reference["verdict"]
|
|
assert sketch["reasons"] == reference["reasons"]
|
|
json.dumps(sketch)
|