Files
OBLITERATUS/obliteratus/experiments/sequence_eval.py
Joseph Magly 3c7a6ca920 fix(models): harden ModelHandle cleanup sites and messages
Moves cleanup_on_exit/cleanup_during_error to the light
obliteratus.handle_cleanup module (re-exported from the loader) and imports
them at module top, so no import runs after a handle exists. Stores the closed
policy before detaching the restore point, names the path to remove in
handle-surfaced SnapshotErrors, guards post-load work in candidate,
smaller_change and the sequence loaders, keeps observer-close and neuron-lab
cleanup failures from replacing the primary error or skipping state reset, and
adds failure-plus-failing-close tests for every listed site.

Fixes #669
2026-11-25 10:06:29 -05:00

226 lines
12 KiB
Python

"""Intermediate and final evaluation of an edit sequence on independent rows.
A session loads the selected reference once, re-derives every component's
target from its registered recipe (refusing a candidate whose saved edit is not
bound to it), resolves the plan's dataset and refuses rows that overlap any
component's discovery data, its dossier confirmation data or any registered
confirmation set. Per step ``k`` it then measures, with the recipe's tasks and
greedy decoding:
1. the reference (baseline, once per session);
2. the reference with the temporary operations of steps ``1..k`` installed in
order, followed by a restoration check;
3. the step artifact loaded again from disk.
A step passes when the loaded artifact reproduces the composed temporary
operations (target rates within ``target_rate``, completion agreement, probe
logits), every applied component's intended effect against the baseline holds
in its declared direction, and every collateral endpoint (tasks no applied
component targets, plus each target task's invalid-output endpoint) is ``PASS``.
"""
from __future__ import annotations
from contextlib import ExitStack
from dataclasses import replace
import torch
from obliteratus.concepts import restoration
from obliteratus.concepts.scorecard import build_scorecard
from obliteratus.experiments import sequence_steps as steps
from obliteratus.experiments.registry_store import Registry, row_digests
from obliteratus.experiments.sequence import SequenceRefused
from obliteratus.handle_cleanup import cleanup_during_error, cleanup_on_exit
def _check(name, passed, detail):
return {"check": name, "passed": bool(passed), "detail": detail}
def component_spec(component):
from obliteratus.experiments.intervention_spec import load_intervention_spec
document = Registry(component["run"]).get(component["registration"]["id"])
return load_intervention_spec(document["frozen"]["recipe"]["spec"])
def _rows(plan, specs, prepared, tokenizer, pack_resolver):
from obliteratus.experiments import intervention_checks as checks
from obliteratus.experiments.inputs import resolve_dataset
from obliteratus.experiments.intervention_spec import load_intervention_spec, observation_view
frozen = plan["frozen"]
spec = load_intervention_spec({**specs[0], "dataset": frozen["dataset"]})
_, tasks, data = resolve_dataset(observation_view(spec), pack_resolver)
margin = min(c["declared"]["collateral_margin"] for c in frozen["components"])
tasks = [replace(t, margin=margin, min_pairs=None) for t in tasks]
independence = [checks.check_evaluation_independence(p, data, tasks, tokenizer)
for p in prepared]
used = set()
for run in {c["run"] for c in frozen["components"]}:
for document in Registry(run).registrations():
confirmation = document["frozen"].get("confirmation")
used |= set(confirmation["row_digests"]) if confirmation else set()
if row_digests(e for t in tasks for e in t.examples) & used:
raise SequenceRefused("Evaluation rows belong to registered confirmation sets; evaluate "
"the sequence on independent rows")
return tasks, data, independence, margin
def complete(model, tokenizer, tasks, frozen, out, device):
from obliteratus.experiments.generation import generate_task, render_prompts
from obliteratus.experiments.store import read_json
settings = {"chat_template": False, "system": None}
decoding = {"decoding": {"strategy": "greedy", **frozen["decoding"]}, "seed": frozen["seed"]}
found = {}
for task in tasks:
target = out / task.name
target.mkdir(parents=True, exist_ok=False)
prompts = render_prompts(tokenizer, task.examples, settings)
generate_task(model, tokenizer, task, prompts, decoding, lambda: None, target, device)
found[task.name] = [r["completion"] for r in read_json(target / "generations.json")["rows"]]
return found
def _prober(tokenizer, tasks, frozen, device):
from obliteratus.experiments.inputs import encode
name = frozen["components"][0]["target_task"]
examples = next(t for t in tasks if t.name == name).examples[:frozen["probes"]]
inputs = [encode(tokenizer, e.prompt) for e in examples]
def probe(model):
with torch.inference_mode():
return [model(**{k: v.to(device) for k, v in batch.items()}).logits[0, -1].float().cpu()
for batch in inputs]
return probe
def open_session(plan, model_io, pack_resolver, out):
"""Reference load, re-derived targets, independent rows and baseline completions."""
from obliteratus.experiments import intervention_checks as checks
from obliteratus.experiments.inputs import require_unmodified
frozen = plan["frozen"]
specs = [component_spec(c) for c in frozen["components"]]
prepared = [checks.prepare(s, c["run"]) for s, c in zip(specs, frozen["components"])]
handle = model_io.load(frozen["reference"]["source"], frozen["model"])
try:
model, tokenizer = handle.model, handle.tokenizer
targets = []
for spec, prep, component in zip(specs, prepared, frozen["components"]):
target = checks.verify_model(prep, spec, model, tokenizer, require_unmodified(model))
bound = component["source"].get("vector_sha256", target["record"]["vector_sha256"])
if bound != target["record"]["vector_sha256"]:
raise SequenceRefused(f"Component {component['position']} is not bound to the target "
"re-derived from its registered recipe")
targets.append(target)
tasks, data, independence, margin = _rows(plan, specs, prepared, tokenizer, pack_resolver)
device = targets[0]["identity"]["device"]
probe = _prober(tokenizer, tasks, frozen, device)
pristine = restoration.snapshot(model, lambda: probe(model))
base = complete(model, tokenizer, tasks, frozen, out / "reference", device)
return {"plan": plan, "model": model, "tokenizer": tokenizer, "targets": targets,
"tasks": tasks, "data": data, "independence": independence, "margin": margin,
"device": device, "probe": probe, "pristine": pristine, "baseline": base,
"baseline_state": steps.loaded_state(model), "handle": handle,
"baseline_logits": [t.tolist() for t in pristine["logits"]]}
except BaseException as error:
# A cleanup failure is noted on the in-flight error, never replacing it.
cleanup_during_error(handle, error)
raise
def temporary(session, position, out):
"""Completions and probe logits with the operations of steps ``1..position`` installed."""
from obliteratus.experiments.intervention_ops import apply_operation
model, frozen = session["model"], session["plan"]["frozen"]
with ExitStack() as stack:
for component, target in zip(frozen["components"][:position],
session["targets"][:position]):
op = component["signature"]["operation"]
stack.enter_context(apply_operation(model, target["layer"], target["vector"],
op["coefficient"], op))
found = complete(model, session["tokenizer"], session["tasks"], frozen, out,
session["device"])
logits = session["probe"](model)
restored = restoration.check(model, session["pristine"], lambda: session["probe"](model))
return found, logits, restored
def measure_stack(session, seq_dir, stack, model_io, out):
"""Completions, probe logits and loaded state of a stack loaded fresh from disk."""
handle = steps.load_stack(seq_dir, session["plan"], stack, model_io)
# Cleans up only a callable ``cleanup``; never replaces an in-flight error.
with cleanup_on_exit(handle):
found = complete(handle.model, handle.tokenizer, session["tasks"],
session["plan"]["frozen"], out, session["device"])
return found, session["probe"](handle.model), steps.loaded_state(handle.model)
def _rate(card, task, arm):
return card["tasks"][task]["task_success"][arm]["rate"]
def judge(session, position, step_found, temp_found, step_logits, temp_logits, restored,
parent_found):
"""Checks and comparison of step ``position`` (1-based)."""
frozen, tasks = session["plan"]["frozen"], session["tasks"]
applied, tol = frozen["components"][:position], frozen["tolerances"]
names = [t.name for t in tasks]
step_card = build_scorecard(tasks, [session["baseline"][n] for n in names],
[step_found[n] for n in names])
temp_card = build_scorecard(tasks, [session["baseline"][n] for n in names],
[temp_found[n] for n in names])
parent_card = build_scorecard(tasks, [parent_found[n] for n in names],
[step_found[n] for n in names])
targeted = {c["target_task"] for c in applied}
rates = {n: {"baseline": _rate(step_card, n, "pristine"), "step": _rate(step_card, n,
"intervened"), "temporary": _rate(temp_card, n, "intervened"),
"parent": _rate(parent_card, n, "pristine")} for n in names}
differences = [abs(rates[n]["step"] - rates[n]["temporary"]) for n in targeted
if None not in (rates[n]["step"], rates[n]["temporary"])]
pairs = [(a, b) for n in names for a, b in zip(step_found[n], temp_found[n])]
agreement = sum(a == b for a, b in pairs) / len(pairs) if pairs else 0.0
logit = max(float((a - b).abs().max()) for a, b in zip(step_logits, temp_logits))
effects, intended = {}, []
for component in applied:
task, declared = component["target_task"], component["declared"]
base, now = rates[task]["baseline"], rates[task]["step"]
effect = None if None in (base, now) else now - base
sign = 1 if declared["effect_direction"] == "increase" else -1
ok = effect is not None and sign * effect >= declared["useful_effect_threshold"]
effects[str(component["position"])] = {"task": task, "effect_vs_baseline": effect,
"threshold": declared["useful_effect_threshold"],
"direction": declared["effect_direction"],
"holds": ok}
intended.append(ok)
collateral = {n: step_card["tasks"][n]["status"] for n in names if n not in targeted}
collateral |= {f"{n}:invalid_output": step_card["tasks"][n]["invalid_output"]["status"]
for n in sorted(targeted)}
checks = [
_check("reference_restored", restored["restored"],
"reference unchanged after the composed temporary arm"),
_check("reproduces_composed_temporary", len(differences) == len(targeted)
and max(differences, default=0.0) <= tol["target_rate"],
f"max |step - temporary| target success rate {max(differences, default=None)} "
f"(tolerance {tol['target_rate']})"),
_check("completion_agreement", agreement >= tol["agreement"],
f"{agreement:.4f} of completions equal the composed temporary arm's"),
_check("intended_effects", all(intended), "every applied component's effect against "
"the baseline meets its threshold in its declared direction"),
_check("collateral_endpoints", all(s == "PASS" for s in collateral.values()),
", ".join(f"{k} {v}" for k, v in sorted(collateral.items())))]
if tol["logit_atol"] is not None:
checks.append(_check("probe_logits", logit <= tol["logit_atol"],
f"max |step - temporary| final-token logit {logit:.3g}"))
comparison = {"rates": rates, "effects": effects, "completion_agreement": agreement,
"max_probe_logit_difference": logit, "collateral": collateral,
"collateral_margin": session["margin"],
"effect_vs_parent": {n: None if None in (rates[n]["step"], rates[n]["parent"])
else rates[n]["step"] - rates[n]["parent"]
for n in names}}
return checks, comparison, {"step_vs_reference": step_card,
"temporary_vs_reference": temp_card,
"step_vs_parent": parent_card}
__all__ = ["complete", "component_spec", "judge", "measure_stack", "open_session", "temporary"]