Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

test(benchmarks): add baseline stability diagnostic

svonava482b59922492
diff --git a/tools/benchmarks/cross_encoder_lora/stability.py b/tools/benchmarks/cross_encoder_lora/stability.py
new file mode 100644
index 000000000..a4d5f05c5
--- /dev/null
+++ b/tools/benchmarks/cross_encoder_lora/stability.py
@@ -0,0 +1,945 @@
+"""Baseline-only stability diagnostic; never qualifies a candidate or provisions compute.
+
+Run with --inputs, --fixtures and --output. Six fresh baseline processes each
+execute C1/C4/C16 with fixed S/L1/L2 windows: 30/200/200 sequential warmup
+requests followed by the existing 200-request/12-second measurement. Later
+window agreement is tested symmetrically; between-process dispersion remains
+descriptive. The original timed methods and automatic GC behavior are reused.
+"""
+
+# Only this checkout's Python worker runs; no shell or allocation.
+# ruff: noqa: S603
+
+from __future__ import annotations
+
+import argparse
+import asyncio
+import gc
+import json
+import math
+import os
+import resource
+import signal
+import socket
+import statistics
+import subprocess
+import sys
+import time
+from http import HTTPStatus
+from pathlib import Path
+from typing import Any
+
+import torch
+import uvicorn
+
+from tools.benchmarks.cross_encoder_lora import worker
+from tools.benchmarks.cross_encoder_lora.adapter import require_forward_selection
+from tools.benchmarks.cross_encoder_lora.evidence import Evidence, atomic_json, digest, fingerprint, vector_delta
+from tools.benchmarks.cross_encoder_lora.protocol import (
+ BASE_REVISION,
+ CPU_ORACLE,
+ MAX_BATCH_ITEMS,
+ MODEL,
+ ORACLE_ATOL,
+ RESTORE_ATOL,
+ SEQUENTIAL,
+ STEADY_SECONDS,
+ THREADS,
+ WEIGHTS_SHA256,
+ paired_gate,
+ percentile,
+ require,
+)
+from tools.benchmarks.cross_encoder_lora.runner import (
+ KILL_GRACE_SECONDS,
+ TERMINATE_GRACE_SECONDS,
+ WAIT_GRACE_SECONDS,
+ extract,
+ harness_manifest,
+ source_environment,
+ stop,
+ terminate,
+)
+
+PROCESSES = 6
+WINDOWS = (("S", 30), ("L1", 200), ("L2", 200))
+CONCURRENCIES = (1, 4, 16)
+METRICS = ((1, "mean_ms"), (1, "p95_ms"), (4, "rps"), (4, "p95_ms"), (16, "rps"), (16, "p95_ms"))
+RUNNER_LIMIT = 1200
+WORKER_LIMIT = 180
+GC_FIELDS = ("collections", "collected", "uncollectable")
+RUSAGE_FIELDS = ("ru_utime", "ru_stime", "ru_minflt", "ru_majflt", "ru_nvcsw", "ru_nivcsw")
+OPTIONAL_FILES = ("cpu.stat", "cpu.stat.local", "cpu.pressure", "cpu.max", "cpu.weight", "cpuset.cpus.effective")
+COUNTER_FILES = ("cpu.stat", "cpu.stat.local")
+CONTROL_FILES = ("cpu.max", "cpu.weight", "cpuset.cpus.effective")
+GC_GENERATIONS = 3
+QUARTILES = 4
+PSI_FIELDS = ("avg10", "avg60", "avg300", "total")
+MAX_PERCENT = 100
+MAX_CPU_WEIGHT = 10000
+
+
+class DiagnosticEvidence(Evidence):
+ def emit(self, event: str, **fields: Any) -> dict[str, Any]:
+ if fields.get("qualification"):
+ fields["hardware_preflight_only"] = True
+ fields["qualification"] = False
+ return super().emit(event, **fields)
+
+
+def unified_cgroup(proc: Path = Path("/proc/self")) -> dict[str, Any]:
+ """Resolve the process's own namespace; never fall back to another cgroup."""
+ try:
+ rows = (proc / "cgroup").read_text().splitlines()
+ membership = [row[3:] for row in rows if row.startswith("0::")]
+ require(len(membership) == 1, "unified cgroup membership is unavailable or ambiguous")
+ group = Path(membership[0])
+ require(group.is_absolute() and ".." not in group.parts, "invalid cgroup membership")
+ matches = []
+ for row in (proc / "mountinfo").read_text().splitlines():
+ left, right = row.split(" - ", 1)
+ if right.split()[0] != "cgroup2":
+ continue
+ fields = left.split()
+ root = Path(fields[3].replace("\\040", " ").replace("\\134", "\\"))
+ mount = Path(fields[4].replace("\\040", " ").replace("\\134", "\\"))
+ if group.is_relative_to(root):
+ matches.append(mount / group.relative_to(root))
+ require(len(matches) == 1, "cgroup mount cannot be resolved uniquely")
+ path = matches[0]
+ info = path.stat()
+ return {"path": str(path), "device": info.st_dev, "inode": info.st_ino, "error": None}
+ except (OSError, ValueError, IndexError, AssertionError) as error:
+ return {"path": None, "error": f"{type(error).__name__}: {error}"}
+
+
+def flat_counters(text: str) -> dict[str, int]:
+ result = {}
+ for row in text.splitlines():
+ key, value = row.split()
+ require(key not in result, "duplicate telemetry counter")
+ result[key] = int(value)
+ require(result[key] >= 0, "negative telemetry counter")
+ require(bool(result), "empty exposed telemetry counter file")
+ return result
+
+
+def pressure(text: str) -> dict[str, dict[str, float | int]]:
+ result = {}
+ for row in text.splitlines():
+ name, *parts = row.split()
+ require(name in {"some", "full"} and name not in result, "invalid PSI row")
+ values = dict(part.split("=", 1) for part in parts)
+ require(len(parts) == len(PSI_FIELDS) and set(values) == set(PSI_FIELDS), "invalid PSI fields")
+ parsed = {key: int(value) if key == "total" else float(value) for key, value in values.items()}
+ require(all(math.isfinite(value) and value >= 0 for value in parsed.values()), "invalid PSI value")
+ require(all(parsed[key] <= MAX_PERCENT for key in PSI_FIELDS[:-1]), "invalid PSI percentage")
+ result[name] = parsed
+ require("some" in result, "missing exposed PSI some row")
+ return result
+
+
+def validate_core(core: dict[str, Any]) -> None:
+ require(
+ all(isinstance(core[key], int) and core[key] > 0 for key in ("wall_ns", "monotonic_ns"))
+ and core["gc_enabled"] is True,
+ "invalid required clock/GC state",
+ )
+ require(
+ set(core["rusage"]) == set(RUSAGE_FIELDS)
+ and all(math.isfinite(value) and value >= 0 for value in core["rusage"].values()),
+ "invalid required rusage",
+ )
+ for key in ("gc_thresholds", "gc_count"):
+ require(
+ len(core[key]) == GC_GENERATIONS and all(type(value) is int and value >= 0 for value in core[key]),
+ "invalid required GC counts/thresholds",
+ )
+ require(core["gc_thresholds"][0] > 0, "automatic GC is disabled by its threshold")
+ require(len(core["gc_stats"]) == GC_GENERATIONS, "missing required GC generations")
+ require(
+ all(
+ set(row) == set(GC_FIELDS) and all(type(row[key]) is int and row[key] >= 0 for key in GC_FIELDS)
+ for row in core["gc_stats"]
+ ),
+ "invalid required GC statistics",
+ )
+
+
+def control_value(name: str, text: str) -> str:
+ if name == "cpu.max":
+ quota, period = text.split()
+ require((quota == "max" or int(quota) > 0) and int(period) > 0, "invalid exposed cpu.max")
+ elif name == "cpu.weight":
+ require(1 <= int(text) <= MAX_CPU_WEIGHT, "invalid exposed cpu.weight")
+ else:
+ for item in text.split(","):
+ bounds = [int(value) for value in item.split("-")]
+ require(len(bounds) in {1, 2} and bounds[0] >= 0 and bounds[-1] >= bounds[0], "invalid exposed cpuset")
+ return text
+
+
+def snapshot() -> dict[str, Any]:
+ """Only called outside performance(), including all filesystem reads."""
+ started = time.monotonic_ns()
+ usage = resource.getrusage(resource.RUSAGE_SELF)
+ core = {
+ "wall_ns": time.time_ns(),
+ "monotonic_ns": started,
+ "rusage": {key: getattr(usage, key) for key in RUSAGE_FIELDS},
+ "gc_enabled": gc.isenabled(),
+ "gc_thresholds": list(gc.get_threshold()),
+ "gc_count": list(gc.get_count()),
+ "gc_stats": gc.get_stats(),
+ }
+ validate_core(core)
+ group = unified_cgroup()
+ files: dict[str, Any] = {}
+ errors = {}
+ if group["path"] is not None:
+ for name in OPTIONAL_FILES:
+ try:
+ text = (Path(group["path"]) / name).read_text().strip()
+ except OSError as error:
+ files[name] = None
+ errors[name] = f"{type(error).__name__}: {error}"
+ continue
+ # Missing optional files are unavailability, but malformed exposed
+ # counters must not be mistaken for trustworthy zero deltas.
+ files[name] = (
+ flat_counters(text)
+ if name in COUNTER_FILES
+ else pressure(text)
+ if name == "cpu.pressure"
+ else control_value(name, text)
+ )
+ else:
+ files = dict.fromkeys(OPTIONAL_FILES)
+ errors["cgroup"] = group["error"]
+ affinity = None
+ try:
+ if hasattr(os, "sched_getaffinity"):
+ affinity = sorted(os.sched_getaffinity(0))
+ require(bool(affinity), "empty exposed affinity")
+ else:
+ errors["affinity"] = "sched_getaffinity unavailable on this platform"
+ except OSError as error:
+ errors["affinity"] = f"{type(error).__name__}: {error}"
+ return {
+ "qualification": False,
+ "core": core,
+ "cgroup": group,
+ "cgroup_files": files,
+ "optional_errors": errors,
+ "affinity": affinity,
+ "snapshot_read_ns": time.monotonic_ns() - started,
+ }
+
+
+def counter_delta(before: dict[str, Any], after: dict[str, Any]) -> dict[str, Any]:
+ require(set(before) == set(after), "counter fields changed within a window")
+ result = {key: after[key] - value for key, value in before.items()}
+ require(all(math.isfinite(value) and value >= 0 for value in result.values()), "telemetry counter reset")
+ return result
+
+
+def telemetry_delta(before: dict[str, Any], after: dict[str, Any]) -> dict[str, Any]:
+ first, last = before["core"], after["core"]
+ validate_core(first)
+ validate_core(last)
+ require(
+ last["wall_ns"] > first["wall_ns"] and last["monotonic_ns"] > first["monotonic_ns"],
+ "required clock did not advance",
+ )
+ require(
+ first["gc_enabled"] and last["gc_enabled"] and first["gc_thresholds"] == last["gc_thresholds"],
+ "GC configuration changed",
+ )
+ group_before, group_after = before["cgroup"], after["cgroup"]
+ require(
+ all(group_before.get(key) == group_after.get(key) for key in ("path", "device", "inode")),
+ "cgroup path or identity changed within a window",
+ )
+ require(before["affinity"] == after["affinity"], "process affinity changed within a window")
+ for name in CONTROL_FILES:
+ require(
+ before["cgroup_files"].get(name) == after["cgroup_files"].get(name),
+ "cgroup quota/weight/cpuset changed within a window",
+ )
+ optional = {}
+ for name in COUNTER_FILES:
+ a, b = before["cgroup_files"].get(name), after["cgroup_files"].get(name)
+ optional[name] = counter_delta(a, b) if a is not None and b is not None else None
+ a, b = before["cgroup_files"].get("cpu.pressure"), after["cgroup_files"].get("cpu.pressure")
+ optional["cpu.pressure"] = (
+ counter_delta({k: v["total"] for k, v in a.items()}, {k: v["total"] for k, v in b.items()})
+ if a is not None and b is not None
+ else None
+ )
+ return {
+ "qualification": False,
+ "scope": "Whole performance call, including sequential warmup, measurement, drain, validation and raw emission",
+ "wall_s": (last["wall_ns"] - first["wall_ns"]) / 1e9,
+ "monotonic_s": (last["monotonic_ns"] - first["monotonic_ns"]) / 1e9,
+ "rusage": counter_delta(first["rusage"], last["rusage"]),
+ "gc_stats": [counter_delta(a, b) for a, b in zip(first["gc_stats"], last["gc_stats"], strict=True)],
+ "optional_cgroup": optional,
+ "cpu_pressure_counters_available": all(optional.get(name) is not None for name in ("cpu.stat", "cpu.pressure")),
+ "resource_telemetry_complete": before["affinity"] is not None
+ and after["affinity"] is not None
+ and all(
+ before["cgroup_files"].get(name) is not None and after["cgroup_files"].get(name) is not None
+ for name in OPTIONAL_FILES
+ ),
+ "optional_errors_before": before["optional_errors"],
+ "optional_errors_after": after["optional_errors"],
+ }
+
+
+def metrics(phase: dict[str, Any]) -> dict[str, Any]:
+ samples = phase["samples"]
+ latencies = [row["latency_ms"] for row in samples]
+ require(bool(latencies) and all(math.isfinite(value) and value > 0 for value in latencies), "invalid raw latency")
+ elapsed = phase["elapsed_s"]
+ require(math.isfinite(elapsed) and elapsed > 0, "invalid phase elapsed")
+ result = {
+ "count": len(samples),
+ "elapsed_s": elapsed,
+ "mean_ms": statistics.mean(latencies),
+ "p95_ms": percentile(latencies, 0.95),
+ "rps": len(samples) / elapsed,
+ }
+ for key, value in result.items():
+ require(math.isclose(value, phase[key], rel_tol=1e-12, abs_tol=0), "raw metric differs from phase aggregate")
+ return result
+
+
+def chronological_quartiles(samples: list[dict[str, Any]]) -> list[dict[str, Any]]:
+ ordered = sorted(samples, key=lambda row: (row["start_ns"], row["label"]))
+ require(len(ordered) >= QUARTILES, "too few requests for fixed chronological quartiles")
+ result = []
+ for index in range(QUARTILES):
+ rows = ordered[len(ordered) * index // QUARTILES : len(ordered) * (index + 1) // QUARTILES]
+ headers: dict[str, Any] = {}
+ keys = sorted({key for row in rows for key in row["responses"][0]["timing_headers"]})
+ for key in keys:
+ values = [
+ float(row["responses"][0]["timing_headers"][key])
+ for row in rows
+ if key in row["responses"][0]["timing_headers"]
+ ]
+ require(all(math.isfinite(value) and value >= 0 for value in values), "invalid timing header")
+ headers[key] = {
+ "count": len(values),
+ "min": min(values),
+ "mean": statistics.mean(values),
+ "max": max(values),
+ "p95": sorted(values)[math.ceil(0.95 * len(values)) - 1],
+ }
+ latencies = [row["latency_ms"] for row in rows]
+ result.append(
+ {
+ "count": len(rows),
+ "start_ns": rows[0]["start_ns"],
+ "last_start_ns": rows[-1]["start_ns"],
+ "mean_ms": statistics.mean(latencies),
+ "p95_ms": percentile(latencies, 0.95),
+ "timing_headers": headers,
+ }
+ )
+ return result
+
+
+def verify_attempt(path: Path, record: dict[str, Any]) -> None:
+ """Reopen raw warmups/statuses as well as measured rows; never time this work."""
+ events = [json.loads(line) for line in path.read_text().splitlines()]
+ require(len(events) == 1 and events[0]["event"] == "performance_attempt", "missing/extra performance attempt")
+ attempt = events[0]
+ require(
+ attempt["qualification"] is False and attempt["concurrency"] == record["concurrency"],
+ "attempt identity differs",
+ )
+ require(len(attempt["warmups"]) == record["warmups"], "sequential warmup count differs")
+ measured = record["performance"]
+ require(
+ attempt["completed_samples"]
+ == [
+ {key: value for key, value in row.items() if key != "diagnostic_matching_oracle"}
+ for row in measured["samples"]
+ ],
+ "attempt/raw measured rows differ",
+ )
+ rows = [*attempt["warmups"], *attempt["completed_samples"]]
+ require(
+ len(attempt["wire_responses"]) == len(rows)
+ and all(row["status"] == HTTPStatus.OK for row in attempt["wire_responses"]),
+ "hidden wire retries or failure",
+ )
+ for row in rows:
+ saved = row["scores"][:]
+ worker.validate_response(row)
+ require(saved == row["scores"] and row["selection"] is None, "baseline scores/selection differ")
+ require(row["end_ns"] >= row["start_ns"] > 0, "invalid request timestamps")
+ if record["concurrency"] == 1:
+ require(measured["count"] == (4 if record["smoke"] else SEQUENTIAL), "sequential request count differs")
+ else:
+ require(measured["elapsed_s"] >= (0.15 if record["smoke"] else STEADY_SECONDS), "closed-loop duration differs")
+
+
+def measure_window(
+ url: str,
+ concurrency: int,
+ window: str,
+ warmups: int,
+ directory: Path,
+ base_scores: list[float],
+ oracles: dict[str, Any],
+ *,
+ smoke: bool,
+ continuity: dict[str, Any] | None = None,
+) -> dict[str, Any]:
+ evidence = DiagnosticEvidence(directory)
+ record: dict[str, Any] = {
+ "qualification": False,
+ "completed": False,
+ "window": window,
+ "concurrency": concurrency,
+ "warmups": 2 if smoke else warmups,
+ "smoke": smoke,
+ }
+ original_warmups = worker.WARMUP
+ try:
+ record["gpu_before"] = worker.telemetry()
+ record["telemetry_before"] = snapshot()
+ if continuity is not None:
+ telemetry_delta(continuity, record["telemetry_before"])
+ worker.WARMUP = warmups # ty: ignore[invalid-assignment] -- scoped diagnostic value, always restored
+ try:
+ measured = worker.performance(url, concurrency, evidence, smoke=smoke)
+ finally:
+ worker.WARMUP = original_warmups
+ record["telemetry_after"] = snapshot()
+ record["gpu_after"] = worker.telemetry()
+ record["telemetry_delta"] = telemetry_delta(record["telemetry_before"], record["telemetry_after"])
+ record["performance"] = measured
+ record["attempt_sha256"] = digest(directory / "events.jsonl")
+ verify_attempt(directory / "events.jsonl", record)
+ for row in measured["samples"]:
+ if concurrency == 1:
+ require(vector_delta(row["scores"], base_scores) <= RESTORE_ATOL, "baseline C1 scores changed")
+ row["diagnostic_matching_oracle"] = worker.matching_vector(row["scores"], oracles)
+ record["metrics"] = metrics(measured)
+ record["chronological_quartiles"] = chronological_quartiles(measured["samples"])
+ record["actual_timed_batch_shape"] = "unknown"
+ record["completed"] = True
+ if continuity is not None:
+ continuity.clear()
+ continuity.update(record["telemetry_after"])
+ except BaseException as error:
+ record["error"] = worker.error_record(error)
+ raise
+ finally:
+ worker.WARMUP = original_warmups
+ atomic_json(directory / "window.json", record)
+ return {
+ "qualification": False,
+ "window": window,
+ "concurrency": concurrency,
+ "completed": True,
+ "metrics": record["metrics"],
+ "resource_telemetry_complete": record["telemetry_delta"]["resource_telemetry_complete"],
+ "record": str((directory / "window.json").relative_to(directory.parent.parent)),
+ "sha256": digest(directory / "window.json"),
+ }
+
+
+async def serve_windows(args: argparse.Namespace, evidence: DiagnosticEvidence, summary: dict[str, Any]) -> None:
+ manifest = json.loads((args.fixtures / "manifest.json").read_text())
+ require(bool(manifest["smoke"]) == args.smoke, "fixture mode mismatch")
+ snapshot_path = Path(manifest["snapshot"])
+ for name, expected in manifest["model_files"].items():
+ require(digest(snapshot_path / name) == expected, "model fixture changed")
+ if not args.smoke:
+ require(digest(snapshot_path / "model.safetensors") == WEIGHTS_SHA256, "checkpoint pin differs")
+ summary["fixtures"] = manifest
+ worker.configs(evidence.directory / "models", args.source, args.fixtures, "B", snapshot_path, smoke=args.smoke)
+ app = worker.AppFactory.create_app(
+ worker.AppStateConfig(
+ models_dir=evidence.directory / "models",
+ device="cpu" if args.smoke else "cuda:0",
+ remote_serving=False,
+ bind_host="127.0.0.1",
+ )
+ )
+ sock = socket.socket()
+ task = None
+ client = None
+ server = None
+ adapter = None
+ try:
+ sock.bind(("127.0.0.1", 0))
+ url = f"http://127.0.0.1:{sock.getsockname()[1]}"
+ server = uvicorn.Server(
+ uvicorn.Config(app, log_level="warning", timeout_graceful_shutdown=20, access_log=False)
+ )
+ task = asyncio.create_task(server.serve(sockets=[sock]))
+ client = worker.Client(url)
+ while not server.started:
+ if task.done():
+ await task
+ raise RuntimeError("server exited before readiness")
+ await asyncio.sleep(0.05)
+ registry = app.state.registry
+ adapter = await registry.load_async(MODEL, "cpu" if args.smoke else "cuda:0")
+ model_worker = registry._loaded[MODEL].worker
+ require(
+ model_worker is not None and getattr(adapter, "_lora", None) is None, "baseline worker/manager mismatch"
+ )
+ state = await asyncio.to_thread(worker.tensor_state, adapter)
+ expected_dtype = "torch.float32" if args.smoke else "torch.float16"
+ require({x["dtype"] for x in state["tensors"].values()} == {expected_dtype}, "baseline precision differs")
+ summary["actual_precision"] = expected_dtype
+ summary["original_tensor_state"] = state
+ summary["loaded_before_forward_memory"] = worker.memory()
+ bases = [await asyncio.to_thread(client.score, None, f"base-{i}") for i in range(3)]
+ baseline = bases[0]["scores"]
+ require(max(vector_delta(baseline, row["scores"]) for row in bases) <= RESTORE_ATOL, "unstable base scores")
+ if not args.smoke:
+ require(vector_delta(baseline, CPU_ORACLE) <= ORACLE_ATOL, "finite checkpoint oracle differs")
+ summary["base_scores"] = baseline
+ summary["shape_oracles"] = await worker.oracle(adapter, model_worker, None, smoke=args.smoke)
+ summary["initial_memory"] = worker.memory()
+ summary["untimed_replays"] = {}
+ adapter.observation_phase = "untimed-stability-setup"
+ for concurrency in CONCURRENCIES:
+ first = len(adapter.batch_observations)
+ rows = await asyncio.to_thread(worker.replay, url, concurrency)
+ batches = adapter.batch_observations[first:]
+ require(bool(batches), "missing actual untimed batch observations")
+ worker.validate_usage(rows, batches)
+ for batch in batches:
+ require_forward_selection(batch, None)
+ require(
+ vector_delta(batch["scores"], summary["shape_oracles"][worker.batch_key(batch)]["scores"])
+ <= RESTORE_ATOL,
+ "untimed shape oracle differs",
+ )
+ summary["untimed_replays"][str(concurrency)] = {"requests": rows, "batches": batches}
+ # These three setup memory observations mirror the shared B harness.
+ # There is no new forced collection between any diagnostic windows.
+ summary["resident_memory"] = worker.memory()
+ summary["windows"] = []
+ continuity = dict(summary["required_telemetry_preflight"])
+ with worker.unobserved(adapter):
+ for concurrency in CONCURRENCIES:
+ for window, warmups in WINDOWS:
+ directory = evidence.directory / f"C{concurrency}" / window
+ row = await asyncio.to_thread(
+ measure_window,
+ url,
+ concurrency,
+ window,
+ warmups,
+ directory,
+ baseline,
+ summary["shape_oracles"],
+ smoke=args.smoke,
+ continuity=continuity,
+ )
+ summary["windows"].append(row)
+ atomic_json(evidence.directory / "process.json", summary)
+ # Only compact metadata survives here. Raw requests live on
+ # disk; ordinary reference release and automatic GC apply.
+ summary["restored_tensor_state"] = await asyncio.to_thread(worker.tensor_state, adapter)
+ worker.base_unchanged(state, summary["restored_tensor_state"], restored=True)
+ await registry.unload_async(MODEL)
+ summary["completed"] = True
+ finally:
+ try:
+ if client is not None:
+ client.close()
+ finally:
+ try:
+ if server is not None and task is not None:
+ server.should_exit = True
+ await asyncio.wait_for(asyncio.shield(task), timeout=30)
+ summary["shutdown_complete"] = True
+ finally:
+ sock.close()
+
+
+async def child(args: argparse.Namespace) -> dict[str, Any]:
+ evidence = DiagnosticEvidence(args.output.resolve())
+ summary: dict[str, Any] = {
+ "qualification": False,
+ "diagnostic_only": True,
+ "completed": False,
+ "shutdown_complete": False,
+ "process": args.process,
+ "pid": os.getpid(),
+ "source_commit": BASE_REVISION,
+ "smoke": args.smoke,
+ }
+ os.environ.update(
+ {
+ "SIE_TELEMETRY_DISABLED": "1",
+ "SIE_DISK_CACHE_ENABLED": "false",
+ "SIE_MAX_LORAS_PER_MODEL": "2",
+ "SIE_MAX_BATCH_REQUESTS": str(MAX_BATCH_ITEMS),
+ "TOKENIZERS_PARALLELISM": "false",
+ }
+ )
+ torch.set_num_threads(THREADS)
+ torch.set_num_interop_threads(1)
+ try:
+ summary["required_telemetry_preflight"] = snapshot()
+ environment = worker.preflight(args, evidence)
+ environment["hardware_preflight_only"] = environment.pop("qualification")
+ environment["qualification"] = False
+ environment["source_imports"] = {}
+ for name, target in (("sdk", worker.SIEClient), ("server", worker.AppFactory)):
+ module_file = sys.modules[target.__module__].__file__
+ assert module_file is not None, "source module has no file"
+ source_file = Path(module_file).resolve() # noqa: ASYNC240 -- preflight before model/server starts
+ require(source_file.is_relative_to(args.source.resolve()), "SDK/server imported outside baseline archive")
+ environment["source_imports"][name] = {
+ "relative_path": str(source_file.relative_to(args.source.resolve())),
+ "sha256": digest(source_file),
+ }
+ environment["source_imports"]["adapter"] = {
+ "relative_path": str(Path(environment["adapter_source"]).relative_to(args.source.resolve())),
+ "sha256": environment["adapter_sha256"],
+ }
+ summary["environment"] = environment
+ async with asyncio.timeout(args.trial_deadline_s):
+ await serve_windows(args, evidence, summary)
+ except BaseException as error:
+ summary["completed"] = False
+ summary["error"] = worker.error_record(error)
+ evidence.emit("failure", **summary["error"])
+ raise
+ finally:
+ atomic_json(evidence.directory / "process.json", summary)
+ evidence.emit("finished", completed=summary["completed"], shutdown_complete=summary["shutdown_complete"])
+ return summary
+
+
+def reopen_window(directory: Path, record: dict[str, Any]) -> dict[str, Any]:
+ relative = f"C{record['concurrency']}/{record['window']}/window.json"
+ require(record["record"] == relative, "window path differs from identity")
+ path = directory / relative
+ require(
+ path.resolve().is_relative_to(directory.resolve()) and digest(path) == record["sha256"],
+ "window path/hash differs",
+ )
+ window = json.loads(path.read_text())
+ require(window["qualification"] is False and window["completed"] is True, "window is incomplete or qualified")
+ require(
+ (window["window"], window["concurrency"]) == (record["window"], record["concurrency"]),
+ "window identity differs",
+ )
+ require(
+ window["warmups"] == (2 if window["smoke"] else dict(WINDOWS)[window["window"]]), "fixed warmup count differs"
+ )
+ attempt = path.parent / "events.jsonl"
+ require(digest(attempt) == window["attempt_sha256"], "raw attempt hash differs")
+ verify_attempt(attempt, window)
+ recalculated = metrics(window["performance"])
+ require(recalculated == window["metrics"] == record["metrics"], "window compact projection differs")
+ require(
+ telemetry_delta(window["telemetry_before"], window["telemetry_after"]) == window["telemetry_delta"],
+ "telemetry projection mismatch",
+ )
+ require(
+ chronological_quartiles(window["performance"]["samples"]) == window["chronological_quartiles"],
+ "quartile projection mismatch",
+ )
+ return window
+
+
+def verify_process_sources(rows: list[dict[str, Any]], source: Path) -> None:
+ require(bool(rows), "missing process environments")
+ first = rows[0]
+ fields = (
+ "versions",
+ "torchao",
+ "python",
+ "threads",
+ "device",
+ "torch_cuda",
+ "installed_distributions",
+ "installed_distributions_sha256",
+ "source_imports",
+ "hardware_preflight_only",
+ )
+ for row in rows:
+ require(row["source_commit"] == BASE_REVISION and row["smoke"] == first["smoke"], "source/mode changed")
+ environment = row["environment"]
+ require(environment["qualification"] is False, "environment incorrectly marks qualification")
+ require(
+ environment["installed_distributions_sha256"] == fingerprint(environment["installed_distributions"]),
+ "installed distribution manifest hash differs",
+ )
+ require(all(environment[key] == first["environment"][key] for key in fields), "process environments differ")
+ require(set(environment["source_imports"]) == {"adapter", "server", "sdk"}, "missing source import proof")
+ for imported in environment["source_imports"].values():
+ path = source / imported["relative_path"]
+ require(
+ path.resolve().is_relative_to(source.resolve()) and digest(path) == imported["sha256"],
+ "imported source differs from immutable baseline",
+ )
+ require(
+ row["actual_precision"] == first["actual_precision"] and row["fixtures"] == first["fixtures"],
+ "process model or precision differs",
+ )
+ initial, reference = row["required_telemetry_preflight"], first["required_telemetry_preflight"]
+ require(
+ initial["core"]["gc_thresholds"] == reference["core"]["gc_thresholds"]
+ and initial["affinity"] == reference["affinity"],
+ "cross-process GC configuration/affinity differs",
+ )
+ require(
+ all(initial["cgroup"].get(key) == reference["cgroup"].get(key) for key in ("path", "device", "inode"))
+ and all(initial["cgroup_files"].get(key) == reference["cgroup_files"].get(key) for key in CONTROL_FILES),
+ "cross-process cgroup identity/configuration differs",
+ )
+ require(
+ vector_delta(row["base_scores"], first["base_scores"]) <= RESTORE_ATOL, "cross-process base scores differ"
+ )
+ require(set(row["shape_oracles"]) == set(first["shape_oracles"]), "cross-process oracle workloads differ")
+ for key, value in row["shape_oracles"].items():
+ require(
+ vector_delta(value["scores"], first["shape_oracles"][key]["scores"]) <= RESTORE_ATOL,
+ "cross-process shape oracle differs",
+ )
+
+
+def analyze(rows: list[dict[str, Any]]) -> dict[str, Any]:
+ require(
+ len(rows) == PROCESSES and [row["process"] for row in rows] == list(range(PROCESSES)),
+ "six process units required",
+ )
+ require(len({row["pid"] for row in rows}) == PROCESSES, "fresh process identities required")
+ require(
+ all(row["qualification"] is False and row["completed"] and row["shutdown_complete"] for row in rows),
+ "incomplete diagnostic process",
+ )
+ schedule = [(c, w) for c in CONCURRENCIES for w, _ in WINDOWS]
+ require(
+ all([(w["concurrency"], w["window"]) for w in row["windows"]] == schedule for row in rows),
+ "all54 fixed windows required",
+ )
+ gates, dispersion = {}, {}
+ for concurrency, field in METRICS:
+ name = f"C{concurrency}_{field}"
+ values = {
+ window: [
+ next(w for w in row["windows"] if w["concurrency"] == concurrency and w["window"] == window)["metrics"][
+ field
+ ]
+ for row in rows
+ ]
+ for window, _ in WINDOWS
+ }
+ require(
+ all(math.isfinite(x) and x > 0 for series in values.values() for x in series), "invalid statistical operand"
+ )
+ ratio = [a / b if field == "rps" else b / a for a, b in zip(values["L1"], values["L2"], strict=True)]
+ gates[name] = {
+ "L2_over_L1_degradation": paired_gate(ratio),
+ "L1_over_L2_degradation": paired_gate([1 / value for value in ratio]),
+ }
+ dispersion[name] = {}
+ for window, series in values.items():
+ dispersion[name][window] = {
+ "values_by_process": series,
+ "min": min(series),
+ "median": statistics.median(series),
+ "max": max(series),
+ "max_over_min": max(series) / min(series),
+ "arithmetic_cv": statistics.stdev(series) / statistics.mean(series),
+ "log_sd": statistics.stdev([math.log(x) for x in series]),
+ }
+ passed = all(gate["passed"] for metric in gates.values() for gate in metric.values())
+ return {
+ "qualification": False,
+ "statistical_stability_passed": passed,
+ "directional_gates": gates,
+ "between_process_dispersion": dispersion,
+ "resource_telemetry_complete": all(w["resource_telemetry_complete"] for row in rows for w in row["windows"]),
+ "interpretation": "Symmetric later-window agreement only; no causal warmup claim or fresh-process candidate qualification",
+ }
+
+
+def admission(now: float, deadline: float, trial_limit: float) -> dict[str, float]:
+ reserve = trial_limit + WAIT_GRACE_SECONDS + TERMINATE_GRACE_SECONDS + KILL_GRACE_SECONDS
+ require(now + reserve < deadline, "insufficient time for complete diagnostic worker and cleanup")
+ return {
+ "monotonic_s": now,
+ "deadline_monotonic_s": deadline,
+ "reserve_s": reserve,
+ "margin_s": deadline - now - reserve,
+ }
+
+
+def run(args: argparse.Namespace) -> dict[str, Any]:
+ started = time.monotonic()
+ args.output.mkdir(parents=True, exist_ok=False)
+ manifest = extract(args.inputs.resolve(), args.output / "sources")
+ summary: dict[str, Any] = {
+ "schema": 1,
+ "qualification": False,
+ "diagnostic_only": True,
+ "completed": False,
+ "statistical_stability_passed": False,
+ "smoke": args.smoke,
+ "source_manifest": manifest,
+ "executed_source": "B",
+ "harness_manifest": harness_manifest(),
+ "processes": [],
+ }
+ if args.environment_receipt is not None:
+ summary["environment_preparation"] = {
+ "sha256": digest(args.environment_receipt),
+ "receipt": json.loads(args.environment_receipt.read_text()),
+ }
+ deadline = started + args.deadline_s
+ summary["runner_clock"] = {
+ "monotonic_start_s": started,
+ "wall_start_ns": time.time_ns(),
+ "deadline_monotonic_s": deadline,
+ "budget_s": args.deadline_s,
+ }
+ summary["methods"] = {
+ "window_order": [{"name": name, "sequential_warmup_calls": count} for name, count in WINDOWS],
+ "phase_order": list(CONCURRENCIES),
+ "fresh_baseline_processes": PROCESSES,
+ "gc": "Original automatic GC; no extra per-window collections, disabling or callbacks",
+ "raw_retention": "Per-window raw disk records; previous payload references are released normally",
+ "telemetry_scope": "Whole performance call including warmup, drain, validation and raw emission; not timed-only",
+ "qualification": False,
+ }
+ try:
+ for index in range(1 if args.smoke else PROCESSES):
+ reserved = admission(time.monotonic(), deadline, args.trial_deadline_s)
+ directory = args.output / f"process-{index}"
+ source = (args.output / "sources/B").resolve()
+ command = [
+ sys.executable,
+ "-m",
+ "tools.benchmarks.cross_encoder_lora.stability",
+ "--worker",
+ "--source",
+ str(source),
+ "--process",
+ str(index),
+ "--fixtures",
+ str(args.fixtures.resolve()),
+ "--output",
+ str(directory.resolve()),
+ "--trial-deadline-s",
+ str(args.trial_deadline_s),
+ ]
+ if args.smoke:
+ command.append("--smoke")
+ print(f"Starting baseline process {index + 1}", flush=True)
+ process = None
+ error = None
+ with (args.output / f"process-{index}.log").open("w") as log:
+ try:
+ process = subprocess.Popen(
+ command, env=source_environment(source), cwd=source, stdout=log, stderr=subprocess.STDOUT
+ )
+ process.wait(timeout=args.trial_deadline_s + WAIT_GRACE_SECONDS)
+ except BaseException as caught: # noqa: BLE001 -- persist error after unconditional cleanup, then re-raise
+ error = caught
+ finally:
+ if process is not None:
+ stop(process)
+ terminal = directory / "process.json"
+ record = (
+ json.loads(terminal.read_text())
+ if terminal.exists()
+ else {"qualification": False, "completed": False, "shutdown_complete": False}
+ )
+ row = {
+ "qualification": False,
+ "process": index,
+ "pid": record.get("pid"),
+ "completed": record["completed"] and error is None,
+ "shutdown_complete": record["shutdown_complete"],
+ "returncode": process.returncode if process is not None else None,
+ "admission": reserved,
+ "record": f"process-{index}/process.json",
+ "sha256": digest(terminal) if terminal.exists() else None,
+ }
+ if error is not None:
+ row["error"] = worker.error_record(error)
+ summary["processes"].append(row)
+ atomic_json(args.output / "summary.json", summary)
+ if error is not None:
+ raise error
+ require(
+ row["returncode"] == 0 and row["completed"] and row["shutdown_complete"],
+ "baseline process failed; evidence retained",
+ )
+ print(f"Finished baseline process {index + 1}", flush=True)
+ complete = []
+ for row in summary["processes"]:
+ path = args.output / row["record"]
+ require(digest(path) == row["sha256"], "process record changed")
+ record = json.loads(path.read_text())
+ require(record["process"] == row["process"] and record["pid"] == row["pid"], "process identity mismatch")
+ previous = record["required_telemetry_preflight"]
+ for entry in record["windows"]:
+ reopened = reopen_window(path.parent, entry)
+ telemetry_delta(previous, reopened["telemetry_before"])
+ previous = reopened["telemetry_after"]
+ entry["metrics"] = metrics(reopened["performance"])
+ require(
+ entry["resource_telemetry_complete"] == reopened["telemetry_delta"]["resource_telemetry_complete"],
+ "telemetry projection mismatch",
+ )
+ complete.append(record)
+ verify_process_sources(complete, (args.output / "sources/B").resolve())
+ summary["completed"] = True
+ if not args.smoke:
+ summary["analysis"] = analyze(complete)
+ summary["statistical_stability_passed"] = summary["analysis"]["statistical_stability_passed"]
+ require(summary["statistical_stability_passed"], "fixed symmetric baseline stability gates did not pass")
+ except BaseException as error:
+ summary["error"] = worker.error_record(error)
+ raise
+ finally:
+ atomic_json(args.output / "summary.json", summary)
+ return summary
+
+
+def main() -> None:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--inputs", type=Path)
+ parser.add_argument("--fixtures", type=Path, required=True)
+ parser.add_argument("--output", type=Path, required=True)
+ parser.add_argument("--deadline-s", type=float, default=RUNNER_LIMIT)
+ parser.add_argument("--trial-deadline-s", type=float, default=WORKER_LIMIT)
+ parser.add_argument("--environment-receipt", type=Path)
+ parser.add_argument("--smoke", action="store_true")
+ parser.add_argument("--worker", action="store_true", help=argparse.SUPPRESS)
+ parser.add_argument("--source", type=Path, help=argparse.SUPPRESS)
+ parser.add_argument("--process", type=int, default=0, help=argparse.SUPPRESS)
+ args = parser.parse_args()
+ require(
+ 0 < args.trial_deadline_s <= WORKER_LIMIT and 0 < args.deadline_s <= RUNNER_LIMIT,
+ "diagnostic deadline outside fixed bounds",
+ )
+ if args.worker:
+ require(args.source is not None, "worker requires exact source")
+ asyncio.run(child(args))
+ else:
+ require(args.inputs is not None, "runner requires immutable inputs")
+ signal.signal(signal.SIGTERM, terminate)
+ run(args)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/tools/benchmarks/cross_encoder_lora/tests/test_stability.py b/tools/benchmarks/cross_encoder_lora/tests/test_stability.py
new file mode 100644
index 000000000..dc16e939b
--- /dev/null
+++ b/tools/benchmarks/cross_encoder_lora/tests/test_stability.py
@@ -0,0 +1,499 @@
+"""Fixed baseline diagnostic controls; CPU smoke never qualifies GPU performance."""
+
+# Owned Python children run the exact local diagnostic, without a shell.
+# ruff: noqa: S603
+
+from __future__ import annotations
+
+import ast
+import copy
+import json
+import subprocess
+import sys
+from pathlib import Path
+from typing import Any
+
+import pytest
+
+from tools.benchmarks.cross_encoder_lora import prepare as source_preparer
+from tools.benchmarks.cross_encoder_lora import stability as probe
+from tools.benchmarks.cross_encoder_lora import worker
+from tools.benchmarks.cross_encoder_lora.evidence import digest, fingerprint
+from tools.benchmarks.cross_encoder_lora.fixtures import prepare
+from tools.benchmarks.cross_encoder_lora.protocol import BASE_REVISION, SEQUENTIAL
+from tools.benchmarks.cross_encoder_lora.runner import ROOT, extract, source_environment
+from tools.benchmarks.cross_encoder_lora.tests.test_probe import tiny_snapshot
+
+
+def observed(tick: int = 1) -> dict[str, Any]:
+ return {
+ "qualification": False,
+ "core": {
+ "wall_ns": tick * 1000000000,
+ "monotonic_ns": tick * 1000000000,
+ "rusage": dict.fromkeys(probe.RUSAGE_FIELDS, tick),
+ "gc_enabled": True,
+ "gc_thresholds": [700, 10, 10],
+ "gc_count": [tick, 0, 0],
+ "gc_stats": [dict.fromkeys(probe.GC_FIELDS, tick) for _ in range(3)],
+ },
+ "cgroup": {"path": None, "error": "unavailable"},
+ "cgroup_files": dict.fromkeys(probe.OPTIONAL_FILES),
+ "optional_errors": {"cgroup": "unavailable"},
+ "affinity": None,
+ "snapshot_read_ns": 1,
+ }
+
+
+def process_rows() -> list[dict[str, Any]]:
+ return [
+ {
+ "qualification": False,
+ "completed": True,
+ "shutdown_complete": True,
+ "process": index,
+ "pid": index + 100,
+ "windows": [
+ {
+ "concurrency": c,
+ "window": name,
+ "resource_telemetry_complete": False,
+ "metrics": {"mean_ms": 10.0, "p95_ms": 12.0, "rps": 100.0},
+ }
+ for c in probe.CONCURRENCIES
+ for name, _ in probe.WINDOWS
+ ],
+ }
+ for index in range(probe.PROCESSES)
+ ]
+
+
+def test_symmetric_stability_and_between_process_dispersion() -> None:
+ records = process_rows()
+ result = probe.analyze(records)
+ assert result["statistical_stability_passed"]
+ assert not result["qualification"]
+ assert not result["resource_telemetry_complete"]
+ assert sum(len(value) for value in result["directional_gates"].values()) == 12
+ for row in records:
+ for window in row["windows"]:
+ if window["window"] == "L2":
+ window["metrics"]["mean_ms"] *= 0.9
+ result = probe.analyze(records)
+ gates = result["directional_gates"]["C1_mean_ms"]
+ assert gates["L2_over_L1_degradation"]["passed"]
+ assert not gates["L1_over_L2_degradation"]["passed"]
+ assert not result["statistical_stability_passed"]
+ assert result["between_process_dispersion"]["C1_mean_ms"]["L2"]["values_by_process"] == [9.0] * 6
+
+
+ "failure", ["missing_process", "same_pid", "missing_window", "order", "cleanup", "negative", "uncertain"]
+)
+def test_all_fixed_process_units_and_positive_metrics_required(failure: str) -> None:
+ rows = process_rows()
+ if failure == "missing_process":
+ rows.pop()
+ elif failure == "same_pid":
+ rows[-1]["pid"] = rows[0]["pid"]
+ elif failure == "missing_window":
+ rows[-1]["windows"].pop()
+ elif failure == "order":
+ rows[-1]["windows"].reverse()
+ elif failure == "cleanup":
+ rows[-1]["shutdown_complete"] = False
+ elif failure == "negative":
+ for row in rows:
+ row["windows"][1]["metrics"]["mean_ms"] = -10
+ row["windows"][2]["metrics"]["mean_ms"] = -10
+ else:
+ for row, ratio in zip(rows, [0.94, 0.94, 0.94, 1.06, 1.06, 1.06], strict=True):
+ row["windows"][2]["metrics"]["mean_ms"] *= ratio
+ assert not probe.analyze(rows)["statistical_stability_passed"]
+ return
+ with pytest.raises(AssertionError):
+ probe.analyze(rows)
+
+
+def test_optional_cgroup_unavailable_is_explicit_not_zero(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(probe, "unified_cgroup", lambda: {"path": None, "error": "not exposed"})
+ first, last = probe.snapshot(), probe.snapshot()
+ delta = probe.telemetry_delta(first, last)
+ assert set(first["cgroup_files"]) == set(probe.OPTIONAL_FILES)
+ assert all(value is None for value in first["cgroup_files"].values())
+ assert delta["optional_cgroup"]["cpu.stat"] is None
+ assert not delta["resource_telemetry_complete"]
+ assert delta["optional_errors_before"]["cgroup"] == "not exposed"
+
+
[email protected]("failure", ["gc_missing", "gc_count", "rusage", "clock", "disabled"])
+def test_required_telemetry_validation(failure: str) -> None:
+ core = observed()["core"]
+ if failure == "gc_missing":
+ core["gc_stats"] = []
+ elif failure == "gc_count":
+ core["gc_count"] = [-1, 0, 0]
+ elif failure == "rusage":
+ core["rusage"]["ru_utime"] = float("nan")
+ elif failure == "clock":
+ core["wall_ns"] = 0
+ else:
+ core["gc_enabled"] = False
+ with pytest.raises(AssertionError):
+ probe.validate_core(core)
+
+
+def test_own_cgroup_resolution_and_no_namespace_fallback(tmp_path: Path) -> None:
+ proc = tmp_path / "proc"
+ proc.mkdir()
+ mount = tmp_path / "mounted"
+ owned = mount / "child"
+ owned.mkdir(parents=True)
+ (proc / "cgroup").write_text("0::/namespace/child\n")
+ line = f"1 0 0:1 /namespace {mount} rw - cgroup2 cgroup rw\n"
+ (proc / "mountinfo").write_text(line)
+ assert probe.unified_cgroup(proc)["path"] == str(owned)
+ (proc / "mountinfo").write_text(line + line)
+ assert probe.unified_cgroup(proc)["path"] is None
+ (proc / "cgroup").write_text("0::/other/child\n")
+ (proc / "mountinfo").write_text(line)
+ assert probe.unified_cgroup(proc)["path"] is None
+
+
+def test_exposed_counters_and_gauges_have_distinct_semantics() -> None:
+ first, last = observed(1), observed(2)
+ for record, ticks, avg in ((first, 100, 90), (last, 120, 1)):
+ record["cgroup"] = {"path": "/owned", "device": 1, "inode": 2, "error": None}
+ record["cgroup_files"]["cpu.stat"] = probe.flat_counters(f"usage_usec {ticks}\nnr_throttled 1\n")
+ record["cgroup_files"]["cpu.pressure"] = probe.pressure(f"some avg10={avg} avg60=0 avg300=0 total={ticks}\n")
+ last["core"]["gc_count"] = [0, 0, 0] # Automatic GC resets this gauge normally.
+ delta = probe.telemetry_delta(first, last)
+ assert delta["cpu_pressure_counters_available"]
+ assert not delta["resource_telemetry_complete"] # Other optional files remain unavailable.
+ assert delta["optional_cgroup"]["cpu.pressure"]["some"] == 20
+ assert delta["optional_cgroup"]["cpu.stat"]["usage_usec"] == 20
+ last["cgroup_files"]["cpu.pressure"]["some"]["total"] = 99
+ with pytest.raises(AssertionError, match="reset"):
+ probe.telemetry_delta(first, last)
+ for text in ("usage_usec -1", "usage_usec 0\nusage_usec 1", ""):
+ with pytest.raises((AssertionError, ValueError)):
+ probe.flat_counters(text)
+ with pytest.raises(AssertionError, match="PSI"):
+ probe.pressure("some avg10=nan avg60=0 avg300=0 total=1")
+
+
+ "changed", ["path", "inode", "visibility", "affinity", "quota", "gc_thresholds", "rusage", "gc_stats"]
+)
+def test_telemetry_continuity_refuses_changes(changed: str) -> None:
+ first, last = observed(2), observed(3)
+ first["cgroup"] = {"path": "/owned", "device": 1, "inode": 2, "error": None}
+ last["cgroup"] = copy.deepcopy(first["cgroup"])
+ if changed in {"path", "inode"}:
+ last["cgroup"][changed] = "changed"
+ elif changed == "visibility":
+ last["cgroup"] = {"path": None, "error": "lost"}
+ elif changed == "affinity":
+ last["affinity"] = [0]
+ elif changed == "quota":
+ last["cgroup_files"]["cpu.max"] = "800000 100000"
+ elif changed == "gc_thresholds":
+ last["core"]["gc_thresholds"][0] = 800
+ elif changed == "rusage":
+ last["core"]["rusage"]["ru_nvcsw"] = 1
+ else:
+ last["core"]["gc_stats"][0]["collections"] = 1
+ with pytest.raises(AssertionError):
+ probe.telemetry_delta(first, last)
+
+
+def response(label: str, index: int) -> dict[str, Any]:
+ scores = [4.0, 3.0, 2.0, 1.0]
+ return {
+ "label": label,
+ "selection": None,
+ "start_ns": index * 10000000 + 1,
+ "end_ns": (index + 1) * 10000000,
+ "latency_ms": 10.0,
+ "scores": scores,
+ "responses": [{"status": 200, "timing_headers": {"x-inference-time": "4.0"}}],
+ "result": {
+ "scores": [{"item_id": f"{label}:{i}", "rank": i, "score": score} for i, score in enumerate(scores)]
+ },
+ }
+
+
+def stub_performance(_url: str, concurrency: int, evidence: probe.DiagnosticEvidence, *, smoke: bool) -> dict[str, Any]:
+ warmups = [response(f"warm-{i}", i) for i in range(2 if smoke else worker.WARMUP)]
+ samples = [response(f"sample-{i}", i) for i in range(4 if smoke else SEQUENTIAL)]
+ elapsed = 0.2 if smoke else 12.0
+ evidence.emit(
+ "performance_attempt",
+ concurrency=concurrency,
+ warmups=warmups,
+ completed_samples=samples,
+ wire_responses=[row["responses"][0] for row in [*warmups, *samples]],
+ )
+ return {
+ "concurrency": concurrency,
+ "count": len(samples),
+ "elapsed_s": elapsed,
+ "mean_ms": 10.0,
+ "p95_ms": 10.0,
+ "rps": len(samples) / elapsed,
+ "samples": samples,
+ }
+
+
+def install_stub(monkeypatch: pytest.MonkeyPatch) -> None:
+ counter = iter(range(1, 100))
+ monkeypatch.setattr(probe, "snapshot", lambda: observed(next(counter)))
+ monkeypatch.setattr(worker, "telemetry", lambda: {"device": "test"})
+ monkeypatch.setattr(worker, "performance", stub_performance)
+
+
+def test_raw_retention_reopen_and_warmup_restoration(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ install_stub(monkeypatch)
+ original = worker.WARMUP
+ continuity = observed(0)
+ continuity["core"]["wall_ns"] = continuity["core"]["monotonic_ns"] = 1
+ oracles = {"shape": {"copies": 1, "scores": [[4.0, 3.0, 2.0, 1.0][index] for index in worker.ORDER]}}
+ compact = probe.measure_window(
+ "unused", 1, "L1", 200, tmp_path / "C1/L1", [4.0, 3.0, 2.0, 1.0], oracles, smoke=False, continuity=continuity
+ )
+ assert original == worker.WARMUP
+ raw = probe.reopen_window(tmp_path, compact)
+ assert len(raw["performance"]["samples"]) == 200
+ assert len(raw["chronological_quartiles"]) == 4
+ assert continuity == raw["telemetry_after"]
+ assert all(row["diagnostic_matching_oracle"]["workload_sha256"] == "shape" for row in raw["performance"]["samples"])
+ assert "samples" not in json.dumps(compact)
+ tampered = copy.deepcopy(compact)
+ tampered["metrics"]["mean_ms"] = 9.0
+ with pytest.raises(AssertionError, match="projection"):
+ probe.reopen_window(tmp_path, tampered)
+ tampered = {**compact, "record": "../outside.json"}
+ with pytest.raises(AssertionError, match="path"):
+ probe.reopen_window(tmp_path, tampered)
+ (tmp_path / "C1/L1/events.jsonl").write_text("{}\n")
+ with pytest.raises(AssertionError, match="attempt hash"):
+ probe.reopen_window(tmp_path, compact)
+
+
+def test_failed_window_keeps_partial_raw_and_restores_warmup(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ install_stub(monkeypatch)
+ original = worker.WARMUP
+
+ def failed(_url: str, _concurrency: int, evidence: probe.DiagnosticEvidence, *, smoke: bool) -> None:
+ assert worker.WARMUP == 200
+ assert not smoke
+ evidence.emit("performance_attempt", completed_samples=[{"error": "retained"}])
+ raise RuntimeError("request failed")
+
+ monkeypatch.setattr(worker, "performance", failed)
+ with pytest.raises(RuntimeError, match="request failed"):
+ probe.measure_window("unused", 1, "L2", 200, tmp_path / "C1/L2", [4.0, 3.0, 2.0, 1.0], {}, smoke=False)
+ assert original == worker.WARMUP
+ terminal = json.loads((tmp_path / "C1/L2/window.json").read_text())
+ assert not terminal["qualification"]
+ assert not terminal["completed"]
+ assert terminal["error"]["message"] == "request failed"
+ assert json.loads((tmp_path / "C1/L2/events.jsonl").read_text())["completed_samples"] == [{"error": "retained"}]
+
+
+def test_full_cleanup_budget_is_strict() -> None:
+ assert probe.admission(1, 272, 180)["reserve_s"] == 270
+ with pytest.raises(AssertionError, match="insufficient"):
+ probe.admission(1, 271, 180)
+
+
+def test_negative_raw_latency_cannot_hide_in_positive_aggregates() -> None:
+ phase = {
+ "samples": [{"latency_ms": value} for value in [-1.0, 2.0, 3.0, 4.0]],
+ "elapsed_s": 1.0,
+ "count": 4,
+ "mean_ms": 2.0,
+ "p95_ms": 4.0,
+ "rps": 4.0,
+ }
+ with pytest.raises(AssertionError, match="raw latency"):
+ probe.metrics(phase)
+
+
+def test_shared_timed_code_and_gc_are_unmodified() -> None:
+ baseline = subprocess.run(
+ ["git", "show", "f06f414a:tools/benchmarks/cross_encoder_lora/worker.py"], # noqa: S607
+ cwd=ROOT,
+ capture_output=True,
+ text=True,
+ check=False,
+ )
+ if baseline.returncode:
+ pytest.skip("timed-code invariant control requires its documented parent commit in git history")
+ names = {"performance", "Client", "unobserved"}
+
+ def code(text: str) -> dict[str, str]:
+ return {
+ node.name: ast.dump(node)
+ for node in ast.parse(text).body
+ if isinstance(node, (ast.FunctionDef, ast.ClassDef)) and node.name in names
+ }
+
+ assert code(Path(worker.__file__).read_text()) == code(baseline.stdout)
+ tree = ast.parse(Path(probe.__file__).read_text())
+ assert not any(
+ isinstance(node, ast.Call)
+ and isinstance(node.func, ast.Attribute)
+ and isinstance(node.func.value, ast.Name)
+ and node.func.value.id == "gc"
+ and node.func.attr in {"collect", "disable", "set_threshold"}
+ for node in ast.walk(tree)
+ )
+
+
+def test_cross_process_source_and_environment_manifest(tmp_path: Path) -> None:
+ (tmp_path / "module.py").write_text("# immutable baseline fixture\n")
+ distribution = [["torch", "exact"]]
+ environment = {
+ "qualification": False,
+ "versions": {},
+ "torchao": None,
+ "python": "fixed",
+ "threads": 4,
+ "device": "cpu",
+ "torch_cuda": None,
+ "hardware_preflight_only": False,
+ "installed_distributions": distribution,
+ "installed_distributions_sha256": fingerprint(distribution),
+ "source_imports": {
+ name: {"relative_path": "module.py", "sha256": digest(tmp_path / "module.py")}
+ for name in ("sdk", "server", "adapter")
+ },
+ }
+ row: dict[str, Any] = {
+ "source_commit": BASE_REVISION,
+ "smoke": True,
+ "environment": environment,
+ "fixtures": {},
+ "actual_precision": "float32",
+ "base_scores": [1.0],
+ "shape_oracles": {"a": {"scores": [1.0]}},
+ "required_telemetry_preflight": observed(),
+ }
+ other = copy.deepcopy(row)
+ probe.verify_process_sources([row, other], tmp_path)
+ other["environment"]["installed_distributions"] = [["torch", "different"]]
+ other["environment"]["installed_distributions_sha256"] = fingerprint(
+ other["environment"]["installed_distributions"]
+ )
+ with pytest.raises(AssertionError, match="environments differ"):
+ probe.verify_process_sources([row, other], tmp_path)
+ other = copy.deepcopy(row)
+ other["required_telemetry_preflight"]["affinity"] = [0]
+ with pytest.raises(AssertionError, match="affinity differs"):
+ probe.verify_process_sources([row, other], tmp_path)
+ (tmp_path / "module.py").write_text("# changed\n")
+ with pytest.raises(AssertionError, match="imported source differs"):
+ probe.verify_process_sources([row], tmp_path)
+
+
[email protected](scope="module")
+def real_inputs(tmp_path_factory: pytest.TempPathFactory) -> tuple[Path, Path, Path]:
+ directory = tmp_path_factory.mktemp("stability-real")
+ available = subprocess.run(["git", "cat-file", "-e", BASE_REVISION], cwd=ROOT, capture_output=True, check=False) # noqa: S607
+ if available.returncode:
+ pytest.skip("immutable B smoke requires the documented baseline in git history")
+ snapshot = tiny_snapshot(directory / "model")
+ fixtures, inputs = directory / "fixtures", directory / "inputs"
+ prepare(fixtures, snapshot, smoke=True)
+ source_preparer.prepare(ROOT, inputs, "HEAD")
+ return directory, fixtures, inputs
+
+
+def test_real_immutable_baseline_nine_window_sdk_smoke(real_inputs: tuple[Path, Path, Path]) -> None:
+ directory, fixtures, inputs = real_inputs
+ output = directory / "diagnostic"
+ result = subprocess.run(
+ [
+ sys.executable,
+ "-m",
+ "tools.benchmarks.cross_encoder_lora.stability",
+ "--inputs",
+ str(inputs),
+ "--fixtures",
+ str(fixtures),
+ "--output",
+ str(output),
+ "--smoke",
+ "--deadline-s",
+ "300",
+ "--trial-deadline-s",
+ "90",
+ ],
+ cwd=ROOT,
+ env=source_environment(ROOT),
+ capture_output=True,
+ text=True,
+ timeout=150,
+ check=False,
+ )
+ summary = json.loads((output / "summary.json").read_text())
+ assert result.returncode == 0, result.stdout + result.stderr + (output / "process-0.log").read_text()
+ assert summary["completed"]
+ assert not summary["qualification"]
+ assert not summary["statistical_stability_passed"]
+ process = json.loads((output / "process-0/process.json").read_text())
+ assert process["shutdown_complete"]
+ assert not process["environment"]["qualification"]
+ assert len(process["windows"]) == 9
+ assert process["source_commit"] == BASE_REVISION
+ for row in process["windows"]:
+ window = probe.reopen_window(output / "process-0", row)
+ assert window["warmups"] == 2
+ assert not window["qualification"]
+ assert all(batch["forwards"] for replay in process["untimed_replays"].values() for batch in replay["batches"])
+ probe.verify_process_sources([process], output / "sources/B")
+
+
+def test_required_preflight_and_live_failure_cleanup(real_inputs: tuple[Path, Path, Path]) -> None:
+ directory, fixtures, inputs = real_inputs
+ sources = directory / "failure-sources"
+ extract(inputs, sources)
+ source = sources / "B"
+ for failure in ("preflight", "window"):
+ output = directory / f"failed-{failure}"
+ script = """
+import argparse, asyncio, json
+from pathlib import Path
+from tools.benchmarks.cross_encoder_lora import stability as s
+def fail(*args, **kwargs):
+ raise RuntimeError('injected diagnostic failure')
+args = argparse.Namespace(source=Path(SOURCE), fixtures=Path(FIXTURES), output=Path(OUTPUT), process=0,
+ smoke=True, trial_deadline_s=90)
+if FAILURE == 'preflight':
+ s.snapshot = fail
+ s.serve_windows = lambda *args: (_ for _ in ()).throw(AssertionError('model work before telemetry'))
+else:
+ s.measure_window = fail
+asyncio.run(s.child(args))
+"""
+ bindings = f"SOURCE={str(source)!r}\nFIXTURES={str(fixtures)!r}\nOUTPUT={str(output)!r}\nFAILURE={failure!r}\n"
+ result = subprocess.run(
+ [sys.executable, "-c", bindings + script],
+ cwd=source,
+ env=source_environment(source),
+ capture_output=True,
+ text=True,
+ timeout=130,
+ check=False,
+ )
+ assert result.returncode != 0
+ record = json.loads((output / "process.json").read_text())
+ assert not record["completed"]
+ assert not record["qualification"]
+ assert record["error"]["message"] == "injected diagnostic failure", result.stdout + result.stderr
+ assert record["shutdown_complete"] == (failure == "window")
+ if failure == "preflight":
+ assert "environment" not in record
+ else:
+ assert record["untimed_replays"]["16"]["batches"]