Files
OBLITERATUS/tests/test_weight_fingerprint.py
Joseph MaglyandClaude Opus 5.5 1cb2a7356b test(experiments): assert fingerprint work instead of recording junit properties
record_property warns under the xunit2 junit family CI uses, and the
suite treats warnings as errors. Assert that sampled mode hashes far
fewer elements than full mode, which hashes every element.

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

194 lines
9.2 KiB
Python

"""Weight fingerprints in capture identity, old-capture refusal and capture retention."""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
import json
import time
import pytest
import torch
from torch import nn
from obliteratus.analysis.activation_capture import CaptureRefused, load_capture
from obliteratus.analysis.capture_identity import (
WEIGHT_SAMPLE_ELEMENTS, _sample_indices, digest, weight_fingerprint,
)
from tests.test_activation_capture import identity, run, tiny_gpt2
pytestmark = pytest.mark.cpu
def with_buffer(model):
model.transformer.register_buffer("calibration", torch.arange(8, dtype=torch.float32))
return model
def outside_sample(tensor, sample=WEIGHT_SAMPLE_ELEMENTS):
"""A flat index of ``tensor`` that the sampled fingerprint does not read."""
taken = set(_sample_indices(tensor.numel(), sample, "cpu").tolist())
return next(i for i in range(tensor.numel()) if i not in taken)
def test_unchanged_models_keep_their_fingerprint_across_reloads():
first, second = with_buffer(tiny_gpt2()), with_buffer(tiny_gpt2())
assert weight_fingerprint(first) == weight_fingerprint(second)
reloaded = with_buffer(tiny_gpt2())
with torch.no_grad():
for tensor in reloaded.state_dict().values():
tensor.zero_()
reloaded.load_state_dict(first.state_dict())
for mode in ("sampled", "full"):
assert weight_fingerprint(reloaded, mode=mode) == weight_fingerprint(first, mode=mode)
print_ = weight_fingerprint(first)
assert print_["algorithm"] == "obliteratus.weight-fingerprint/1"
assert print_["mode"] == "sampled" and print_["sample_elements_per_tensor"] == WEIGHT_SAMPLE_ELEMENTS
# Tied lm_head/wte weights are counted once; the extra buffer is included.
assert print_["tensors"] == len(list(first.named_parameters())) + 1
assert weight_fingerprint(first, mode="full")["sample_elements_per_tensor"] is None
@pytest.mark.parametrize("target", ["parameter", "buffer"])
def test_one_element_edit_changes_the_fingerprint(target):
model = with_buffer(tiny_gpt2())
before = weight_fingerprint(model)
tensor = (model.transformer.h[1].mlp.c_fc.weight if target == "parameter"
else model.transformer.calibration)
with torch.no_grad():
tensor.view(-1)[0] += 1.0 # index 0 is always sampled
assert weight_fingerprint(model)["sha256"] != before["sha256"]
def test_full_digest_detects_an_edit_outside_the_sample():
model = tiny_gpt2()
weight = model.transformer.h[0].mlp.c_fc.weight
assert weight.numel() > WEIGHT_SAMPLE_ELEMENTS
sampled, full = weight_fingerprint(model), weight_fingerprint(model, mode="full")
with torch.no_grad():
weight.view(-1)[outside_sample(weight)] += 1.0
assert weight_fingerprint(model) == sampled # documented blind spot of sampled mode
assert weight_fingerprint(model, mode="full")["sha256"] != full["sha256"]
def test_fingerprint_inputs_are_validated_and_layouts_are_stable():
model = tiny_gpt2()
with pytest.raises(ValueError, match="mode"):
weight_fingerprint(model, mode="fast")
for bad in (0, True, 1.5):
with pytest.raises(ValueError, match="sample size"):
weight_fingerprint(model, sample_elements=bad)
with torch.device("meta"):
meta = nn.Linear(4, 4)
with pytest.raises(ValueError, match="meta"):
weight_fingerprint(meta)
# A non-contiguous parameter fingerprints like its contiguous copy with the same layout.
left, right = nn.Linear(300, 2), nn.Linear(300, 2)
right.load_state_dict(left.state_dict())
right.weight = nn.Parameter(left.weight.detach().t().contiguous().t())
assert not right.weight.is_contiguous()
assert weight_fingerprint(left) == weight_fingerprint(right)
one = weight_fingerprint(left, sample_elements=1)
assert one["elements_hashed"] == 2 # one sampled element of the weight and of the bias
def test_fingerprint_cost_is_reported(capsys):
from transformers import GPT2Config, GPT2LMHeadModel
torch.manual_seed(0)
model = GPT2LMHeadModel(GPT2Config(n_layer=4, n_embd=256, n_head=4, vocab_size=8000,
n_positions=128)).eval()
parameters = sum(p.numel() for p in model.parameters())
timings = {}
for mode in ("sampled", "full"):
started = time.perf_counter()
result = weight_fingerprint(model, mode=mode)
timings[mode] = (time.perf_counter() - started, result["elements_hashed"])
# Sampled mode bounds the work; full mode reads every parameter and buffer element.
assert timings["sampled"][1] < timings["full"][1] // 100
total = sum(t.numel() for t in {id(t): t for t in (*model.parameters(), *model.buffers())}.values())
assert timings["full"][1] == total
with capsys.disabled():
print(f"\nweight fingerprint on {parameters} parameters: " + ", ".join(
f"{mode} {seconds * 1000:.1f} ms ({hashed} elements)"
for mode, (seconds, hashed) in timings.items()))
@pytest.mark.parametrize("target", ["parameter", "buffer"])
def test_in_place_edits_refuse_stored_captures(tmp_path, target):
model = with_buffer(tiny_gpt2())
run(model, tmp_path / "cap")
load_capture(tmp_path / "cap", identity(model))
tensor = model.transformer.h[0].attn.c_attn.bias if target == "parameter" \
else model.transformer.calibration
with torch.no_grad():
tensor.view(-1)[0] -= 0.25
with pytest.raises(CaptureRefused, match="differs at weights.sha256") as error:
load_capture(tmp_path / "cap", identity(model))
assert error.value.field == "weights.sha256"
def _rewrite_manifest(path, change):
manifest = json.loads((path / "manifest.json").read_text())
change(manifest)
manifest["identity_sha256"] = digest(manifest["identity"])
(path / "manifest.json").write_text(json.dumps(manifest))
def test_captures_without_a_fingerprint_are_refused_with_a_reason(tmp_path):
model = tiny_gpt2()
run(model, tmp_path / "cap")
_rewrite_manifest(tmp_path / "cap", lambda m: m["identity"].pop("weights"))
with pytest.raises(CaptureRefused, match="predates weight fingerprint; capture again") as error:
load_capture(tmp_path / "cap", identity(model))
assert error.value.field == "weights"
# Integrity-only loads that pass the stored identity back (as result comparison does) still work.
stored = json.loads((tmp_path / "cap" / "manifest.json").read_text())["identity"]
assert len(load_capture(tmp_path / "cap", stored).shards) == 2
# A capture identity built in full mode never matches a sampled one.
run(model, tmp_path / "fresh")
with pytest.raises(CaptureRefused, match="differs at weights"):
load_capture(tmp_path / "fresh", identity(model, weight_digest="full"))
def test_expired_captures_are_refused_unless_inspection_is_requested(tmp_path):
model = tiny_gpt2()
manifest = run(model, tmp_path / "cap", retention_days=1)
created = datetime.fromisoformat(manifest["retention"]["created_at"])
current = identity(model)
load_capture(tmp_path / "cap", current, clock=lambda: created + timedelta(hours=23))
later = created + timedelta(days=2)
with pytest.raises(CaptureRefused, match="retention.expires_at") as error:
load_capture(tmp_path / "cap", current, clock=lambda: later)
assert error.value.field == "retention.expires_at" and "allow_expired" in str(error.value)
# Another UTC offset for the same instant compares correctly.
offset = (created + timedelta(days=1)).astimezone(timezone(timedelta(hours=-5)))
with pytest.raises(CaptureRefused, match="expired"):
load_capture(tmp_path / "cap", current, clock=lambda: offset)
inspected = load_capture(tmp_path / "cap", current, allow_expired=True, clock=lambda: later)
assert len(inspected.shards) == 2
with pytest.raises(ValueError, match="timezone-aware"):
load_capture(tmp_path / "cap", current, clock=lambda: later.replace(tzinfo=None))
# The default clock is real UTC time; a capture that expired long ago is refused.
_rewrite_manifest(tmp_path / "cap", lambda m: m["retention"].update(
expires_at="2000-01-01T00:00:00+00:00"))
with pytest.raises(CaptureRefused, match="expired at 2000"):
load_capture(tmp_path / "cap", current)
def test_keep_until_deleted_captures_never_expire_and_bad_retention_is_refused(tmp_path):
model = tiny_gpt2()
manifest = run(model, tmp_path / "cap")
assert manifest["retention"]["expires_at"] is None
far = datetime(9999, 1, 1, tzinfo=timezone.utc)
load_capture(tmp_path / "cap", identity(model), clock=lambda: far)
for value, message in (("2000-01-01T00:00:00", "no timezone"), ("soon", "ISO-8601"),
(5, "ISO-8601")):
_rewrite_manifest(tmp_path / "cap", lambda m, v=value: m["retention"].update(expires_at=v))
with pytest.raises(CaptureRefused, match=message) as error:
load_capture(tmp_path / "cap", identity(model))
assert error.value.field == "retention.expires_at"
_rewrite_manifest(tmp_path / "cap", lambda m: m.pop("retention"))
with pytest.raises(CaptureRefused, match="no retention record") as error:
load_capture(tmp_path / "cap", identity(model))
assert error.value.field == "retention"