mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
Extend observation from prompt readouts to token-time-layer traces (F09), with matched-prefix comparison kept separate from free-running generation. - Each forward call is recorded with its phase (full, prefill, decode), new positions, cache history and a prefix digest as decode-step identity; every position is observed exactly once, and masks select calls, prompt/continuation segments or caller labels. - Teacher-forced replay of fixed tokens and free-running greedy decoding are distinct modes; comparing them is refused. Matched-prefix comparison reports per-layer and per-position differences; free running reports the divergence point. - Cached execution is supported on dense CPU Transformers with past_key_values and refused (never silently recomputed) when the model, cache type or cache length doesn't fit. Qualification checks cached against uncached traces within tolerance. - Caller labels such as "thinking" are stored as unverified metadata only. Traces are stored as chunked safetensors bound to the capture identity; hooks are removed on errors and cancellation. - `obliteratus experiments trace` with --mode, --cache and --qualify. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
92 lines
5.3 KiB
Python
92 lines
5.3 KiB
Python
"""``obliteratus experiments trace``: temporal traces and cached-execution qualification."""
|
|
|
|
|
|
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"})
|
|
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
|