mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Add a bounded evaluation of whether shifting a known predictive mean preserves a known non-target spread (#511), producing reproducible evidence and negative results rather than a universal steering claim. - Whole sequences are split into fit, calibration and held-out partitions per seed block before any activation is collected. Geometry is fitted on fit rows only, matched-mean scales are found on calibration rows only, and the preregistration (tolerances, arm choices, scales) is frozen by digest before any held-out pass. - Seven arms with exposed choices and reversible, restoration-checked hooks; matched-displacement and matched-mean comparisons are reported separately, and PCA-plane proximity can never establish preservation. - Paired intervals over seed blocks, separate AdamW and L-BFGS probe fits (optimizer-dependent outcomes flagged conditional), explicit zero handling, separate costs, and exploratory labels for extrapolated cells. Curvature stays null. - Golden synthetic arc fixtures with known answers: a spread-preserving variant and a radius-dependent variant the chord arm distorts. - Every attempted cell, arm and comparison is logged; synthetic runs are rated hypothesis and never claim pretrained reproduction. The opt-in pretrained campaign refuses until both model revisions are pinned and runs only behind an explicit environment flag. - `obliteratus experiments numeric-geometry`, catalog rows and a new documentation section. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
235 lines
12 KiB
Python
235 lines
12 KiB
Python
"""Bounded numeric geometry preservation campaign runner (issue #511).
|
|
|
|
Order: generate and split whole sequences; collect fit activations and fit the
|
|
geometry on ``fit`` only; calibrate matched-achieved-mean scales on
|
|
``calibration`` only; freeze the preregistration (scales, thresholds, arm
|
|
choices, digest) with zero held-out forward passes; then evaluate every cell x
|
|
arm variant x comparison on ``heldout``. Every attempted cell is logged: rows
|
|
are ``evaluated``, ``missing`` (with a reason), ``not_applicable`` or ``unrun``
|
|
(budget). Negative outcomes stay in the rows. Fitting, calibration and
|
|
inference costs are recorded separately.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
import torch
|
|
|
|
from obliteratus.analysis import numeric_geometry_fit as fit
|
|
from obliteratus.analysis import numeric_geometry_metrics as metrics
|
|
from obliteratus.analysis import numeric_geometry_spec as spec
|
|
from obliteratus.analysis.numeric_geometry_arms import Geometry, calibrate, probe_sensitivity
|
|
from obliteratus.analysis.numeric_geometry_synthetic import rounded_clamped_normal
|
|
|
|
RESULT_SCHEMA = "obliteratus.numeric_geometry_result"
|
|
SCHEMA_VERSION = 1
|
|
COMPARISONS = ("matched_displacement", "matched_achieved_mean")
|
|
|
|
|
|
class _Budget:
|
|
def __init__(self, model, budget):
|
|
self.model, self.budget, self.start = model, budget, time.perf_counter()
|
|
|
|
def exhausted(self):
|
|
if self.model.forward_passes >= self.budget["max_forward_passes"]:
|
|
return "forward-pass budget exhausted"
|
|
if time.perf_counter() - self.start >= self.budget["max_seconds"]:
|
|
return "time budget exhausted"
|
|
return None
|
|
|
|
|
|
def _cell_prompts(sequences, cell, context_lengths):
|
|
chosen = [s for s in sequences if s["generating"] == {"mean": cell["source_mean"],
|
|
"sd": cell["sd"]}]
|
|
return spec.prompts(chosen, context_lengths)
|
|
|
|
|
|
def _rows_for_status(cell, label, variants, status, reason):
|
|
return [{"cell": spec.cell_id(cell), **cell, **label, "arm": arm, "variant": variant,
|
|
"comparison": comparison, "status": status, "reason": reason}
|
|
for arm, variant in variants for comparison in COMPARISONS]
|
|
|
|
|
|
def _measure(geometry, model, prompts, cell, index, displacement, base):
|
|
dist, mass, _ = model.readout(prompts, displacement)
|
|
positions = model.last_positions
|
|
mean, sd = metrics.moments(dist)
|
|
target = rounded_clamped_normal(cell["target_mean"], cell["sd"])
|
|
js, kl = metrics.divergences(dist, target)
|
|
moved = base["acts"] + displacement
|
|
residual = fit.manifold_distance(moved, geometry.curve)
|
|
pca = fit.pca_plane_distance(moved, geometry.cents)
|
|
return [{"sequence_id": p["sequence_id"], "seed_block": p["seed_block"],
|
|
"number_index": p["number_index"], "token_position": int(positions[i]),
|
|
"achieved_mean": float(mean[i]), "sd": float(sd[i]),
|
|
"target_mean_error": float(mean[i]) - cell["target_mean"],
|
|
"sd_drift": float(sd[i] - base["sd"][i]), "numeric_mass": float(mass[i]),
|
|
"nonnumeric_mass": float(1 - mass[i]), "js_divergence": float(js[i]),
|
|
"kl_divergence": kl[i], "reconstruction_residual": float(residual[i]),
|
|
"pca_plane_distance": float(pca[i])} for i, p in enumerate(prompts)]
|
|
|
|
|
|
def _summarize(per, interval, tolerances, comparison, base_residual):
|
|
groups = [r["sequence_id"] for r in per]
|
|
blocks = [r["seed_block"] for r in per]
|
|
err = metrics.paired_interval([r["target_mean_error"] for r in per], groups, **interval)
|
|
drift = metrics.paired_interval([r["sd_drift"] for r in per], groups, **interval)
|
|
mass = sum(r["numeric_mass"] for r in per) / len(per)
|
|
residual = torch.tensor([r["reconstruction_residual"] for r in per]).median()
|
|
out = {"target_mean_error": err, "sd_drift": drift, "numeric_mass": mass,
|
|
"nonnumeric_mass": 1 - mass,
|
|
"achieved_mean": sum(r["achieved_mean"] for r in per) / len(per),
|
|
"js_divergence": sum(r["js_divergence"] for r in per) / len(per),
|
|
"kl_unsupported_prompts": sum(r["kl_divergence"] is None for r in per),
|
|
"reconstruction_residual": {"median": float(residual),
|
|
"heldout_unintervened_median": base_residual,
|
|
"excess": float(residual) - base_residual},
|
|
"pca_plane_distance_median": float(torch.tensor(
|
|
[r["pca_plane_distance"] for r in per]).median()),
|
|
"seed_blocks": {
|
|
"sd_drift": metrics.seed_block_sensitivity([r["sd_drift"] for r in per], blocks),
|
|
"target_mean_error": metrics.seed_block_sensitivity(
|
|
[r["target_mean_error"] for r in per], blocks)},
|
|
"sequences": per}
|
|
if comparison == "matched_achieved_mean":
|
|
out["verdict"], out["verdict_reasons"] = metrics.verdict(err, drift, mass, tolerances)
|
|
sens = out["seed_blocks"]["sd_drift"]
|
|
out["seed_block_stable"] = max(abs(sens["min"]), abs(sens["max"])) <= tolerances["sd_abs"]
|
|
else:
|
|
out["verdict"], out["verdict_reasons"] = None, [
|
|
"matched displacement compares unequal achieved means; no preservation verdict"]
|
|
return out
|
|
|
|
|
|
def _evaluate_cell(geometry, model, prompts, cell, index, label, prereg):
|
|
_, _, free_logits = model.readout(prompts, hooked=False)
|
|
zero = geometry.displacement("noop", None, cell, index, 0.0)
|
|
noop_dist, _, noop_logits = model.readout(prompts, zero)
|
|
acts = model.activations(prompts)
|
|
base = {"acts": acts, "sd": metrics.moments(noop_dist)[1]}
|
|
base_residual = float(fit.manifold_distance(acts, geometry.curve).median())
|
|
drift = float((noop_logits - free_logits).abs().max())
|
|
reference = geometry.reference_norm(cell, index)
|
|
frozen = prereg["calibrated_scales"][spec.cell_id(cell)]
|
|
rows = []
|
|
for arm, variant in geometry.variants():
|
|
key = arm if variant is None else f"{arm}@{variant}"
|
|
for comparison in COMPARISONS:
|
|
row = {"cell": spec.cell_id(cell), **cell, **label, "arm": arm,
|
|
"variant": variant, "comparison": comparison, "hook_drift": drift}
|
|
if comparison == "matched_displacement":
|
|
scale = geometry.scale_for_norm(arm, variant, cell, index, reference)
|
|
elif frozen[key]["status"] != "calibrated":
|
|
rows.append({**row, "status": frozen[key]["status"],
|
|
"reason": frozen[key]["reason"]})
|
|
continue
|
|
else:
|
|
scale = frozen[key]["scale"]
|
|
disp = geometry.displacement(arm, variant, cell, index, scale)
|
|
per = _measure(geometry, model, prompts, cell, index, disp, base)
|
|
rows.append({**row, "status": "evaluated", "reason": None, "scale": scale,
|
|
"displacement_norm": float(disp.norm()),
|
|
**_summarize(per, prereg["interval"], prereg["tolerances"],
|
|
comparison, base_residual)})
|
|
return rows
|
|
|
|
|
|
def _flag_optimizer_conditional(rows):
|
|
by_cell = {}
|
|
for row in rows:
|
|
if row["arm"] == "linear_probe" and row["comparison"] == "matched_achieved_mean":
|
|
by_cell.setdefault(row["cell"], []).append(row)
|
|
for group in by_cell.values():
|
|
outcomes = {r.get("verdict") if r["status"] == "evaluated" else r["status"]
|
|
for r in group}
|
|
for row in group:
|
|
row["optimizer_conditional"] = len(outcomes) > 1
|
|
|
|
|
|
def run_campaign(campaign, preregistration, model, *, pretrained=False):
|
|
"""Run the bounded campaign on ``model`` (a ``HookedNumericModel``)."""
|
|
spec.validate_campaign(campaign, pretrained=pretrained)
|
|
if preregistration.get("frozen") is not None:
|
|
raise ValueError("Supply the unfrozen preregistration; the runner freezes it")
|
|
data = campaign["data"]
|
|
budget = _Budget(model, campaign["budget"])
|
|
sequences = spec.build_sequences(campaign)
|
|
fit_prompts = spec.prompts(sequences["fit"], data["context_lengths"])
|
|
passes = {"fit": 0, "calibration": 0, "heldout": 0}
|
|
acts = model.activations(fit_prompts)
|
|
passes["fit"] = model.forward_passes
|
|
geometry = Geometry(acts, [p["generating"]["mean"] for p in fit_prompts], data["fit_means"],
|
|
preregistration["arms"], preregistration["scales"]["reference_arm"])
|
|
variants = geometry.variants()
|
|
cells = [(i, cell, spec.cell_label(cell, data["fit_means"]))
|
|
for i, cell in enumerate(data["cells"])]
|
|
calibration, unrun, start = {}, {}, time.perf_counter()
|
|
for index, cell, _label in cells:
|
|
reason = budget.exhausted()
|
|
if reason:
|
|
unrun[spec.cell_id(cell)] = reason
|
|
continue
|
|
prompts = _cell_prompts(sequences["calibration"], cell, data["context_lengths"])
|
|
calibration[spec.cell_id(cell)] = calibrate(geometry, model, prompts, cell, index,
|
|
preregistration["scales"])
|
|
calibration_seconds = time.perf_counter() - start
|
|
passes["calibration"] = model.forward_passes - passes["fit"]
|
|
frozen = spec.freeze(preregistration, campaign, calibration)
|
|
frozen_sha = spec.verify_frozen(frozen)
|
|
reference_prompts = fit_prompts[:2]
|
|
_, _, reference_logits = model.readout(reference_prompts, hooked=False)
|
|
rows, start = [], time.perf_counter()
|
|
for index, cell, label in cells:
|
|
cid = spec.cell_id(cell)
|
|
reason = unrun.get(cid) or budget.exhausted()
|
|
if reason:
|
|
unrun[cid] = reason
|
|
rows += _rows_for_status(cell, label, variants, "unrun", reason)
|
|
continue
|
|
prompts = _cell_prompts(sequences["heldout"], cell, data["context_lengths"])
|
|
rows += _evaluate_cell(geometry, model, prompts, cell, index, label, frozen)
|
|
inference_seconds = time.perf_counter() - start
|
|
restoration = model.restoration_check(reference_prompts, reference_logits)
|
|
passes["heldout"] = model.forward_passes - passes["fit"] - passes["calibration"]
|
|
_flag_optimizer_conditional(rows)
|
|
record = geometry.record()
|
|
return assemble(campaign, frozen, frozen_sha, model, sequences, record, geometry, rows,
|
|
unrun, {"fit_seconds": record["fit_seconds"],
|
|
"calibration_seconds": calibration_seconds,
|
|
"inference_seconds": inference_seconds, "forward_passes": passes},
|
|
restoration)
|
|
|
|
|
|
def assemble(campaign, frozen, frozen_sha, model, sequences, record, geometry, rows, unrun,
|
|
cost, restoration):
|
|
from obliteratus.analysis import numeric_geometry_dossier as dossier
|
|
peak = (torch.cuda.max_memory_allocated() if campaign["runtime"]["device"].startswith("cuda")
|
|
and torch.cuda.is_available() else None)
|
|
attempted = [r for r in rows if r["comparison"] == "matched_achieved_mean"]
|
|
result = {
|
|
"schema": RESULT_SCHEMA, "schema_version": SCHEMA_VERSION, "campaign": campaign,
|
|
"campaign_sha256": spec.canonical_sha256(campaign), "preregistration": frozen,
|
|
"preregistration_sha256": frozen_sha, "source": model.identity,
|
|
"partitions": {p: {"n_sequences": len(v),
|
|
"sequence_ids_sha256": spec.canonical_sha256(
|
|
[s["sequence_id"] for s in v])}
|
|
for p, v in sequences.items()},
|
|
"geometry": {**record, "probe_sensitivity": probe_sensitivity(geometry)},
|
|
"rows": rows, "unrun_cells": unrun,
|
|
"missing": [{"cell": r["cell"], "arm": r["arm"], "variant": r["variant"],
|
|
"comparison": r["comparison"], "reason": r["reason"]}
|
|
for r in rows if r["status"] in ("missing", "unrun")],
|
|
"coverage": dossier.coverage(attempted),
|
|
"cost": {**cost, "peak_memory_bytes": peak,
|
|
"peak_memory_reason": None if peak is not None else
|
|
"peak memory is measured only on CUDA devices"},
|
|
"restoration": restoration,
|
|
}
|
|
result["measurement_evidence"] = metrics.evidence(result)
|
|
result.update(dossier.assess_claim(result))
|
|
return result
|
|
|
|
|
|
__all__ = ["COMPARISONS", "RESULT_SCHEMA", "assemble", "run_campaign"]
|