mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
Remove the observation runner's execution limits. - Chat templates: an optional prompt section renders each prompt through the tokenizer's chat template (optional system message, generation prompt), tokenized without extra special tokens. The rendered-prompt digest joins the generation step identity and each record; capture uses the same rendered prompts. - Decoding: seeded sampling (temperature, top-p, top-k) alongside greedy, with a per-example seed derived from the spec seed and example id. Comparison treats sampled outcomes as exact only when strategy, device, dtype and seed match, and says why otherwise. - Batching: optional left-padded greedy batches with an explicit, recorded pad token; rows are cut at their first eos so batched and unbatched outcomes agree. Sampling with batches is refused because per-example seeds cannot hold. - Devices: dense CUDA for the runner and capture, with shards written from CPU copies and device/dtype recorded. Quantized, offloaded, dispatched, meta, split-device and MPS models are refused with explicit reasons. The schema stays additive within version 1; options at their defaults are dropped from the filled spec, so existing spec digests are unchanged (pinned in tests). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
165 lines
7.0 KiB
Python
165 lines
7.0 KiB
Python
"""Prompt rendering, decoding settings and generation for observation runs.
|
|
|
|
Defaults reproduce the first release exactly: prompts are used as supplied,
|
|
decoding is greedy and each prompt is generated on its own. A spec can render
|
|
prompts through the tokenizer's chat template, sample with a per-example seed,
|
|
and generate greedy batches with explicit left padding. Settings that are at
|
|
their defaults add nothing to a generation step identity, so existing steps
|
|
keep their identities.
|
|
"""
|
|
|
|
import hashlib
|
|
|
|
from obliteratus.analysis.capture_identity import digest
|
|
from obliteratus.concepts.dataset import identity
|
|
from obliteratus.experiments import inputs as _inputs
|
|
from obliteratus.experiments.spec import SpecError
|
|
from obliteratus.experiments.store import atomic_json
|
|
|
|
|
|
def example_seed(seed, example_id):
|
|
"""Sampling seed of one example.
|
|
|
|
The first 8 bytes, read big-endian, of SHA-256 over the UTF-8 text
|
|
``"<spec seed>:<example id>"``. It depends on neither batch position nor
|
|
task order, so an example samples the same way wherever it appears.
|
|
"""
|
|
text = f"{seed}:{example_id}".encode("utf-8")
|
|
return int.from_bytes(hashlib.sha256(text).digest()[:8], "big")
|
|
|
|
|
|
def prompt_settings(spec):
|
|
prompt = spec.get("prompt") or {}
|
|
return {"chat_template": prompt.get("chat_template", False), "system": prompt.get("system")}
|
|
|
|
|
|
def batch_size(spec):
|
|
return spec["decoding"].get("batch_size", 1)
|
|
|
|
|
|
def render_prompts(tokenizer, examples, settings):
|
|
"""Map example id to the text given to the tokenizer (chat-rendered when enabled)."""
|
|
if not settings["chat_template"]:
|
|
return {example.example_id: example.prompt for example in examples}
|
|
apply = getattr(tokenizer, "apply_chat_template", None)
|
|
if not callable(apply) or not getattr(tokenizer, "chat_template", None):
|
|
raise SpecError("prompt.chat_template requires a tokenizer that defines a chat template")
|
|
rendered = {}
|
|
for example in examples:
|
|
messages = [{"role": "user", "content": example.prompt}]
|
|
if settings["system"] is not None:
|
|
messages.insert(0, {"role": "system", "content": settings["system"]})
|
|
text = apply(messages, tokenize=False, add_generation_prompt=True)
|
|
if not isinstance(text, str) or not text:
|
|
raise SpecError("The tokenizer's chat template did not render a prompt string")
|
|
rendered[example.example_id] = text
|
|
return rendered
|
|
|
|
|
|
def encode_prompt(tokenizer, text, settings):
|
|
"""Unpadded CPU inputs; a rendered chat prompt already carries its special tokens."""
|
|
return _inputs.encode(tokenizer, text, add_special_tokens=not settings["chat_template"])
|
|
|
|
|
|
def pad_token(tokenizer):
|
|
"""``(pad_token_id, source)``: the tokenizer's pad token, else its eos token."""
|
|
pad = getattr(tokenizer, "pad_token_id", None)
|
|
if pad is not None:
|
|
return pad, "pad_token"
|
|
eos = getattr(tokenizer, "eos_token_id", None)
|
|
return (eos, "eos_token") if eos is not None else (None, None)
|
|
|
|
|
|
def step_identity(spec, tokenizer, prompts):
|
|
"""Generation step identity fields for non-default prompt and batching settings."""
|
|
extra = {}
|
|
settings = prompt_settings(spec)
|
|
if settings["chat_template"]:
|
|
extra["prompt"] = settings
|
|
extra["rendered_prompts"] = identity(sorted(prompts.items()))
|
|
if batch_size(spec) > 1:
|
|
extra["padding"] = _padding(tokenizer)
|
|
return extra
|
|
|
|
|
|
def _padding(tokenizer):
|
|
pad, source = pad_token(tokenizer)
|
|
if pad is None:
|
|
raise SpecError("Batched generation requires a tokenizer pad or eos token id")
|
|
return {"side": "left", "pad_token_id": int(pad), "source": source}
|
|
|
|
|
|
def left_pad(encoded, pad_id):
|
|
"""Left-pad unpadded single-row inputs into one batch with a zero attention mask on padding."""
|
|
import torch
|
|
width = max(item["input_ids"].shape[1] for item in encoded)
|
|
ids, masks = [], []
|
|
for item in encoded:
|
|
missing = width - item["input_ids"].shape[1]
|
|
ids.append(torch.cat([item["input_ids"].new_full((1, missing), pad_id), item["input_ids"]], 1))
|
|
masks.append(torch.cat([item["attention_mask"].new_zeros((1, missing)),
|
|
item["attention_mask"]], 1))
|
|
return {"input_ids": torch.cat(ids), "attention_mask": torch.cat(masks)}
|
|
|
|
|
|
def _eos_ids(model, tokenizer):
|
|
config = getattr(model, "generation_config", None)
|
|
eos = getattr(config, "eos_token_id", None)
|
|
eos = getattr(tokenizer, "eos_token_id", None) if eos is None else eos
|
|
return set() if eos is None else set(eos) if isinstance(eos, (list, tuple)) else {eos}
|
|
|
|
|
|
def _until_eos(tokens, eos):
|
|
"""Unbatched generation stops after the first eos; drop what a batch adds beyond it."""
|
|
for index, token in enumerate(tokens.tolist()):
|
|
if token in eos:
|
|
return tokens[:index + 1]
|
|
return tokens
|
|
|
|
|
|
def _decoding_kwargs(decoding):
|
|
if decoding["strategy"] == "sample":
|
|
return {"do_sample": True, "num_beams": 1, "temperature": decoding["temperature"],
|
|
"top_p": decoding["top_p"], "top_k": decoding["top_k"]}
|
|
return {"do_sample": False, "num_beams": 1}
|
|
|
|
|
|
def generate_task(model, tokenizer, task, prompts, spec, cancel, out, device="cpu"):
|
|
"""Generate every example of ``task`` and write ``generations.json`` under ``out``."""
|
|
import torch
|
|
decoding, size, settings = spec["decoding"], batch_size(spec), prompt_settings(spec)
|
|
if size > 1:
|
|
padding = _padding(tokenizer)
|
|
pad = padding["pad_token_id"]
|
|
else:
|
|
padding, (pad, _source) = None, pad_token(tokenizer)
|
|
eos = _eos_ids(model, tokenizer)
|
|
examples, rows = list(task.examples), []
|
|
for start in range(0, len(examples), size):
|
|
cancel()
|
|
chunk = examples[start:start + size]
|
|
encoded = [encode_prompt(tokenizer, prompts[e.example_id], settings) for e in chunk]
|
|
batch = encoded[0] if size == 1 else left_pad(encoded, pad)
|
|
torch.manual_seed(spec["seed"] if decoding["strategy"] == "greedy"
|
|
else example_seed(spec["seed"], chunk[0].example_id))
|
|
with torch.inference_mode():
|
|
output = model.generate(**{k: v.to(device) for k, v in batch.items()},
|
|
max_new_tokens=decoding["max_new_tokens"], pad_token_id=pad,
|
|
**_decoding_kwargs(decoding))
|
|
width = batch["input_ids"].shape[1]
|
|
for index, (example, item) in enumerate(zip(chunk, encoded)):
|
|
new = output[index, width:].detach().to("cpu")
|
|
if size > 1:
|
|
new = _until_eos(new, eos)
|
|
row = {"example_id": example.example_id, "prompt_tokens": int(item["input_ids"].shape[1]),
|
|
"generated_tokens": int(new.shape[0]),
|
|
"completion": tokenizer.decode(new, skip_special_tokens=True)}
|
|
if settings["chat_template"]:
|
|
row["rendered_prompt_sha256"] = digest(prompts[example.example_id])
|
|
rows.append(row)
|
|
cancel()
|
|
payload = {"task": task.name, "rows": rows}
|
|
if padding is not None:
|
|
payload["padding"] = padding
|
|
atomic_json(out / "generations.json", payload)
|