Files
OBLITERATUS/tests/test_numeric_geometry_eval.py

404 lines
21 KiB
Python

"""Numeric geometry preservation campaign on golden synthetic fixtures (#511).
The preserving fixture keeps the spread under every mean shift that leaves its
spread coordinate alone; the distorting fixture couples spread to the arc
radius, so a chord distorts it while the spline along the arc does not.
"""
import copy
import json
import re
from pathlib import Path
import numpy as np
import pytest
import torch
from scipy.special import ndtr
from obliteratus.analysis import numeric_geometry_dossier as dossier
from obliteratus.analysis import numeric_geometry_eval as ev
from obliteratus.analysis import numeric_geometry_spec as spec
from obliteratus.analysis.numeric_geometry_arms import ARMS, Geometry
from obliteratus.analysis.numeric_geometry_metrics import CATALOG
from obliteratus.analysis.numeric_geometry_synthetic import C_SD, R, SPAN, THETA0, synthetic_model
from obliteratus.cli import main
pytestmark = pytest.mark.cpu
DOC = Path(__file__).resolve().parents[1] / "docs" / "geometry-measurement-validity.md"
CELLS = [{"source_mean": 300, "target_mean": 500, "sd": 100},
{"source_mean": 300, "target_mean": 350, "sd": 100},
{"source_mean": 300, "target_mean": 650, "sd": 100},
{"source_mean": 300, "target_mean": 800, "sd": 100}]
def small_campaign(**data):
campaign = spec.default_campaign()
campaign["runtime"]["device"] = "cpu"
campaign["data"].update({"cells": copy.deepcopy(CELLS), "context_lengths": [96, 128],
"seed_blocks": [0, 1, 2],
"sequences_per_block": {"fit": 20, "calibration": 12,
"heldout": 10}, **data})
return campaign
def small_prereg():
prereg = spec.preregistration_template()
prereg["interval"]["n_bootstrap"] = 300
prereg["scales"]["bisection_iterations"] = 20
return prereg
@pytest.fixture(scope="module")
def results():
return {variant: ev.run_campaign(small_campaign(), small_prereg(), synthetic_model(variant))
for variant in ("preserving", "distorting")}
def row(result, cell, arm, comparison="matched_achieved_mean", variant=None):
return next(r for r in result["rows"] if r["cell"] == cell and r["arm"] == arm
and r["comparison"] == comparison and r["variant"] == variant)
def _distorting_fixture_sd(prefix_means, prefix_sds, step):
"""Compute the spread of the rounded, clipped Gaussian from its CDF."""
angles = THETA0 + prefix_means * SPAN / 1000
x, y = R * np.cos(angles) + step[0], R * np.sin(angles) + step[1]
means = (np.arctan2(y, x) - THETA0) * 1000 / SPAN
spreads = prefix_sds * np.exp(step[2] / C_SD) * (np.hypot(x, y) / R) ** 2
boundaries = np.arange(999) + 0.5
cdf = ndtr((boundaries - means[:, None]) / spreads[:, None])
pmf = np.diff(cdf, axis=1, prepend=0, append=1)
support = np.arange(1000)
centered = support - (pmf @ support)[:, None]
return np.sqrt(np.sum(pmf * centered ** 2, axis=1))
# Golden known answers ------------------------------------------------------------
def test_golden_preserving_fixture_preserves_spread(results):
result = results["preserving"]
for arm in ("centroid_chord", "spline"):
r = row(result, "m300-to-m500-sd100", arm)
assert r["verdict"] == "preserved", (arm, r["verdict_reasons"])
assert abs(r["sd_drift"]["estimate"]) < 2.5 and r["seed_block_stable"]
assert abs(r["target_mean_error"]["estimate"]) < 10
assert result["coverage"]["centroid_chord"]["source_supported_preserved"] == 2
def test_golden_distorting_fixture_detects_distortion(results):
result = results["distorting"]
chord = row(result, "m300-to-m500-sd100", "centroid_chord")
assert chord["verdict"] == "not_preserved" and chord["sd_drift"]["high"] < -5
assert chord["verdict_reasons"] == ["spread distorted: interval outside the sd tolerance"]
assert abs(chord["target_mean_error"]["estimate"]) < 10
spline = row(result, "m300-to-m500-sd100", "spline")
assert spline["verdict"] == "preserved" and abs(spline["sd_drift"]["estimate"]) < 2.5
preserving_chord = row(results["preserving"], "m300-to-m500-sd100", "centroid_chord")
assert chord["scale"] == pytest.approx(preserving_chord["scale"])
def test_distorting_fixture_probe_outcomes_follow_paired_analytic_spread(results):
campaign = small_campaign()
prereg = small_prereg()
data = campaign["data"]
sequences = spec.build_sequences(campaign)
fit_prompts = spec.prompts(sequences["fit"], data["context_lengths"])
geometry = Geometry(
synthetic_model("distorting").activations(fit_prompts),
[prompt["generating"]["mean"] for prompt in fit_prompts],
data["fit_means"], prereg["arms"], prereg["scales"]["reference_arm"],
)
heldout = spec.prompts(sequences["heldout"], data["context_lengths"])
prefixes = {(p["sequence_id"], p["number_index"]): p["empirical"] for p in heldout}
# The fitted probe can offset a radial change via the spread coordinate.
# Compare each prediction with the analytic paired drift before checking verdicts.
expected_verdicts = []
probes = []
interval = prereg["interval"]
for optimizer in ("lbfgs", "adamw"):
probe = row(results["distorting"], "m300-to-m500-sd100", "linear_probe", variant=optimizer)
samples = probe["sequences"]
stats = [prefixes[sample["sequence_id"], sample["number_index"]] for sample in samples]
means = np.array([s["mean"] for s in stats])
sds = np.array([s["sd"] for s in stats])
weights = geometry.probes[optimizer]["weights"]
step = (probe["scale"] * weights / weights.norm()).numpy()
oracle = _distorting_fixture_sd(means, sds, step) - _distorting_fixture_sd(means, sds, np.zeros(3))
np.testing.assert_allclose([s["sd_drift"] for s in samples], oracle, atol=1e-8, rtol=0)
groups = np.array([sample["sequence_id"] for sample in samples])
paired = np.array([oracle[groups == group].mean() for group in np.unique(groups)])
generator = torch.Generator().manual_seed(interval["seed"])
indices = torch.randint(len(paired), (interval["n_bootstrap"], len(paired)), generator=generator)
tail = (1 - interval["level"]) / 2
low, high = np.quantile(paired[indices.numpy()].mean(axis=1), [tail, 1 - tail])
assert probe["sd_drift"]["estimate"] == pytest.approx(paired.mean(), abs=1e-8)
assert probe["sd_drift"]["low"] == pytest.approx(low, abs=1e-8)
assert probe["sd_drift"]["high"] == pytest.approx(high, abs=1e-8)
tolerances = prereg["tolerances"]
mean = probe["target_mean_error"]
assert -tolerances["mean_abs"] <= mean["low"] <= mean["high"] <= tolerances["mean_abs"]
assert probe["numeric_mass"] >= tolerances["min_numeric_mass"]
limit = tolerances["sd_abs"]
if low > limit or high < -limit:
verdict = "not_preserved"
elif -limit <= low <= high <= limit:
verdict = "preserved"
else:
verdict = "inconclusive"
assert probe["verdict"] == verdict
expected_verdicts.append(verdict)
probes.append(probe)
assert expected_verdicts[0] == "not_preserved"
assert all(p["optimizer_conditional"] == (len(set(expected_verdicts)) > 1) for p in probes)
def test_pca_plane_proximity_cannot_establish_preservation(results):
result = results["distorting"]
noop = row(result, "m300-to-m500-sd100", "noop", "matched_displacement")
chord = row(result, "m300-to-m500-sd100", "centroid_chord")
assert chord["pca_plane_distance_median"] <= noop["pca_plane_distance_median"] + 1.0
assert chord["verdict"] == "not_preserved"
def test_controls_and_failure_states_are_logged(results):
result = results["distorting"]
off = row(result, "m300-to-m650-sd100", "off_manifold", "matched_displacement")
noop = row(result, "m300-to-m650-sd100", "noop", "matched_displacement")
assert off["reconstruction_residual"]["excess"] > 20
assert off["numeric_mass"] < noop["numeric_mass"] - 0.1
assert noop["hook_drift"] == 0.0 and noop["sd_drift"]["estimate"] == 0.0
assert noop["reconstruction_residual"]["excess"] == pytest.approx(0.0, abs=1e-5)
missing = {(m["arm"], m["reason"]) for m in result["missing"]}
assert ("random_norm_matched",
"target mean not reached within the preregistered max scale") in missing
assert row(result, "m300-to-m500-sd100", "noop")["status"] == "not_applicable"
assert result["coverage"]["off_manifold"]["fraction"] == 0.0
assert "noop" not in result["coverage"]
# Protocol, comparisons and record ------------------------------------------------
def test_every_attempted_cell_arm_and_comparison_is_a_row(results):
result = results["preserving"]
variants = 1 + len(ARMS)
assert len(result["rows"]) == len(CELLS) * variants * 2
assert {r["arm"] for r in result["rows"]} == set(ARMS)
assert {r["status"] for r in result["rows"]} <= {"evaluated", "missing", "not_applicable"}
assert result["unrun_cells"] == {}
extrapolated = row(result, "m300-to-m800-sd100", "spline", "matched_displacement")
assert extrapolated["support"] == "extrapolation" and extrapolated["exploratory"]
assert row(result, "m300-to-m350-sd100", "spline")["regime"] == "source_supported"
assert row(result, "m300-to-m650-sd100", "spline")["regime"] == "exploratory"
def test_matched_displacement_and_matched_mean_are_separate(results):
result = results["distorting"]
displaced = [r for r in result["rows"] if r["cell"] == "m300-to-m500-sd100"
and r["comparison"] == "matched_displacement" and r["arm"] != "noop"]
norms = {round(r["displacement_norm"], 6) for r in displaced}
assert len(norms) == 1 and all(r["verdict"] is None for r in displaced)
assert "unequal achieved means" in displaced[0]["verdict_reasons"][0]
matched = row(result, "m300-to-m500-sd100", "spline")
for key in ("target_mean_error", "sd_drift", "numeric_mass", "nonnumeric_mass",
"js_divergence", "kl_unsupported_prompts", "reconstruction_residual"):
assert key in matched
first = matched["sequences"][0]
assert first["token_position"] == first["number_index"] - 1
assert first["nonnumeric_mass"] == pytest.approx(1 - first["numeric_mass"])
assert matched["target_mean_error"]["n_sequences"] == 30
def test_scales_are_frozen_from_calibration_before_heldout(results, monkeypatch):
result = results["preserving"]
prereg = result["preregistration"]
assert spec.verify_frozen(prereg) == result["preregistration_sha256"]
assert prereg["frozen"]["heldout_forward_passes"] == 0
scale = prereg["calibrated_scales"]["m300-to-m500-sd100"]["spline"]
assert row(result, "m300-to-m500-sd100", "spline")["scale"] == scale["scale"]
build = spec.build_sequences
def shifted(campaign):
seqs = build(campaign)
for seq in seqs["heldout"]:
seq["values"] = [min(999, v + 40) for v in seq["values"]]
return seqs
monkeypatch.setattr(spec, "build_sequences", shifted)
campaign = small_campaign(cells=CELLS[:1])
other = ev.run_campaign(campaign, small_prereg(), synthetic_model("preserving"))
assert other["preregistration"]["calibrated_scales"]["m300-to-m500-sd100"] == \
prereg["calibrated_scales"]["m300-to-m500-sd100"]
with pytest.raises(ValueError, match="unfrozen"):
ev.run_campaign(campaign, prereg, synthetic_model("preserving"))
def test_sensitivity_costs_restoration_and_curvature(results):
result = results["preserving"]
sens = result["geometry"]["probe_sensitivity"]
assert sens["optimizers"] == ["lbfgs", "adamw"] and sens["direction_cosine"] > 0.95
assert set(result["geometry"]["probes"]) == {"lbfgs", "adamw"}
assert result["geometry"]["dual_field"]["rank"] == 2
assert result["geometry"]["probes"]["lbfgs"]["l2"] == 1e-3
cost = result["cost"]
assert cost["fit_seconds"] > 0 and cost["inference_seconds"] > 0
assert cost["forward_passes"]["fit"] == 1 and cost["forward_passes"]["heldout"] > 0
assert cost["peak_memory_bytes"] is None and "CUDA" in cost["peak_memory_reason"]
assert result["restoration"] == {"hooks_restored": True, "hook_free_logits_identical": True}
assert result["curvature"] is None and result["geometry"]["curvature"] is None
assert result["measurement_evidence"]["curvature"]["status"] == "unsupported"
assert "curvature" not in result["geometry"]["tangent_variation"]["note"].split(";")[0]
r = row(result, "m300-to-m500-sd100", "spline")
assert set(r["seed_blocks"]["sd_drift"]["per_block"]) == {"0", "1", "2"}
json.dumps(result, allow_nan=False)
def test_optimizer_dependent_outcomes_are_flagged_conditional():
rows = [{"arm": "linear_probe", "comparison": "matched_achieved_mean", "cell": "c",
"status": "evaluated", "verdict": v} for v in ("preserved", "not_preserved")]
rows.append({"arm": "linear_probe", "comparison": "matched_achieved_mean", "cell": "d",
"status": "evaluated", "verdict": "preserved"})
ev._flag_optimizer_conditional(rows)
assert [r["optimizer_conditional"] for r in rows] == [True, True, False]
def test_budget_exhaustion_logs_unrun_cells():
campaign = small_campaign(cells=CELLS[:2])
campaign["budget"]["max_forward_passes"] = 2
result = ev.run_campaign(campaign, small_prereg(), synthetic_model("preserving"))
assert set(result["unrun_cells"]) == {"m300-to-m500-sd100", "m300-to-m350-sd100"}
assert {r["status"] for r in result["rows"]} == {"unrun"}
assert result["measurement_evidence"]["sd_drift"]["status"] == "unavailable"
assert result["reproduction_claim"]["status"] == "not_claimed"
campaign["budget"].update(max_forward_passes=10 ** 6, max_seconds=0)
timed = ev.run_campaign(campaign, small_prereg(), synthetic_model("preserving"))
assert set(timed["unrun_cells"].values()) == {"time budget exhausted"}
# Claim boundary, dossier, catalog and CLI ----------------------------------------
def test_synthetic_evidence_never_claims_pretrained_reproduction(results):
for result in results.values():
assert result["scientific_evidence"]["level"] == "hypothesis"
assert result["reproduction_claim"] == {
"status": "not_claimed", "gaps": ["synthetic fixture: no pretrained evidence"]}
assert result["scientific_evidence"]["broad_transfer_certainty"] == "very low"
def test_pretrained_claim_requires_complete_evidence(results):
result = copy.deepcopy(results["preserving"])
result["source"] = {"kind": "pretrained"}
for r in result["rows"]:
if r["arm"] == "dual_field" and r["regime"] == "source_supported" \
and r["comparison"] == "matched_achieved_mean":
r.update(verdict="preserved", seed_block_stable=True)
claim = dossier.assess_claim(result)
assert claim["scientific_evidence"]["level"] == "controlled intervention result"
assert claim["reproduction_claim"] == {"status": "supported_in_source_regime", "gaps": []}
broken = copy.deepcopy(result)
target = row(broken, "m300-to-m350-sd100", "dual_field")
target["seed_block_stable"] = False
broken["unrun_cells"] = {"x": "time budget exhausted"}
row(broken, "m300-to-m500-sd100", "linear_probe", variant="adamw")[
"optimizer_conditional"] = True
broken["geometry"]["probe_sensitivity"] = None
gaps = dossier.assess_claim(broken)["reproduction_claim"]["gaps"]
assert len(gaps) == 4 and dossier.assess_claim(broken)["reproduction_claim"]["status"] == \
"not_claimed"
target.update(verdict="inconclusive")
assert any("not preserved (inconclusive)" in g
for g in dossier.assess_claim(broken)["reproduction_claim"]["gaps"])
broken["rows"] = [r for r in broken["rows"] if r["arm"] != "dual_field"]
assert "no source-supported dual-field row" in \
dossier.assess_claim(broken)["reproduction_claim"]["gaps"]
def test_dossier_lists_rows_missing_conditions_and_claim(results):
text = dossier.render_dossier(results["distorting"])
assert text.startswith("# Numeric geometry preservation result dossier")
assert "Pretrained reproduction: not_claimed" in text
assert "not_preserved" in text and "missing: target mean not reached" in text
assert "(exploratory)" in text and "Curvature: null (unsupported)" in text
assert text.count("\n| m300-") == len(results["distorting"]["rows"])
def test_every_result_metric_has_a_catalog_row(results):
text = DOC.read_text(encoding="utf-8")
match = re.search(r"^### Numeric geometry preservation result\s*$(.*?)(?=^#|\Z)", text,
re.M | re.S)
assert match, "numeric geometry catalog section missing"
assert set(re.findall(r"^\| `([a-z_]+)` \|", match.group(1), re.M)) == set(CATALOG)
for entry in results["preserving"]["measurement_evidence"].values():
assert entry["definition"] and entry["units"] and entry["scope"]
assert (entry["status"] == "diagnostic") == (entry["unavailable_reason"] is None)
def run_cli(capsys, *argv):
main(["experiments", "numeric-geometry", *argv])
return json.loads(capsys.readouterr().out)
def test_cli_template_synthetic_dossier_and_opt_in(tmp_path, capsys, monkeypatch):
out = run_cli(capsys, "template", "--out", str(tmp_path / "t"))
campaign = json.loads(Path(out["campaign"]).read_text())
assert campaign["model"]["revision"] == "4e20de362430cd3b72f300e6b0f18e50e7166e08"
assert json.loads(Path(out["preregistration"]).read_text())["frozen"] is None
small = small_campaign(cells=CELLS[:1])
(tmp_path / "c.json").write_text(json.dumps(small))
(tmp_path / "p.json").write_text(json.dumps(small_prereg()))
files = ["--campaign", str(tmp_path / "c.json"), "--preregistration", str(tmp_path / "p.json")]
summary = run_cli(capsys, "synthetic", "--variant", "distorting", *files,
"--out", str(tmp_path / "r.json"), "--dossier", str(tmp_path / "r.md"))
assert summary["reproduction_claim"] == "not_claimed" and summary["missing"] >= 2
assert summary["coverage"]["spline"] == 1.0 and summary["coverage"]["centroid_chord"] == 0.0
assert (tmp_path / "r.md").read_text().startswith("# Numeric geometry")
rendered = run_cli(capsys, "dossier", str(tmp_path / "r.json"), "--out",
str(tmp_path / "d" / "r.md"))
assert Path(rendered["dossier"]).is_file()
monkeypatch.delenv("OBLITERATUS_NUMERIC_GEOMETRY_E2E", raising=False)
with pytest.raises(SystemExit) as stop:
main(["experiments", "numeric-geometry", "pretrained", *files, "--out",
str(tmp_path / "x.json")])
assert stop.value.code == 4 and "opt-in" in capsys.readouterr().err
monkeypatch.setenv("OBLITERATUS_NUMERIC_GEOMETRY_E2E", "1")
unpinned = copy.deepcopy(small)
unpinned["model"].update(revision=None)
(tmp_path / "u.json").write_text(json.dumps(unpinned))
with pytest.raises(SystemExit) as stop:
main(["experiments", "numeric-geometry", "pretrained", "--campaign",
str(tmp_path / "u.json"), "--preregistration", str(tmp_path / "p.json"),
"--out", str(tmp_path / "x.json")])
assert stop.value.code == 4 and "pinned" in capsys.readouterr().err
from obliteratus.analysis import numeric_geometry_adapter
toy = synthetic_model("distorting")
toy.identity = {"kind": "pretrained", "note": "stand-in for the loader"}
monkeypatch.setattr(numeric_geometry_adapter, "load_pretrained", lambda campaign: toy)
monkeypatch.setattr(spec, "validate_campaign", lambda campaign, pretrained=False: campaign)
run = run_cli(capsys, "pretrained", *files, "--out", str(tmp_path / "x.json"))
assert run["scientific_evidence"] == "controlled intervention result"
assert run["reproduction_claim"] == "not_claimed"
def test_geometry_refusals_spline_reference_and_single_optimizer():
from obliteratus.analysis.numeric_geometry_arms import Geometry, probe_sensitivity
campaign, prereg = small_campaign(), small_prereg()
fit_prompts = spec.prompts(spec.build_sequences(campaign)["fit"], [64])
acts = synthetic_model("preserving").activations(fit_prompts)
means = [p["generating"]["mean"] for p in fit_prompts]
fit_means = campaign["data"]["fit_means"]
with pytest.raises(ValueError, match="reference_arm"):
Geometry(acts, means, fit_means, prereg["arms"], "random_norm_matched")
with pytest.raises(ValueError, match="cover every fit mean"):
Geometry(acts, means, fit_means + [800], prereg["arms"])
arms = copy.deepcopy(prereg["arms"])
arms["linear_probe"]["optimizers"] = ["lbfgs"]
geometry = Geometry(acts, means, fit_means, arms, "spline")
cell = CELLS[0]
assert geometry.reference_norm(cell, 0) == pytest.approx(
float((geometry.spline(500) - geometry.spline(300)).norm()))
assert probe_sensitivity(geometry) is None
assert [v for v in geometry.variants() if v[0] == "linear_probe"] == [("linear_probe",
"lbfgs")]