Files
OBLITERATUS/obliteratus/experiments/runner.py
Joseph Magly 55aad5525b fix(models): keep handle cleanup running through observer, storage and rmtree failures
Muse BT6 audit of 3c7a6ca for #671: the experiments runner cleans up the
handle even when observers.close() fails after a successful run; the neuron
lab notes a storage cleanup failure on the model-cleanup error and lets
interrupts win; candidate and smaller_change wrap directory removal so handle
cleanup always runs and the primary error stays primary (shutil imported at
module top). A shared run_during_error helper carries the logic, and the
replaced-restore-point warning names the path to remove.
2026-11-25 11:21:46 -05:00

277 lines
13 KiB
Python

"""Baseline observation runner: score generations, capture activations, profile.
``run_observation`` is the single entry point shared by the CLI and any UI. It
runs only on an unmodified model; see ``inputs.require_unmodified``. Each
step (one generation step per task, then an optional capture) has an identity
computed from the spec digest, dataset revision, observed model/tokenizer/
adapter identity and step parameters. A later attempt of the same spec reuses
a step only when an earlier ``complete`` step has exactly that identity and its
artifact digests still verify; everything else is recomputed.
"""
from contextlib import ExitStack, nullcontext
from copy import deepcopy
from pathlib import Path
from obliteratus.analysis.activation_capture import capture_activations
from obliteratus.analysis.activation_observer import decoder_output_readout
from obliteratus.analysis.capture_identity import capture_identity, digest
from obliteratus.concepts.dataset import identity
from obliteratus.concepts.scorecard import build_baseline_scorecard
from obliteratus.experiments import generation as _generation
from obliteratus.experiments import inputs as _inputs
from obliteratus.experiments.spec import load_spec
from obliteratus.experiments.store import (
TERMINAL, ObservationStore, StoreError, atomic_json, now, read_json,
)
from obliteratus.handle_cleanup import cleanup_during_error, run_during_error
RESULT_SCHEMA = "obliteratus.observation-result/1"
STEP_VERSION = 1
READOUTS = {"decoder_output": decoder_output_readout}
class ObservationCancelled(RuntimeError):
"""Raise from ``check_cancelled`` to stop a run cooperatively."""
class _Cancel:
def __init__(self, check):
self.check, self.fired = check or (lambda: None), False
def __call__(self, *_args):
try:
self.check()
except BaseException:
self.fired = True
raise
class _NoCollection:
"""Work counters patch a pipeline collector; observation runs have none."""
def _collect_activations(self, *_args, **_kwargs): # pragma: no cover - never called
raise RuntimeError("observation runs do not collect pipeline activations")
def run_observation(spec, store, *, model_factory=None, pack_resolver=None,
check_cancelled=None, rerun=False):
"""Run (or return the completed result of) one baseline observation spec.
``spec`` is a mapping or JSON path. ``model_factory(model_section)`` returns
an object with ``model``, ``tokenizer`` and optional ``cleanup()``; the
default uses the project's model loader. Exceptions from
``check_cancelled`` cancel the attempt and propagate.
"""
spec = load_spec(spec)
run = ObservationStore(store).open(spec)
latest = run.latest_complete()
if latest is not None and not rerun:
return load_result(run.attempt_path(latest))
cancel = _Cancel(check_cancelled)
with run.lock():
attempt = run.new_attempt()
try:
result = _attempt(spec, run, attempt, model_factory, pack_resolver, cancel)
atomic_json(attempt.path / "result.json", result)
attempt.transition("complete")
except BaseException as error:
if attempt.state not in TERMINAL:
cancelled = cancel.fired or isinstance(error, KeyboardInterrupt)
attempt.transition("cancelled" if cancelled else "failed",
reason=f"{type(error).__name__}: {error}"[:2000],
error_type=type(error).__name__)
raise
return load_result(attempt.path)
def _attempt(spec, run, attempt, model_factory, pack_resolver, cancel):
profiler = None
if spec["profile"]:
from obliteratus.analysis.profiling import LocalCostProfile
from obliteratus.analysis.work_counters import WorkCounters
profiler = LocalCostProfile()
profiler.work = WorkCounters()
recording = nullcontext() if profiler is None else profiler.invocation(
attempt.path / "profile.json", model_id=spec["model"]["id"],
pack_id=spec["dataset"]["pack"],
settings={"spec_digest": run.digest, "seed": spec["seed"], **spec["decoding"]})
stage = profiler.stage if profiler is not None else (lambda _name: nullcontext())
handle = None
with recording, ExitStack() as observers:
if profiler is not None:
profiler.report["workflow"] = "observation_baseline"
try:
with stage("resolve"):
dataset, tasks, data = _inputs.resolve_dataset(spec, pack_resolver)
with stage("load"):
handle = (model_factory or _inputs.default_model_factory)(deepcopy(spec["model"]))
model, tokenizer = handle.model, handle.tokenizer
adapter = _inputs.require_unmodified(model)
model_ident = _inputs.execution_identity(spec, model, tokenizer, adapter)
cancel()
attempt.transition("running")
if profiler is not None:
observers.enter_context(profiler.work.observe(model, _NoCollection()))
result = _execute(spec, run, attempt, dataset, tasks, data, model, tokenizer,
model_ident, cancel, stage)
except BaseException as error:
# Neither an observer close failure nor a cleanup failure replaces
# the in-flight error: each is noted on it, and cleanup always runs.
try:
run_during_error(error, "closing the work observers", observers.close)
finally:
cleanup_during_error(handle, error)
raise
try:
observers.close()
except BaseException as error:
# An observer close failure is the primary error; the handle is
# still cleaned up and a cleanup failure is noted on it.
cleanup_during_error(handle, error)
raise
cleanup = getattr(handle, "cleanup", None)
if callable(cleanup):
cleanup()
if profiler is not None:
result["profile"] = {"path": _relative(run, attempt.path / "profile.json"),
"counters": profiler.report["counters"],
"counter_reasons": profiler.report["counter_reasons"]}
return result
def _step(run, attempt, name, ident, produce):
"""Reuse an identity-matching completed step or produce it in this attempt."""
sha = digest(ident)
found = run.find_step(name, sha, attempt.number)
if found is not None:
number, source = found
record = attempt.record_step(name, ident, sha, reused_from={
"attempt": number, "path": _relative(run, source)})
return record, source
refused = run.reuse_refusal(name, ident, attempt.number)
target = attempt.step_dir(name)
target.mkdir(parents=True, exist_ok=False)
try:
produce(target)
except BaseException as error:
attempt.record_step(name, ident, sha, state="incomplete", reuse_refused=refused,
reason=f"{type(error).__name__}: {error}"[:2000])
raise
return attempt.record_step(name, ident, sha, reuse_refused=refused), target
def _execute(spec, run, attempt, dataset, tasks, data, model, tokenizer, model_ident, cancel, stage):
base = {"version": STEP_VERSION, "spec_digest": run.digest, "model": model_ident,
"dataset": {"revision": data["revision"], "role": data["role"]}}
steps, completions = {}, []
settings, device = _generation.prompt_settings(spec), model_ident["device"]
for task in tasks:
name = f"generate-{task.name}"
prompts = _generation.render_prompts(tokenizer, task.examples, settings)
ident = {**base, "step": "generate", "task": task.name, "seed": spec["seed"],
"decoding": spec["decoding"],
"examples": identity([[e.example_id, e.prompt] for e in task.examples]),
**_generation.step_identity(spec, tokenizer, prompts)}
with stage(name):
record, source = _step(run, attempt, name, ident, lambda out, task=task, prompts=prompts:
_generation.generate_task(model, tokenizer, task, prompts, spec,
cancel, out, device))
rows = read_json(source / "generations.json")["rows"]
if [row["example_id"] for row in rows] != [e.example_id for e in task.examples]:
raise StoreError(f"Generation artifact for {task.name} does not match its examples")
steps[name] = _step_summary(run, record, source)
completions.append(rows)
scorecard = build_baseline_scorecard(tasks, [[r["completion"] for r in rows]
for rows in completions])
for task, rows in zip(tasks, completions):
for record, row in zip(scorecard["tasks"][task.name]["records"], rows):
record.update(completion_sha256=digest(row["completion"]),
generated_tokens=row["generated_tokens"])
capture = None
if spec["capture"] is not None:
with stage("capture"):
capture, steps["capture"] = _capture(spec, run, attempt, base, dataset, tasks,
model, tokenizer, cancel, device)
execution = {"device": device, "dtype": model_ident["checkpoint"]["dtype"],
"strategy": spec["decoding"]["strategy"], "seed": spec["seed"],
"batch_size": _generation.batch_size(spec),
"chat_template": settings["chat_template"]}
return {"schema": RESULT_SCHEMA, "spec_digest": run.digest, "attempt": attempt.number,
"state": "complete", "completed_at": now(), "execution": execution,
"inputs": {"dataset": data, "model": model_ident}, "steps": steps,
"scorecard": scorecard, "capture": capture, "profile": None,
"tool": _tool()}
def _capture(spec, run, attempt, base, dataset, tasks, model, tokenizer, cancel, device):
config, settings = spec["capture"], _generation.prompt_settings(spec)
layers = _inputs.capture_sites(model, config["sites"])
seen, batches, order = set(), [], []
for task in tasks:
prompts = _generation.render_prompts(tokenizer, task.examples, settings)
for example in task.examples:
if example.example_id not in seen:
seen.add(example.example_id)
order.append(example.example_id)
batches.append(_generation.encode_prompt(tokenizer, prompts[example.example_id],
settings))
options = {"model_id": spec["model"]["id"], "phase": "prompt",
"readout_name": config["readout"]}
cap_ident = capture_identity(model, tokenizer, layers, batches, dataset=dataset, **options)
ident = {**base, "step": "capture", "capture_identity_sha256": digest(cap_ident),
"budget_bytes": config["budget_bytes"]}
record, source = _step(run, attempt, "capture", ident, lambda out: capture_activations(
model, tokenizer, layers, batches, out / "capture", readout=READOUTS[config["readout"]],
dataset=dataset, budget_bytes=config["budget_bytes"], check_cancelled=cancel,
device=device, **options))
manifest = read_json(source / "capture" / "manifest.json")
if manifest.get("state") != "complete" or manifest.get("identity_sha256") != digest(cap_ident):
raise StoreError("Capture artifact is incomplete or has a different identity")
summary = {"path": _relative(run, source / "capture"), "state": manifest["state"],
"identity_sha256": manifest["identity_sha256"],
"sites": list(layers), "batches": manifest["batches_expected"],
"shards": len(manifest["shards"]), "estimate": manifest["estimate"],
"measured": manifest["measured"],
# Shard i holds example_ids[i]; readers need not reconstruct the order.
"example_ids": order}
return summary, _step_summary(run, record, source)
def _step_summary(run, record, source):
return {"identity_sha256": record["identity_sha256"], "reused_from": record["reused_from"],
"path": _relative(run, source)}
def _relative(run, path):
return Path(path).relative_to(run.path).as_posix()
def _tool():
from obliteratus.provenance import tool_provenance
return tool_provenance()
def load_result(source):
"""Load a result from ``result.json``, an attempt directory or a run directory.
A run directory resolves to its newest ``complete`` attempt. The returned
mapping carries a ``location`` entry (not stored on disk) used to resolve
artifact paths.
"""
path = Path(source)
if path.is_dir() and (path / "spec.json").is_file():
from obliteratus.experiments.store import RunRecord
run = RunRecord(path, None)
latest = run.latest_complete()
if latest is None:
raise StoreError(f"Run {path} has no complete attempt")
path = run.attempt_path(latest)
if path.is_dir():
path = path / "result.json"
result = read_json(path)
if not isinstance(result, dict) or result.get("schema") != RESULT_SCHEMA:
raise StoreError(f"{path} is not an observation result")
result["location"] = {"result": str(path), "run_dir": str(path.parents[2])}
return result