mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
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>
167 lines
7.3 KiB
Python
167 lines
7.3 KiB
Python
"""Opt-in local, process-scoped cost measurements without payload capture."""
|
|
|
|
from contextlib import contextmanager
|
|
import hashlib
|
|
from importlib.metadata import PackageNotFoundError, version
|
|
import json
|
|
import os
|
|
import platform
|
|
from pathlib import Path
|
|
import time
|
|
import warnings
|
|
|
|
|
|
COUNTER_SCOPE = (
|
|
"observed from load completion through cleanup at the loaded top-level model: forwards "
|
|
"are top-level forward calls (including generation steps); token_presentations sum "
|
|
"attention_mask over those calls; unique_examples are distinct non-padding input token "
|
|
"sequences (truncation can merge prompts); generated_tokens are new positions returned "
|
|
"by model.generate; tensor_bytes are logical element bytes of activation rows and "
|
|
"auxiliary readouts returned to the host by activation collection"
|
|
)
|
|
|
|
|
|
def identity_digest(value):
|
|
"""Identify supplied strings without retaining paths, URLs or prompt text."""
|
|
return hashlib.sha256(str(value).encode("utf-8")).hexdigest()
|
|
|
|
|
|
def process_resources():
|
|
"""Sample this process (all threads, no children); RSS is a lifetime high water."""
|
|
try:
|
|
import resource
|
|
usage = resource.getrusage(resource.RUSAGE_SELF)
|
|
except (ImportError, OSError, ValueError):
|
|
return {"user_cpu_seconds": None, "system_cpu_seconds": None,
|
|
"peak_rss_bytes": None}
|
|
scale = {"Linux": 1024, "Darwin": 1}.get(platform.system())
|
|
return {"user_cpu_seconds": usage.ru_utime, "system_cpu_seconds": usage.ru_stime,
|
|
"peak_rss_bytes": usage.ru_maxrss * scale if scale is not None else None}
|
|
|
|
|
|
def _runtime():
|
|
from obliteratus.provenance import tool_provenance
|
|
packages = {}
|
|
for name in ("torch", "transformers", "numpy", "safetensors"):
|
|
try:
|
|
packages[name] = version(name)
|
|
except PackageNotFoundError:
|
|
packages[name] = None
|
|
return {"python": platform.python_version(), "system": platform.system(),
|
|
"machine": platform.machine(), "processor": platform.processor() or None,
|
|
"logical_cpu_count": os.cpu_count(),
|
|
"hub_offline": {"1": True, "0": False}.get(os.environ.get("HF_HUB_OFFLINE")),
|
|
"transformers_offline": {"1": True, "0": False}.get(
|
|
os.environ.get("TRANSFORMERS_OFFLINE")),
|
|
"packages": packages, "tool": tool_provenance()}
|
|
|
|
|
|
def encode_profile(report):
|
|
"""Versioned JSON writer; reject NaN/Infinity anywhere before writing bytes."""
|
|
if type(report.get("schema_version")) is not int or report["schema_version"] != 1:
|
|
raise ValueError("Unsupported local cost profile schema_version")
|
|
return json.dumps(report, allow_nan=False, sort_keys=True, indent=2) + "\n"
|
|
|
|
|
|
class LocalCostProfile:
|
|
"""One invocation, injected monotonic seconds clock and process resource reader.
|
|
|
|
Stages are sequential, host wall timings. No accelerator synchronization is
|
|
introduced. Work counters come from an attached ``WorkCounters`` observer;
|
|
without one, or where work is not observable, counters remain null with a
|
|
reason.
|
|
"""
|
|
|
|
def __init__(self, *, clock=time.perf_counter, resources=process_resources):
|
|
self.clock = clock
|
|
self.resources = resources
|
|
self.report = None
|
|
self.overhead = 0.0
|
|
self.work = None
|
|
|
|
@contextmanager
|
|
def stage(self, name):
|
|
start = self.clock()
|
|
row = {"name": name, "status": "success", "error_type": None}
|
|
if self.work is not None:
|
|
self.work.begin()
|
|
try:
|
|
yield
|
|
except BaseException as exc:
|
|
row.update(status="failure", error_type=type(exc).__name__)
|
|
raise
|
|
finally:
|
|
end = self.clock()
|
|
row["wall_seconds"] = end - start
|
|
if self.work is not None:
|
|
row["counters"], row["counter_reasons"] = self.work.end()
|
|
self.report["stages"].append(row)
|
|
self.overhead += self.clock() - end
|
|
|
|
@contextmanager
|
|
def invocation(self, destination, *, model_id, pack_id, settings):
|
|
if self.report is not None:
|
|
raise ValueError("LocalCostProfile instances are single-use")
|
|
start = self.clock()
|
|
before = self.resources()
|
|
self.report = {
|
|
"schema_version": 1, "workflow": "concept_baseline", "status": "success",
|
|
"error_type": None, "stages": [], "runtime": _runtime(),
|
|
"identities": {"model_request_sha256": identity_digest(model_id),
|
|
"pack_request_sha256": identity_digest(pack_id)},
|
|
"settings": settings, "cache": {"model_reused": None, "data_reused": None},
|
|
"counters": dict.fromkeys(("unique_examples", "token_presentations", "forwards",
|
|
"generated_tokens", "tensor_bytes")),
|
|
"counter_reasons": dict.fromkeys(
|
|
("unique_examples", "token_presentations", "forwards", "generated_tokens",
|
|
"tensor_bytes"), "no work observer attached"),
|
|
"counter_scope": None,
|
|
"timing_scope": "host wall seconds; accelerator work is unsynchronized",
|
|
"wall_scope": "invocation entry through cleanup and final resource sampling; "
|
|
"excludes final JSON encoding and sidecar I/O",
|
|
"memory_scope": "RUSAGE_SELF lifetime peak RSS bytes; all threads, no children",
|
|
"cpu_scope": "RUSAGE_SELF user/system seconds delta; all threads, no children",
|
|
"resources_before": before, "gpu_seconds": None,
|
|
"dollar_cost": None, "energy_joules": None,
|
|
}
|
|
self.overhead += self.clock() - start
|
|
error = None
|
|
try:
|
|
yield self
|
|
except BaseException as exc:
|
|
error = exc
|
|
self.report.update(status="failure", error_type=type(exc).__name__)
|
|
raise
|
|
finally:
|
|
try:
|
|
self._finish(destination, start, before)
|
|
except Exception:
|
|
if error is None:
|
|
raise
|
|
warnings.warn("Cost profile could not be written; preserving workflow error",
|
|
RuntimeWarning, stacklevel=2)
|
|
|
|
def _finish(self, destination, start, before):
|
|
finish_start = self.clock()
|
|
after = self.resources()
|
|
self.report["resources_after"] = after
|
|
if self.work is not None:
|
|
self.report["counters"], self.report["counter_reasons"] = self.work.totals()
|
|
self.report["counter_scope"] = COUNTER_SCOPE
|
|
self.report["cpu_seconds"] = {
|
|
key: after[key] - before[key] if after[key] is not None and before[key] is not None
|
|
else None for key in ("user_cpu_seconds", "system_cpu_seconds")
|
|
}
|
|
end = self.clock()
|
|
self.report["wall_seconds"] = end - start
|
|
self.report["profiling_overhead"] = {
|
|
"measured_bookkeeping_wall_seconds": self.overhead + end - finish_start,
|
|
"total_wall_seconds": None,
|
|
"exclusions": "clock calls, context dispatch, final JSON encoding and sidecar I/O; "
|
|
"not an unprofiled-versus-profiled benchmark",
|
|
}
|
|
encoded = encode_profile(self.report)
|
|
path = Path(destination)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(encoded, encoding="utf-8")
|