Sub topic 7
Using GPUs and NPUs through delegates and execution providers
Assume you know tensors, model export, and basic TensorFlow Lite or ONNX usage. A delegate or execution provider is a runtime component that sends model operations to hardware other than the default CPU.
Why acceleration is not automatic
A phone may contain several compute units:
- CPU: Flexible and widely supported, but often slower and less power-efficient for large neural networks.
- GPU: Performs many operations in parallel. It often helps convolution-heavy vision models.
- NPU: A neural processing unit designed for common tensor operations. It can provide good speed per watt, but it supports fewer operators and data types.
- DSP: A specialized low-power processor available on some devices.
The runtime chooses where each operation runs. If the selected accelerator does not support an operation, the runtime may place that operation on the CPU. This is called fallback.
Fallback can reduce performance. Moving tensors between CPU and accelerator memory can cost more time than the operation itself. A model that reports “GPU enabled” can still spend much of its time on the CPU.
| Runtime | Acceleration mechanism | Typical target |
|---|---|---|
| TensorFlow Lite | Delegates | Android GPU, NNAPI, iOS Metal |
| ONNX Runtime | Execution providers | CUDA, TensorRT, Core ML, NNAPI |
| Core ML | Compute units | Apple CPU, GPU, Neural Engine |
| ExecuTorch | Backends and delegates | Mobile CPU, GPU, and vendor hardware |
Selecting a delegate or provider
Start with the target device, not the fastest option on your development computer. A practical selection process is:
- Confirm that the device exposes the accelerator you want.
- Check which operators and tensor types it supports.
- Load the model with the accelerator configured first.
- Inspect logs or profiling data for CPU fallback.
- Compare latency, accuracy, and power against a CPU-only run.
Execution providers are often listed in priority order. In this example, ONNX Runtime tries TensorRT first, then CUDA, then CPU:
import onnxruntime as ort
session = ort.InferenceSession(
"vision_model.onnx",
providers=[
"TensorrtExecutionProvider",
"CUDAExecutionProvider",
"CPUExecutionProvider",
],
)
print("active providers:", session.get_providers())
input_name = session.get_inputs()[0].name
print("input:", input_name, session.get_inputs()[0].shape)The provider list does not prove that every node uses the first provider. The runtime can partition the graph and assign unsupported nodes to another provider.
Delegates and model compatibility
Accelerators usually prefer models with:
- Standard convolutions and matrix operations.
- Static input shapes.
- Supported activation functions.
- Float16 or int8 tensors, depending on the hardware.
- Few custom or control-flow operations.
Quantization can improve NPU performance, but only if the device supports the chosen quantized format. For example, a fully int8 model may run efficiently on one NPU while requiring CPU fallback on another. Float16 may be the better choice for a mobile GPU.
A delegate can also change numerical behavior. Reduced precision, different kernels, and operation fusion may produce slightly different outputs. Always compare predictions with the original model.
A repeatable diagnosis
When acceleration is slower than expected, check these causes:
- Unsupported operators caused graph partitioning.
- Tensors are copied repeatedly between CPU and accelerator memory.
- The input is resized or normalized on the CPU for every frame.
- The accelerator needs warm-up before steady-state timing.
- The model is too small to offset accelerator setup overhead.
- The device is already thermally throttled.
Exercise
Why can adding a GPU or NPU delegate make a model slower?
Show answer
Unsupported operations or repeated memory transfers can force CPU fallback. The transfer and scheduling overhead may exceed the accelerator's computational savings.