Sub topic 2
Inference runtimes: TensorFlow Lite, ONNX Runtime, Core ML, and ExecuTorch
This chapter assumes you have exported a vision model to a deployable file and can write basic Python. An inference runtime is the library that loads a converted model and runs it on a device. Each platform has its own runtime: TensorFlow Lite (TFLite) for Android and microcontrollers, ONNX Runtime for cross-platform use, Core ML for Apple devices, and ExecuTorch for PyTorch models on mobile and embedded targets.
Choosing a runtime
Match the runtime to the model file format and the target OS. A .tflite file runs with the TFLite interpreter. An .onnx file runs with ONNX Runtime. A .mlpackage runs with Core ML on iOS and macOS. A .pte file, exported from PyTorch, runs with ExecuTorch.
Using GPUs and NPUs through delegates and execution providers
A delegate (TFLite) or execution provider (ONNX Runtime) hands supported operators to a GPU or NPU. Unsupported operators fall back to the CPU. List your preferred providers in order, and keep the CPU last as a safe fallback.
import onnxruntime as ort
preferred = ["CoreMLExecutionProvider", "CUDAExecutionProvider", "CPUExecutionProvider"]
available = ort.get_available_providers()
providers = [p for p in preferred if p in available]
session = ort.InferenceSession("mobilenet_v3_small.onnx", providers=providers)
print("using:", session.get_providers())Preprocessing and postprocessing that match training
The input must use the same resize, channel order, and normalization as training. Put these steps in one function and reuse it on every platform.
| Step | Training value | Common mistake |
|---|---|---|
| Channel order | RGB | BGR from OpenCV |
| Scale | 0 to 1 | 0 to 255 |
| Normalize | Training mean and std | Step skipped |
Benchmarking latency on the real device
Desktop timings do not predict phone timings. Run warm-up iterations first, then time many runs and report the median and 95th percentile.
import time
import numpy as np
import tensorflow as tf
interpreter = tf.lite.Interpreter(model_path="mobilenet_v2_int8.tflite", num_threads=4)
interpreter.allocate_tensors()
inp = interpreter.get_input_details()[0]
image = np.zeros(inp["shape"], dtype=inp["dtype"])
for _ in range(10): # warm-up, not timed
interpreter.set_tensor(inp["index"], image)
interpreter.invoke()
times = []
for _ in range(100):
start = time.perf_counter()
interpreter.set_tensor(inp["index"], image)
interpreter.invoke()
times.append((time.perf_counter() - start) * 1000)
print(f"median {np.median(times):.1f} ms, p95 {np.percentile(times, 95):.1f} ms")Exercise
Exercise
Your ONNX Runtime session reports only CPUExecutionProvider on a device with an NPU. What are two likely causes, and what would you check first?
Show answer
The installed runtime build may lack the NPU provider, or the model may contain operators the NPU does not support, so the runtime fell back to the CPU. Check ort.getavailableproviders() first to confirm the NPU provider exists, then review the session log for operators that were not assigned to it.