Files
OBLITERATUS/obliteratus/experiments/spec.py
Joseph MaglyandClaude Opus 5.5 a559c7fd6c Merge main into feat/obs-execution and unify device-aware model identity
Integrate the weight fingerprint from #451 with CUDA execution:

- model_identity is now device-aware (require_dense_device) and always
  includes the weight fingerprint, so CUDA runs carry it too;
  execution_identity simply delegates. The fingerprint is
  device-independent: the same weights give the same digest on CPU and
  CUDA.
- New optional spec setting model.fingerprint ("sampled" default,
  "full"), dropped from the filled spec at its default so existing
  digests are unchanged. It applies to the run's model identity and
  therefore every step's reuse check; capture manifests keep a sampled
  fingerprint.
- Resolve additive conflicts in capture_identity.py, inputs.py, the risk
  map and OBSERVATION_RUNS.md by keeping both sides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 16:47:38 -05:00

140 lines
5.4 KiB
Python

"""Versioned baseline observation specifications and their run identity.
The shipped JSON schema is the single schema authority. A spec references its
inputs (pack, dataset revision and role, model id) instead of copying them. The
canonical digest of the default-filled spec is the run identity, so changing
any field, including a default made explicit with a different value, yields a
new identity.
Options added after the first release (``decoding.batch_size``, the ``prompt``
section and the sampling settings) are omitted from the filled spec while
they hold their defaults, so specs written before them keep their digests.
"""
from copy import deepcopy
from functools import lru_cache
import json
from pathlib import Path
from obliteratus.concepts.dataset import identity
from obliteratus.concepts.scoring import ScorerSpec, get_scorer
from obliteratus.concepts.validation import PackError, _check
SCHEMA_VERSION = "1"
SCHEMA_PATH = (Path(__file__).resolve().parents[1] / "concepts" / "schema"
/ "observation-spec.v1.schema.json")
DEFAULTS = {
"dataset": {"pack_sha256": None, "allow_locked_test": False},
"model": {"revision": None, "device": "cpu"},
"decoding": {"strategy": "greedy"},
"capture": {"readout": "decoder_output"},
}
SAMPLING_DEFAULTS = {"temperature": 1.0, "top_p": 1.0, "top_k": 0}
DEFAULT_TASK_PATH = "positive.jsonl"
MAX_SPEC_BYTES = 1 << 20
class SpecError(ValueError):
"""An observation spec is malformed or asks for a refused input."""
@lru_cache(maxsize=1)
def schema():
return json.loads(SCHEMA_PATH.read_text(encoding="utf-8"))
def _read(source):
if isinstance(source, dict):
return deepcopy(source)
path = Path(source)
if path.stat().st_size > MAX_SPEC_BYTES:
raise SpecError("Observation spec exceeds the 1 MiB size limit")
def constant(name):
raise SpecError(f"Observation spec contains non-standard JSON constant {name}")
try:
value = json.loads(path.read_text(encoding="utf-8"), parse_constant=constant)
except json.JSONDecodeError as exc:
raise SpecError(f"Observation spec is not valid JSON: {exc}") from exc
if not isinstance(value, dict):
raise SpecError("Observation spec must be a JSON object")
return value
def load_spec(source):
"""Validate a spec (mapping or JSON file) and return its default-filled copy.
Locked ``test`` access is refused unless ``dataset.allow_locked_test`` is
exactly ``true``; the acknowledgement is part of the run identity.
"""
spec = _read(source)
_refuse_unqualified(spec)
dataset = spec.get("dataset")
if (isinstance(dataset, dict) and dataset.get("role") == "test"
and dataset.get("allow_locked_test") is not True):
raise SpecError("Locked test role requires dataset.allow_locked_test: true")
try:
_check(spec, schema(), schema(), "observation_spec")
except PackError as exc:
raise SpecError(str(exc)) from exc
for section, values in DEFAULTS.items():
if isinstance(spec.get(section), dict):
spec[section] = {**values, **spec[section]}
_normalize_execution(spec)
spec.setdefault("capture", None)
spec.setdefault("profile", False)
spec.setdefault("seed", 0)
names = set()
for task in spec["tasks"]:
task.setdefault("path", DEFAULT_TASK_PATH)
if task["name"] in names:
raise SpecError(f"Observation task names must be unique: {task['name']}")
names.add(task["name"])
try:
get_scorer(ScorerSpec(task["scorer"]["id"], task["scorer"]["version"]))
except ValueError as exc:
raise SpecError(f"Task {task['name']}: {exc}") from exc
try:
spec_digest(spec)
except PackError as exc:
raise SpecError(str(exc)) from exc
return spec
def _refuse_unqualified(spec):
"""Explicit reasons for refusals the schema would only call invalid."""
model, decoding = spec.get("model"), spec.get("decoding")
device = model.get("device") if isinstance(model, dict) else None
if isinstance(device, str) and device.split(":")[0] == "mps":
raise SpecError("model.device mps is not qualified for observation runs; "
"qualified devices are cpu and cuda[:N]")
if (isinstance(decoding, dict) and decoding.get("strategy") == "sample"
and decoding.get("batch_size", 1) != 1):
raise SpecError("Sampled decoding uses one seed per example and requires batch_size 1")
def _normalize_execution(spec):
"""Drop post-v1 options at their defaults; make sampling settings explicit floats."""
decoding = spec["decoding"]
if decoding.get("batch_size") == 1:
del decoding["batch_size"]
if decoding["strategy"] == "sample":
decoding.update({**SAMPLING_DEFAULTS, **decoding})
decoding["temperature"] = float(decoding["temperature"])
decoding["top_p"] = float(decoding["top_p"])
if "prompt" in spec and not spec["prompt"].get("chat_template", False):
del spec["prompt"]
if spec["model"].get("fingerprint") == "sampled":
del spec["model"]["fingerprint"]
def spec_digest(spec):
"""Run identity: ``sha256:`` over canonical JSON of the default-filled spec."""
return identity(spec)
def digest_hex(digest):
if not isinstance(digest, str) or not digest.startswith("sha256:") or len(digest) != 71:
raise SpecError("Run identity must be a sha256: digest")
return digest.removeprefix("sha256:")