Files
OBLITERATUS/obliteratus/experiments/intervention_spec.py
Joseph MaglyandClaude Opus 5.5 5f0eafe14b feat(experiments): controlled interventions on direction and subspace estimates
Extend the controlled intervention harness beyond single channels.

- Fixed directions (rows of the pack's stored basis) and rank-k
  subspaces load from their direction artifact before the model does.
  The artifact must match the file, tensor and metadata digests that the
  dossier's source report recorded; discovery and subspace reports now
  record that binding, and reports without it are refused. Refitted
  difference-of-means directions stay refused.
- Subspace ablation removes the dose fraction of the projection onto the
  whole span. Steering a rank > 1 span needs one declared direction
  inside it (within 1e-4).
- Random controls are rank- and norm-matched: a seeded rank-k span
  orthogonal to the target span, and for a declared steering direction a
  rank-1 vector orthogonal to the whole span. Mismatched controls are
  refused before the arms run and at attachment.
- Each arm records target kind, rank, digests and orthogonalization.
  Evaluation rows on the artifact's own fit role and revision are
  refused. Channel specs and digests are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:26:07 -05:00

100 lines
4.2 KiB
Python

"""Versioned controlled-intervention and activation-patching experiment specs.
A separate schema (``intervention-spec.v1.schema.json``) from baseline
observation specs, so observation specs and their digests are unchanged. The
canonical digest of the default-filled spec is the experiment identity.
"""
from copy import deepcopy
from functools import lru_cache
import json
from pathlib import Path
from obliteratus.concepts.scoring import ScorerSpec, get_scorer
from obliteratus.concepts.validation import PackError, _check
from obliteratus.experiments.spec import DEFAULT_TASK_PATH, SpecError, _read
SCHEMA_PATH = (Path(__file__).resolve().parents[1] / "concepts" / "schema"
/ "intervention-spec.v1.schema.json")
DEFAULTS = {"dataset": {"pack_sha256": None, "allow_locked_test": False},
"model": {"revision": None, "device": "cpu"}}
DEFAULT_PROBES = 4
@lru_cache(maxsize=1)
def schema():
return json.loads(SCHEMA_PATH.read_text(encoding="utf-8"))
def load_intervention_spec(source):
"""Validate a spec (mapping or JSON file) and return its default-filled copy."""
spec = deepcopy(_read(source))
dataset = spec.get("dataset")
if (isinstance(dataset, dict) and dataset.get("role") == "test"
and dataset.get("allow_locked_test") is not True):
raise SpecError("Locked test role requires dataset.allow_locked_test: true")
try:
_check(spec, schema(), schema(), "intervention_spec")
except PackError as exc:
raise SpecError(str(exc)) from exc
for section, values in DEFAULTS.items():
spec[section] = {**values, **spec[section]}
spec.setdefault("seed", 0)
names = []
for task in spec["tasks"]:
task.setdefault("path", DEFAULT_TASK_PATH)
names.append(task["name"])
try:
get_scorer(ScorerSpec(task["scorer"]["id"], task["scorer"]["version"]))
except ValueError as exc:
raise SpecError(f"Task {task['name']}: {exc}") from exc
if len(set(names)) != len(names):
raise SpecError("Experiment task names must be unique")
if spec["kind"] == "controlled_intervention":
_controlled(spec, names)
else:
_patching(spec, names)
return spec
def _controlled(spec, names):
spec.setdefault("probes", DEFAULT_PROBES)
if spec["target_task"] not in names:
raise SpecError(f"target_task {spec['target_task']!r} is not a declared task")
if len(names) < 2:
raise SpecError("A controlled intervention needs at least one collateral task besides "
"the target task")
operation = spec["operation"]
if len(set(operation["coefficients"])) != len(operation["coefficients"]):
raise SpecError("Dose ladder coefficients must be unique")
if operation["decision_coefficient"] not in operation["coefficients"]:
raise SpecError("decision_coefficient must be one of the declared coefficients")
if operation.get("steer_direction") is not None:
if operation["kind"] != "steer":
raise SpecError("operation.steer_direction applies to steer only")
if (operation["steer_direction"]["source"] == "basis_row") != (
"index" in operation["steer_direction"]):
raise SpecError("steer_direction needs an index exactly when its source is basis_row")
def _patching(spec, names):
spec.setdefault("probes", DEFAULT_PROBES)
patching = spec["patching"]
patching.setdefault("token_scope", "last_token")
for key in ("target_task", "source_task"):
if patching[key] not in names:
raise SpecError(f"patching.{key} {patching[key]!r} is not a declared task")
if patching["target_task"] == patching["source_task"]:
raise SpecError("Patching needs different target and source tasks")
metric = patching["metric"]
if metric["target_token"] == metric["foil_token"]:
raise SpecError("Patching metric needs different target and foil tokens")
def observation_view(spec):
"""The dataset/tasks/model sections in the shape the observation input helpers read."""
return {"dataset": spec["dataset"], "tasks": spec["tasks"], "model": spec["model"]}
__all__ = ["SCHEMA_PATH", "load_intervention_spec", "observation_view", "schema"]