Files
OBLITERATUS/tests/test_reuse_cache.py
Joseph MaglyandClaude Opus 5.5 4c67cd3da9 feat(analysis): safe reuse of activation statistics, fits and nulls
Add an opt-in, in-memory reuse cache (O04) for work repeated across
analyses: per-layer and per-fold extraction fits, candidate-discovery
nulls and labeled row selections, plus incremental pooled moments.

- Artifacts are keyed by kind, algorithm version, site, partition/role,
  fold, normalization, tokenizer, model, adapter, weight fingerprint,
  dataset revision, parameters and a data digest. Missing or unknown
  fields never match.
- A statistic fitted on all rows, another fold or held-out rows is never
  served inside a train or held-out fold (contamination refused); other
  mismatches are refused with per-field reasons.
- Exact results only; incremental moments carry a declared error budget
  and a distinct algorithm name. Reports cover hits, misses, avoided work
  and setup cost amortized over hits.
- extract_directions and discover_candidates take reuse=; outputs are
  bit-identical to fresh computation and defaults are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:21:01 -05:00

261 lines
11 KiB
Python

"""O04 reuse cache: identity refusal, exactness, accounting and incremental statistics."""
from __future__ import annotations
import json
from dataclasses import dataclass, replace
import pytest
import torch
from obliteratus.analysis.incremental_stats import EXACT, INCREMENTAL, PooledMoments
from obliteratus.analysis.reuse_cache import ReuseCache, clone, nbytes
from obliteratus.analysis.reuse_identity import (
FIELDS, ReuseContamination, ReuseIdentity, ReuseRefused, ReuseScope, admit, compare,
content_digest, fold_scope,
)
pytestmark = pytest.mark.cpu
def identity(**changes):
base = {name: f"{name}-v1" for name in FIELDS}
base.update(fold="all")
return ReuseIdentity(**{**base, **changes})
class Clock:
def __init__(self):
self.now = 0.0
def __call__(self):
self.now += 1.0
return self.now
def test_fold_scope_and_content_digest():
assert fold_scope() == "all"
assert fold_scope(1, 5) == "train:1/5"
assert fold_scope(0, 3, "heldout") == "heldout:0/3"
with pytest.raises(ValueError):
fold_scope(5, 5)
with pytest.raises(ValueError):
fold_scope(0, 2, "test")
x = torch.arange(6.0).reshape(2, 3)
assert content_digest(x) == content_digest(x.clone())
assert content_digest(x) != content_digest(x.double())
assert content_digest(x) != content_digest(x.reshape(3, 2))
assert content_digest(torch.empty(0)) != content_digest([])
@pytest.mark.parametrize("field", [f for f in FIELDS if f != "fold"])
def test_every_identity_field_refuses_reuse(field):
stored, requested = identity(), identity(**{field: f"{field}-v2"})
reasons = compare(stored, requested)
assert [r["field"] for r in reasons] == [field]
assert not reasons[0]["contamination"]
with pytest.raises(ReuseRefused) as error:
admit(stored, requested)
assert not isinstance(error.value, ReuseContamination)
cache = ReuseCache()
cache.fetch(stored, lambda: torch.ones(2))
value, outcome = cache.fetch(requested, lambda: torch.zeros(2))
assert torch.equal(value, torch.zeros(2))
event = cache.report()["events"][-1]
if field in ("kind", "site"):
# Entries are compared only within one kind and site: a different artifact, not a refusal.
assert outcome == event["outcome"] == "miss" and event["reasons"][0]["field"] is None
else:
assert outcome == event["outcome"] == "refused"
assert event["reasons"][0]["field"] == field
@pytest.mark.parametrize("stored_fold,requested_fold", [
("all", "train:0/5"), ("all", "heldout:0/5"), ("train:1/5", "train:0/5"),
("heldout:0/5", "train:0/5")])
def test_cross_fold_and_all_data_statistics_are_contaminated(stored_fold, requested_fold):
stored, requested = identity(fold=stored_fold), identity(fold=requested_fold)
with pytest.raises(ReuseContamination, match="leak"):
admit(stored, requested)
cache = ReuseCache()
cache.fetch(stored, lambda: torch.ones(1))
value, outcome = cache.fetch(requested, lambda: torch.full((1,), 7.0))
assert outcome == "refused" and value.item() == 7.0
assert cache.report()["events"][-1]["reasons"][0]["contamination"] is True
def test_fold_statistic_cannot_serve_all_rows_but_is_not_contamination():
reasons = compare(identity(fold="train:0/5"), identity(fold="all"))
assert reasons == [{"field": "fold", "contamination": False,
"reason": "stored fold differs from the requested fold"}]
@pytest.mark.parametrize("field", ["tokenizer", "weights", "adapter", "dataset_revision"])
def test_unknown_fields_are_never_reusable(field):
unknown = identity(**{field: "unknown"})
assert unknown.unobservable() == [field]
with pytest.raises(ReuseRefused):
admit(unknown, unknown)
cache = ReuseCache()
calls = []
for _ in range(2):
_, outcome = cache.fetch(unknown, lambda: calls.append(1) or torch.ones(1))
assert outcome == "not_reusable"
assert len(calls) == 2 and len(cache) == 0
assert cache.report()["not_reusable"] == 2
def test_exact_hit_is_bitwise_and_isolated_from_mutation():
cache = ReuseCache(clock=Clock())
source = torch.randn(4, 3, generator=torch.Generator().manual_seed(1))
first, outcome = cache.fetch(identity(), lambda: {"m": source.clone(), "n": [1, (2,)]},
ops=100, input_bytes=48)
assert outcome == "miss"
first["m"].zero_()
second, outcome = cache.fetch(identity(), lambda: pytest.fail("recomputed"))
assert outcome == "hit" and torch.equal(second["m"], source)
assert second["n"] == [1, (2,)]
report = cache.report()
assert (report["lookups"], report["hits"], report["misses"]) == (2, 1, 1)
assert report["avoided"]["estimated_ops"] == 100
assert report["avoided"]["input_bytes"] == 48
assert report["avoided"]["recorded_compute_seconds"] == 1.0
assert report["setup"]["compute_seconds"] == 1.0
assert report["setup"]["overhead_seconds"] > 0
amortization = report["amortization"]
assert amortization["setup_seconds"] == pytest.approx(
report["setup"]["compute_seconds"] + report["setup"]["overhead_seconds"])
assert amortization["uses"] == 2
assert "does not disappear" in amortization["note"]
hit = report["events"][1]
assert hit["outcome"] == "hit" and hit["validity"]["matched_fields"] == list(FIELDS)
assert report["approximation"]["sketches"] == []
json.dumps(report, allow_nan=False)
def test_eviction_bound_and_oversized_entries():
with pytest.raises(ValueError):
ReuseCache(max_bytes=-1)
cache = ReuseCache(max_bytes=64)
cache.fetch(identity(data="a"), lambda: torch.zeros(8)) # 32 bytes
cache.fetch(identity(data="b"), lambda: torch.zeros(8))
cache.fetch(identity(data="c"), lambda: torch.zeros(8))
assert len(cache) == 2 and cache.report()["evicted"] == 1
cache.fetch(identity(data="big"), lambda: torch.zeros(100))
assert len(cache) == 2 and cache.report()["resident_bytes"] == 64
@dataclass
class Box:
tensor: torch.Tensor
label: str
def test_clone_and_nbytes_cover_dataclasses():
box = Box(torch.ones(3), "x")
copied = clone(box)
copied.tensor.zero_()
assert box.tensor.sum() == 3 and nbytes(box) == 12 and nbytes(None) == 0
def scope(**changes):
capture = {"checkpoint": {"model_id": "m"}, "tokenizer": {"name": "t"},
"adapter": {"state": "none"}, "readout": {"name": "decoder_output",
"phase": "prompt"},
"dataset_revision": "rev1", "weights": {"mode": "sampled", "sha256": "w" * 64}}
capture.update(changes)
return ReuseScope.from_capture_identity(ReuseCache(), capture)
def test_scope_splits_capture_identity_into_fields():
base = scope()
assert base.weights == "sampled:" + "w" * 64 and base.readout == "decoder_output/prompt"
for field, change in (("tokenizer", {"tokenizer": {"name": "u"}}),
("model", {"checkpoint": {"model_id": "n"}}),
("adapter", {"adapter": {"state": "peft", "adapters": []}}),
("weights", {"weights": {"mode": "sampled", "sha256": "v" * 64}}),
("dataset_revision", {"dataset_revision": "rev2"})):
assert getattr(scope(**change), field) != getattr(base, field)
assert scope(adapter={"state": "unknown"}).adapter == "unknown"
assert scope(weights=None).weights == "unknown"
built = base.identity(kind="k", algorithm="a/1", site="layer:3", partition="p",
fold="all", normalization="none", parameters={"x": 1}, data="d")
assert built.site == "decoder_output/prompt:layer:3" and not built.unobservable()
@dataclass
class Labeled:
manifest: dict
def test_scope_from_labeled_binds_combined_capture_digest():
from obliteratus.analysis.capture_identity import digest
capture = {"checkpoint": {"model_id": "m"}, "tokenizer": {}, "adapter": {"state": "none"},
"readout": {"name": "r", "phase": "p"}, "weights": {"mode": "full",
"sha256": "a" * 64}}
labeled = Labeled({"capture_identity_sha256": digest(capture), "dataset_revision": "r1"})
combined = ReuseScope.from_labeled(ReuseCache(), labeled)
assert combined.tokenizer == combined.weights == "capture:" + digest(capture)
full = ReuseScope.from_labeled(ReuseCache(), labeled, capture)
assert full.dataset_revision == "r1" and full.weights == "full:" + "a" * 64
with pytest.raises(ValueError, match="does not match"):
ReuseScope.from_labeled(ReuseCache(), labeled, {**capture, "tokenizer": {"x": 1}})
def rows(seed, n, d=5, offset=0.0):
return torch.randn(n, d, generator=torch.Generator().manual_seed(seed),
dtype=torch.float64) * 3 + offset
def test_incremental_moments_agree_with_full_recomputation():
scope_id = identity(algorithm="moments")
blocks = [rows(1, 30, offset=100.0), rows(2, 7, offset=-50.0), rows(3, 64)]
moments = PooledMoments.from_rows(blocks[0], scope_id)
assert moments.exact and moments.identity.algorithm.endswith(EXACT)
for block in blocks[1:]:
moments = moments.append(block)
full = PooledMoments.from_rows(torch.cat(blocks), scope_id)
agreement = moments.agreement(full)
assert agreement["within_budget"] and not agreement["exact"]
assert moments.identity.algorithm.endswith(INCREMENTAL)
assert moments.identity.key != full.identity.key
assert torch.allclose(moments.covariance(), torch.cov(torch.cat(blocks).T), atol=1e-9)
assert torch.allclose(moments.mean, torch.cat(blocks).mean(0), atol=1e-12)
with pytest.raises(ValueError):
moments.agreement(PooledMoments.from_rows(blocks[0], scope_id))
def test_incremental_moments_refuse_unsafe_merges():
scope_id = identity()
base = PooledMoments.from_rows(rows(1, 4), scope_id, row_keys=["a", "b", "c", "d"])
with pytest.raises(ReuseRefused, match="overlapping"):
base.append(rows(2, 2), row_keys=["d", "e"])
with pytest.raises(ReuseRefused, match="one side"):
base.append(rows(2, 2))
with pytest.raises(ReuseRefused, match="twice"):
PooledMoments.from_rows(rows(1, 4), scope_id).append(rows(1, 4))
with pytest.raises(ReuseRefused, match="widths"):
PooledMoments.from_rows(rows(1, 4), scope_id).append(rows(2, 4, d=3))
with pytest.raises(ReuseRefused, match="site"):
base.merge(PooledMoments.from_rows(rows(3, 2), replace(scope_id, site="other"),
["x", "y"]))
train = PooledMoments.from_rows(rows(1, 4), identity(fold="train:0/5"))
with pytest.raises(ReuseContamination):
train.merge(PooledMoments.from_rows(rows(2, 4), identity(fold="heldout:0/5")))
with pytest.raises(ValueError):
PooledMoments.from_rows(torch.empty(0, 3), scope_id)
with pytest.raises(ValueError):
PooledMoments.from_rows(rows(1, 3), scope_id, row_keys=["a"])
with pytest.raises(ValueError):
PooledMoments.from_rows(rows(1, 1), scope_id).covariance()
merged = base.append(rows(4, 2), row_keys=["e", "f"])
assert merged.count == 6 and merged.row_keys == frozenset("abcdef")
def test_float32_rows_are_accumulated_in_float64():
x = torch.randn(10, 4, generator=torch.Generator().manual_seed(9))
moments = PooledMoments.from_rows(x[:5], identity()).append(x[5:])
assert moments.sum.dtype == torch.float64
assert moments.agreement(PooledMoments.from_rows(x, identity()))["within_budget"]