Files
OBLITERATUS/tests/test_labeled_subspace.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

208 lines
11 KiB
Python

"""Model-level subspace report on labeled activation sets, its entry points and Explorer table.
The planted model (see ``tests/test_labeled_activations.py``) puts the four
set classes at known positions in the first three activation dimensions, and
the stored basis is the first ``k`` coordinate axes, so the expected
``supported_rank`` is known in advance.
"""
from __future__ import annotations
import json
import pytest
import torch
from obliteratus import ui_evidence_explorer as explorer
from obliteratus.analysis.labeled_subspace import (
SCHEMA, TABLE_COLUMNS, basis_fit_overlap, labeled_subspace_report, read_reports, site_layer,
stored_basis, table_rows,
)
from obliteratus.cli import main
from obliteratus.concepts.direction import ConceptDirection
from obliteratus.concepts.pack import ConceptPack
from obliteratus.experiments import SpecError
from obliteratus.experiments.subspace import resolve_and_run, run_subspace
from tests.test_labeled_activations import (
COLLINEAR, LEGACY, PlantedModel, WordTokenizer, build_pack, capture, load,
)
pytestmark = pytest.mark.cpu
FAST = {"n_bootstrap": 5, "n_null": 30}
def make_direction(pack, rank, *, role="fit", revision=None, receipt=True):
basis = torch.eye(10)[:rank]
provenance = {"id": pack.manifest["id"], "sha256": pack.sha256}
if receipt:
provenance["dataset_access"] = {
"revision": revision or pack.dataset().revision, "role": role,
"locked_test_acknowledged": False}
return ConceptDirection(directions={0: basis[0]}, subspaces={0: basis}, extraction_layers=[0],
intervention_layers=[0], pack_provenance=provenance,
concept_id="user/planted", model_id="local/planted")
@pytest.fixture(scope="module")
def planted(tmp_path_factory):
root = tmp_path_factory.mktemp("planted")
pack = build_pack(root / "pack")
out = {"root": root, "pack": pack}
for name, model in (("tetra", PlantedModel()), ("line", PlantedModel(planted=COLLINEAR))):
capture(model, pack, root / name, roles=["fit", "select"])
out[name] = load(model, pack, root / name)
return out
def test_planted_rank_k_is_supported_at_k_on_held_out_select_rows(planted):
result = labeled_subspace_report(planted["tetra"], make_direction(planted["pack"], 3),
"layer.0", roles=["select"], **FAST)
json.dumps(result, allow_nan=False)
report = result["report"]
assert result["schema"] == SCHEMA and result["layer"] == 0 and result["roles"] == ["select"]
assert result["class_counts"] == {"control:neutral": 50, "control:other": 50,
"neighbour:near": 50, "positive": 50}
assert report["verdict"]["supported_rank"] == 3 and report["verdict"]["status"] == "pass"
assert report["parameters"]["grouped"] is True and result["groups"]["count"] == 100
assert result["basis_fit_on_evaluation_data"]["value"] is False
assert report["parameters"]["basis_fit_on_evaluation_data"] is False
assert not any("basis_fit" in q for q in report["verdict"]["qualifiers"])
def test_planted_rank_one_in_rank_three_basis_supports_one_of_three(planted):
result = labeled_subspace_report(planted["line"], make_direction(planted["pack"], 3),
"layer.0", roles=["select"], **FAST)
verdict = result["report"]["verdict"]
assert verdict["supported_rank"] == 1 and verdict["summary"] == "supported_rank: 1 of 3"
assert verdict["status"] == "partial"
def test_fit_rows_carry_the_fit_on_evaluation_data_qualifier(planted):
pack = planted["pack"]
result = labeled_subspace_report(planted["tetra"], make_direction(pack, 2), "layer.0",
roles=["fit"], **FAST)
qualifiers = result["report"]["verdict"]["qualifiers"]
assert result["basis_fit_on_evaluation_data"]["value"] is True
assert "basis_fit_data_not_declared_disjoint" in qualifiers
assert any(q.startswith("basis_fit_on_evaluation_data: evaluated rows include role 'fit'")
for q in qualifiers)
mixed = labeled_subspace_report(planted["tetra"], make_direction(pack, 2), "layer.0",
**FAST)
assert mixed["roles"] == ["fit", "select"]
assert mixed["basis_fit_on_evaluation_data"]["value"] is True
def test_unknown_overlap_is_qualified_not_declared_disjoint(planted):
pack, manifest = planted["pack"], planted["tetra"].manifest
for direction, reason in ((make_direction(pack, 2, receipt=False), "no dataset access"),
(make_direction(pack, 2, revision="sha256:other"), "revision")):
value, why = basis_fit_overlap(manifest, direction, {"select"})
assert value is None and reason in why
result = labeled_subspace_report(planted["tetra"], make_direction(pack, 2, receipt=False),
"layer.0", roles=["select"], **FAST)
qualifiers = result["report"]["verdict"]["qualifiers"]
assert "basis_fit_data_not_declared_disjoint" in qualifiers
assert any(q.startswith("basis_fit_overlap_unknown") for q in qualifiers)
legacy = {"partitioned": False, "pack": {"sha256": pack.sha256}}
assert basis_fit_overlap(legacy, make_direction(pack, 2), {"fit"})[0] is True
assert basis_fit_overlap({**legacy, "pack": {"sha256": "x"}}, make_direction(pack, 2),
{"fit"})[0] is None
def test_site_and_basis_lookup_refusals(planted):
direction = make_direction(planted["pack"], 2)
assert site_layer("layer.12") == 12
with pytest.raises(ValueError, match="pass layer explicitly"):
site_layer("blocks.1")
assert stored_basis(direction, 0).shape == (2, 10)
direction.subspaces = {}
assert stored_basis(direction, 0).shape == (1, 10)
with pytest.raises(ValueError, match="no layer 5"):
stored_basis(direction, 5)
with pytest.raises(ValueError, match="no site"):
labeled_subspace_report(planted["tetra"], direction, "layer.1")
def test_python_api_and_cli_write_versioned_reports_and_reuse_captures(tmp_path, capsys,
monkeypatch):
pack = build_pack(tmp_path / "pack")
direction = make_direction(pack, 3)
direction.save(tmp_path / "direction.safetensors")
model = PlantedModel()
summary = run_subspace(pack, model, WordTokenizer(), direction, 0, tmp_path / "api",
model_id="local/planted", roles=["select"],
report_options=FAST)
assert summary["verdict"]["supported_rank"] == 3 and not summary["reused_labeled_set"]
assert summary["basis_fit_on_evaluation_data"]["value"] is False
again = run_subspace(pack, model, WordTokenizer(), direction, 0, tmp_path / "api",
model_id="local/planted", roles=["select"], report_options=FAST)
assert again["reused_labeled_set"] and again["report"] == summary["report"]
[(path, stored)] = read_reports(tmp_path / "api")
assert path.name == "layer.0-select.json" and stored["schema_version"] == 1
loaded = []
def factory(section):
loaded.append(section)
return type("Handle", (), {"model": PlantedModel(), "tokenizer": WordTokenizer()})()
monkeypatch.setattr("obliteratus.experiments.inputs.default_model_factory", factory)
main(["experiments", "subspace", "--pack", str(tmp_path / "pack"), "--model", "local/planted",
"--direction", str(tmp_path / "direction.safetensors"), "--layer", "0",
"--role", "fit", "--out", str(tmp_path / "cli")])
printed = json.loads(capsys.readouterr().out)
assert loaded == [{"id": "local/planted", "dtype": "float32", "revision": None,
"device": "cpu"}]
assert printed["basis_fit_on_evaluation_data"]["value"] is True
assert (tmp_path / "cli" / "layer.0-fit.json").is_file()
with pytest.raises(SystemExit) as refused:
main(["experiments", "subspace", "--pack", str(tmp_path / "pack"), "--layer", "0",
"--direction", str(tmp_path / "direction.safetensors")])
assert refused.value.code == 4 and "--model" in capsys.readouterr().err
with pytest.raises(SystemExit) as locked:
main(["experiments", "subspace", "--pack", str(tmp_path / "pack"), "--model", "m",
"--direction", str(tmp_path / "direction.safetensors"), "--layer", "0",
"--role", "test", "--out", str(tmp_path / "locked")])
assert locked.value.code == 4 and "allow_locked_test" in capsys.readouterr().err
def test_run_directory_entry_point_and_explorer_table(tmp_path):
from tests.test_observation_runs import make_spec
pack = build_pack(tmp_path / "pack")
make_direction(pack, 3).save(tmp_path / "direction.safetensors")
run = tmp_path / "run"
run.mkdir()
(run / "spec.json").write_text(json.dumps(make_spec(tmp_path / "pack", capture=None)))
assert explorer.subspace_rows({"run_dir": run}) == []
def factory(section):
return type("Handle", (), {"model": PlantedModel(), "tokenizer": WordTokenizer(),
"cleanup": lambda self: None})()
summary = resolve_and_run(direction=tmp_path / "direction.safetensors", layer=0, run=run,
model_factory=factory, pack_resolver=ConceptPack.load)
assert summary["report"].endswith("subspace/layer.0-select.json")
(run / "subspace" / "notes.json").write_text("{}")
(run / "subspace" / "broken.json").write_text("{")
[row] = explorer.subspace_rows({"run_dir": run})
assert len(row) == len(TABLE_COLUMNS) and row[0] == "layer.0" and row[2] == "select"
assert row[5] == "pass · supported_rank: 3 of 3" and row[6] == "False"
assert table_rows(tmp_path / "absent") == []
with pytest.raises(SpecError, match="not both"):
resolve_and_run(direction="d", layer=0, run=run, pack="p")
other = build_pack(tmp_path / "other", lambda layout: layout["positive.jsonl"][30].update(
text="alpha sample 398"))
with pytest.raises(SpecError, match="revision differs"):
resolve_and_run(direction=tmp_path / "direction.safetensors", layer=0, run=run,
model_factory=factory, pack_resolver=lambda _ref: other)
def test_legacy_labeled_set_report_is_fit_on_evaluation_data(tmp_path):
pack, model = ConceptPack.load(LEGACY), PlantedModel()
capture(model, pack, tmp_path / "legacy")
labeled = load(model, pack, tmp_path / "legacy")
direction = make_direction(pack, 1, receipt=False)
result = labeled_subspace_report(labeled, direction, "layer.0", **FAST)
assert result["basis_fit_on_evaluation_data"]["value"] is True
assert result["groups"]["source"].startswith("legacy")
assert result["report"]["verdict"]["supported_rank"] == 1