mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Add two-estimate interaction experiments (F14): measure whether two ready dossier estimates interact when applied together, in which order, and under which conversation history, without treating orthogonality as independence. - Arms: baseline, A, B, each declared order, matched random controls for each, optional protected arms (one component restricted to the complement of the other's span) and a baseline replay; restoration is checked after every arm and a failure stops the attempt. - Orders and history mode (single turn, matched context, diverged) are part of the spec digest. Components pass the harness's model and independence checks; confirmation-locked rows are refused. - Interaction is the per-example difference-in-differences of task success with a seeded paired bootstrap interval and an exact sign test, compared against the random-control sequence; order sensitivity is reported. "Independent" is never an outcome, and geometry never feeds one. - Protection reports projection preservation and behaviour preservation separately. - Results link to dossiers by reference as descriptive evidence only, re-verified on read, never granting labels. `obliteratus experiments interact` and an Explorer interaction view. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
290 lines
14 KiB
Python
290 lines
14 KiB
Python
"""Interaction and preservation experiments: outcome fixtures on a planted model.
|
|
|
|
The fixture model plants the positive set on channels 1 and 2 of layer 0, so a
|
|
dossier has two ready channel estimates (orthogonal standard-basis vectors).
|
|
Its yes/no readout is a non-parameter rule, so one dossier serves every rule
|
|
(weights and fingerprint are unchanged):
|
|
|
|
* ``and``: "yes" when channel 1 + channel 2 > 15. Steering either channel by 10
|
|
alone never flips a row; both together flip every row. Orthogonal edits that
|
|
interact.
|
|
* ``second``: "yes" when channel 2 > 5. Steering channel 2 flips every row and
|
|
channel 1 is ignored: an additive pair. With A = raw steer of channel 1 by 20
|
|
and B = residual-norm steer of channel 2 by 0.4, A>B scales B by the inflated
|
|
norm and flips rows while B>A does not: order sensitive.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from contextlib import contextmanager
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
from torch import nn
|
|
|
|
from obliteratus.analysis import interaction_stats as stats
|
|
from obliteratus.analysis.target_dossier import build_dossier, write_dossier
|
|
from obliteratus.concepts.pack import ConceptPack
|
|
from obliteratus.concepts.restoration import hook_counts
|
|
from obliteratus.experiments.discover import run_discover
|
|
from obliteratus.experiments.interaction_runner import (
|
|
INTERACTION_DIR, load_interaction_result, run_interaction,
|
|
)
|
|
from obliteratus.experiments.interaction_spec import load_interaction_spec
|
|
from obliteratus.experiments.intervention_ops import apply_operation
|
|
from obliteratus.experiments.intervention_runner import RestorationFailed
|
|
from obliteratus.experiments.spec import SpecError, spec_digest
|
|
from obliteratus.experiments.store import ObservationStore
|
|
from tests.test_controlled_interventions import DECLARED, AnswerTokenizer, _answers
|
|
from tests.test_labeled_activations import WIDTH, PlantedModel, build_pack
|
|
from tests.test_target_dossier import FAST, HYPOTHESIS, LEXICAL, REPORT
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
CH1, CH2 = "channel:layer.0:1", "channel:layer.0:2"
|
|
|
|
|
|
class RuleModel(PlantedModel):
|
|
"""Planted channels 1 and 2 with a rule-based yes/no readout."""
|
|
|
|
def __init__(self):
|
|
planted = torch.zeros(4, 3)
|
|
planted[0, 1] = planted[0, 2] = 4.0
|
|
super().__init__(planted=planted)
|
|
self.vocab = AnswerTokenizer().vocab
|
|
old = self.embed.weight.detach().clone()
|
|
self.embed = nn.Embedding(len(self.vocab), WIDTH)
|
|
self.head = nn.Linear(WIDTH, len(self.vocab))
|
|
with torch.no_grad():
|
|
self.embed.weight.zero_()
|
|
self.embed.weight[:len(old)] = old
|
|
self.head.weight.zero_()
|
|
self.head.bias.fill_(-100.0)
|
|
self.head.bias[self.vocab["no"]] = 0.0
|
|
self.rule = "second"
|
|
self.eval()
|
|
|
|
def forward(self, input_ids, attention_mask=None):
|
|
hidden = self.embed(input_ids)
|
|
for layer in self.layers:
|
|
hidden = layer(hidden)
|
|
logits = self.head(hidden)
|
|
yes = (4 * (hidden[..., 1] + hidden[..., 2]) - 60 if self.rule == "and"
|
|
else 4 * hidden[..., 2] - 20)
|
|
logits = logits.clone()
|
|
logits[..., self.vocab["yes"]] = yes
|
|
return SimpleNamespace(logits=logits)
|
|
|
|
def generate(self, input_ids, attention_mask=None, max_new_tokens=1, **_options):
|
|
ids = input_ids
|
|
for _ in range(max_new_tokens):
|
|
ids = torch.cat([ids, self(ids).logits[:, -1].argmax(-1, keepdim=True)], 1)
|
|
return ids
|
|
|
|
|
|
def build_env(root):
|
|
"""Pack, rule model and a run with one dossier holding both channel estimates."""
|
|
pack = build_pack(root / "pack", _answers)
|
|
pack.location = root / "pack"
|
|
model, run = RuleModel(), root / "run"
|
|
run_discover(pack, model, AnswerTokenizer(), 0, run / "candidates", model_id="local/answers",
|
|
options=FAST)
|
|
dossier = build_dossier("alpha", run, hypothesis={**HYPOTHESIS, **DECLARED},
|
|
discovery=REPORT, estimates=[CH1, CH2],
|
|
confound_resolutions=[LEXICAL])
|
|
write_dossier(dossier, run)
|
|
return {"pack": pack, "model": model, "run": run}
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def env(tmp_path_factory):
|
|
return build_env(tmp_path_factory.mktemp("interactions"))
|
|
|
|
|
|
def component(estimate, coefficient, units="raw"):
|
|
return {"dossier": "dossiers/alpha.json", "estimate": estimate,
|
|
"operation": {"kind": "steer", "site": "layer.0", "token_scope": "last_token",
|
|
"units": units, "coefficient": coefficient}}
|
|
|
|
|
|
def make_spec(pack, a, b, **changes):
|
|
spec = {"schema_version": "1", "kind": "interaction_experiment",
|
|
"components": {"A": a, "B": b},
|
|
"model": {"id": "local/answers", "dtype": "float32", "model_class": "fixture"},
|
|
"dataset": {"pack": str(pack.location), "revision": pack.dataset().revision,
|
|
"role": "calibrate"},
|
|
"tasks": [{"name": "flip", "scorer": {"id": "exact_answer", "version": 1},
|
|
"target_field": "answer", "path": "controls/neutral.jsonl"},
|
|
{"name": "keep", "scorer": {"id": "exact_answer", "version": 1},
|
|
"target_field": "answer", "path": "positive.jsonl"}],
|
|
"target_task": "flip", "decoding": {"max_new_tokens": 1}, "control": {"seed": 7},
|
|
"statistics": {"bootstrap_samples": 500}}
|
|
spec.update(changes)
|
|
return spec
|
|
|
|
|
|
def run_rule(env, spec, rule, **options):
|
|
env["model"].rule = rule
|
|
factory = lambda section: SimpleNamespace(model=env["model"], # noqa: E731
|
|
tokenizer=AnswerTokenizer())
|
|
return run_interaction(spec, env["run"], model_factory=factory,
|
|
pack_resolver=ConceptPack.load, **options)
|
|
|
|
|
|
def _flip(result, order="A>B"):
|
|
return result["analysis"]["interactions"]["flip"][order]
|
|
|
|
|
|
def test_orthogonal_but_interacting_edits_report_interaction_not_independence(env):
|
|
spec = make_spec(env["pack"], component(CH1, 10.0), component(CH2, 10.0))
|
|
result = run_rule(env, spec, "and")
|
|
geometry = result["analysis"]["geometry"]
|
|
assert geometry["orthogonal"] is True and "never used" in geometry["rule"]
|
|
entry = _flip(result)
|
|
target = entry["target"]
|
|
assert target["rates"] == {"baseline": 0.0, "first": 0.0, "second": 0.0, "sequence": 1.0}
|
|
assert target["estimate"] == 1.0 and target["interval"][0] > 0
|
|
assert target["p_value"] < 0.05 and target["outcome"] == stats.DETECTED
|
|
assert entry["matched_control"]["estimate"] == 0.0
|
|
assert entry["matched_control"]["outcome"] == stats.NOT_DETECTED
|
|
assert entry["beyond_control"]["estimate"] == 1.0
|
|
# Raw steering commutes: both orders agree.
|
|
order = result["analysis"]["order_sensitivity"]["flip"]["target"]
|
|
assert order["estimate"] == 0.0 and order["outcome"] == stats.NO_ORDER_DIFFERENCE
|
|
assert "independent" not in json.dumps(
|
|
[target["outcome"], order["outcome"], entry["matched_control"]["outcome"]])
|
|
names = [a["arm"] for a in result["arms"]]
|
|
assert names == ["baseline", "A", "random:A", "B", "random:B", "A>B", "random:A>B", "B>A",
|
|
"random:B>A", "replay"]
|
|
assert all(a["restoration"]["restored"] for a in result["arms"])
|
|
assert result["arms"][-1]["identical_to_baseline"] is True
|
|
sequence = result["arms"][5]
|
|
assert [h["component"] for h in sequence["hooks"]] == ["A", "B"]
|
|
assert [h["direction"] for h in result["arms"][6]["hooks"]] == ["random", "random"]
|
|
for name in ("A", "B"):
|
|
control = result["components"][name]["control"]
|
|
assert control["orthogonal_to"].startswith("both")
|
|
assert max(control["span_row_cosines"]) < 1e-12
|
|
assert result["components"][name]["matches_preregistered_decision"] is False
|
|
assert result["evidence_scope"]["scope"] == "software-contract"
|
|
assert result["label_effect"].startswith("none")
|
|
assert hook_counts(env["model"]) == [0] * len(hook_counts(env["model"]))
|
|
loaded = load_interaction_result(result["location"]["result"], verify=True)
|
|
assert loaded["analysis"]["interactions"] == result["analysis"]["interactions"]
|
|
|
|
|
|
def test_additive_pair_interval_covers_zero(env):
|
|
spec = make_spec(env["pack"], component(CH2, 10.0), component(CH1, 10.0))
|
|
result = run_rule(env, spec, "second")
|
|
for order in ("A>B", "B>A"):
|
|
target = _flip(result, order)["target"]
|
|
assert target["effects"]["first" if order == "A>B" else "second"] == 1.0
|
|
low, high = target["interval"]
|
|
assert low <= 0 <= high and target["outcome"] == stats.NOT_DETECTED
|
|
assert "never evidence of independence" in target["rule"]
|
|
|
|
|
|
def test_order_sensitive_pair_detects_order_difference(env):
|
|
spec = make_spec(env["pack"], component(CH1, 20.0), component(CH2, 0.4, "residual_norm"))
|
|
result = run_rule(env, spec, "second")
|
|
rates = result["analysis"]["matrix"]["flip"]
|
|
assert rates["A>B"] == 1.0 and rates["B>A"] == 0.0
|
|
assert rates["A"] == rates["B"] == rates["baseline"] == 0.0
|
|
order = result["analysis"]["order_sensitivity"]["flip"]
|
|
assert order["target"]["outcome"] == stats.ORDER_SENSITIVE
|
|
assert order["target"]["estimate"] == 1.0 and order["target"]["interval"][0] > 0
|
|
assert order["matched_control"]["outcome"] == stats.NO_ORDER_DIFFERENCE
|
|
assert _flip(result, "A>B")["target"]["outcome"] == stats.DETECTED
|
|
assert _flip(result, "B>A")["target"]["outcome"] == stats.NOT_DETECTED
|
|
|
|
|
|
@contextmanager
|
|
def _leaky(model, layer, vector, coefficient, operation):
|
|
with apply_operation(model, layer, vector, coefficient, operation) as manager:
|
|
layer.register_forward_hook(lambda *_: None)
|
|
yield manager
|
|
|
|
|
|
def test_leaky_operation_fails_restoration_and_records_attempt(env):
|
|
spec = make_spec(env["pack"], component(CH1, 3.0), component(CH2, 3.0))
|
|
layer = env["model"].layers[0]
|
|
try:
|
|
with pytest.raises(RestorationFailed) as failed:
|
|
run_rule(env, spec, "second", operations={"steer": _leaky})
|
|
finally:
|
|
layer._forward_hooks.clear()
|
|
assert failed.value.restoration["arm"] == "A"
|
|
assert failed.value.restoration["hooks_restored"] is False
|
|
digest = spec_digest(load_interaction_spec(spec))
|
|
[attempt] = ObservationStore(env["run"] / INTERACTION_DIR).get(digest).history()
|
|
assert attempt["state"] == "failed"
|
|
event = attempt["events"][-1]
|
|
assert event["restoration"]["arm"] == "A" and event["hooks_removed"] is False
|
|
|
|
|
|
def test_order_and_history_mode_are_part_of_run_identity(env):
|
|
a, b = component(CH2, 10.0), component(CH1, 10.0)
|
|
base = make_spec(env["pack"], a, b)
|
|
digests = {spec_digest(load_interaction_spec(s)) for s in (
|
|
base, make_spec(env["pack"], a, b, orders=["A>B"]),
|
|
make_spec(env["pack"], a, b, orders=["B>A"]),
|
|
make_spec(env["pack"], b, a),
|
|
make_spec(env["pack"], a, b, history={"mode": "matched_context", "follow_up": "again"}),
|
|
make_spec(env["pack"], a, b, history={"mode": "diverged", "follow_up": "again"}))}
|
|
assert len(digests) == 6
|
|
assert load_interaction_spec(make_spec(env["pack"], a, b, orders=["B>A", "A>B"]))[
|
|
"orders"] == ["A>B", "B>A"]
|
|
matched = run_rule(env, make_spec(env["pack"], a, b, orders=["A>B"],
|
|
history={"mode": "matched_context", "follow_up": "again"}),
|
|
"second")
|
|
diverged = run_rule(env, make_spec(env["pack"], a, b, orders=["A>B"],
|
|
history={"mode": "diverged", "follow_up": "again"}), "second")
|
|
assert matched["spec_digest"] != diverged["spec_digest"]
|
|
assert matched["history_mode"] == "matched_context"
|
|
arm = {a["arm"]: a for a in matched["arms"]}["A"]
|
|
assert arm["history"]["turn_1_source"].startswith("baseline")
|
|
assert arm["history"]["turn_1_diverged"]["changed"] == 0
|
|
arm = {a["arm"]: a for a in diverged["arms"]}["A"]
|
|
assert arm["history"]["turn_1_source"] == "this arm"
|
|
assert arm["history"]["turn_1_diverged"]["rate"] > 0.5
|
|
ids = {a["arm_id"] for a in matched["arms"]} & {a["arm_id"] for a in diverged["arms"]}
|
|
assert not ids
|
|
order = matched["analysis"]["order_sensitivity"]["flip"]
|
|
assert order["outcome"] == stats.UNTESTED
|
|
assert all(a["restoration"]["restored"] for a in diverged["arms"])
|
|
|
|
|
|
def test_protection_coverage_separates_projection_from_behaviour(env):
|
|
spec = make_spec(env["pack"], component(CH2, 10.0), component(CH1, 10.0),
|
|
protection={"preserve_task": "flip", "margin": 0.1})
|
|
result = run_rule(env, spec, "second")
|
|
assert [a["arm"] for a in result["arms"]][-3:] == ["A|protect:B", "B|protect:A", "replay"]
|
|
coverage = result["analysis"]["protection"]["components"]
|
|
for name in ("A", "B"):
|
|
assert coverage[name]["status"] == "identity"
|
|
assert "does not show" in coverage[name]["reason"]
|
|
quantity = coverage[name]["projected_quantity"]
|
|
assert quantity["protected"]["preserved"] is True
|
|
assert quantity["unprotected"]["preserved"] is True
|
|
behaviour = result["analysis"]["behaviour_preservation"]["components"]
|
|
# A edits channel 2 orthogonally to B's span: the projection onto B is preserved,
|
|
# the flip behaviour is not.
|
|
assert behaviour["A"]["protected"]["preserved"] is False
|
|
assert behaviour["A"]["protected"]["estimate"] == 1.0
|
|
assert behaviour["B"]["protected"]["preserved"] is True
|
|
|
|
|
|
def test_spec_refusals(env):
|
|
a = component(CH1, 1.0)
|
|
with pytest.raises(SpecError, match="same estimate"):
|
|
load_interaction_spec(make_spec(env["pack"], a, a))
|
|
with pytest.raises(SpecError, match="follow_up"):
|
|
load_interaction_spec(make_spec(env["pack"], a, component(CH2, 1.0),
|
|
history={"mode": "diverged"}))
|
|
with pytest.raises(SpecError, match="preserve_task"):
|
|
load_interaction_spec(make_spec(env["pack"], a, component(CH2, 1.0),
|
|
protection={"preserve_task": "nope"}))
|
|
with pytest.raises(SpecError):
|
|
load_interaction_spec(make_spec(env["pack"], a, component(CH2, 1.0), orders=[]))
|