Files
OBLITERATUS/obliteratus/experiments/patching.py
Joseph MaglyandClaude Opus 5.5 878796adb2 feat(experiments): controlled intervention harness with matched controls and restoration checks
Start milestone 3 (F12): run a temporary intervention on a ready
dossier estimate and record the result as the only route to a
"controlled intervention result" label.

- Versioned intervention spec (own schema; baseline observation specs
  unchanged) for steering or ablating a channel estimate at its site
  through the existing hook manager. Four arms on the same examples:
  baseline, intended, a seeded norm- and rank-matched random control
  orthogonal to the target, and a fresh-baseline replay that must
  reproduce the baseline exactly. Each arm records site, module, hook,
  token scope, units, dose and ordering.
- Dose ladders with paired scorecards against the baseline. The deciding
  dose and target task must equal the values pre-registered in the
  dossier hypothesis, alongside its effect threshold, direction and
  collateral margin.
- Restoration (parameter/buffer digest, hook counts, probe logits) is
  checked after every arm through a helper shared with pack evaluation.
  Exceptions and cancellation remove hooks and leave a recorded attempt.
- Refused before any forward pass: stale or unready dossiers, geometry
  mismatches (site, width, model, dtype, fingerprint, tokenizer,
  readout, device), evaluation rows overlapping discovery inputs, and
  evaluation on the dossier's confirmation rows.
- A minimal last-token activation-patching experiment with an explicit
  corruption strategy and logit-difference metric (descriptive only).
- Dossiers attach results by reference and recompute the verdict from
  per-example records on every assessment. The label is granted only if
  the intended arm beats the control by the threshold (one-sided exact
  McNemar), collateral endpoints pass and restoration held. Fixture or
  local models are software-contract results that never upgrade a label
  by default.
- `obliteratus experiments intervene` and a Python API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 18:09:42 -05:00

146 lines
6.5 KiB
Python

"""Minimal activation-patching experiment at a ready dossier estimate's site.
Corruption strategies (declared in ``patching.corruption``), at the final prompt
position of the estimate's decoder layer output:
* ``source_swap``: replace the target example's activation with the activation
of a source example (target and source examples paired by order, the source
list cycled when shorter);
* ``mean_ablation``: replace it with the mean activation over all source examples.
``scope`` ``estimate`` replaces only the component along the estimate's
coordinate; ``site`` replaces the whole activation vector. The declared metric
``logit_difference`` is ``logit[target_token] - logit[foil_token]`` at the final
position; the effect per example is patched minus clean. Hooks are owned by
``try/finally`` blocks; restoration is checked after each phase. The existing
``analysis.activation_patching.ActivationPatcher`` replaces whole-sequence
outputs and ignores token positions, so it is not used for this contract.
"""
import torch
from obliteratus.concepts import restoration
from obliteratus.experiments import inputs as _inputs
from obliteratus.experiments.intervention_checks import InterventionRefused
from obliteratus.experiments.intervention_ops import HOOK
CORRUPTION = {
"source_swap": "final-position activation replaced by the paired source example's",
"mean_ablation": "final-position activation replaced by the mean over all source examples",
}
def _token(tokenizer, text):
ids = tokenizer(text, return_tensors="pt")["input_ids"].reshape(-1).tolist()
if len(ids) != 1:
raise InterventionRefused(f"Metric token {text!r} is {len(ids)} tokens; use one token")
return ids[0]
def plan(ctx):
"""Resolve metric tokens and examples before any forward pass."""
from obliteratus.experiments.intervention_runner import _probe
patching, tokenizer = ctx["spec"]["patching"], ctx["tokenizer"]
tasks = {t.name: t for t in ctx["tasks"]}
return {"target": tasks[patching["target_task"]].examples,
"source": tasks[patching["source_task"]].examples,
"tokens": (_token(tokenizer, patching["metric"]["target_token"]),
_token(tokenizer, patching["metric"]["foil_token"])),
"probe": _probe(ctx)}
def _hidden(output):
return output[0] if isinstance(output, tuple) else output
def _forward(ctx, example, hook=None):
batch = _inputs.encode(ctx["tokenizer"], example.prompt)
handle = None if hook is None else ctx["target"]["layer"].register_forward_hook(hook)
try:
with torch.inference_mode():
return ctx["model"](**{k: v.to(ctx["device"]) for k, v in batch.items()}
).logits[0, -1].float().cpu()
finally:
if handle is not None:
handle.remove()
def _capture(ctx, example):
found = {}
def hook(module, args, output):
found["value"] = _hidden(output)[0, -1].detach().clone()
_forward(ctx, example, hook)
return found["value"]
def _patch_hook(replacement, unit, scope):
def hook(module, args, output):
hidden = _hidden(output).clone()
current = hidden[0, -1]
value = replacement.to(current.device, current.dtype)
if scope == "estimate":
value = current + ((value - current) @ unit) * unit
hidden[0, -1] = value
return (hidden, *output[1:]) if isinstance(output, tuple) else hidden
return hook
def execute(ctx, plan):
model, patching, probe = ctx["model"], ctx["spec"]["patching"], plan["probe"]
pristine = restoration.snapshot(model, probe)
arms, target_id, foil_id = [], *plan["tokens"]
unit = ctx["target"]["vector"] / ctx["target"]["vector"].norm()
def metric(logits):
return float(logits[target_id] - logits[foil_id])
def phase(name, run, hook):
ctx["cancel"]()
values = run()
check = restoration.check(model, pristine, probe)
arms.append({"order": len(arms), "arm": name, "hook": hook, "restoration": check})
if not check["restored"]:
from obliteratus.experiments.intervention_runner import RestorationFailed
raise RestorationFailed(name, None, check)
return values
clean = phase("clean", lambda: [metric(_forward(ctx, e)) for e in plan["target"]], None)
sources = phase("source_capture", lambda: [_capture(ctx, e) for e in plan["source"]],
{"site": ctx["target"]["geometry"]["site"], "module": ctx["target"]["module"],
"hook": HOOK, "role": "read-only capture at the final position"})
mean = torch.stack(sources).mean(0)
pairs = []
for index, example in enumerate(plan["target"]):
source = index % len(sources)
replacement = sources[source] if patching["corruption"] == "source_swap" else mean
pairs.append((example, None if patching["corruption"] == "mean_ablation"
else plan["source"][source].example_id, replacement))
def patched_run():
out = []
for example, _source, replacement in pairs:
ctx["cancel"]()
out.append(metric(_forward(ctx, example,
_patch_hook(replacement, unit, patching["scope"]))))
return out
patched = phase("patched", patched_run, {
"site": ctx["target"]["geometry"]["site"], "module": ctx["target"]["module"],
"hook": HOOK, "token_scope": "last_token", "scope": patching["scope"],
"corruption": patching["corruption"], "ordering": "one replacement hook per forward, "
"removed in a finally block"})
records = [{"target_example": e.example_id, "source_example": s, "clean": c, "patched": p,
"effect": p - c} for (e, s, _), c, p in zip(pairs, clean, patched)]
effects = [r["effect"] for r in records]
return {"patching": {**patching, "corruption_rule": CORRUPTION[patching["corruption"]],
"metric_rule": "logit[target_token] - logit[foil_token] at the final "
"position; effect = patched - clean",
"status": "DESCRIPTIVE", "records": records,
"summary": {"n": len(effects), "mean_effect": sum(effects) / len(effects),
"positive": sum(e > 0 for e in effects),
"negative": sum(e < 0 for e in effects)}},
"arms": arms}
__all__ = ["CORRUPTION", "execute", "plan"]