Files
OBLITERATUS/tests/test_observation_runs.py
Joseph MaglyandClaude Opus 5.5 2539cdb2b7 feat(experiments): chat templates, seeded sampling, batching and CUDA observation
Remove the observation runner's execution limits.

- Chat templates: an optional prompt section renders each prompt
  through the tokenizer's chat template (optional system message,
  generation prompt), tokenized without extra special tokens. The
  rendered-prompt digest joins the generation step identity and each
  record; capture uses the same rendered prompts.
- Decoding: seeded sampling (temperature, top-p, top-k) alongside
  greedy, with a per-example seed derived from the spec seed and example
  id. Comparison treats sampled outcomes as exact only when strategy,
  device, dtype and seed match, and says why otherwise.
- Batching: optional left-padded greedy batches with an explicit,
  recorded pad token; rows are cut at their first eos so batched and
  unbatched outcomes agree. Sampling with batches is refused because
  per-example seeds cannot hold.
- Devices: dense CUDA for the runner and capture, with shards written
  from CPU copies and device/dtype recorded. Quantized, offloaded,
  dispatched, meta, split-device and MPS models are refused with
  explicit reasons.

The schema stays additive within version 1; options at their defaults
are dropped from the filled spec, so existing spec digests are
unchanged (pinned in tests).

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

365 lines
16 KiB
Python

"""Baseline observation specs, runner, run store, resume and intervention refusal."""
from __future__ import annotations
import json
from types import SimpleNamespace
import pytest
import torch
from torch import nn
from obliteratus.concepts.pack import ConceptPack
from obliteratus.experiments import (
InterventionStateRefused, ObservationCancelled, ObservationStore, SpecError,
compare_results, load_spec, run_observation, spec_digest,
)
from obliteratus.experiments.store import StoreError
from tests.test_concept_datasets import FIXTURE, alter
pytestmark = pytest.mark.cpu
def tiny_gpt2():
from transformers import GPT2Config, GPT2LMHeadModel
torch.manual_seed(0)
config = GPT2Config(n_layer=2, n_embd=16, n_head=2, vocab_size=40, n_positions=64,
bos_token_id=1, eos_token_id=2)
return GPT2LMHeadModel(config).eval()
class CharTokenizer:
"""Offline character tokenizer over the tiny model's 40-token vocabulary."""
pad_token_id, bos_token_id, eos_token_id = 0, 1, 2
chat_template = None
def __init__(self, name="local/char"):
self.name_or_path = name
def get_vocab(self):
return {"<pad>": 0, "<s>": 1, "</s>": 2, " ": 3,
**{chr(97 + i): 4 + i for i in range(26)}, **{str(i): 30 + i for i in range(10)}}
def __call__(self, text, return_tensors="pt"):
vocab = self.get_vocab()
ids = torch.tensor([[vocab.get(char, 3) for char in text.lower()]])
return {"input_ids": ids, "attention_mask": torch.ones_like(ids)}
def decode(self, ids, skip_special_tokens=True):
inverse = {v: k for k, v in self.get_vocab().items()}
return "".join(inverse[int(i)] for i in ids if int(i) > 2)
def factory(log=None, tokenizer=None, model=None):
def make(section):
if log is not None:
log.append(section)
return SimpleNamespace(model=model if model is not None else tiny_gpt2(),
tokenizer=tokenizer or CharTokenizer())
return make
@pytest.fixture
def pack_dir(tmp_path):
return build_pack(tmp_path / "pack")
def build_pack(root):
"""The F02 fixture pack with ``answer`` and ``alias`` targets on every positive row."""
files = dict(ConceptPack.load(FIXTURE).files)
def annotate(rows):
for index, row in enumerate(rows):
row.update(answer=f"amber {index}", alias=["kite", f"prism {index}"])
files = alter(files, "positive.jsonl", annotate)
for name, data in files.items():
(root / name).parent.mkdir(parents=True, exist_ok=True)
(root / name).write_bytes(data)
return root
def make_spec(pack_dir, **changes):
spec = {
"schema_version": "1", "kind": "baseline_observation",
"dataset": {"pack": str(pack_dir), "role": "select",
"revision": ConceptPack.load(pack_dir).dataset().revision},
"model": {"id": "local/tiny-gpt2", "dtype": "float32"},
"decoding": {"max_new_tokens": 3},
"tasks": [
{"name": "answers", "scorer": {"id": "exact_answer", "version": 1},
"target_field": "answer"},
{"name": "aliases", "scorer": {"id": "exact_answer", "version": 1},
"target_field": "alias"},
],
"capture": {"sites": [0, 1], "budget_bytes": 1_000_000},
"profile": True, "seed": 0,
}
for section, values in changes.items():
if isinstance(values, dict) and isinstance(spec.get(section), dict):
spec[section] = {**spec[section], **values}
else:
spec[section] = values
return spec
def test_spec_identity_is_canonical_and_changes_with_every_field(pack_dir):
from jsonschema import Draft202012Validator
from obliteratus.experiments.spec import schema
spec = make_spec(pack_dir)
Draft202012Validator.check_schema(schema())
Draft202012Validator(schema()).validate(spec)
explicit = make_spec(pack_dir, dataset={"pack_sha256": None, "allow_locked_test": False},
model={"revision": None, "device": "cpu"},
decoding={"strategy": "greedy"},
capture={"sites": [0, 1], "budget_bytes": 1_000_000,
"readout": "decoder_output"})
for task in explicit["tasks"]:
task["path"] = "positive.jsonl"
base = spec_digest(load_spec(spec))
assert spec_digest(load_spec(explicit)) == base
changed = [
{"dataset": {"role": "calibrate"}}, {"model": {"id": "other"}},
{"model": {"revision": "abc"}}, {"model": {"dtype": "bfloat16"}},
{"decoding": {"max_new_tokens": 4}}, {"capture": {"sites": [0], "budget_bytes": 1_000_000}},
{"capture": None}, {"profile": False}, {"seed": 1},
{"tasks": [spec["tasks"][0]]},
{"dataset": {"pack_sha256": "0" * 64}},
]
digests = {spec_digest(load_spec(make_spec(pack_dir, **change))) for change in changed}
assert base not in digests and len(digests) == len(changed)
path = pack_dir.parent / "spec.json"
path.write_text(json.dumps(spec))
assert spec_digest(load_spec(path)) == base
@pytest.mark.parametrize("change,message", [
({"kind": "intervention"}, "unsupported value"),
({"decoding": {"strategy": "beam"}}, "unsupported value"),
({"tasks": [{"name": "x", "scorer": {"id": "nope", "version": 1}, "target_field": "a"}]},
"Unknown scorer"),
({"tasks": [{"name": "x", "scorer": {"id": "exact_answer", "version": 1},
"target_field": "a"}] * 2}, "unique"),
({"extra": True}, "unknown field"),
])
def test_invalid_specs_are_refused(pack_dir, change, message):
with pytest.raises(SpecError, match=message):
load_spec(make_spec(pack_dir, **change))
def test_locked_test_role_requires_acknowledgement(pack_dir, tmp_path):
with pytest.raises(SpecError, match="allow_locked_test"):
load_spec(make_spec(pack_dir, dataset={"role": "test"}))
with pytest.raises(SpecError, match="allow_locked_test"):
load_spec(make_spec(pack_dir, dataset={"role": "test", "allow_locked_test": False}))
assert not (tmp_path / "store").exists()
spec = make_spec(pack_dir, dataset={"role": "test", "allow_locked_test": True},
capture=None, profile=False, decoding={"max_new_tokens": 1})
result = run_observation(spec, tmp_path / "store", model_factory=factory())
assert result["inputs"]["dataset"]["locked_test_acknowledged"] is True
source = result["scorecard"]["tasks"]["answers"]["source"]
assert source["role"] == "test" and source["locked_test_acknowledged"] is True
def test_complete_run_scores_captures_profiles_and_is_idempotent(pack_dir, tmp_path):
model = tiny_gpt2()
state = {k: v.clone() for k, v in model.state_dict().items()}
log = []
result = run_observation(make_spec(pack_dir), tmp_path / "store",
model_factory=factory(log, model=model))
assert result["state"] == "complete" and result["attempt"] == 1
card = result["scorecard"]
assert card["status"] == "DESCRIPTIVE" and card["verdict"] is None
task = card["tasks"]["answers"]
assert task["n"] == 3 and [r["example_id"] for r in task["records"]] == ["p3", "p4", "p5"]
assert task["scorable"] + len(task["excluded"]) == 3
assert task["invalid_output"]["n"] == 3 and task["task_success"]["n"] == task["scorable"]
assert all(len(r["completion_sha256"]) == 64 for r in task["records"])
assert task["source"]["revision"] == result["inputs"]["dataset"]["revision"]
capture = result["capture"]
assert capture["state"] == "complete" and capture["shards"] == 3
assert capture["sites"] == ["layer.0", "layer.1"]
assert result["profile"]["counters"]["forwards"] > 0
assert result["inputs"]["model"]["adapter"]["state"] == "none"
assert all(not m._forward_hooks and not m._forward_pre_hooks for m in model.modules())
assert all(torch.equal(state[k], v) for k, v in model.state_dict().items())
assert "generate" not in vars(model)
again = run_observation(make_spec(pack_dir), tmp_path / "store",
model_factory=factory(log, model=model))
assert again["attempt"] == 1 and len(log) == 1
run = ObservationStore(tmp_path / "store").get(result["spec_digest"])
assert [a["state"] for a in run.history()] == ["complete"]
assert [e["state"] for e in run.history()[0]["events"]] == ["pending", "running", "complete"]
class CancelAfter:
def __init__(self, calls):
self.calls, self.seen = calls, 0
def __call__(self):
self.seen += 1
if self.seen >= self.calls:
raise ObservationCancelled("stop requested")
def test_cancelled_run_resumes_reusing_only_identity_matching_steps(pack_dir, tmp_path):
spec = make_spec(pack_dir)
store = tmp_path / "store"
# Call 1 precedes running; calls 2-5 complete "answers"; call 7 stops "aliases".
with pytest.raises(ObservationCancelled):
run_observation(spec, store, model_factory=factory(), check_cancelled=CancelAfter(7))
run = ObservationStore(store).get(spec_digest(load_spec(spec)))
assert [a["state"] for a in run.history()] == ["cancelled"]
first = run.attempt_path(1) / "steps"
assert json.loads((first / "generate-answers/step.json").read_text())["state"] == "complete"
partial = json.loads((first / "generate-aliases/step.json").read_text())
assert partial["state"] == "incomplete" and "stop requested" in partial["reason"]
assert not (run.attempt_path(1) / "result.json").exists()
resumed = run_observation(spec, store, model_factory=factory())
assert resumed["attempt"] == 2
steps = resumed["steps"]
assert steps["generate-answers"]["reused_from"]["attempt"] == 1
assert steps["generate-aliases"]["reused_from"] is None
assert steps["capture"]["reused_from"] is None
assert [a["state"] for a in run.history()] == ["cancelled", "complete"]
fresh = run_observation(spec, tmp_path / "fresh", model_factory=factory())
assert compare_results(resumed, fresh)["equal"] is True
# Same spec, different tokenizer identity: nothing from earlier attempts is reused.
other = run_observation(spec, store, rerun=True,
model_factory=factory(tokenizer=CharTokenizer("local/other")))
assert other["attempt"] == 3
assert all(step["reused_from"] is None for step in other["steps"].values())
assert set(compare_results(other, fresh)["differences"]) == {
"inputs.model", "steps.*.identity_sha256", "capture.identity_sha256"}
# A tampered artifact is never reused.
(run.attempt_path(3) / "steps/generate-answers/generations.json").write_text("{}")
again = run_observation(spec, store, rerun=True,
model_factory=factory(tokenizer=CharTokenizer("local/other")))
assert again["steps"]["generate-answers"]["reused_from"] is None
assert again["steps"]["capture"]["reused_from"]["attempt"] == 3
def test_failed_and_abandoned_attempts_remain_visible(pack_dir, tmp_path):
spec = make_spec(pack_dir, capture=None, profile=False, decoding={"max_new_tokens": 1})
store = ObservationStore(tmp_path / "store")
run = store.open(load_spec(spec))
stale = run.new_attempt()
stale.transition("running")
def broken(_section):
raise RuntimeError("loader exploded")
with pytest.raises(RuntimeError, match="loader exploded"):
run_observation(spec, tmp_path / "store", model_factory=broken)
result = run_observation(spec, tmp_path / "store", model_factory=factory())
history = run.history()
assert [a["state"] for a in history] == ["failed", "failed", "complete"]
assert history[0]["error_type"] == "Abandoned"
assert history[1]["error_type"] == "RuntimeError" and "loader exploded" in history[1]["reason"]
assert result["attempt"] == 3
with pytest.raises(StoreError, match="Invalid attempt transition"):
stale.transition("complete")
def test_capture_budget_failure_is_recorded(pack_dir, tmp_path):
spec = make_spec(pack_dir, capture={"sites": [0], "budget_bytes": 1}, profile=False)
with pytest.raises(ValueError, match="budget"):
run_observation(spec, tmp_path / "store", model_factory=factory())
run = ObservationStore(tmp_path / "store").get(spec_digest(load_spec(spec)))
assert [a["error_type"] for a in run.history()] == ["CaptureRefused"]
def no_generation(model):
def generate(*_args, **_kwargs):
pytest.fail("A refused model must not generate")
model.generate = generate
return model
@pytest.mark.parametrize("modify,message", [
(lambda m: m.transformer.h[0].register_forward_hook(lambda *a: None), "forward_hooks"),
(lambda m: m.transformer.h[1].mlp.register_forward_pre_hook(lambda *a: None),
"forward_pre_hooks"),
(lambda m: m.transformer.h[0].add_module("lora_A", nn.Linear(2, 2)), "unknown"),
(lambda m: setattr(m, "peft_config", {"default": SimpleNamespace(to_dict=dict)}) or setattr(
m, "active_adapters", ["default"]), "peft"),
(lambda m: setattr(m, "_obliteratus_surgery_invalid", True), "surgery"),
])
def test_intervention_state_is_refused_before_generation(pack_dir, tmp_path, modify, message):
model = no_generation(tiny_gpt2())
modify(model)
spec = make_spec(pack_dir, profile=False)
with pytest.raises(InterventionStateRefused, match=message):
run_observation(spec, tmp_path / "store", model_factory=factory(model=model))
run = ObservationStore(tmp_path / "store").get(spec_digest(load_spec(spec)))
[attempt] = run.history()
assert attempt["state"] == "failed" and attempt["error_type"] == "InterventionStateRefused"
assert [e["state"] for e in attempt["events"]] == ["pending", "failed"]
assert not (run.attempt_path(1) / "steps").exists()
def test_global_hooks_and_disabled_adapters(pack_dir, tmp_path):
from obliteratus.experiments import require_unmodified
model = tiny_gpt2()
handle = nn.modules.module.register_module_forward_hook(lambda *a: None)
try:
with pytest.raises(InterventionStateRefused, match="Global"):
require_unmodified(model)
finally:
handle.remove()
model.peft_config = {"default": SimpleNamespace(to_dict=dict)}
model.active_adapters = ["default"]
model.transformer.h[0].disable_adapters = True
assert require_unmodified(model)["disabled"] is True
@pytest.mark.parametrize("changes,model_changes,message", [
({"model": {"dtype": "bfloat16"}}, {}, "dtype"),
({"model": {"revision": "abc123"}}, {}, "revision"),
({"dataset": {"pack_sha256": "0" * 64}}, {}, "Pack sha256"),
({"dataset": {"revision": "sha256:" + "0" * 64}}, {}, "Dataset revision"),
({"capture": {"sites": [5], "budget_bytes": 10}}, {}, "Capture site"),
({}, {"train": True}, "eval mode"),
])
def test_input_mismatches_fail_the_attempt(pack_dir, tmp_path, changes, model_changes, message):
model = tiny_gpt2()
if model_changes.get("train"):
model.train()
spec = make_spec(pack_dir, profile=False, **changes)
with pytest.raises(ValueError, match=message):
run_observation(spec, tmp_path / "store", model_factory=factory(model=model))
run = ObservationStore(tmp_path / "store").get(spec_digest(load_spec(spec)))
assert [a["state"] for a in run.history()] == ["failed"]
def test_store_refuses_mixed_specs_and_concurrent_attempts(pack_dir, tmp_path):
spec = load_spec(make_spec(pack_dir))
store = ObservationStore(tmp_path / "store")
run = store.open(spec)
(run.path / "spec.json").write_text(json.dumps({**spec, "seed": 9}))
with pytest.raises(StoreError, match="different spec"):
store.open(spec)
with pytest.raises(StoreError, match="No observation run"):
store.get("sha256:" + "1" * 64)
other = store.open(load_spec(make_spec(pack_dir, seed=3)))
with other.lock():
with pytest.raises(StoreError, match="running"):
with other.lock():
pass
@pytest.mark.parametrize("text,message", [
("[1]", "JSON object"), ("{bad", "not valid JSON"), ('{"seed": NaN}', "NaN"),
])
def test_spec_files_must_be_standard_json_objects(tmp_path, text, message):
path = tmp_path / "spec.json"
path.write_text(text)
with pytest.raises(SpecError, match=message):
load_spec(path)