"""Activation capture manifests, cache invalidation, cancellation and integrity.""" from __future__ import annotations import json from copy import deepcopy from types import SimpleNamespace import pytest import torch from torch import nn from obliteratus.analysis.activation_capture import ( CaptureRefused, capture_activations, load_capture, preview_capture, ) from obliteratus.analysis.capture_identity import ( adapter_identity, capture_identity, require_dense_cpu, ) 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=16) return GPT2LMHeadModel(config).eval() def tokenizer(**overrides): values = dict(chat_template="{{ bos_token }}{{ messages }}", truncation_side="right", special_tokens_map={"bos_token": ""}, bos_token_id=1, eos_token_id=2, add_bos_token=True, add_eos_token=False, name_or_path="local/tiny", get_vocab=lambda: {"": 1, "": 2, "a": 3}) values.update(overrides) return SimpleNamespace(**values) def batches(): return [ {"input_ids": torch.tensor([[3, 4, 5], [6, 7, 0]]), "attention_mask": torch.tensor([[1, 1, 1], [1, 1, 0]])}, {"input_ids": torch.tensor([[8, 9, 10]]), "attention_mask": torch.tensor([[1, 1, 1]])}, ] def sites(model): return {str(i): block for i, block in enumerate(model.transformer.h)} def run(model, out, tok=None, **kwargs): kwargs = {"model_id": "local/tiny-gpt2", "phase": "prompt", **kwargs} return capture_activations(model, tok or tokenizer(), sites(model), batches(), out, **kwargs) def identity(model, tok=None, data=None, **kwargs): kwargs = {"model_id": "local/tiny-gpt2", "phase": "prompt", **kwargs} return capture_identity(model, tok or tokenizer(), sites(model), data or batches(), **kwargs) def test_complete_capture_round_trips_and_leaves_model_unchanged(tmp_path): model = tiny_gpt2() state = {k: v.clone() for k, v in model.state_dict().items()} probe = batches()[0] with torch.no_grad(): before = model(**probe).logits.clone() manifest = run(model, tmp_path / "cap", dataset=SimpleNamespace(revision="rev-1"), retention_days=7) with torch.no_grad(): after = model(**probe).logits assert torch.equal(before, after) assert all(torch.equal(state[k], v) for k, v in model.state_dict().items()) assert all(not block._forward_hooks and not block._forward_pre_hooks for block in model.transformer.h) assert manifest["state"] == "complete" and manifest["estimate"]["label"] == "estimate" on_disk = sum(p.stat().st_size for p in (tmp_path / "cap" / "raw").iterdir()) assert manifest["measured"]["bytes_written"] == on_disk > 0 assert manifest["measured"]["wall_seconds"] > 0 assert manifest["retention"]["policy"] == "expires" and manifest["retention"]["expires_at"] assert manifest["identity"]["dataset_revision"] == "rev-1" assert manifest["identity"]["adapter"]["state"] == "none" loaded = load_capture(tmp_path / "cap", identity(model, dataset_revision="rev-1")) assert [shard["values"]["0"].shape for shard in loaded.shards] == [(2, 16), (1, 16)] assert loaded.shards[0]["token_positions"].tolist() == [2, 1] with torch.no_grad(): hidden = model(**probe, output_hidden_states=True).hidden_states[1] # block 0 output torch.testing.assert_close(loaded.shards[0]["values"]["0"], hidden[[0, 1], [2, 1]]) def _changed(model, field): tok, kwargs, data = tokenizer(), {}, None if field == "tokenizer.special_tokens": tok = tokenizer(special_tokens_map={"bos_token": ""}) elif field == "tokenizer.chat_template": tok = tokenizer(chat_template="{{ messages }}") elif field == "tokenizer.vocab_sha256": tok = tokenizer(get_vocab=lambda: {"": 1}) elif field == "checkpoint.model_revision": model.config._commit_hash = "abc123" elif field == "checkpoint.model_id": kwargs["model_id"] = "local/other" elif field == "checkpoint.dtype": model = deepcopy(model).to(torch.float64) elif field == "readout.name": kwargs.update(readout_name="decoder_output.v2") elif field == "readout.phase": kwargs["phase"] = "answer" elif field == "dataset_revision": kwargs["dataset_revision"] = "rev-2" elif field == "inputs": data = batches() data[1]["input_ids"] = torch.tensor([[8, 9, 11]]) elif field == "adapter.state": model = deepcopy(model) model.peft_config = {"default": SimpleNamespace(to_dict=lambda: {"r": 4})} model.active_adapters = ["default"] elif field == "sites": return capture_identity(model, tok, {"0": model.transformer.h[0]}, batches(), model_id="local/tiny-gpt2", phase="prompt", dataset_revision="rev-1") kwargs.setdefault("dataset_revision", "rev-1") return identity(model, tok, data, **kwargs) @pytest.mark.parametrize("field", [ "tokenizer.special_tokens", "tokenizer.chat_template", "tokenizer.vocab_sha256", "checkpoint.model_revision", "checkpoint.model_id", "checkpoint.dtype", "readout.name", "readout.phase", "dataset_revision", "inputs", "adapter.state", "sites", ]) def test_any_identity_change_refuses_the_cache_and_names_the_field(tmp_path, field): model = tiny_gpt2() run(model, tmp_path / "cap", dataset_revision="rev-1") with pytest.raises(CaptureRefused, match="differs at") as error: load_capture(tmp_path / "cap", _changed(model, field)) assert error.value.field == field or error.value.field.startswith(field + ".") def test_cancellation_removes_hooks_and_leaves_an_unloadable_partial_capture(tmp_path): model = tiny_gpt2() calls = [] class Cancelled(Exception): pass def check(): calls.append(1) if len(calls) > 6: # after the first batch's shard raise Cancelled("operator stop") with pytest.raises(Cancelled): run(model, tmp_path / "cap", check_cancelled=check) assert all(not block._forward_hooks and not block._forward_pre_hooks for block in model.transformer.h) manifest = json.loads((tmp_path / "cap" / "manifest.json").read_text()) assert manifest["state"] == "cancelled" and "operator stop" in manifest["reason"] assert len(manifest["shards"]) == 1 and manifest["batches_expected"] == 2 with pytest.raises(CaptureRefused, match="cancelled") as error: load_capture(tmp_path / "cap", identity(model)) assert error.value.field == "state" inspected = load_capture(tmp_path / "cap", identity(model), allow_incomplete=True) assert inspected.manifest["state"] == "cancelled" and len(inspected.shards) == 1 def test_corrupted_shard_and_tampered_manifest_are_refused(tmp_path): model = tiny_gpt2() run(model, tmp_path / "cap") shard = tmp_path / "cap" / "raw" / "00001.safetensors" data = bytearray(shard.read_bytes()) data[-1] ^= 0xFF shard.write_bytes(bytes(data)) with pytest.raises(CaptureRefused, match="digest") as error: load_capture(tmp_path / "cap", identity(model)) assert error.value.field == "shards" path = tmp_path / "cap" / "manifest.json" manifest = json.loads(path.read_text()) manifest["identity"]["readout"]["phase"] = "answer" path.write_text(json.dumps(manifest)) with pytest.raises(CaptureRefused, match="digest") as error: load_capture(tmp_path / "cap", identity(model)) assert error.value.field == "identity_sha256" @pytest.mark.parametrize("mutate", [ lambda m: m["shards"][0].update(file="../manifest.json"), lambda m: m["shards"][0].update(file="/etc/hostname"), lambda m: m["shards"][0].update(file=7), lambda m: m.update(shards="raw/00000.safetensors"), lambda m: m.update(identity=["not", "a", "mapping"]), ]) def test_malformed_manifest_cannot_redirect_or_crash_the_loader(tmp_path, mutate): model = tiny_gpt2() run(model, tmp_path / "cap") path = tmp_path / "cap" / "manifest.json" manifest = json.loads(path.read_text()) mutate(manifest) path.write_text(json.dumps(manifest)) with pytest.raises(CaptureRefused) as error: load_capture(tmp_path / "cap", identity(model)) assert error.value.field in {"manifest", "shards"} def test_preview_is_an_estimate_and_over_budget_runs_nothing(tmp_path): model = tiny_gpt2() preview = run(model, tmp_path / "dry", dry_run=True, budget_bytes=10**6) assert preview["label"] == "estimate" and preview["within_budget"] assert not (tmp_path / "dry").exists() assert preview == preview_capture(model, sites(model), batches(), budget_bytes=10**6) with pytest.raises(CaptureRefused, match="exceeds") as error: run(model, tmp_path / "cap", budget_bytes=preview["estimated_bytes"] - 1) assert error.value.field == "budget_bytes" and not (tmp_path / "cap").exists() assert all(not block._forward_hooks for block in model.transformer.h) def test_measured_overrun_fails_the_capture(tmp_path, monkeypatch): import obliteratus.analysis.activation_capture as capture model = tiny_gpt2() monkeypatch.setattr(capture, "_HEADER_BYTES_PER_TENSOR", 0) # An understated hidden size passes the estimate; measured bytes still enforce the budget. budget = preview_capture(model, sites(model), batches(), hidden_size=1)["estimated_bytes"] with pytest.raises(CaptureRefused, match="Measured"): run(model, tmp_path / "cap", budget_bytes=budget, hidden_size=1) manifest = json.loads((tmp_path / "cap" / "manifest.json").read_text()) assert manifest["state"] == "failed" and manifest["measured"]["bytes_written"] > budget def test_unsupported_execution_fails_clearly(): model = tiny_gpt2() with pytest.raises(ValueError, match="eval mode"): require_dense_cpu(deepcopy(model).train()) quantized = deepcopy(model) quantized.config.quantization_config = {"load_in_4bit": True} with pytest.raises(ValueError, match="quantized"): require_dense_cpu(quantized) dispatched = deepcopy(model) dispatched.hf_device_map = {"": "cpu"} with pytest.raises(ValueError, match="dispatched"): require_dense_cpu(dispatched) with torch.device("meta"): meta = nn.Linear(2, 2) with pytest.raises(ValueError, match="meta"): require_dense_cpu(meta) integer = nn.Module() integer.weight = nn.Parameter(torch.zeros(2, dtype=torch.int8), requires_grad=False) with pytest.raises(ValueError, match="integer weights"): require_dense_cpu(integer) def test_adapter_identity_never_reports_unknown_as_none(): plain = nn.Sequential(nn.Linear(2, 2)) assert adapter_identity(plain)["state"] == "none" marked = nn.Module() marked.lora_A = nn.Linear(2, 2) assert adapter_identity(marked)["state"] == "unknown" config = SimpleNamespace(to_dict=lambda: {"r": 8}) peft = nn.Module() peft.peft_config = {"a": config, "b": config} assert adapter_identity(peft)["state"] == "unknown" # active set unobservable peft.active_adapters = lambda: ["a"] result = adapter_identity(peft) assert result["state"] == "peft" and result["disabled"] is False assert [(a["name"], a["active"]) for a in result["adapters"]] == [("a", True), ("b", False)] peft.active_adapter = "b" peft.active_adapters = None assert [a["active"] for a in adapter_identity(peft)["adapters"]] == [False, True] peft.peft_config = {"a": object()} assert adapter_identity(peft)["state"] == "unknown" peft.peft_config = [] assert adapter_identity(peft)["state"] == "unknown" def test_unknown_adapter_state_cannot_be_reused(tmp_path): model = tiny_gpt2() run(model, tmp_path / "cap") current = identity(model) current["adapter"] = {"state": "unknown", "reason": "test"} with pytest.raises(CaptureRefused, match="unknown") as error: load_capture(tmp_path / "cap", current) assert error.value.field == "adapter" def test_invalid_requests_are_refused(tmp_path): model = tiny_gpt2() with pytest.raises(CaptureRefused, match="hidden_size"): preview_capture(nn.Linear(2, 2), {"x": nn.Linear(2, 2)}, batches()) with pytest.raises(CaptureRefused, match="budget"): preview_capture(model, sites(model), batches(), budget_bytes=-1) with pytest.raises(CaptureRefused, match="attention_mask"): preview_capture(model, sites(model), [{"input_ids": torch.ones(1, 2)}]) with pytest.raises(CaptureRefused, match="at least one"): capture_activations(model, tokenizer(), sites(model), [], tmp_path / "e", model_id="m", phase="p") with pytest.raises(CaptureRefused, match="Retention"): run(model, tmp_path / "r", retention_days=0) with pytest.raises(ValueError, match="disagrees"): identity(model, dataset=SimpleNamespace(revision="a"), dataset_revision="b") with pytest.raises(ValueError, match="model id"): identity(model, model_id="") (tmp_path / "full").mkdir() (tmp_path / "full" / "x").write_text("x") with pytest.raises(CaptureRefused, match="not empty"): run(model, tmp_path / "full") with pytest.raises(CaptureRefused, match="unreadable"): load_capture(tmp_path / "missing", identity(model)) (tmp_path / "bad").mkdir() (tmp_path / "bad" / "manifest.json").write_text(json.dumps({"schema": "other"})) with pytest.raises(CaptureRefused, match="schema"): load_capture(tmp_path / "bad", identity(model)) def test_failed_forward_records_failure_not_cancellation(tmp_path): model = tiny_gpt2() def boom(_layer, _output): raise RuntimeError("readout broke") with pytest.raises(RuntimeError, match="readout broke"): run(model, tmp_path / "cap", readout=boom, readout_name="broken.v1") manifest = json.loads((tmp_path / "cap" / "manifest.json").read_text()) assert manifest["state"] == "failed" and manifest["shards"] == [] assert all(not block._forward_hooks and not block._forward_pre_hooks for block in model.transformer.h)