"""Check the ONNX Runtime setup for AE3-TL on this machine. Run from the repository root: python check_env.py It prints the runtime, the platform, the available providers, and the time per page for CPU and, on a Mac, for CoreML, plus a parity check between the two. """ import os import platform import sys import time import numpy as np from PIL import Image import onnxruntime as ort PAGES = "weights/ae3tl_pages_v3_ep1_fp32_opset17.onnx" def coreml_providers(): cache = os.environ.get("AE3_COREML_CACHE", os.path.expanduser("~/.cache/ae3tl-coreml")) return [("CoreMLExecutionProvider", {"ModelFormat": "MLProgram", "MLComputeUnits": "ALL", "RequireStaticInputShapes": "1", "ModelCacheDirectory": cache}), "CPUExecutionProvider"] def time_session(prov, x, label): model, opts = PAGES, None if any("CoreML" in str(p) for p in prov): b1 = os.path.splitext(PAGES)[0] + "_b1.onnx" if os.path.exists(b1): model = b1 else: opts = ort.SessionOptions() opts.add_free_dimension_override_by_name("batch", 1) t0 = time.time() try: session = (ort.InferenceSession(model, opts, providers=prov) if opts else ort.InferenceSession(model, providers=prov)) except Exception as e: print(f"{label}: session failed: {type(e).__name__}: {e}") return None t1 = time.time() outs = session.run(None, {"images": x}) t2 = time.time() for _ in range(3): session.run(None, {"images": x}) t3 = time.time() print(f"{label}: session {t1 - t0:.1f}s (compile on first run), first run {t2 - t1:.2f}s, " f"{(t3 - t2) / 3 * 1000:.0f} ms/page") return outs def main(): print("onnxruntime:", ort.__version__) print("platform:", platform.platform(), "| machine:", platform.machine()) print("python:", sys.version.split()[0]) have = ort.get_available_providers() print("available providers:", ", ".join(have)) if not os.path.exists(PAGES): print("weights not found at", PAGES, "- run this from the repository root") return x = (np.asarray(Image.new("RGB", (768, 768), "white"), dtype=np.float32) / 255.0 ).transpose(2, 0, 1)[None] outs = {"cpu": time_session(["CPUExecutionProvider"], x, "cpu")} if "CoreMLExecutionProvider" in have: outs["coreml"] = time_session(coreml_providers(), x, "coreml") if outs.get("cpu") is not None and outs.get("coreml") is not None: diffs = [float(np.max(np.abs(a - b))) for a, b in zip(outs["cpu"], outs["coreml"]) if a.dtype.kind == "f"] print(f"parity: max abs difference between CPU and CoreML float outputs: {max(diffs):.2e}") else: print("parity: skipped, both providers are needed") if __name__ == "__main__": main()