File size: 1,104 Bytes
dbe0ec7 d734701 dbe0ec7 d734701 dbe0ec7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | """Tensorize Verdict's native rendered text for one fixed Core ML bucket."""
from __future__ import annotations
import numpy as np
def prepare(
tokenizer, class_token_index: int, rendered: str, length: int, max_candidates: int
) -> dict[str, np.ndarray]:
full = tokenizer(rendered, truncation=False)
if len(full["input_ids"]) > length:
raise ValueError(f"Verdict prompt needs {len(full['input_ids'])} tokens; L{length} has no room")
encoded = tokenizer(rendered, truncation=False, padding="max_length", max_length=length, return_tensors="np")
ids = encoded["input_ids"].astype(np.int32)
positions = np.flatnonzero(ids[0] == class_token_index)
if len(positions) > max_candidates:
raise ValueError("candidate markers exceed exported head capacity")
markers = np.zeros((1, max_candidates, length), dtype=np.float32)
for row, position in enumerate(positions):
markers[0, row, position] = 1.0
return {
"input_ids": ids,
"attention_mask": encoded["attention_mask"].astype(np.int32),
"class_marker_map": markers,
}
|