Sub topic 6
Inference runtimes: TensorFlow Lite, ONNX Runtime, Core ML, and ExecuTorch
Assume you know Python, tensors, image preprocessing, and the basics of PyTorch or TensorFlow. This chapter focuses on the runtime: the software that loads an exported model, prepares its inputs, executes operators, and returns predictions on an edge device.
What an inference runtime does
A training framework is designed to build and train models. An inference runtime is designed to run a finished model efficiently. It usually:
- Loads a model file.
- Allocates input and output buffers.
- Executes the model's operators.
- Uses a CPU, GPU, or neural processing unit when supported.
- Returns tensors for postprocessing.
The exported model and the runtime must agree on operator types, tensor shapes, data types, and preprocessing. A model that runs in PyTorch is not automatically ready for a phone.
Comparing the main runtimes
| Runtime | Common model format | Best fit | Main trade-off |
|---|---|---|---|
| TensorFlow Lite | `.tflite` | Android, embedded devices, cross-platform mobile | Conversion and operator support require checking |
| ONNX Runtime | `.onnx` | Cross-platform applications and many hardware providers | Package size and provider setup can vary |
| Core ML | `.mlmodel` or compiled Core ML package | Native Apple apps | Primarily targets Apple platforms |
| ExecuTorch | ExecuTorch program format | PyTorch models on mobile and embedded targets | Ecosystem and backend support are still more specialized |
TensorFlow Lite provides a small interpreter and supports delegates, which are plug-ins for accelerators. ONNX Runtime uses execution providers for similar hardware-specific execution. Core ML integrates closely with Apple hardware and its operating systems. ExecuTorch is PyTorch's edge-focused deployment path.
Choosing a runtime
Choose based on the target device, model source, supported operators, and hardware needs.
- Choose TensorFlow Lite when Android or embedded deployment is important and the model converts cleanly.
- Choose ONNX Runtime when you need one model format across several operating systems or already use ONNX.
- Choose Core ML when the application is Apple-only and native integration matters.
- Choose ExecuTorch when the model is developed in PyTorch and its target backend supports the required operators.
Do not choose only by desktop inference speed. A runtime may be fast on a development computer but fall back to the CPU on the phone because one operator is unsupported by the accelerator.
A small runtime example
This ONNX Runtime example loads a model, reads its input metadata, runs one tensor, and prints the output shape.
import numpy as np
import onnxruntime as ort
session = ort.InferenceSession(
"mobilenet_v3_small.onnx",
providers=["CPUExecutionProvider"],
)
input_info = session.get_inputs()[0]
input_tensor = np.zeros(input_info.shape, dtype=np.float32)
outputs = session.run(None, {input_info.name: input_tensor})
print("output shape:", outputs[0].shape)The zero tensor is only a structural test. A real application must resize and normalize an image exactly as training required.
Common runtime failures
An unsupported operator can prevent conversion or cause part of the graph to run on the CPU. A wrong input layout, such as using height-width-channels when the model expects channels-height-width, can produce poor predictions without raising an error. Quantized models also require the correct scale, zero point, and input data type.
Test the converted model against the original model on the same inputs. Then test it on the target device with the intended delegate or execution provider. Record both accuracy and latency.
Exercise
You need one model for Android and iOS, and the model already exports successfully to ONNX. Which runtime is a reasonable first choice, and what must you verify?
Show answer
Start with ONNX Runtime. Verify that the required operators, input preprocessing, output postprocessing, and hardware execution providers work on both target devices.