Files
OBLITERATUS/obliteratus/analysis/kernel_benchmark.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

202 lines
9.3 KiB
Python

"""Benchmark report for the batched null kernels and reduced-rank analysis (O05).
Runs every arm on the deterministic fixtures in ``kernel_fixtures`` and
reports wall time, peak resident-memory growth, subspace angles, projection
and AUC errors, target-signal (tail) loss and gate-decision disagreement.
Timings and memory are host-specific observations, never assertions; the
agreement fields are the reproducible part of the report.
Usage: ``python -m obliteratus.analysis.kernel_benchmark --output report.json``
"""
from __future__ import annotations
import argparse
import json
import os
import platform
import threading
import time
import torch
from obliteratus.analysis import batched_lda as bl
from obliteratus.analysis import batched_nulls as bn
from obliteratus.analysis import low_rank as lr
from obliteratus.analysis import subspace_stats as st
from obliteratus.analysis import kernel_fixtures as kf
SCHEMA = "obliteratus.kernel_benchmark"
SCHEMA_VERSION = 1
THRESHOLDS = (95.0, 99.0)
POOLING = "mean_of_means"
HOST_NOTE = ("Wall time and memory are host-specific (CPU, BLAS, thread count, load); "
"compare arms within one report only.")
def _rss():
try:
with open("/proc/self/statm", encoding="ascii") as handle:
return int(handle.read().split()[1]) * os.sysconf("SC_PAGE_SIZE")
except (OSError, ValueError, IndexError, AttributeError):
return None
def measure(fn, repeats=1, warmup=1):
"""``(result, {"wall_seconds": [...], "peak_rss_growth_bytes": int | None})`` after warm-up."""
for _ in range(warmup):
fn()
start, peak, stop = _rss(), [0], threading.Event()
def sample():
while not stop.is_set():
peak[0] = max(peak[0], _rss() or 0)
stop.wait(0.001)
sampler = threading.Thread(target=sample, daemon=True) if start is not None else None
if sampler:
sampler.start()
walls, result = [], None
for _ in range(repeats):
began = time.perf_counter()
result = fn()
walls.append(time.perf_counter() - began)
if sampler:
stop.set()
sampler.join()
growth = None if start is None else max(peak[0], _rss() or 0) - start
return result, {"wall_seconds": walls, "peak_rss_growth_bytes": growth}
def _decision(percentile):
return "fail" if percentile < THRESHOLDS[0] else "warn" if percentile < THRESHOLDS[1] else "pass"
def gate_case(kind, *, count=1000, seed=0, repeats=1, **shape):
"""Reference loop, batched kernel and sketch on one gate fixture."""
from obliteratus.concepts.gate import _auc
positive, controls, vector = kf.gate_fixture(kind, seed=seed, **shape)
observed = _auc(positive, controls, vector, POOLING)
def gen():
return torch.Generator().manual_seed(seed)
ref, ref_cost = measure(lambda: bn.reference_gate_random_aucs(
positive, controls, vector, count, gen(), POOLING), repeats)
batched, batched_cost = measure(lambda: bn.gate_random_aucs(
positive, controls, vector, count, gen(), POOLING), repeats)
draws = bn.gate_draws(vector, count, gen())
(sketch, meta), sketch_cost = measure(lambda: lr.sketch_gate_random_aucs(
positive, controls, vector, draws, POOLING, observed, THRESHOLDS), repeats)
ref_pct = bn.random_percentile(observed, ref)
sketch_record = {"status": meta["status"], "reason": meta["reason"], "cost": sketch_cost,
"selected_rank": meta["spectrum_test"]["selected_rank"],
"variance_only_rank": meta["spectrum_test"]["variance_only_rank"],
"audit": meta["audit"], "percentile_interval": meta["percentile_interval"]}
if sketch is not None:
sketch_record["agreement"] = bn.compare_nulls(ref, sketch, observed, THRESHOLDS,
tolerance=lr.DEFAULT_BUDGET["auc"])
sketch_record["gate_decision"] = _decision(bn.random_percentile(observed, sketch))
return {
"fixture": kind, "rows": len(positive) + sum(len(v) for v in controls.values()),
"width": int(vector.numel()), "random_directions": count, "observed_auc": observed,
"reference": {"cost": ref_cost, "percentile": ref_pct, "gate_decision": _decision(ref_pct)},
"batched": {"cost": batched_cost, "agreement": bn.compare_nulls(
ref, batched, observed, THRESHOLDS),
"gate_decision": _decision(bn.random_percentile(observed, batched))},
"sketch": sketch_record,
}
def low_rank_case(kind, *, seed=0, max_rank=16, **shape):
"""Exact versus randomized rank selection: angles, energy, signal loss, rank choice."""
positive, controls, _ = kf.gate_fixture(kind, seed=seed, **shape)
rows = torch.cat([positive, *controls.values()])
labels = torch.cat([torch.ones(len(positive)), torch.zeros(len(rows) - len(positive))])
(exact, exact_basis), exact_cost = measure(lambda: lr.select_rank(
rows, labels, max_rank=max_rank))
(approx, approx_basis), approx_cost = measure(lambda: lr.select_rank(
rows, labels, max_rank=max_rank, method="randomized"))
variance_rank = exact["variance_only_rank"]
record = {"fixture": kind, "exact": exact, "randomized": approx,
"exact_cost": exact_cost, "randomized_cost": approx_cost,
"variance_only_signal_loss": None if variance_rank is None
else exact["signal_loss_fraction"][variance_rank - 1],
"rank_disagreement": exact["selected_rank"] != approx["selected_rank"]}
if exact_basis is not None and approx_basis is not None:
record["subspace_angles_deg"] = st.principal_angles(exact_basis, approx_basis)
return record
def subspace_case(*, n_null=50, repeats=1, **shape):
"""Subspace-report increment and Haar nulls: reference loop versus batched LDA."""
from obliteratus.analysis.subspace_report import build_subspace_report
x, codes, basis = kf.subspace_fixture(**shape)
reports = {}
costs = {}
for kernel in ("reference", "batched"):
reports[kernel], costs[kernel] = measure(lambda kernel=kernel: build_subspace_report(
x, codes, basis, n_null=n_null, n_bootstrap=0, null_kernel=kernel), repeats)
ref, new = reports["reference"], reports["batched"]
increments = [abs(a["increment_null_quantile"] - b["increment_null_quantile"])
for a, b in zip(ref["per_rank"], new["per_rank"])]
return {"n_null": n_null, "costs": costs,
"subspace_null": bn.compare_nulls(ref["subspace_null"]["scores"],
new["subspace_null"]["scores"]),
"max_increment_quantile_error": max(increments, default=0.0),
"verdict_disagreement": ref["verdict"] != new["verdict"]}
def discovery_case(*, n_random=1000, folds=5, seed=0, chunk_size=128, repeats=1):
"""Discovery random-direction null: one full product versus bounded column chunks."""
from obliteratus.analysis.candidate_discovery import _random_direction_null, _splits
x, y, groups = kf.discovery_fixture()
splits = _splits(groups, y, folds, seed)
ref, ref_cost = measure(lambda: _random_direction_null(
x, y, splits, {"n_random": n_random}, seed), repeats)
new, new_cost = measure(lambda: bn.discovery_random_aucs(
x, y, splits, n_random, seed, chunk_size=chunk_size), repeats)
return {"n_random": n_random, "chunk_size": chunk_size,
"reference_cost": ref_cost, "chunked_cost": new_cost,
"agreement": bn.compare_nulls(ref, new)}
def run_benchmark(*, quick=False, repeats=1):
count = 200 if quick else 1000
shape = {"n": 48, "d": 64} if quick else {}
return {
"schema": SCHEMA, "schema_version": SCHEMA_VERSION,
"host": {"platform": platform.platform(), "python": platform.python_version(),
"torch": torch.__version__, "threads": torch.get_num_threads()},
"host_note": HOST_NOTE, "quick": quick, "repeats": repeats,
"budget": dict(lr.DEFAULT_BUDGET), "batched_auc_tolerance": bn.AUC_TOLERANCE,
"gate": [gate_case(kind, count=count, repeats=repeats, **shape)
for kind in kf.GATE_FIXTURES],
"low_rank": [low_rank_case(kind, max_rank=8 if quick else 16, **shape)
for kind in kf.GATE_FIXTURES],
"subspace": subspace_case(n_null=20 if quick else 50, repeats=repeats),
"discovery": discovery_case(n_random=count, repeats=repeats),
"batched_lda_chunk": bl.DEFAULT_CHUNK,
}
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("--output", required=True, help="JSON report path")
parser.add_argument("--quick", action="store_true", help="smaller fixtures and draws")
parser.add_argument("--repeats", type=int, default=1)
parser.add_argument("--threads", type=int, default=None, help="torch CPU threads")
args = parser.parse_args(argv)
if args.repeats < 1 or (args.threads is not None and args.threads < 1):
parser.error("--repeats and --threads must be positive")
if args.threads is not None:
torch.set_num_threads(args.threads)
report = run_benchmark(quick=args.quick, repeats=args.repeats)
with open(args.output, "w", encoding="utf-8") as handle:
json.dump(report, handle, indent=2, sort_keys=True)
return 0
if __name__ == "__main__": # pragma: no cover
raise SystemExit(main())