mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
Connect the full-subspace report to real captured activations (F08b) and add the labeled-activation foundation that candidate discovery and example selection build on. - Labeled sets: capture a pack's positive, control, neighbour and optional battery sets for declared dataset roles. Labels (set, kind, role, group, family, record id, input digest) live in a sidecar whose digest, the capture identity and the dataset revision are bound in a labeled-set manifest; prompt text is never stored. Locked test needs explicit acknowledgement. Legacy packs are fit-only, bound to the pack bytes. - Loader: recomputes rows and capture identity and refuses any mismatch, naming the field. Selection helpers pick rows by role, set and kind, and build deterministic group-disjoint folds. A capture that failed or was cancelled is replaced on retry; complete sets never are. - Subspace report on a labeled set with the pack's extracted rank-k basis; groups come from dataset independence units, and the basis-fit overlap is read from the basis's dataset access receipt. - `obliteratus experiments subspace` and a Python API write versioned reports next to the run; the Explorer lists them as a descriptive table. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
95 lines
4.8 KiB
Python
95 lines
4.8 KiB
Python
"""Model-level subspace report on a labeled activation set (``experiments subspace``).
|
|
|
|
Captures (or reuses, after full verification) a pack's labeled sets for the
|
|
requested dataset roles at one decoder layer of an unmodified model, then
|
|
writes the full-subspace report of the pack's extracted basis next to an
|
|
observation run (``<run>/subspace/``) or into a chosen directory.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
|
|
from obliteratus.analysis.labeled_activations import (
|
|
MANIFEST, capture_labeled_set, load_labeled_set,
|
|
)
|
|
from obliteratus.analysis.labeled_subspace import (
|
|
REPORT_DIR, labeled_subspace_report, write_report,
|
|
)
|
|
from obliteratus.experiments import inputs as _inputs
|
|
from obliteratus.experiments.spec import SpecError, load_spec
|
|
|
|
|
|
def labeled_dir(out_dir, site, roles, include_battery=False):
|
|
suffix = "+battery" if include_battery else ""
|
|
return Path(out_dir) / f"labeled-{site}-{'+'.join(roles)}{suffix}"
|
|
|
|
|
|
def run_subspace(pack, model, tokenizer, direction, layer, out_dir, *, model_id,
|
|
roles=("select",), include_battery=False, allow_locked_test=False, seed=0,
|
|
device="cpu", budget_bytes=None, layers=None, direction_source=None,
|
|
report_options=None, cache_dir=None):
|
|
"""Capture-or-reuse the labeled set, report the basis at ``layer`` and write the JSON.
|
|
|
|
``layers`` maps one site name to its module; the default is the model's
|
|
decoder layer ``layer`` named ``layer.<index>``. Returns a summary with
|
|
the written report path.
|
|
"""
|
|
roles = [roles] if isinstance(roles, str) else list(roles)
|
|
_inputs.require_unmodified(model)
|
|
layers = layers or _inputs.capture_sites(model, [layer])
|
|
if len(layers) != 1:
|
|
raise ValueError("A subspace report reads exactly one site")
|
|
site = next(iter(layers))
|
|
target = labeled_dir(out_dir, site, roles, include_battery)
|
|
options = {"model_id": model_id, "allow_locked_test": allow_locked_test,
|
|
"cache_dir": cache_dir}
|
|
reused = (target / MANIFEST).is_file()
|
|
if not reused:
|
|
capture_labeled_set(model, tokenizer, layers, pack, target, roles=roles,
|
|
include_battery=include_battery, device=device,
|
|
budget_bytes=budget_bytes, **options)
|
|
labeled = load_labeled_set(target, model, tokenizer, layers, pack, **options)
|
|
result = labeled_subspace_report(labeled, direction, site, layer=layer, roles=roles, seed=seed,
|
|
direction_source=direction_source, **(report_options or {}))
|
|
path = write_report(result, out_dir)
|
|
return {"report": str(path), "labeled_set": str(target), "reused_labeled_set": reused,
|
|
"verdict": result["report"]["verdict"],
|
|
"basis_fit_on_evaluation_data": result["basis_fit_on_evaluation_data"]}
|
|
|
|
|
|
def resolve_and_run(*, direction, layer, run=None, pack=None, model=None, dtype="float32",
|
|
revision=None, device="cpu", roles=None, out=None, include_battery=False,
|
|
allow_locked_test=False, seed=0, budget_bytes=None, model_factory=None,
|
|
pack_resolver=None):
|
|
"""Resolve the pack and model from an observation run directory or explicit references."""
|
|
from obliteratus.concepts.direction import ConceptDirection
|
|
|
|
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"])
|
|
roles = roles or [spec["dataset"]["role"]]
|
|
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}
|
|
roles = roles or ["select"]
|
|
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")
|
|
basis = ConceptDirection.load(direction)
|
|
handle = (model_factory or _inputs.default_model_factory)(section)
|
|
try:
|
|
return run_subspace(resolved, handle.model, handle.tokenizer, basis, layer, out,
|
|
model_id=section["id"], roles=roles, include_battery=include_battery,
|
|
allow_locked_test=allow_locked_test, seed=seed,
|
|
device=section.get("device", "cpu"), budget_bytes=budget_bytes,
|
|
direction_source=direction)
|
|
finally:
|
|
cleanup = getattr(handle, "cleanup", None)
|
|
if callable(cleanup):
|
|
cleanup()
|