Files
OBLITERATUS/examples/analysis_first_baseline.py
Joseph Magly 545ed4281d fix(ci): clear gitleaks false positives without suppression
All 22 supply-chain secret-scan hits were false positives: a public
Llama-3.2-1B commit id under a "tokenizer_revision" key, and SHA-256 digests of
tokenizer fixture files keyed by filename. numeric_geometry_spec uses one
module constant for the commit (values unchanged). The analysis-first evidence
JSONs record hashes as ordered {"path","sha256"} lists (all 63 values
preserved, verified), and the two writers emit the same shape. CI scans the
clean checkout before uv sync builds into the workspace. No allowlist, ignore
file or inline allow comment is added.

Refs #679
2026-11-25 15:13:02 -05:00

178 lines
8.2 KiB
Python

"""Replay a synthetic CPU baseline and measure opt-in profiling overhead.
Run from a repository checkout with its locked development environment:
python examples/analysis_first_baseline.py --output-dir /tmp/new-baseline
No downloaded or pretrained weights are used. Output directories must be new.
"""
from __future__ import annotations
import argparse
from copy import deepcopy
import hashlib
import json
import platform
from pathlib import Path
import statistics
import subprocess
import sys
import time
def write_json(path, value):
path.write_text(json.dumps(value, indent=2, sort_keys=True, allow_nan=False) + "\n")
def file_sha256_entries(root, directories):
"""Return ``[{"path", "sha256"}, ...]`` for files under ``directories``, in sorted path order.
A list of records (not a map keyed by file name) keeps names such as
``tokenizer.json`` out of key position next to a hex digest, which secret
scanners otherwise report as a generic API key (#679).
"""
return [{"path": str(p.relative_to(root)), "sha256": hashlib.sha256(p.read_bytes()).hexdigest()}
for directory in directories for p in sorted((root / directory).rglob("*"))
if p.is_file()]
def make_fixture(root):
import yaml
from obliteratus.concepts.registry import ConceptRegistry
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
build_tiny_offline_model(root / "model")
pack = root / "pack"
(pack / "controls").mkdir(parents=True)
recipe = deepcopy(ConceptRegistry().pack("pain").manifest["recipe"])
recipe["readout"]["suffix"] = " answer"
manifest = {
"schema_version": "1.1", "id": "synthetic", "tier": "user",
"version": "0.1.0", "title": "Synthetic CPU cost fixture",
"intended_use": "Software contracts only; no model efficacy evidence",
"recipe": recipe, "sets": {}, "licence": [],
}
for positive, word, name in [
(True, "harmful", "positive.jsonl"),
(False, "harmless", "controls/neutral.jsonl"),
]:
rows = [{"id": str(index), "text": f"{word} {item}"}
for index, item in enumerate(["hello", "world", "test", "request"] * 2)]
payload = "".join(json.dumps(row, sort_keys=True) + "\n" for row in rows).encode()
(pack / name).write_bytes(payload)
entry = {"path": name, "n": len(rows), "sha256": hashlib.sha256(payload).hexdigest()}
if positive:
manifest["sets"]["positive"] = entry
else:
manifest["sets"]["controls"] = [{**entry, "name": "neutral"}]
manifest["licence"].append({"path": name, "spdx": "CC0-1.0",
"source": "local synthetic fixture",
"attribution": "OBLITERATUS test authors"})
(pack / "manifest.yaml").write_text(yaml.safe_dump(manifest, sort_keys=True))
def child(root, mode, repetitions):
import torch
from obliteratus.concepts.baseline import run_baseline
from obliteratus.concepts.direction import ConceptDirection
torch.set_num_threads(1)
rows = []
reference = None
for index in range(repetitions):
output = root / mode / str(index)
start = time.perf_counter()
run_baseline(root / "pack", root / "model", output, seed=42, device="cpu",
dtype="float32", max_seq_length=8, extraction_layer=0,
cache_dir=root / "cache", profile=mode == "profiled")
elapsed = time.perf_counter() - start
direction = ConceptDirection.load(output / "direction.safetensors")
validation = json.loads((output / "validation.json").read_text())
numerical = {
"directions": {str(k): v.tolist() for k, v in direction.directions.items()},
"metrics": validation["metrics"], "verdict": validation["verdict"],
}
if reference is None:
reference = numerical
if reference != numerical:
raise RuntimeError("Repeated baseline numerical output changed")
rows.append({"index": index, "process_first_baseline": index == 0,
"workflow_wall_seconds": elapsed,
"profile": json.loads((output / "cost-profile.json").read_text())
if mode == "profiled" else None})
write_json(root / (mode + ".json"), {"runs": rows, "numerical": reference})
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output-dir", type=Path, required=True)
parser.add_argument("--repetitions", type=int, default=4)
parser.add_argument("--child", choices=["plain", "profiled"])
parser.add_argument("--child-timeout", type=int, default=180)
args = parser.parse_args()
root = args.output_dir.resolve()
if args.child_timeout <= 0:
parser.error("--child-timeout must be positive")
if args.repetitions < 2:
parser.error("--repetitions must be at least 2 (first and subsequent runs)")
if args.child:
child(root, args.child, args.repetitions)
return
root.mkdir(parents=True, exist_ok=False)
start = time.perf_counter()
make_fixture(root)
preparation = time.perf_counter() - start
process_wall = {}
for mode in ["plain", "profiled"]:
start = time.perf_counter()
with (root / (mode + ".log")).open("w") as log:
subprocess.run([sys.executable, str(Path(__file__).resolve()),
"--output-dir", str(root), "--repetitions", str(args.repetitions),
"--child", mode], check=True, stdout=log, stderr=subprocess.STDOUT,
timeout=args.child_timeout)
process_wall[mode] = time.perf_counter() - start
results = {mode: json.loads((root / (mode + ".json")).read_text())
for mode in ["plain", "profiled"]}
if results["plain"]["numerical"] != results["profiled"]["numerical"]:
raise RuntimeError("Profiling changed the numerical baseline output")
warm = {mode: statistics.median(row["workflow_wall_seconds"] for row in data["runs"][1:])
for mode, data in results.items()}
import torch
cpu_model = platform.processor() or None
if cpu_model is None and Path("/proc/cpuinfo").is_file():
cpu_model = next((line.split(":", 1)[1].strip()
for line in Path("/proc/cpuinfo").read_text().splitlines()
if line.startswith("model name")), None)
hashes = file_sha256_entries(root, ["model", "pack"])
report = {
"schema_version": 1, "workload": "random-init GPT-2, one layer, width 16, CPU float32",
"row_count": 16, "distinct_prompt_strings": 8, "independent_group_count": None,
"seed": 42, "fixture_seed": 20260814, "max_seq_length": 8, "extraction_layer": 0,
"torch_threads": torch.get_num_threads(), "child_torch_threads": 1,
"hardware": {"cpu_model": cpu_model, "machine": platform.machine(),
"kernel": platform.release(), "gpu": "not exercised"},
"child_timeout_seconds": args.child_timeout,
"fixture_sha256": hashes, "fixture_preparation_wall_seconds": preparation,
"process_wall_seconds": process_wall, "warm_median_workflow_wall_seconds": warm,
"warm_profiled_minus_plain_seconds": warm["profiled"] - warm["plain"],
"warm_profiled_to_plain_ratio": warm["profiled"] / warm["plain"] if warm["plain"] > 0 else None,
"numerical_outputs_equal": True, "results": results,
"limitations": [
"First means first baseline in a fresh Python process; OS caches were not flushed.",
"Warm means later calls in that process; each call reloads the local model.",
"Plain runs precede profiled runs and share a data cache; this can favor profiled runs.",
"Preparation and process startup are separate from workflow timings.",
"Repeated synthetic strings do not establish independent model efficacy evidence.",
"No pretrained model, targeting campaign, GPU, energy or financial qualification.",
"Profile RSS is lifetime process peak, not an incremental per-stage allocation.",
],
}
write_json(root / "report.json", report)
print(root / "report.json")
if __name__ == "__main__":
main()