Files
OBLITERATUS/obliteratus/analysis/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

189 lines
8.7 KiB
Python

"""Process-scoped, exact reuse cache with avoided-work and amortization accounting.
Entries are keyed by the digest of a full ``ReuseIdentity``. A lookup serves
an entry only on an exact key match; values are deep-copied on store and on
serve, so a consumer can never mutate a cached artifact. When the key misses,
entries of the same kind and site are compared field by field and the
reasons (including fold contamination) are recorded. Identities with an
unobservable field are computed fresh and never stored.
All reuse here is exact: a hit returns the bytes the same computation
produced earlier. No approximate sketch is stored, so no error budget applies;
incremental statistics live in ``incremental_stats`` with their own tolerance.
"""
from __future__ import annotations
import dataclasses
import time
from collections import OrderedDict
import torch
from obliteratus.analysis.reuse_identity import FIELDS, ReuseIdentity, admit, compare
SCHEMA = "obliteratus.reuse_report"
SCHEMA_VERSION = 1
MAX_EVENTS = 200
_LABEL = {"hits": "hit", "misses": "miss", "refused": "refused", "not_reusable": "not_reusable"}
OPS_SCOPE = ("estimated floating-point operations declared by the consumer for one "
"computation (order of magnitude, not measured)")
def clone(value):
"""Deep copy of tensors nested in dicts, lists, tuples and dataclasses."""
if isinstance(value, torch.Tensor):
return value.detach().clone()
if isinstance(value, dict):
return {k: clone(v) for k, v in value.items()}
if isinstance(value, (list, tuple)):
return type(value)(clone(v) for v in value)
if dataclasses.is_dataclass(value) and not isinstance(value, type):
return dataclasses.replace(value, **{f.name: clone(getattr(value, f.name))
for f in dataclasses.fields(value) if f.init})
return value
def nbytes(value) -> int:
"""Logical element bytes of tensors nested in ``value``."""
if isinstance(value, torch.Tensor):
return value.numel() * value.element_size()
if isinstance(value, dict):
return sum(nbytes(v) for v in value.values())
if isinstance(value, (list, tuple)):
return sum(nbytes(v) for v in value)
if dataclasses.is_dataclass(value) and not isinstance(value, type):
return sum(nbytes(getattr(value, f.name)) for f in dataclasses.fields(value))
return 0
@dataclasses.dataclass
class _Entry:
identity: ReuseIdentity
value: object
seconds: float
ops: int
input_bytes: int
stored_bytes: int
hits: int = 0
class ReuseCache:
"""Bounded in-memory store; ``max_bytes`` evicts least recently used entries."""
def __init__(self, *, max_bytes: int | None = None, clock=time.perf_counter):
if max_bytes is not None and (isinstance(max_bytes, bool) or not isinstance(
max_bytes, int) or max_bytes < 0):
raise ValueError("max_bytes must be a non-negative integer or None")
self.max_bytes = max_bytes
self.clock = clock
self._entries: OrderedDict[str, _Entry] = OrderedDict()
self.events: list[dict] = []
self.counts = dict.fromkeys(("lookups", "hits", "misses", "refused", "not_reusable",
"evicted"), 0)
self.avoided = {"ops": 0, "input_bytes": 0, "compute_seconds": 0.0}
self.setup = {"compute_seconds": 0.0, "overhead_seconds": 0.0, "stored_bytes": 0}
def __len__(self):
return len(self._entries)
def add_overhead(self, seconds: float) -> None:
"""Charge identity/digest time spent by a consumer to the reuse setup cost."""
self.setup["overhead_seconds"] += seconds
def _event(self, identity, outcome, **detail):
self.counts[outcome] += 1
if len(self.events) < MAX_EVENTS:
self.events.append({"kind": identity.kind, "site": identity.site,
"fold": identity.fold, "outcome": _LABEL[outcome], **detail})
def _candidates(self, identity):
return [e for e in self._entries.values()
if e.identity.kind == identity.kind and e.identity.site == identity.site]
def fetch(self, identity: ReuseIdentity, compute, *, ops: int = 0, input_bytes: int = 0):
"""Serve an exact match or run ``compute()``; returns ``(value, outcome)``."""
start = self.clock()
self.counts["lookups"] += 1
unobservable = identity.unobservable()
if unobservable:
self._event(identity, "not_reusable", reason="unobservable fields: "
+ ", ".join(unobservable))
self.setup["overhead_seconds"] += self.clock() - start
return compute(), "not_reusable"
entry = self._entries.get(identity.key)
if entry is not None:
matched = admit(entry.identity, identity)
self._entries.move_to_end(identity.key)
entry.hits += 1
value = clone(entry.value)
self.avoided["ops"] += entry.ops
self.avoided["input_bytes"] += entry.input_bytes
self.avoided["compute_seconds"] += entry.seconds
self._event(identity, "hits", validity={
"matched_fields": matched, "fold": f"stored fold {entry.identity.fold!r} equals "
"the requested fold",
"exact": "bitwise copy of the stored result of the same computation"})
self.setup["overhead_seconds"] += self.clock() - start
return value, "hit"
reasons = [compare(e.identity, identity) for e in self._candidates(identity)]
closest = min(reasons, key=len) if reasons else []
if any(r["contamination"] for rs in reasons for r in rs):
closest = next(rs for rs in reasons if any(r["contamination"] for r in rs))
self.setup["overhead_seconds"] += self.clock() - start
began = self.clock()
value = compute()
seconds = self.clock() - began
self.setup["compute_seconds"] += seconds
outcome = "refused" if closest else "misses"
self._event(identity, outcome, reasons=closest or [{"field": None, "reason":
"no stored entry of this kind/site",
"contamination": False}])
if outcome == "refused":
self.counts["misses"] += 1
self._store(identity, value, seconds, ops, input_bytes)
return value, "refused" if closest else "miss"
def _store(self, identity, value, seconds, ops, input_bytes):
stored = clone(value)
size = nbytes(stored)
if self.max_bytes is not None and size > self.max_bytes:
return
self._entries[identity.key] = _Entry(identity, stored, seconds, int(ops),
int(input_bytes), size)
self.setup["stored_bytes"] += size
while self.max_bytes is not None and sum(
e.stored_bytes for e in self._entries.values()) > self.max_bytes:
self._entries.popitem(last=False)
self.counts["evicted"] += 1
def report(self) -> dict:
"""Hits, misses, refusals, avoided work and amortization (JSON-ready)."""
lookups = self.counts["lookups"]
setup = self.setup["compute_seconds"] + self.setup["overhead_seconds"]
return {
"schema": SCHEMA, "schema_version": SCHEMA_VERSION,
"identity_fields": list(FIELDS), **self.counts,
"hit_rate": self.counts["hits"] / lookups if lookups else None,
"entries": len(self._entries),
"resident_bytes": sum(e.stored_bytes for e in self._entries.values()),
"avoided": {"estimated_ops": self.avoided["ops"], "ops_scope": OPS_SCOPE,
"input_bytes": self.avoided["input_bytes"],
"recorded_compute_seconds": self.avoided["compute_seconds"]},
"setup": {"compute_seconds": self.setup["compute_seconds"],
"overhead_seconds": self.setup["overhead_seconds"],
"stored_bytes": self.setup["stored_bytes"]},
"amortization": {
"setup_seconds": setup, "uses": lookups,
"setup_seconds_per_use": setup / lookups if lookups else None,
"net_seconds_saved": self.avoided["compute_seconds"]
- self.setup["overhead_seconds"],
"note": "setup (cold computation, identity hashing and copies) is paid once "
"and spread over later hits; it does not disappear"},
"approximation": {"sketches": [], "note": "all cached results are exact"},
"events": list(self.events), "events_truncated": lookups > len(self.events),
}
__all__ = ["ReuseCache", "clone", "nbytes"]