mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
Add optimization study O03: can a coarse layer pass followed by targeted fine captures find the same targets as the full sweep at lower end-to-end cost? - Three arms over the same rows and target rule: the reference sweep (every candidate layer), a fixed layer grid, and adaptive capture (coarse grid plus random exploration, then fine captures ranked by neighbouring AUC, uncertainty, direction disagreement and gap position, plus coverage probes). - Forwards are counted by a hook; inference savings are claimed only when every seed needs fewer complete forwards. Bytes and extraction/CV work are counted; host timings stay outside the report digest. - Unobserved regions are listed with "no evidence either way", and each missed target names its region. - The recommendation is "no improvement" unless the missed-target rate stays within the pre-declared tolerance over at least 3 seeds with lower counted cost, and adoption also needs measured end-to-end time below the reference in every seed. Confirmation-exposed rows are refused. - `obliteratus experiments adaptive-capture` and a Python API. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
4.5 KiB
Python
93 lines
4.5 KiB
Python
"""``experiments adaptive-capture``: Python API and CLI on the planted dataset pack.
|
|
|
|
Uses the dataset pack from ``tests/test_labeled_activations.py`` (role ``fit``:
|
|
20 positive and 40 control rows) and the nine-layer planted model from
|
|
``tests/test_adaptive_capture.py``. Synthetic fixture: not evidence for real models.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from obliteratus.cli import main
|
|
from obliteratus.concepts.pack import ConceptPack
|
|
from obliteratus.experiments import SpecError
|
|
from obliteratus.experiments.adaptive_capture import (
|
|
pack_rows, resolve_and_run, run_adaptive_capture,
|
|
)
|
|
from tests.test_adaptive_capture import PlantedLayersModel
|
|
from tests.test_labeled_activations import WordTokenizer, build_pack
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
CONFIG = {"seeds": 3, "folds": 3}
|
|
MODEL = PlantedLayersModel()
|
|
|
|
|
|
def factory(section):
|
|
return type("Handle", (), {"model": MODEL, "tokenizer": WordTokenizer(),
|
|
"cleanup": lambda self: None})()
|
|
|
|
|
|
def test_python_api_writes_a_text_free_versioned_report(tmp_path):
|
|
pack = build_pack(tmp_path / "pack")
|
|
summary = run_adaptive_capture(pack, PlantedLayersModel(), WordTokenizer(), tmp_path / "api",
|
|
model_id="local/planted", config=CONFIG)
|
|
report = json.loads(Path(summary["report"]).read_text())
|
|
assert report["schema"] == "obliteratus.adaptive-capture/1"
|
|
assert report["rows"] == {**report["rows"], "total": 60, "positive": 20, "negative": 40}
|
|
assert report["inputs"]["roles"] == ["fit"] and report["inputs"]["model_id"] == "local/planted"
|
|
assert report["candidate_layers"] == [f"layer.{i}" for i in range(9)]
|
|
assert report["arms"]["reference_sweep"]["runs"][0]["targets"] == ["layer.5"]
|
|
assert report["host_measurements"]["recorded"]
|
|
assert summary["adoption"] in {"improvement", "no improvement"}
|
|
assert "alpha sample" not in json.dumps(report)
|
|
subset = run_adaptive_capture(pack, PlantedLayersModel(), WordTokenizer(), tmp_path / "sub",
|
|
model_id="local/planted", layers=[2, 3, 4, 5],
|
|
config=CONFIG, timings=False)
|
|
assert json.loads(Path(subset["report"]).read_text())["candidate_layers"] == [
|
|
"layer.2", "layer.3", "layer.4", "layer.5"]
|
|
with pytest.raises(ValueError, match="two candidate"):
|
|
run_adaptive_capture(pack, PlantedLayersModel(), WordTokenizer(), tmp_path / "x",
|
|
model_id="m", layers=[3])
|
|
with pytest.raises(ValueError, match="locked test"):
|
|
pack_rows(pack, WordTokenizer(), roles=["fit", "test"])
|
|
|
|
|
|
def test_cli_and_run_directory_entry_points(tmp_path, capsys, monkeypatch):
|
|
from tests.test_observation_runs import make_spec
|
|
|
|
build_pack(tmp_path / "pack")
|
|
(tmp_path / "config.json").write_text(json.dumps(CONFIG))
|
|
monkeypatch.setattr("obliteratus.experiments.inputs.default_model_factory", factory)
|
|
argv = ["experiments", "adaptive-capture", "--pack", str(tmp_path / "pack"), "--model",
|
|
"local/planted", "--config", str(tmp_path / "config.json"),
|
|
"--out", str(tmp_path / "cli"), "--no-timings"]
|
|
main(argv)
|
|
printed = json.loads(capsys.readouterr().out)
|
|
first = (tmp_path / "cli" / "adaptive-capture.json").read_bytes()
|
|
assert printed["adoption"] == "no improvement"
|
|
assert json.loads(first)["adoption"]["reasons"] in (
|
|
["end-to-end runtime was not measured"],
|
|
["counted-resource recommendation is no improvement"])
|
|
main(argv)
|
|
capsys.readouterr()
|
|
assert (tmp_path / "cli" / "adaptive-capture.json").read_bytes() == first
|
|
with pytest.raises(SystemExit) as refused:
|
|
main(["experiments", "adaptive-capture", "--pack", str(tmp_path / "pack")])
|
|
assert refused.value.code == 4 and "--model" in capsys.readouterr().err
|
|
run = tmp_path / "run"
|
|
run.mkdir()
|
|
(run / "spec.json").write_text(json.dumps(make_spec(tmp_path / "pack", capture=None)))
|
|
summary = resolve_and_run(run=run, config=CONFIG, timings=False, model_factory=factory,
|
|
pack_resolver=ConceptPack.load)
|
|
assert summary["report"].endswith("adaptive-capture/adaptive-capture.json")
|
|
with pytest.raises(SpecError, match="not both"):
|
|
resolve_and_run(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(run=run, model_factory=factory, pack_resolver=lambda _r: other)
|