mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Extend observation from prompt readouts to token-time-layer traces (F09), with matched-prefix comparison kept separate from free-running generation. - Each forward call is recorded with its phase (full, prefill, decode), new positions, cache history and a prefix digest as decode-step identity; every position is observed exactly once, and masks select calls, prompt/continuation segments or caller labels. - Teacher-forced replay of fixed tokens and free-running greedy decoding are distinct modes; comparing them is refused. Matched-prefix comparison reports per-layer and per-position differences; free running reports the divergence point. - Cached execution is supported on dense CPU Transformers with past_key_values and refused (never silently recomputed) when the model, cache type or cache length doesn't fit. Qualification checks cached against uncached traces within tolerance. - Caller labels such as "thinking" are stored as unverified metadata only. Traces are stored as chunked safetensors bound to the capture identity; hooks are removed on errors and cancellation. - `obliteratus experiments trace` with --mode, --cache and --qualify. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
189 lines
8.4 KiB
Python
189 lines
8.4 KiB
Python
"""Chunked trace manifests bound to capture identity, and the experiments trace entry point."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.analysis.activation_capture import CaptureRefused
|
|
from obliteratus.analysis.temporal_trace import trace_free_running, trace_teacher_forced
|
|
from obliteratus.analysis.trace_store import load_trace, trace_identity, write_trace
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
SEQUENCE = [3, 4, 5, 6, 7, 8, 9]
|
|
|
|
|
|
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=32,
|
|
bos_token_id=1, eos_token_id=2)
|
|
return GPT2LMHeadModel(config).eval()
|
|
|
|
|
|
class ToyTokenizer:
|
|
chat_template = None
|
|
truncation_side = "right"
|
|
special_tokens_map = {"bos_token": "<s>", "eos_token": "</s>"}
|
|
bos_token_id, eos_token_id = 1, 2
|
|
add_bos_token, add_eos_token = True, False
|
|
name_or_path = "local/toy"
|
|
|
|
def get_vocab(self):
|
|
return {"<s>": 1, "</s>": 2, **{chr(97 + i): 3 + i for i in range(26)}}
|
|
|
|
def __call__(self, text, return_tensors="pt", add_special_tokens=True):
|
|
ids = ([1] if add_special_tokens else []) + [3 + ord(c) - 97 for c in text if c.isalpha()]
|
|
return {"input_ids": torch.tensor([ids])}
|
|
|
|
|
|
def sites(model):
|
|
return {f"layer.{i}": block for i, block in enumerate(model.transformer.h)}
|
|
|
|
|
|
@pytest.fixture
|
|
def model():
|
|
return tiny_gpt2()
|
|
|
|
|
|
def write(model, trace, out, **kwargs):
|
|
return write_trace(trace, out, model=model, tokenizer=ToyTokenizer(), layers=sites(model),
|
|
model_id="local/tiny", **kwargs)
|
|
|
|
|
|
def test_trace_round_trips_through_chunks_bound_to_identity(model, tmp_path):
|
|
trace = trace_teacher_forced(model, sites(model), SEQUENCE, 3, cache="cached",
|
|
labels={"thinking": [(4, 7)]})
|
|
manifest = write(model, trace, tmp_path / "t", chunk_tokens=3)
|
|
assert manifest["schema"] == "obliteratus.temporal-trace/1" and manifest["state"] == "complete"
|
|
assert [(c["start"], c["end"]) for c in manifest["chunks"]] == [(0, 3), (3, 6), (6, 7)]
|
|
assert manifest["identity"]["weights"]["sha256"]
|
|
assert manifest["identity"]["readout"]["phase"] == "temporal:teacher-forced:cached"
|
|
assert manifest["labels"]["thinking"]["verified"] is False
|
|
expected = trace_identity(trace, model, ToyTokenizer(), sites(model), model_id="local/tiny")
|
|
loaded = load_trace(tmp_path / "t", expected)
|
|
assert torch.equal(loaded.token_ids, trace.token_ids)
|
|
assert torch.equal(loaded.call_index, trace.call_index) and loaded.calls == trace.calls
|
|
assert all(torch.equal(loaded.values[k], trace.values[k]) for k in trace.values)
|
|
assert loaded.labels == trace.labels and loaded.logits is None
|
|
with torch.no_grad():
|
|
model.transformer.h[0].mlp.c_fc.weight.add_(1.0)
|
|
changed = trace_identity(trace, model, ToyTokenizer(), sites(model), model_id="local/tiny")
|
|
with pytest.raises(CaptureRefused, match="weights") as refused:
|
|
load_trace(tmp_path / "t", changed)
|
|
assert refused.value.field.startswith("weights")
|
|
|
|
|
|
def test_tampered_incomplete_or_mismatched_traces_are_refused(model, tmp_path):
|
|
trace = trace_free_running(model, sites(model), SEQUENCE[:3], 3)
|
|
write(model, trace, tmp_path / "t", chunk_tokens=4)
|
|
with pytest.raises(CaptureRefused, match="not empty"):
|
|
write(model, trace, tmp_path / "t")
|
|
with pytest.raises(CaptureRefused, match="chunk_tokens"):
|
|
write(model, trace, tmp_path / "u", chunk_tokens=0)
|
|
with pytest.raises(CaptureRefused, match="layers differ"):
|
|
write_trace(trace, tmp_path / "v", model=model, tokenizer=ToyTokenizer(),
|
|
layers={"layer.0": model.transformer.h[0]}, model_id="local/tiny")
|
|
manifest_path = tmp_path / "t" / "manifest.json"
|
|
original = manifest_path.read_text()
|
|
chunk = tmp_path / "t" / "chunks" / "00001.safetensors"
|
|
good = chunk.read_bytes()
|
|
chunk.write_bytes(good[:-1] + bytes([good[-1] ^ 1]))
|
|
with pytest.raises(CaptureRefused, match="digest"):
|
|
load_trace(tmp_path / "t")
|
|
chunk.write_bytes(good)
|
|
for edit, match in ((lambda m: m.update(state="running"), "not complete"),
|
|
(lambda m: m.update(schema="other"), "schema"),
|
|
(lambda m: m["identity"].update(inputs=[]), "digest"),
|
|
(lambda m: m["chunks"].pop(), "cover every position"),
|
|
(lambda m: m["chunks"].pop(0), "contiguous"),
|
|
(lambda m: m["chunks"][0].update(file="../manifest.json"), "outside")):
|
|
manifest = json.loads(original)
|
|
edit(manifest)
|
|
manifest_path.write_text(json.dumps(manifest))
|
|
with pytest.raises(CaptureRefused, match=match):
|
|
load_trace(tmp_path / "t")
|
|
manifest_path.write_text("{")
|
|
with pytest.raises(CaptureRefused, match="unreadable"):
|
|
load_trace(tmp_path / "t")
|
|
manifest = json.loads(original)
|
|
manifest["trace"]["token_ids_sha256"] = "0" * 64
|
|
manifest_path.write_text(json.dumps(manifest))
|
|
with pytest.raises(CaptureRefused, match="token ids"):
|
|
load_trace(tmp_path / "t")
|
|
manifest_path.write_text(original)
|
|
expected = json.loads(original)["identity"]
|
|
expected["adapter"] = {"state": "unknown"}
|
|
with pytest.raises(CaptureRefused, match="Adapter state"):
|
|
load_trace(tmp_path / "t", expected)
|
|
|
|
|
|
def test_failed_write_leaves_a_failed_manifest(model, tmp_path, monkeypatch):
|
|
import obliteratus.analysis.trace_store as store
|
|
|
|
def boom(*_args, **_kwargs):
|
|
raise OSError("disk full")
|
|
|
|
monkeypatch.setattr(store, "save_file", boom)
|
|
trace = trace_teacher_forced(model, sites(model), SEQUENCE, 3)
|
|
with pytest.raises(OSError, match="disk full"):
|
|
write(model, trace, tmp_path / "t")
|
|
assert json.loads((tmp_path / "t" / "manifest.json").read_text())["state"] == "failed"
|
|
|
|
|
|
@pytest.fixture
|
|
def cli(model, monkeypatch, capsys):
|
|
from obliteratus.cli import main
|
|
from obliteratus.experiments import inputs
|
|
|
|
monkeypatch.setattr(inputs, "default_model_factory",
|
|
lambda spec: SimpleNamespace(model=model, tokenizer=ToyTokenizer()))
|
|
|
|
def invoke(*argv):
|
|
main(["experiments", "trace", "--model", "local/tiny", "--layer", "0", "--layer", "1",
|
|
*argv])
|
|
return json.loads(capsys.readouterr().out)
|
|
return invoke
|
|
|
|
|
|
def test_trace_entry_point_writes_and_qualifies(cli, tmp_path, model, capsys, monkeypatch):
|
|
out = cli("--prompt", "abc", "--continuation", "de", "--cache", "cached",
|
|
"--label", "thinking:4:6", "--out", str(tmp_path / "t"), "--chunk-tokens", "2")
|
|
assert out["mode"] == "teacher-forced" and out["token_ids"] == [1, 3, 4, 5, 6, 7]
|
|
assert out["prompt_tokens"] == 4 and out["chunks"] == 3
|
|
assert [c["phase"] for c in out["calls"]] == ["prefill", "decode", "decode"]
|
|
assert out["labels"]["thinking"]["spans"] == [[4, 6]]
|
|
assert load_trace(tmp_path / "t").length == 6
|
|
free = cli("--prompt-ids", "3,4,5", "--mode", "free-running", "--max-new-tokens", "2")
|
|
assert free["mode"] == "free-running" and free["tokens"] == 5
|
|
assert [c["phase"] for c in free["calls"]] == ["full", "decode", "decode"]
|
|
qualified = cli("--prompt-ids", "3,4,5", "--continuation-ids", "6,7", "--qualify")
|
|
assert qualified["passed"] and qualified["comparison"]["mode"] == "matched-prefix"
|
|
assert cli("--prompt-ids", "3,4")["tokens"] == 2
|
|
|
|
from obliteratus.analysis import trace_compare
|
|
monkeypatch.setattr(trace_compare, "qualify_cached_execution",
|
|
lambda *a, **k: {"passed": False})
|
|
from obliteratus.cli import main
|
|
with pytest.raises(SystemExit) as failed:
|
|
main(["experiments", "trace", "--model", "m", "--layer", "0", "--prompt-ids", "3,4",
|
|
"--qualify"])
|
|
assert failed.value.code == 1
|
|
|
|
|
|
@pytest.mark.parametrize("argv, message", [
|
|
(["--prompt-ids", "3", "--mode", "free-running", "--qualify"], "cannot be qualified"),
|
|
(["--prompt-ids", "3,x"], "comma-separated"),
|
|
(["--prompt-ids", "3,4", "--label", "thinking:1"], "NAME:START:END"),
|
|
(["--prompt-ids", "3,4", "--layer", "9"], "outside"),
|
|
])
|
|
def test_trace_entry_point_refusals_exit_4(cli, capsys, argv, message):
|
|
with pytest.raises(SystemExit) as refused:
|
|
cli(*argv)
|
|
assert refused.value.code == 4 and message in capsys.readouterr().err
|