mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Moves cleanup_on_exit/cleanup_during_error to the light obliteratus.handle_cleanup module (re-exported from the loader) and imports them at module top, so no import runs after a handle exists. Stores the closed policy before detaching the restore point, names the path to remove in handle-surfaced SnapshotErrors, guards post-load work in candidate, smaller_change and the sequence loaders, keeps observer-close and neuron-lab cleanup failures from replacing the primary error or skipping state reset, and adds failure-plus-failing-close tests for every listed site. Fixes #669
107 lines
5.6 KiB
Python
107 lines
5.6 KiB
Python
"""``experiments select-examples``: the O02 example-selection study on a labeled set.
|
|
|
|
Captures (or reuses, after full verification) the pack's labeled sets for
|
|
roles ``fit`` and ``select`` (legacy packs: ``fit`` only, evaluated on a
|
|
group-disjoint holdout fold) at one decoder layer of an unmodified model,
|
|
derives hashed lexical features from the in-memory row text (the text is
|
|
never written), and writes ``<site>.json`` next to an
|
|
observation run (``<run>/example-selection/``) or into a chosen directory,
|
|
alongside the verified labeled set it used.
|
|
"""
|
|
|
|
import json
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from obliteratus.analysis.example_selection import lexical_features
|
|
from obliteratus.analysis.example_selection_report import REPORT_DIR, write_report
|
|
from obliteratus.analysis.example_selection_study import run_selection_study
|
|
from obliteratus.analysis.labeled_activations import (
|
|
CAPTURE_DIR, MANIFEST, capture_labeled_set, load_labeled_set, plan_labeled_set,
|
|
)
|
|
from obliteratus.experiments import inputs as _inputs
|
|
from obliteratus.experiments.registry_store import assert_unexposed
|
|
from obliteratus.experiments.spec import SpecError, load_spec
|
|
from obliteratus.experiments.subspace import labeled_dir
|
|
from obliteratus.handle_cleanup import cleanup_on_exit
|
|
|
|
|
|
def _capture_cost(target, reused):
|
|
try:
|
|
manifest = json.loads((target / CAPTURE_DIR / "manifest.json").read_text())
|
|
measured = manifest["measured"]
|
|
except (OSError, ValueError, KeyError) as error:
|
|
return {"measured": False, "reason": f"capture manifest unreadable: {error}"}
|
|
return {"measured": True, "reused_labeled_set": reused,
|
|
"wall_seconds": measured.get("wall_seconds"),
|
|
"bytes_written": measured.get("bytes_written"),
|
|
"rows": len(manifest.get("shards", [])),
|
|
"note": "measured when the labeled set was captured (all captured rows and sites)"}
|
|
|
|
|
|
def run_select_examples(pack, model, tokenizer, layer, out_dir, *, model_id, config=None,
|
|
device="cpu", budget_bytes=None, layers=None, cache_dir=None,
|
|
timings=True, registry_dir=None):
|
|
"""Capture-or-reuse the labeled set, run the study at ``layer`` and write the JSON.
|
|
|
|
``registry_dir`` names a run's confirmation registry to consult when ``out_dir``
|
|
is not inside that run (``<out>`` and its parent are always consulted).
|
|
"""
|
|
_inputs.require_unmodified(model)
|
|
layers = layers or _inputs.capture_sites(model, [layer])
|
|
if len(layers) != 1:
|
|
raise ValueError("Example selection reads exactly one site")
|
|
site = next(iter(layers))
|
|
roles = ["fit", "select"] if "dataset" in pack.manifest else ["fit"]
|
|
assert_unexposed(lambda: plan_labeled_set(pack, roles, cache_dir=cache_dir)["rows"],
|
|
out_dir, Path(out_dir).parent, registry_dir)
|
|
target = labeled_dir(out_dir, site, roles)
|
|
options = {"model_id": model_id, "cache_dir": cache_dir}
|
|
reused = (target / MANIFEST).is_file()
|
|
if not reused:
|
|
capture_labeled_set(model, tokenizer, layers, pack, target, roles=roles, device=device,
|
|
budget_bytes=budget_bytes, **options)
|
|
labeled = load_labeled_set(target, model, tokenizer, layers, pack, **options)
|
|
started = time.perf_counter()
|
|
lexical = lexical_features(plan_labeled_set(pack, roles, cache_dir=cache_dir)["rows"])
|
|
lexical_seconds = time.perf_counter() - started
|
|
report = run_selection_study(labeled, site, lexical=lexical, config=config,
|
|
capture_cost=_capture_cost(target, reused),
|
|
lexical_seconds=lexical_seconds, timings=timings)
|
|
path = write_report(report, out_dir)
|
|
return {"report": str(path), "labeled_set": str(target), "reused_labeled_set": reused,
|
|
"recommendation": report["recommendation"]["verdict"],
|
|
"report_sha256": report["report_sha256"]}
|
|
|
|
|
|
def resolve_and_run(*, layer, run=None, pack=None, model=None, dtype="float32", revision=None,
|
|
device="cpu", out=None, config=None, budget_bytes=None, timings=True,
|
|
model_factory=None, pack_resolver=None):
|
|
"""Resolve the pack and model from an observation run directory or explicit references."""
|
|
if run is not None:
|
|
if pack is not None or model is not None:
|
|
raise SpecError("Pass either a run directory or --pack and --model, not both")
|
|
spec = load_spec(Path(run) / "spec.json")
|
|
pack_ref, section = spec["dataset"]["pack"], dict(spec["model"])
|
|
out = out or Path(run) / REPORT_DIR
|
|
else:
|
|
if pack is None or model is None or out is None:
|
|
raise SpecError("Without a run directory, --pack, --model and --out are required")
|
|
spec, pack_ref = None, pack
|
|
section = {"id": model, "dtype": dtype, "revision": revision, "device": device}
|
|
if isinstance(config, (str, Path)):
|
|
config = json.loads(Path(config).read_text())
|
|
resolved = (pack_resolver or _inputs.default_pack_resolver)(pack_ref)
|
|
if spec is not None and "dataset" in resolved.manifest and (
|
|
resolved.dataset().revision != spec["dataset"]["revision"]):
|
|
raise SpecError("Pack dataset revision differs from the run's spec")
|
|
handle = (model_factory or _inputs.default_model_factory)(section)
|
|
with cleanup_on_exit(handle):
|
|
return run_select_examples(resolved, handle.model, handle.tokenizer, layer, out,
|
|
model_id=section["id"], config=config,
|
|
device=section.get("device", "cpu"),
|
|
budget_bytes=budget_bytes, timings=timings)
|
|
|
|
|
|
__all__ = ["REPORT_DIR", "resolve_and_run", "run_select_examples"]
|