#!/usr/bin/env python3 """ Test ONNX Runtime CoreML Execution Provider """ import os import sys import time import numpy as np import onnxruntime as ort def test_coreml(model_path: str, num_runs: int = 10): """Test CoreML EP performance""" print(f"[TEST] Loading model: {model_path}") # Test 1: CPU print(f"\n[TEST] CPU Execution Provider:") cpu_session = ort.InferenceSession( model_path, providers=['CPUExecutionProvider'] ) input_name = cpu_session.get_inputs()[0].name dummy_input = np.random.randn(1, 3, 160, 160).astype(np.float32) # Warmup cpu_session.run(None, {input_name: dummy_input}) # Test cpu_times = [] for _ in range(num_runs): start = time.time() cpu_session.run(None, {input_name: dummy_input}) cpu_times.append((time.time() - start) * 1000) print(f" Average: {np.mean(cpu_times):.2f} ms") print(f" Min: {np.min(cpu_times):.2f} ms") print(f" Max: {np.max(cpu_times):.2f} ms") # Test 2: CoreML (if available) print(f"\n[TEST] CoreML Execution Provider:") available_providers = ort.get_available_providers() print(f" Available providers: {available_providers}") if 'CoreMLExecutionProvider' in available_providers: try: coreml_session = ort.InferenceSession( model_path, providers=['CoreMLExecutionProvider'] ) # Warmup coreml_session.run(None, {input_name: dummy_input}) # Test coreml_times = [] for _ in range(num_runs): start = time.time() coreml_session.run(None, {input_name: dummy_input}) coreml_times.append((time.time() - start) * 1000) print(f" Average: {np.mean(coreml_times):.2f} ms") print(f" Min: {np.min(coreml_times):.2f} ms") print(f" Max: {np.max(coreml_times):.2f} ms") # Compare speedup = np.mean(cpu_times) / np.mean(coreml_times) print(f"\n Speedup: {speedup:.2f}x") except Exception as e: print(f" ❌ CoreML failed: {e}") else: print(f" ❌ CoreML EP not available") print(f" Note: CoreML EP requires macOS and onnxruntime-coreml package") print(f" Install: pip install onnxruntime-coreml") if __name__ == "__main__": model_path = sys.argv[1] if len(sys.argv) > 1 else "models/facenet_vggface2.onnx" num_runs = int(sys.argv[2]) if len(sys.argv) > 2 else 10 test_coreml(model_path, num_runs)