Core AI model zoo

Conversion guide (PyTorch → .aimodel)

Re-author the model with coreai_models primitives (so it lowers cleanly), export via torch.export, convert with TorchConverter, optimize, save. Verify numerically against the Hugging Face reference (cosine / top-1 argmax) before trusting a bundle.

Canonical API (gotchas burned in)

import torch, shutil
from pathlib import Path
from coreai_torch import TorchConverter, get_decomp_table
import coreai.runtime as rt

ep = torch.export.export(m.eval(), args=(), kwargs=inputs).run_decompositions(get_decomp_table())
prog = (TorchConverter()
        .add_exported_program(exported_program=ep, input_names=[...], output_names=[...])
        .to_coreai())
prog.optimize()
shutil.rmtree(out, ignore_errors=True)                 # save_asset will NOT overwrite
prog.save_asset(Path(out), rt.AIModelAssetMetadata())  # Path (not str); minimum_os defaults to v27

model = await rt.AIModel.load(Path(out), rt.SpecializationOptions.cpu_only())  # async; cpu_only for parity
fn = model.load_function("main")                       # SYNC; model.function_names is a list attr
res = await fn({"x": rt.NDArray(np_or_tensor)})        # __call__ is ASYNC -> dict[str, NDArray]

Gotchas that cost real time

Verify, don’t trust

Compare to an HF eager reference: cosine ≈ 1.0 and top-1 argmax match on a fixed prompt is the real pass criterion. A large hidden-state maxdiff alone is usually h16c compute noise, not a bug — let argmax/top-5 decide. Re-run parity after any OS/toolchain bump (the runtime is OS-coupled).

Detection transformers

Found by the RF-DETR port (zoo’s first detector, models/rf-detr/README.md). Four platform bugs, each pinned by a minimal repro; all four bite ANY model, not just detection — detection transformers just happen to use the trigger ops (sine position embeds, bilinear sampling masks, coordinate floors).

  1. aten.arange with float start/end/step aborts the converter — C++ bad_optional_access, no Python error. Repro: any graph containing torch.arange(8.0) (torch.arange(8) is fine; the dtype doesn’t matter, the argument types do). DETR-class models hit it via gen_sineembed_for_position(…, d_model / 2) — a float dim. Fix: precompute the dim_t vector as a Python-list constant (kills the runtime arange/floordiv/pow chain too).
  2. int64-comparison bool chains clobber unrelated live buffers at runtime. A chain like ((ix0 >= 0) & (ix0 < W)).to(float) on .long() tensors makes a different, still-live fp tensor (two ops upstream; even a graph OUTPUT) read back garbage/NaN once the subgraph executes. Deterministic, unit-independent (CPU too); clone() / contiguous() barriers do NOT protect the victim; skipping optimize() doesn’t either. Diagnosis pattern: a tensor is provably computed right (its other consumer is exact) but reads wrong later → buffer-liveness bug, hunt the comparison chain. Fix: compute 0/1 masks in float arithmetic — 1 - (x - x.clamp(lo, hi)).abs().clamp(max=1) is exact on integer-valued floats.
  3. aten.floor / trunc / ceil lower to IDENTITY on the GPU delegate (CPU correct; round rounds ties away-from-zero instead of to-even). Two natural workarounds also fail: div(x, 1, rounding_mode="floor") simplifies to identity, and float→long→float roundtrips are cast-cancelled by the converter dropping truncation semantics (CPU too). The floor that survives every unit: torch.div(x * 2.0, 2.0, rounding_mode="floor") — floor-div with divisor ≠ 1 lowers correctly, and ×2/2 is a power-of-two scale (exact in fp). Corollary: the “compute remainder with floor” advice above must use THIS floor on GPU paths.
  4. torch._assert on data-dependent comparisons breaks torch.export non-strict (GuardOnDataDependentSymNode, torch 2.11) — ironically added by upstreams for export compatibility. For static-shape exports the check is vacuous: no-op torch._assert around torch.export.export and restore after.

Detection-gate design note: gate detector numerics with set-based matching (per confident oracle detection: same class, IoU ≥ 0.75, score within tolerance), not positional top-k compare — DETR-family models emit near-duplicate predictions whose ranks swap under fp16/h16c noise, and positional compare overflags healthy conversions. Random-noise inputs flip two-stage top-k proposal selection at near-ties (the detection analog of the LLM argmax margin rule) — use real images for the gate, noise only as an informational probe.

Publishing & fetching artifacts (HF Xet)

Hugging Face now stores large LFS files in Xet content-addressed storage, and this bites both download and upload of multi-GB .aimodel bundles:

Device-integration traps (detection/CV apps; from the RF-DETR device work, added 2026-07-14)