#!/usr/bin/env python3 """Validate the self-contained Bina CoreML packages plus native host assets.""" from __future__ import annotations import argparse import json import time from pathlib import Path import coremltools as ct import numpy as np EOS_TOKEN_ID = 2 def load_raw(path: Path, shape: list[int], dtype: str) -> np.ndarray: array = np.fromfile(path, dtype=dtype) expected = int(np.prod(shape)) if array.size != expected: raise ValueError(f"{path}: expected {expected} scalars, got {array.size}") return array.reshape(shape) def contiguous_prefix(left: list[int], right: list[int]) -> int: for index, (actual, expected) in enumerate(zip(left, right)): if actual != expected: return index return min(len(left), len(right)) def through_eos(tokens: list[int]) -> list[int]: if EOS_TOKEN_ID in tokens: return tokens[: tokens.index(EOS_TOKEN_ID) + 1] return tokens def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--assets-dir", type=Path, required=True) parser.add_argument("--vision-package", type=Path, required=True) parser.add_argument("--prefill-package", type=Path, required=True) parser.add_argument("--decode-package", type=Path, required=True) parser.add_argument("--pixel-values", type=Path, required=True) parser.add_argument("--expected-receipt", type=Path) parser.add_argument("--output", type=Path, required=True) parser.add_argument("--max-new-tokens", type=int, default=64) args = parser.parse_args() started = time.perf_counter() assets = args.assets_dir.expanduser().resolve() constants = json.loads((assets / "surya_native_constants.json").read_text()) shapes = constants["shapes"] files = constants["files"] source_dtype = "