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