Files
OBLITERATUS/tests/test_labeled_activations.py
Joseph MaglyandClaude Opus 5.5 ca94280c9b feat(analysis): labeled activation sets and model-level subspace validation
Connect the full-subspace report to real captured activations (F08b)
and add the labeled-activation foundation that candidate discovery and
example selection build on.

- Labeled sets: capture a pack's positive, control, neighbour and
  optional battery sets for declared dataset roles. Labels (set, kind,
  role, group, family, record id, input digest) live in a sidecar whose
  digest, the capture identity and the dataset revision are bound in a
  labeled-set manifest; prompt text is never stored. Locked test needs
  explicit acknowledgement. Legacy packs are fit-only, bound to the pack
  bytes.
- Loader: recomputes rows and capture identity and refuses any
  mismatch, naming the field. Selection helpers pick rows by role, set
  and kind, and build deterministic group-disjoint folds. A capture that
  failed or was cancelled is replaced on retry; complete sets never are.
- Subspace report on a labeled set with the pack's extracted rank-k
  basis; groups come from dataset independence units, and the
  basis-fit overlap is read from the basis's dataset access receipt.
- `obliteratus experiments subspace` and a Python API write versioned
  reports next to the run; the Explorer lists them as a descriptive
  table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 21:46:05 -05:00

372 lines
17 KiB
Python

"""Labeled activation sets: capture, identity/revision binding, tamper refusal, selection, folds.
Fixture design: a tiny model whose decoder "layer 0" is a running sum of token
embeddings and "layer 1" an identity linear map, read at the last token. A
word tokenizer maps each record ``"<class word> sample <n>"`` to three tokens.
Class words carry a planted vector in the first dimensions; number tokens
carry isotropic noise and ``sample`` is zero. The activation of a record at
either site is therefore ``planted[set] + noise[n]``, a known structure.
"""
from __future__ import annotations
import json
from types import SimpleNamespace
import pytest
import torch
import yaml
from torch import nn
from obliteratus.analysis.activation_capture import CaptureRefused
from obliteratus.analysis.labeled_activations import (
LABELS, MANIFEST, capture_labeled_set, load_labeled_set, plan_labeled_set,
)
from obliteratus.analysis.labeled_selection import fold_splits, group_folds
from obliteratus.concepts.pack import ConceptPack
from obliteratus.concepts.pack_io import digest
from tests.test_concept_datasets import FIXTURE
pytestmark = pytest.mark.cpu
WIDTH = 10
WORDS = {"positive.jsonl": "alpha", "controls/neutral.jsonl": "beta",
"controls/other.jsonl": "gamma", "neighbours/near.jsonl": "delta"}
TETRAHEDRON = torch.tensor([[1, 1, 1], [1, -1, -1], [-1, 1, -1], [-1, -1, 1]]) * 1.5
COLLINEAR = torch.tensor([[-4.5, 0, 0], [-1.5, 0, 0], [1.5, 0, 0], [4.5, 0, 0]])
LEGACY = FIXTURE.parent / "valid_minimal"
def _rows(word, base):
"""20 fit, 50 select (family pairs), 4 calibrate and 6 test rows."""
rows = []
for i in range(80):
role = "fit" if i < 20 else "select" if i < 70 else "calibrate" if i < 74 else "test"
row = {"id": f"{word}{i}", "text": f"{word} sample {base + i}", "role": role,
"group_id": f"{word}-g{i}"}
if role == "select":
row["family_id"] = f"{word}-family-{i // 2}"
rows.append(row)
return rows
def _jsonl(rows):
return ("\n".join(json.dumps(row) for row in rows) + "\n").encode()
def build_pack(root, mutate=None):
"""Dataset pack: four classes (positive, two controls, one neighbour) plus a battery."""
files = dict(ConceptPack.load(FIXTURE).files)
manifest = yaml.safe_load(files["manifest.yaml"])
manifest["sets"]["controls"].append({"name": "other", "path": "controls/other.jsonl"})
manifest["licence"].append({**manifest["licence"][1], "path": "controls/other.jsonl"})
layout = {path: _rows(word, 80 * k) for k, (path, word) in enumerate(WORDS.items())}
layout["battery/probe.jsonl"] = [
{"id": f"omega{i}", "text": f"omega sample {320 + i}", "role": "fit" if i < 4 else "select",
"group_id": f"omega-g{i}"} for i in range(8)]
if mutate is not None:
mutate(layout)
for path, rows in layout.items():
files[path] = _jsonl(rows)
kinds = ("controls", "neighbours", "battery")
for entry in [manifest["sets"]["positive"], *(e for k in kinds for e in manifest["sets"][k])]:
entry.update(sha256=digest(files[entry["path"]]), n=len(layout[entry["path"]]))
files["manifest.yaml"] = yaml.safe_dump(manifest).encode()
for name, data in files.items():
(root / name).parent.mkdir(parents=True, exist_ok=True)
(root / name).write_bytes(data)
return ConceptPack.load(root)
class WordTokenizer:
pad_token_id, bos_token_id, eos_token_id = 0, 1, 2
chat_template = None
def __init__(self, name="local/words"):
self.name_or_path = name
words = ["<pad>", "<s>", "</s>", "sample", *WORDS.values(), "omega", "present"]
self.vocab = {w: i for i, w in enumerate(words + [str(n) for n in range(400)])}
def get_vocab(self):
return dict(self.vocab)
def __call__(self, text, return_tensors="pt"):
ids = torch.tensor([[self.vocab.get(word, 3) for word in text.lower().split()]])
return {"input_ids": ids, "attention_mask": torch.ones_like(ids)}
class RunningSum(nn.Module):
def forward(self, hidden):
return hidden.cumsum(1)
class PlantedModel(nn.Module):
"""Embedding, running-sum layer 0, identity-linear layer 1 and a head."""
def __init__(self, planted=TETRAHEDRON, seed=0, noise=1.0):
super().__init__()
vocab = WordTokenizer().vocab
generator = torch.Generator().manual_seed(seed)
table = torch.zeros(len(vocab), WIDTH)
for n in range(400):
table[vocab[str(n)]] = torch.randn(WIDTH, generator=generator) * noise
for k, word in enumerate(WORDS.values()):
table[vocab[word], :planted.shape[1]] = planted[k].float()
table[vocab["present"], 0] = 3.0 # the legacy fixture's positive rows
self.config = SimpleNamespace(hidden_size=WIDTH, model_type="planted-test",
num_hidden_layers=2)
self.embed = nn.Embedding(len(vocab), WIDTH)
identity = nn.Linear(WIDTH, WIDTH, bias=False)
with torch.no_grad():
self.embed.weight.copy_(table)
identity.weight.copy_(torch.eye(WIDTH))
self.layers = nn.ModuleList([RunningSum(), identity])
self.head = nn.Linear(WIDTH, len(vocab))
self.eval()
def forward(self, input_ids, attention_mask=None):
hidden = self.embed(input_ids)
for layer in self.layers:
hidden = layer(hidden)
return SimpleNamespace(logits=self.head(hidden))
def sites(model, *indices):
return {f"layer.{i}": model.layers[i] for i in (indices or (0,))}
def capture(model, pack, out, tokenizer=None, **options):
options = {"model_id": "local/planted", **options}
return capture_labeled_set(model, tokenizer or WordTokenizer(), sites(model), pack, out,
**options)
def load(model, pack, out, tokenizer=None, layers=None, **options):
options = {"model_id": "local/planted", **options}
return load_labeled_set(out, model, tokenizer or WordTokenizer(), layers or sites(model), pack,
**options)
@pytest.fixture(scope="module")
def stored(tmp_path_factory):
root = tmp_path_factory.mktemp("labeled")
pack, model = build_pack(root / "pack"), PlantedModel()
manifest = capture(model, pack, root / "select", roles=["select", "calibrate"])
return {"root": root, "pack": pack, "model": model, "manifest": manifest,
"path": root / "select"}
def test_capture_round_trips_aligned_labels_and_planted_values(stored):
labeled = load(stored["model"], stored["pack"], stored["path"])
manifest = stored["manifest"]
assert manifest["rows"] == len(labeled) == 4 * 54
assert manifest["dataset_revision"] == stored["pack"].dataset().revision
assert manifest["set_counts"] == {"positive": 54, "control:neutral": 54, "control:other": 54,
"neighbour:near": 54}
assert set(labeled.roles) == {"select", "calibrate"} and "battery" not in labeled.kinds
assert labeled.values["layer.0"].shape == (216, WIDTH)
vocab, table = WordTokenizer().vocab, stored["model"].embed.weight.detach()
for i in (0, 60, 150):
records = stored["pack"].dataset().access_role(labeled.roles[i])[0][labeled.paths[i]]
word, _, number = next(r["text"] for r in records
if r["id"] == labeled.record_ids[i]).split()
expected = table[vocab[word]] + table[vocab[number]]
assert torch.allclose(labeled.values["layer.0"][i], expected, atol=1e-5)
unit = stored["pack"].dataset().unit
assert labeled.groups[0] == unit(labeled.record_ids[0])
# Family pairs share one independence unit; distinct families do not.
assert labeled.groups[0] == labeled.groups[1] != labeled.groups[2]
labels = json.loads((stored["path"] / LABELS).read_text())
assert "text" not in json.dumps(labels["rows"]) and "sample" not in json.dumps(labels)
def test_selection_and_group_disjoint_folds_are_deterministic(stored):
labeled = load(stored["model"], stored["pack"], stored["path"])
chosen = labeled.select(roles="calibrate", sets=["positive", "control:neutral"])
assert len(chosen) == 8 and set(chosen.roles) == {"calibrate"}
assert chosen.values["layer.0"].shape == (8, WIDTH)
assert chosen.index == tuple(labeled.rows(roles="calibrate",
sets=["positive", "control:neutral"]))
assert labeled.rows(kinds="neighbour")[0] == 162
with pytest.raises(ValueError, match="No labeled rows"):
labeled.select(roles="test")
folds = labeled.folds(5, seed=3)
again = labeled.folds(5, seed=3)
assert len(folds) == 5 and all(torch.equal(a[1], b[1]) for a, b in zip(folds, again))
for train, test in folds:
assert not {labeled.groups[i] for i in train.tolist()} & {labeled.groups[i]
for i in test.tolist()}
assert len({labeled.sets[i] for i in test.tolist()}) == 4
assert torch.cat([t for _, t in folds]).sort().values.tolist() == list(range(len(labeled)))
# Assignment follows group names, not row order.
groups = list(labeled.groups)
assignment = dict(zip(groups, group_folds(groups, 5, 3)))
assert dict(zip(groups[::-1], group_folds(groups[::-1], 5, 3))) == assignment
assert group_folds(groups, 5, 4) != group_folds(groups, 5, 3)
unstratified = fold_splits(group_folds(groups, 4, 0), 4)
assert sum(len(t) for _, t in unstratified) == len(groups)
for bad, match in (((["a", "a"], 2), "distinct group"), ((["a", "b"], 1), "folds must"),
((["a", "b"], 2, 0, ["x"]), "one entry per row")):
with pytest.raises(ValueError, match=match):
group_folds(*bad)
@pytest.mark.parametrize("change", ["model_id", "phase", "readout", "site", "weights",
"tokenizer", "revision"])
def test_any_identity_field_or_dataset_revision_change_refuses(stored, tmp_path, change):
model, pack, options, layers = stored["model"], stored["pack"], {}, None
tokenizer = None
if change == "model_id":
options["model_id"] = "local/other"
elif change == "phase":
options["phase"] = "generation"
elif change == "readout":
options["readout_name"] = "other_readout"
elif change == "site":
layers = sites(model, 1)
elif change == "weights":
model = PlantedModel(seed=1)
elif change == "tokenizer":
tokenizer = WordTokenizer(name="local/other-words")
else:
def edit(layout):
layout["positive.jsonl"][25]["text"] = "alpha sample 399"
pack = build_pack(tmp_path / "changed", edit)
with pytest.raises(CaptureRefused) as refused:
load(model, pack, stored["path"], tokenizer=tokenizer, layers=layers, **options)
expected = {"model_id": "checkpoint.model_id", "phase": "readout.phase",
"readout": "readout.name", "site": "sites", "weights": "weights.sha256",
"tokenizer": "tokenizer.name_or_path", "revision": "dataset_revision"}[change]
assert refused.value.field == expected
def _rewrite(path, name, mutate, *, fix_digest=False):
value = json.loads((path / name).read_text())
mutate(value)
(path / name).write_text(json.dumps(value, indent=2, sort_keys=True))
if fix_digest:
import hashlib
manifest = json.loads((path / MANIFEST).read_text())
manifest["labels_sha256"] = hashlib.sha256((path / name).read_bytes()).hexdigest()
(path / MANIFEST).write_text(json.dumps(manifest))
@pytest.mark.parametrize("tamper", ["label", "label_with_digest", "order_with_digest",
"missing", "manifest_schema"])
def test_tampered_labels_or_manifest_are_refused(stored, tmp_path, tamper):
import shutil
path = tmp_path / "copy"
shutil.copytree(stored["path"], path)
def relabel(value):
value["rows"][0]["set"] = "control:neutral"
def swap(value):
value["rows"][0], value["rows"][1] = value["rows"][1], value["rows"][0]
value["rows"][0]["batch"], value["rows"][1]["batch"] = 0, 1
if tamper == "label":
_rewrite(path, LABELS, relabel)
elif tamper == "label_with_digest":
_rewrite(path, LABELS, relabel, fix_digest=True)
elif tamper == "order_with_digest":
_rewrite(path, LABELS, swap, fix_digest=True)
elif tamper == "missing":
(path / LABELS).unlink()
else:
_rewrite(path, MANIFEST, lambda m: m.update(schema="other"))
with pytest.raises(CaptureRefused) as refused:
load(stored["model"], stored["pack"], path)
assert refused.value.field == {
"label": "labels_sha256", "label_with_digest": "labels", "order_with_digest": "labels",
"missing": "labels_sha256", "manifest_schema": "schema"}[tamper]
def test_capture_binding_and_input_alignment_are_checked(stored, tmp_path):
import shutil
path = tmp_path / "copy"
shutil.copytree(stored["path"], path)
manifest = json.loads((path / MANIFEST).read_text())
manifest["capture_identity_sha256"] = "0" * 64
(path / MANIFEST).write_text(json.dumps(manifest))
with pytest.raises(CaptureRefused) as refused:
load(stored["model"], stored["pack"], path)
assert refused.value.field == "capture_identity_sha256"
shutil.rmtree(path)
shutil.copytree(stored["path"], path)
_rewrite(path, LABELS, lambda v: v["rows"][0].update(input_sha256="1" * 64), fix_digest=True)
with pytest.raises(CaptureRefused, match="aligned") as refused:
load(stored["model"], stored["pack"], path)
_rewrite(path, MANIFEST, lambda m: m.pop("roles"))
with pytest.raises(CaptureRefused, match="malformed"):
load(stored["model"], stored["pack"], path)
(path / MANIFEST).write_text("not json")
with pytest.raises(CaptureRefused, match="unreadable"):
load(stored["model"], stored["pack"], path)
def test_locked_test_requires_acknowledgement_at_capture_and_load(stored, tmp_path):
model, pack = stored["model"], stored["pack"]
with pytest.raises(CaptureRefused, match="allow_locked_test"):
capture(model, pack, tmp_path / "test", roles=["test"])
with pytest.raises(CaptureRefused, match="allow_locked_test"):
capture(model, pack, tmp_path / "test", roles=["test"], allow_locked_test=1)
assert not (tmp_path / "test").exists()
manifest = capture(model, pack, tmp_path / "test", roles=["test"], allow_locked_test=True)
assert manifest["locked_test_acknowledged"] is True and manifest["rows"] == 24
with pytest.raises(CaptureRefused, match="locked test"):
load(model, pack, tmp_path / "test")
assert len(load(model, pack, tmp_path / "test", allow_locked_test=True)) == 24
def test_roles_battery_and_directory_refusals(stored, tmp_path):
pack = stored["pack"]
planned = plan_labeled_set(pack, "fit", include_battery=True)
assert [r["set"] for r in planned["rows"]][-4:] == ["battery:probe"] * 4
assert planned["group_source"].startswith("dataset unit (independence-v1")
for roles in ([], ["fit", "fit"], ["nope"]):
with pytest.raises(CaptureRefused, match="distinct"):
plan_labeled_set(pack, roles)
with pytest.raises(CaptureRefused, match="not empty"):
capture(stored["model"], pack, stored["path"], roles=["select"])
def test_legacy_pack_is_fit_only_and_bound_to_pack_bytes(tmp_path):
pack, model = ConceptPack.load(LEGACY), PlantedModel()
with pytest.raises(CaptureRefused, match="Legacy pack has no declared partitions"):
plan_labeled_set(pack, ["select"])
manifest = capture(model, pack, tmp_path / "legacy")
assert manifest["partitioned"] is False and manifest["dataset_revision"] is None
assert manifest["pack"]["sha256"] == pack.sha256 and manifest["rows"] == 64
labeled = load(model, pack, tmp_path / "legacy")
assert set(labeled.roles) == {"fit"} and labeled.groups[0] == "record:positive.jsonl#0"
manifest = json.loads((tmp_path / "legacy" / MANIFEST).read_text())
manifest["pack"]["sha256"] = "0" * 64
(tmp_path / "legacy" / MANIFEST).write_text(json.dumps(manifest))
with pytest.raises(CaptureRefused) as refused:
load(model, pack, tmp_path / "legacy")
assert refused.value.field == "pack.sha256"
def test_a_failed_or_cancelled_capture_is_replaced_on_retry_but_complete_ones_are_not(
stored, tmp_path):
pack, model = stored["pack"], stored["model"]
target = tmp_path / "retry"
def cancel():
raise KeyboardInterrupt("stop")
with pytest.raises(KeyboardInterrupt):
capture(model, pack, target, roles=["select"], check_cancelled=cancel)
state = json.loads((target / "capture" / "manifest.json").read_text())["state"]
assert state == "cancelled" and {p.name for p in target.iterdir()} == {"capture"}
capture(model, pack, target, roles=["select"])
assert load(model, pack, target).manifest["roles"] == ["select"]
# A complete labeled set is never replaced.
with pytest.raises(CaptureRefused, match="not empty"):
capture(model, pack, target, roles=["select"])
# Unknown contents are left alone.
other = tmp_path / "other"
(other / "capture").mkdir(parents=True)
(other / "notes.txt").write_text("keep")
with pytest.raises(CaptureRefused, match="not empty"):
capture(model, pack, other, roles=["select"])