Files
OBLITERATUS/obliteratus/analysis/work_counters.py
Joseph MaglyandClaude Opus 5.5 bbb0177915 feat(analysis): fill baseline profile work counters
Baseline profiles reported unique examples, token presentations,
forwards, generated tokens and tensor bytes as null. Count them from
load completion through cleanup with pass-through observers: a forward
pre-hook on the loaded model and instance-level wrappers on generate and
activation collection, all removed on exit and after failures.

Counters are recorded per stage and totalled. A counter that cannot be
observed stays null with a reason (stages before load, calls without an
attention mask or input ids, batched generation, forwards outside
activation collection, or embedding calls that bypass the watched
model). Changes are additive within profile schema version 1; default
baseline artifacts are unchanged, profiled or not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:35:46 -05:00

191 lines
8.2 KiB
Python

"""Observe baseline model work without changing model inputs, outputs or weights.
Counts are taken at the loaded top-level model boundary with a forward pre-hook
that returns ``None`` (inputs pass through unchanged), an instance-level
``generate`` pass-through and an instance-level activation-collection
pass-through. Every installed observer is removed when observation ends,
including on error. Token identities are reduced to in-memory digests used only
for distinct counting; neither tokens nor digests are written to the profile.
"""
from contextlib import contextmanager
import hashlib
KEYS = ("unique_examples", "token_presentations", "forwards", "generated_tokens",
"tensor_bytes")
UNOBSERVED = "model work observer not installed during this stage (no loaded model yet)"
NOT_TORCH = "loaded model is not a torch module; no observer installed"
def tensor_bytes(value):
"""Logical element bytes of tensors nested in dicts/lists/tuples."""
if hasattr(value, "element_size") and hasattr(value, "numel"):
return value.numel() * value.element_size()
if isinstance(value, dict):
value = value.values()
if isinstance(value, (list, tuple, type({}.values()))):
return sum(tensor_bytes(item) for item in value)
return 0
def _rows(input_ids, mask):
ids = input_ids.detach().cpu()
keep = mask.detach().cpu().bool() if mask is not None else None
for index in range(ids.shape[0]):
row = ids[index][keep[index]] if keep is not None else ids[index]
yield hashlib.sha256(row.numpy().tobytes()).digest()
class _Stage:
def __init__(self):
self.forwards = self.embedding_forwards = self.tokens = 0
self.outside_collection = self.generated = self.bytes = 0
self.digests = set()
self.reasons = {}
def unknown(self, key, reason):
self.reasons.setdefault(key, reason)
class WorkCounters:
"""Per-stage and observed-scope totals for one baseline invocation."""
def __init__(self):
self.rows = []
self.current = None
self.observing = False
self.never = UNOBSERVED
self._generating = 0
self._collecting = 0
def begin(self):
self.current = _Stage() if self.observing else None
def end(self):
stage, self.current = self.current, None
if stage is None:
return dict.fromkeys(KEYS), dict.fromkeys(KEYS, self.never)
if stage.embedding_forwards != stage.forwards:
for key in KEYS:
stage.unknown(key, "input embedding calls differ from top-level forwards; "
"model work bypassed the observed module")
if stage.outside_collection:
stage.unknown("tensor_bytes", "forwards outside activation collection; "
"their tensor materialization is not instrumented")
values = {"unique_examples": len(stage.digests), "token_presentations": stage.tokens,
"forwards": stage.forwards, "generated_tokens": stage.generated,
"tensor_bytes": stage.bytes}
self.rows.append(stage)
counters = {key: None if key in stage.reasons else values[key] for key in KEYS}
return counters, {key: stage.reasons.get(key) for key in KEYS}
def totals(self):
if not self.rows:
return dict.fromkeys(KEYS), dict.fromkeys(KEYS, self.never)
counters, reasons = {}, {}
for key in KEYS:
missing = next((row.reasons[key] for row in self.rows if key in row.reasons), None)
reasons[key] = None if missing is None else f"an observed stage is unknown: {missing}"
counters["unique_examples"] = len(set().union(*(row.digests for row in self.rows)))
for key, attr in (("token_presentations", "tokens"), ("forwards", "forwards"),
("generated_tokens", "generated"), ("tensor_bytes", "bytes")):
counters[key] = sum(getattr(row, attr) for row in self.rows)
return {key: None if reasons[key] else counters[key] for key in KEYS}, reasons
def _pre_forward(self, module, args, kwargs):
stage = self.current
if stage is None:
return None
stage.forwards += 1
if not self._collecting:
stage.outside_collection += 1
if self._generating:
stage.unknown("token_presentations", "cached generation decode steps do not "
"expose per-step new-token counts")
return None
input_ids = kwargs.get("input_ids", args[0] if args else None)
mask = kwargs.get("attention_mask")
if input_ids is None:
stage.unknown("unique_examples", "forward without input_ids")
stage.unknown("token_presentations", "forward without input_ids")
return None
if mask is None:
stage.unknown("token_presentations", "forward without attention_mask")
else:
stage.tokens += int(mask.detach().bool().sum().item())
stage.digests.update(_rows(input_ids, mask))
return None
def _embedding_forward(self, module, args):
if self.current is not None:
self.current.embedding_forwards += 1
def _generate(self, original):
def generate(*args, **kwargs):
stage = self.current
self._generating += 1
try:
output = original(*args, **kwargs)
finally:
self._generating -= 1
if stage is not None:
input_ids = kwargs.get("input_ids", args[0] if args else None)
sequences = getattr(output, "sequences", output)
if input_ids is None or input_ids.shape[0] != 1:
stage.unknown("generated_tokens", "batched or embedding-only generation "
"has unobserved per-row stop positions")
stage.unknown("unique_examples", "batched or embedding-only generation")
else:
stage.generated += int(sequences.shape[-1] - input_ids.shape[-1])
stage.digests.update(_rows(input_ids, kwargs.get("attention_mask")))
return output
return generate
def _collect(self, original):
def collect(layer_modules, prompts, label, **kwargs):
stage = self.current
owner = getattr(original, "__self__", None)
readouts = getattr(owner, "_validation_readouts", {})
before = readouts.get(label)
self._collecting += 1
try:
activations = original(layer_modules, prompts, label, **kwargs)
finally:
self._collecting -= 1
if stage is not None:
stage.bytes += tensor_bytes(activations)
after = readouts.get(label)
if after is not None and after is not before:
stage.bytes += tensor_bytes(after)
return activations
return collect
@contextmanager
def observe(self, model, pipeline):
"""Install pass-through observers; always remove them on exit."""
import torch
if not isinstance(model, torch.nn.Module):
self.never = NOT_TORCH
yield self
return
handles, patched = [], []
try:
handles.append(model.register_forward_pre_hook(self._pre_forward, with_kwargs=True))
handles.append(model.get_input_embeddings().register_forward_pre_hook(
self._embedding_forward))
for owner, name, wrap in ((model, "generate", self._generate),
(pipeline, "_collect_activations", self._collect)):
patched.append((owner, name, name in vars(owner), vars(owner).get(name)))
setattr(owner, name, wrap(getattr(owner, name)))
self.observing = True
yield self
finally:
self.observing = False
for handle in handles:
handle.remove()
for owner, name, existed, previous in reversed(patched):
if existed:
setattr(owner, name, previous)
else:
delattr(owner, name)