mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
251 lines
12 KiB
Python
251 lines
12 KiB
Python
"""Declarative prompt entry points and token-span readout alignment."""
|
|
|
|
from dataclasses import dataclass, field
|
|
import re
|
|
from string import Formatter
|
|
|
|
|
|
_NAME = r"[A-Za-z_][A-Za-z0-9_]*"
|
|
_MARKER = re.compile(r"\{\{(/?)(" + _NAME + r")\}\}")
|
|
|
|
|
|
def _template(text, record):
|
|
pieces = []
|
|
for literal, name, spec, conversion in Formatter().parse(text):
|
|
pieces.append(literal)
|
|
if name is not None:
|
|
if not re.fullmatch(_NAME, name) or spec or conversion:
|
|
raise ValueError("Entry point templates accept only {field} variables")
|
|
if name not in record or not isinstance(record[name], (str, int, float, bool)):
|
|
raise ValueError(f"Entry point template requires scalar record field {name!r}")
|
|
pieces.append(str(record[name]))
|
|
return "".join(pieces)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class EntryPoint:
|
|
"""Prior messages precede the templated user prompt; prefill follows it."""
|
|
|
|
messages: list[dict[str, str]] | None = None
|
|
prefill: str = ""
|
|
readout: str = "final_token"
|
|
template: str = "{text}"
|
|
group_by: str | None = None
|
|
spans: dict[str, str] = field(default_factory=dict)
|
|
special_token_policy: dict[str, str] | None = None
|
|
|
|
def __post_init__(self):
|
|
if self.special_token_policy is not None and (
|
|
not isinstance(self.special_token_policy, dict)
|
|
or set(self.special_token_policy) != {"bos"}
|
|
or self.special_token_policy["bos"] not in ("tokenizer_default", "none", "require")
|
|
):
|
|
raise ValueError("Entry point special_token_policy requires bos: tokenizer_default, none or require")
|
|
if not isinstance(self.template, str) or not isinstance(self.prefill, str):
|
|
raise ValueError("Entry point template and prefill must be strings")
|
|
if not isinstance(self.readout, str) or not re.fullmatch(
|
|
r"final_token|mean_pool|span:" + _NAME + r"|last_k:[1-9][0-9]*", self.readout,
|
|
):
|
|
raise ValueError("Entry point readout requires final_token, mean_pool, span:<name> or last_k:<k>")
|
|
if self.group_by is not None and (
|
|
not isinstance(self.group_by, str) or not re.fullmatch(_NAME, self.group_by)
|
|
):
|
|
raise ValueError("Entry point group_by must name a record field")
|
|
if not isinstance(self.spans, dict) or any(
|
|
not isinstance(name, str) or not re.fullmatch(_NAME, name)
|
|
or not isinstance(text, str) or not text for name, text in self.spans.items()
|
|
):
|
|
raise ValueError("Entry point spans must map names to nonempty literals")
|
|
if self.messages is not None:
|
|
if not isinstance(self.messages, list):
|
|
raise ValueError("Entry point messages must be a list of prior messages")
|
|
for i, message in enumerate(self.messages):
|
|
if (not isinstance(message, dict) or set(message) != {"role", "content"}
|
|
or not isinstance(message["role"], str)
|
|
or message["role"] not in {"system", "user", "assistant"}
|
|
or not isinstance(message["content"], str)
|
|
or message["role"] == "system" and i != 0):
|
|
raise ValueError("Entry point messages require role/content; system must be first")
|
|
|
|
|
|
class RenderedPrompt(str):
|
|
"""A model input with the selected character span and rendering provenance."""
|
|
|
|
def __new__(cls, text, span=None, fallback=False, chat_rendered=False,
|
|
chat_template_special_tokens=None):
|
|
value = super().__new__(cls, text)
|
|
value.span = span
|
|
value.fallback = fallback
|
|
value.chat_rendered = chat_rendered
|
|
value.chat_template_special_tokens = chat_template_special_tokens
|
|
return value
|
|
|
|
|
|
def render_entry_point(entry, record, tokenizer, *, use_chat_template):
|
|
spans, opened, pieces, cursor, length = {}, None, [], 0, 0
|
|
for match in _MARKER.finditer(entry.template):
|
|
chunk = _template(entry.template[cursor:match.start()], record)
|
|
pieces.append(chunk)
|
|
length += len(chunk)
|
|
closing, name = match.groups()
|
|
if closing:
|
|
if opened is None or opened[0] != name or length == opened[1]:
|
|
raise ValueError(f"Unmatched or empty entry point span {name!r}")
|
|
spans[name] = (opened[1], length)
|
|
opened = None
|
|
else:
|
|
if opened is not None or name in spans:
|
|
raise ValueError(f"Nested or duplicate entry point span {name!r}")
|
|
opened = (name, length)
|
|
cursor = match.end()
|
|
if opened is not None:
|
|
raise ValueError(f"Unclosed entry point span {opened[0]!r}")
|
|
pieces.append(_template(entry.template[cursor:], record))
|
|
prompt = "".join(pieces)
|
|
selected = None
|
|
if entry.readout.startswith("span:"):
|
|
name = entry.readout.removeprefix("span:")
|
|
if name in spans and name in entry.spans:
|
|
raise ValueError(f"Entry point span {name!r} has both markers and a literal")
|
|
if name in spans:
|
|
selected = spans[name]
|
|
elif name in entry.spans:
|
|
literal = entry.spans[name]
|
|
if prompt.count(literal) != 1:
|
|
raise ValueError(f"Entry point literal span {name!r} must occur exactly once")
|
|
start = prompt.index(literal)
|
|
selected = (start, start + len(literal))
|
|
else:
|
|
raise ValueError(f"Entry point span {name!r} is not declared")
|
|
messages = [
|
|
{"role": message["role"], "content": _template(message["content"], record)}
|
|
for message in entry.messages or []
|
|
] + [{"role": "user", "content": prompt}]
|
|
chat = entry.messages is not None or use_chat_template
|
|
fallback = chat and not (
|
|
getattr(tokenizer, "chat_template", None)
|
|
and callable(getattr(tokenizer, "apply_chat_template", None))
|
|
)
|
|
|
|
def apply(messages):
|
|
try:
|
|
return tokenizer.apply_chat_template(
|
|
messages, tokenize=False, add_generation_prompt=True, enable_thinking=False,
|
|
)
|
|
except TypeError:
|
|
return tokenizer.apply_chat_template(
|
|
messages, tokenize=False, add_generation_prompt=True,
|
|
)
|
|
|
|
if chat and not fallback:
|
|
rendered = apply(messages)
|
|
else:
|
|
rendered = "\n".join(message["content"] for message in messages)
|
|
if selected is not None:
|
|
if chat and not fallback:
|
|
marker = "\ufff0entry_point\ufff1"
|
|
while any(marker in m["content"] for m in messages):
|
|
marker += "\ufff1"
|
|
located = apply(messages[:-1] + [{"role": "user", "content": marker + prompt + marker}])
|
|
if (located.count(marker) != 2 or located.replace(marker, "") != rendered
|
|
or located.split(marker)[1] != prompt):
|
|
raise ValueError("Chat template must preserve the span's user prompt verbatim")
|
|
start = located.index(marker)
|
|
else:
|
|
start = sum(len(m["content"]) + 1 for m in messages[:-1])
|
|
# Whitespace at the boundaries need not have its own tokenizer offsets.
|
|
left, right = selected
|
|
while left < right and prompt[left].isspace():
|
|
left += 1
|
|
while right > left and prompt[right - 1].isspace():
|
|
right -= 1
|
|
if left == right:
|
|
raise ValueError("Entry point span contains only whitespace")
|
|
selected = (start + left, start + right)
|
|
inserted = {}
|
|
for name in ("bos", "eos"):
|
|
token = getattr(tokenizer, name + "_token", None)
|
|
# Literal special tokens in source messages make template attribution
|
|
# ambiguous. Report unknown rather than claiming the template added them.
|
|
inserted[name] = (False if not chat or fallback else None if not token or any(
|
|
token in message["content"] for message in messages
|
|
) else token in rendered)
|
|
return RenderedPrompt(rendered + _template(entry.prefill, record), selected, fallback,
|
|
chat_rendered=chat and not fallback,
|
|
chat_template_special_tokens=inserted)
|
|
|
|
|
|
def special_token_observations(entry, tokenizer, prompts, inputs):
|
|
"""Inspect actual, unpadded model inputs without changing tokenization.
|
|
|
|
The tokenizer's mask distinguishes postprocessor additions from special
|
|
tokens already in rendered text (chat template or literal prompt content).
|
|
Keep only short examples, but check every row, including truncated rows.
|
|
"""
|
|
bos = getattr(tokenizer, "bos_token_id", None)
|
|
eos = getattr(tokenizer, "eos_token_id", None)
|
|
policy = (entry.special_token_policy or {"bos": "tokenizer_default"})["bos"]
|
|
ids = inputs["input_ids"].tolist()
|
|
attention = inputs.get("attention_mask")
|
|
attention = attention.tolist() if attention is not None else [[1] * len(row) for row in ids]
|
|
special = inputs.get("special_tokens_mask")
|
|
special = special.tolist() if special is not None else None
|
|
counts, example, first_violation = {}, None, None
|
|
for index, (prompt, row, mask) in enumerate(zip(prompts, ids, attention)):
|
|
tokens = [token for token, keep in zip(row, mask) if keep]
|
|
added = ([token for token, keep, extra in zip(row, mask, special[index]) if keep and extra]
|
|
if special is not None else None)
|
|
rendered = ([token for token, keep, extra in zip(row, mask, special[index]) if keep and not extra]
|
|
if special is not None else None)
|
|
first = tokens[0] if tokens else None
|
|
issues = []
|
|
if first is None:
|
|
issues.append("empty_input")
|
|
if eos is not None and first == eos:
|
|
issues.append("eos_as_bos")
|
|
if policy == "require" and (bos is None or first != bos):
|
|
issues.append("bos_required")
|
|
if policy == "none" and bos is not None and first == bos:
|
|
issues.append("bos_forbidden")
|
|
observed = {
|
|
"first_token_ids": tokens[:8],
|
|
"add_special_tokens": not prompt.chat_rendered,
|
|
"chat_template_applied": prompt.chat_rendered,
|
|
"plain_text_fallback": prompt.fallback,
|
|
"chat_template_inserted": prompt.chat_template_special_tokens,
|
|
"bos_added_by_tokenizer": bos in added if added is not None and bos is not None else None,
|
|
"eos_added_by_tokenizer": eos in added if added is not None and eos is not None else None,
|
|
"bos_in_rendered_text": bos in rendered if rendered is not None and bos is not None else None,
|
|
"eos_in_rendered_text": eos in rendered if rendered is not None and eos is not None else None,
|
|
}
|
|
if example is None:
|
|
example = observed
|
|
if issues and first_violation is None:
|
|
first_violation = {"row": index, "issues": issues, **observed}
|
|
for issue in issues:
|
|
counts[issue] = counts.get(issue, 0) + 1
|
|
return {
|
|
"bos_token_id": bos, "eos_token_id": eos, "policy": {"bos": policy},
|
|
"prompt_count": len(prompts), "example": example,
|
|
"issue_counts": counts, "first_violation": first_violation,
|
|
}
|
|
|
|
|
|
def span_token_mask(prompts, offsets, attention_mask):
|
|
"""Require full span coverage, including after left or right truncation."""
|
|
import torch
|
|
|
|
result = torch.zeros_like(attention_mask, dtype=torch.bool)
|
|
for row, prompt in enumerate(prompts):
|
|
if not isinstance(prompt, RenderedPrompt) or prompt.span is None:
|
|
raise ValueError("Span readout requires an entry-point-rendered prompt")
|
|
start, end = prompt.span
|
|
covered = []
|
|
for token, (left, right) in enumerate(offsets[row].tolist()):
|
|
if attention_mask[row, token] and right > start and left < end:
|
|
result[row, token] = True
|
|
covered.append((left, right))
|
|
if not covered or min(a for a, _ in covered) > start or max(b for _, b in covered) < end:
|
|
raise ValueError("Entry point span is empty or truncated; increase max_seq_length")
|
|
return result
|