Files
OBLITERATUS/obliteratus/experiments/cli.py
Joseph Magly 284d9e09dc Merge pull request #517 from elder-plinius/feat/issue-511
Opt-in numeric geometry preservation evaluation
2026-11-16 22:17:27 -05:00

238 lines
13 KiB
Python

"""``obliteratus experiments run|compare|history|prune|subspace|discover|select-examples|dossier|intervene|map|registry|geometry-lab|trace|screening|adaptive-capture|candidate|recovery|interact|acquisition|sequence|smaller-change|search|qualify|regression|transfer|arch-profile|field-eval``.
Thin wrappers.
"""
import json
import sys
def add_parser(subparsers):
parser = subparsers.add_parser(
"experiments", help="Run baseline observation specs and compare their results",
)
commands = parser.add_subparsers(dest="experiments_command", required=True)
run = commands.add_parser("run", help="Run (or resume) a baseline observation spec")
run.add_argument("spec", help="Observation spec JSON file")
run.add_argument("--store", required=True, help="Local run store directory")
run.add_argument("--rerun", action="store_true",
help="Start a new attempt even when a complete attempt exists")
compare = commands.add_parser("compare", help="Compare two observation results")
compare.add_argument("left", help="result.json, attempt directory or run directory")
compare.add_argument("right", help="result.json, attempt directory or run directory")
history = commands.add_parser("history", help="List every attempt of one spec")
history.add_argument("spec", help="Observation spec JSON file")
history.add_argument("--store", required=True, help="Local run store directory")
prune = commands.add_parser(
"prune", help="List (and with --apply remove) old attempts of one spec; dry run by default")
prune.add_argument("spec", help="Observation spec JSON file")
prune.add_argument("--store", required=True, help="Local run store directory")
prune.add_argument("--keep", type=int, default=1,
help="Newest attempts to keep in addition to protected ones (default 1)")
prune.add_argument("--apply", action="store_true", help="Remove the listed attempts")
_add_subspace(commands)
from obliteratus.experiments.discover_cli import add_discover
add_discover(commands)
_add_select_examples(commands)
from obliteratus.experiments.dossier_cli import add_dossier
add_dossier(commands)
from obliteratus.experiments.intervene_cli import add_intervene
add_intervene(commands)
from obliteratus.experiments.map_cli import add_map
add_map(commands)
from obliteratus.experiments.registry_cli import add_registry
add_registry(commands)
from obliteratus.experiments.interact_cli import add_interact
add_interact(commands)
from obliteratus.experiments.candidate_cli import add_candidate
add_candidate(commands)
from obliteratus.experiments.backend_cli import add_qualify
add_qualify(commands)
from obliteratus.experiments.recovery_cli import add_recovery
add_recovery(commands)
from obliteratus.experiments.sequence_cli import add_sequence
add_sequence(commands)
from obliteratus.experiments.regression_cli import add_regression
add_regression(commands)
from obliteratus.experiments.adaptive_capture import add_adaptive_capture
add_adaptive_capture(commands)
from obliteratus.experiments.geometry_lab_cli import add_geometry_lab
add_geometry_lab(commands)
from obliteratus.experiments.numeric_geometry_cli import add_numeric_geometry
add_numeric_geometry(commands)
from obliteratus.experiments.trace_cli import add_trace
add_trace(commands)
from obliteratus.experiments.acquisition_cli import add_acquisition
add_acquisition(commands)
from obliteratus.experiments.screening_cli import add_screening
add_screening(commands)
from obliteratus.experiments.transfer_cli import add_arch_profile, add_transfer
add_transfer(commands)
add_arch_profile(commands)
from obliteratus.experiments.smaller_change_cli import add_smaller_change
add_smaller_change(commands)
from obliteratus.experiments.search_cli import add_search
add_search(commands)
from obliteratus.experiments.field_eval_cli import add_field_eval
add_field_eval(commands)
def _add_subspace(commands):
sub = commands.add_parser(
"subspace", help="Full-subspace report of a pack's extracted basis on labeled activations")
sub.add_argument("--direction", required=True, help="Extracted direction.safetensors")
sub.add_argument("--layer", type=int, required=True, help="Decoder layer index (site)")
sub.add_argument("--run", help="Observation run directory (pack, model and role from its spec)")
sub.add_argument("--pack", help="Pack reference when no run directory is given")
sub.add_argument("--model", help="Model id when no run directory is given")
sub.add_argument("--dtype", default="float32")
sub.add_argument("--revision")
sub.add_argument("--device", default="cpu")
sub.add_argument("--role", action="append", choices=("fit", "select", "calibrate", "test"),
help="Dataset role to evaluate (repeatable; default: the run's role, "
"else select)")
sub.add_argument("--battery", action="store_true", help="Also capture battery sets")
sub.add_argument("--allow-locked-test", action="store_true",
help="Acknowledge access to the locked test partition")
sub.add_argument("--out", help="Output directory (default <run>/subspace)")
sub.add_argument("--seed", type=int, default=0)
sub.add_argument("--budget-bytes", type=int, help="Capture byte budget")
def _add_select_examples(commands):
sub = commands.add_parser(
"select-examples",
help="O02 study: stratified, lexical and representation-based example selection")
sub.add_argument("--layer", type=int, required=True, help="Decoder layer index (site)")
sub.add_argument("--run", help="Observation run directory (pack and model from its spec)")
sub.add_argument("--pack", help="Pack reference when no run directory is given")
sub.add_argument("--model", help="Model id when no run directory is given")
sub.add_argument("--dtype", default="float32")
sub.add_argument("--revision")
sub.add_argument("--device", default="cpu")
sub.add_argument("--config", help="Study config JSON (budgets, seeds, coverage limits, ...)")
sub.add_argument("--out", help="Output directory (default <run>/example-selection)")
sub.add_argument("--no-timings", action="store_true",
help="Omit host timing measurements (byte-identical reports per seed)")
sub.add_argument("--budget-bytes", type=int, help="Capture byte budget")
def run(args):
"""Print JSON; exit 1 on a differing comparison, stale dossier or failed trace qualification,
4 on refusal and 130 on cancel."""
from obliteratus.experiments import compare_results, load_spec, run_observation, spec_digest
from obliteratus.experiments.store import ObservationStore
try:
if args.experiments_command == "run":
result = run_observation(args.spec, args.store, rerun=args.rerun)
result = {key: result[key] for key in
("spec_digest", "attempt", "state", "steps", "location")}
elif args.experiments_command == "compare":
result = compare_results(args.left, args.right)
elif args.experiments_command == "prune":
from obliteratus.experiments.prune import prune_attempts
result = prune_attempts(args.spec, args.store, keep=args.keep, apply=args.apply)
elif args.experiments_command == "subspace":
from obliteratus.experiments.subspace import resolve_and_run
result = resolve_and_run(
direction=args.direction, layer=args.layer, run=args.run, pack=args.pack,
model=args.model, dtype=args.dtype, revision=args.revision, device=args.device,
roles=args.role, out=args.out, include_battery=args.battery,
allow_locked_test=args.allow_locked_test, seed=args.seed,
budget_bytes=args.budget_bytes)
elif args.experiments_command == "discover":
from obliteratus.experiments.discover_cli import run_discover_command
result = run_discover_command(args)
elif args.experiments_command == "dossier":
from obliteratus.experiments.dossier_cli import run_dossier_command
result = run_dossier_command(args)
elif args.experiments_command == "intervene":
from obliteratus.experiments.intervene_cli import run_intervene_command
result = run_intervene_command(args)
elif args.experiments_command == "map":
from obliteratus.experiments.map_cli import run_map_command
result = run_map_command(args)
elif args.experiments_command == "registry":
from obliteratus.experiments.registry_cli import run_registry_command
result = run_registry_command(args)
elif args.experiments_command == "interact":
from obliteratus.experiments.interact_cli import run_interact_command
result = run_interact_command(args)
elif args.experiments_command == "candidate":
from obliteratus.experiments.candidate_cli import run_candidate_command
result = run_candidate_command(args)
elif args.experiments_command == "qualify":
from obliteratus.experiments.backend_cli import run_qualify_command
result = run_qualify_command(args)
elif args.experiments_command == "recovery":
from obliteratus.experiments.recovery_cli import run_recovery_command
result = run_recovery_command(args)
elif args.experiments_command == "sequence":
from obliteratus.experiments.sequence_cli import run_sequence_command
result = run_sequence_command(args)
elif args.experiments_command == "regression":
from obliteratus.experiments.regression_cli import run_regression_command
result = run_regression_command(args)
elif args.experiments_command == "adaptive-capture":
from obliteratus.experiments.adaptive_capture import run_adaptive_capture_command
result = run_adaptive_capture_command(args)
elif args.experiments_command == "geometry-lab":
from obliteratus.experiments.geometry_lab_cli import run_geometry_lab_command
result = run_geometry_lab_command(args)
elif args.experiments_command == "numeric-geometry":
from obliteratus.experiments.numeric_geometry_cli import run_numeric_geometry_command
result = run_numeric_geometry_command(args)
elif args.experiments_command == "trace":
from obliteratus.experiments.trace_cli import run_trace_command
result = run_trace_command(args)
elif args.experiments_command == "acquisition":
from obliteratus.experiments.acquisition_cli import run_acquisition_command
result = run_acquisition_command(args)
elif args.experiments_command == "screening":
from obliteratus.experiments.screening_cli import run_screening_command
result = run_screening_command(args)
elif args.experiments_command == "transfer":
from obliteratus.experiments.transfer_cli import run_transfer_command
result = run_transfer_command(args)
elif args.experiments_command == "arch-profile":
from obliteratus.experiments.transfer_cli import run_arch_profile_command
result = run_arch_profile_command(args)
elif args.experiments_command == "smaller-change":
from obliteratus.experiments.smaller_change_cli import run_smaller_change_command
result = run_smaller_change_command(args)
elif args.experiments_command == "search":
from obliteratus.experiments.search_cli import run_search_command
result = run_search_command(args)
elif args.experiments_command == "field-eval":
from obliteratus.experiments.field_eval_cli import run_field_eval_command
result = run_field_eval_command(args)
elif args.experiments_command == "select-examples":
from obliteratus.experiments.select_examples import resolve_and_run as select
result = select(
layer=args.layer, run=args.run, pack=args.pack, model=args.model,
dtype=args.dtype, revision=args.revision, device=args.device, out=args.out,
config=args.config, budget_bytes=args.budget_bytes, timings=not args.no_timings)
else:
run_record = ObservationStore(args.store).get(spec_digest(load_spec(args.spec)))
result = {"spec_digest": run_record.digest, "attempts": run_record.history()}
except KeyboardInterrupt as exc:
print("Observation cancelled", file=sys.stderr)
raise SystemExit(130) from exc
except (ValueError, OSError) as exc:
print(f"Observation error: {exc}", file=sys.stderr)
raise SystemExit(4) from exc
print(json.dumps(result, sort_keys=True, allow_nan=False))
if args.experiments_command == "compare" and not result["equal"]:
raise SystemExit(1)
if args.experiments_command == "dossier" and args.dossier_command == "check" \
and result["stale"]:
raise SystemExit(1)
if args.experiments_command == "trace" and result.get("passed") is False:
raise SystemExit(1)
if args.experiments_command == "arch-profile" \
and (result.get("semantics_check") or {}).get("passed") is False:
raise SystemExit(1)
if args.experiments_command == "regression" and result.get("regressions"):
raise SystemExit(1)