mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Section D of #675: the neuron lab's load() drops the previous handle and its state even when its cleanup fails (the failure is raised and nothing new is loaded); `experiments trace` cleans up its handle on success and failure without masking the trace error; default_model_factory cleans up the handle when .to(device) fails, keeping the move error; a snapshot error raised through a handle names the restore-point path once. Adds tests for the sequence refusal branch and write_step(changed=False), and registers the cleanup-site tests for inputs.py and trace_cli.py in the risk map. Refs #675
97 lines
5.6 KiB
Python
97 lines
5.6 KiB
Python
"""``obliteratus experiments trace``: temporal traces and cached-execution qualification."""
|
|
|
|
from obliteratus.handle_cleanup import cleanup_on_exit
|
|
|
|
|
|
def add_trace(commands):
|
|
sub = commands.add_parser(
|
|
"trace", help="Token-time-layer trace over prompt, prefill and decode calls "
|
|
"(teacher-forced replay or free-running), or cached/uncached qualification")
|
|
sub.add_argument("--model", required=True, help="Model id or local path")
|
|
sub.add_argument("--dtype", default="float32")
|
|
sub.add_argument("--revision")
|
|
sub.add_argument("--layer", type=int, action="append", required=True,
|
|
help="Decoder layer index to trace (repeatable)")
|
|
prompt = sub.add_mutually_exclusive_group(required=True)
|
|
prompt.add_argument("--prompt", help="Prompt text (tokenized with special tokens)")
|
|
prompt.add_argument("--prompt-ids", help="Comma-separated prompt token ids")
|
|
sub.add_argument("--mode", choices=("teacher-forced", "free-running"), default="teacher-forced")
|
|
sub.add_argument("--continuation", help="Teacher-forced continuation text (no special tokens)")
|
|
sub.add_argument("--continuation-ids", help="Comma-separated teacher-forced continuation ids")
|
|
sub.add_argument("--max-new-tokens", type=int, default=16, help="Free-running greedy tokens")
|
|
sub.add_argument("--cache", choices=("cached", "uncached"), default="uncached")
|
|
sub.add_argument("--qualify", action="store_true",
|
|
help="Teacher-force the sequence cached and uncached and compare them")
|
|
sub.add_argument("--atol", type=float, default=1e-5, help="Qualification tolerance")
|
|
sub.add_argument("--condition", default="baseline", help="Condition label stored in the trace")
|
|
sub.add_argument("--label", action="append", default=[], metavar="NAME:START:END",
|
|
help="Caller span label, e.g. thinking:5:12 (metadata only; repeatable)")
|
|
sub.add_argument("--out", help="Write the trace (manifest and chunks) to this empty directory")
|
|
sub.add_argument("--chunk-tokens", type=int, default=256)
|
|
|
|
|
|
def _ids(text):
|
|
try:
|
|
return [int(v) for v in text.split(",") if v.strip()]
|
|
except ValueError as error:
|
|
raise ValueError("Token ids must be comma-separated integers") from error
|
|
|
|
|
|
def _labels(values):
|
|
labels = {}
|
|
for value in values:
|
|
parts = value.rsplit(":", 2)
|
|
if len(parts) != 3 or not parts[1].lstrip("-").isdigit() or not parts[2].lstrip("-").isdigit():
|
|
raise ValueError(f"Label {value!r} must be NAME:START:END")
|
|
labels.setdefault(parts[0], []).append((int(parts[1]), int(parts[2])))
|
|
return labels
|
|
|
|
|
|
def run_trace_command(args, model_factory=None):
|
|
from obliteratus.analysis.temporal_trace import trace_free_running, trace_teacher_forced
|
|
from obliteratus.analysis.trace_compare import qualify_cached_execution
|
|
from obliteratus.analysis.trace_store import write_trace
|
|
from obliteratus.experiments import inputs
|
|
|
|
if args.mode == "free-running" and (args.continuation or args.continuation_ids or args.qualify):
|
|
raise ValueError("Free-running traces take no continuation and cannot be qualified; "
|
|
"qualification replays a fixed sequence (teacher-forced)")
|
|
handle = (model_factory or inputs.default_model_factory)(
|
|
{"id": args.model, "dtype": args.dtype, "revision": args.revision, "device": "cpu"})
|
|
# The handle is cleaned up on every exit; a cleanup failure is raised on
|
|
# success and only noted while another error propagates.
|
|
with cleanup_on_exit(handle):
|
|
model, tokenizer = handle.model, handle.tokenizer
|
|
layers = inputs.capture_sites(model, args.layer)
|
|
prompt = _ids(args.prompt_ids) if args.prompt_ids is not None else \
|
|
inputs.encode(tokenizer, args.prompt)["input_ids"][0].tolist()
|
|
common = {"readout_name": "decoder_output", "labels": _labels(args.label)}
|
|
if args.mode == "free-running":
|
|
eos = getattr(tokenizer, "eos_token_id", None)
|
|
trace = trace_free_running(model, layers, prompt, args.max_new_tokens, cache=args.cache,
|
|
condition=args.condition,
|
|
eos_token_ids=() if eos is None else (eos,), **common)
|
|
else:
|
|
if args.continuation_ids is not None:
|
|
extra = _ids(args.continuation_ids)
|
|
elif args.continuation:
|
|
encoded = inputs.encode(tokenizer, args.continuation, add_special_tokens=False)
|
|
extra = encoded["input_ids"][0].tolist()
|
|
else:
|
|
extra = []
|
|
if args.qualify:
|
|
return qualify_cached_execution(model, layers, prompt + extra, len(prompt),
|
|
atol=args.atol, **common)
|
|
trace = trace_teacher_forced(model, layers, prompt + extra, len(prompt),
|
|
cache=args.cache, condition=args.condition, **common)
|
|
summary = {"mode": trace.mode, "cache": trace.cache, "condition": trace.condition,
|
|
"tokens": trace.length, "prompt_tokens": trace.prompt_tokens,
|
|
"token_ids": trace.token_ids.tolist(), "layers": list(trace.values),
|
|
"calls": [call.to_dict() for call in trace.calls], "labels": trace.labels}
|
|
if args.out:
|
|
manifest = write_trace(trace, args.out, model=model, tokenizer=tokenizer, layers=layers,
|
|
model_id=args.model, chunk_tokens=args.chunk_tokens)
|
|
summary.update(out=str(args.out), identity_sha256=manifest["identity_sha256"],
|
|
chunks=len(manifest["chunks"]))
|
|
return summary
|