Sub topic 13
Unsupported operators, CPU fallback, and other common failures
This chapter assumes you can export a model to ONNX or TensorFlow Lite, know what a delegate or execution provider is, and have run a basic latency benchmark. It covers the failures you are most likely to hit when a model that works on your desktop misbehaves on a phone, and how to find the cause of each.
Why failures happen at three different stages
An edge deployment fails in one of three places. Knowing which one narrows the search quickly.
- Conversion time. The exporter or converter cannot translate an operation. You get an error and no model file.
- Load time. The file exists, but the runtime or delegate cannot take part of the graph. The model loads with a warning, or it refuses to load.
- Run time. The model loads and runs, but it is slow, wrong, or crashes under sustained use.
Run-time failures are the hardest to notice, because nothing raises an error. A model that silently runs on the CPU instead of the NPU still returns correct answers.
Unsupported operators
An operator (op) is one node in the graph, such as Conv, Resize, or NonMaxSuppression. Each format and each accelerator supports a fixed set of ops. Three different lists matter:
- The ops the exporter can write (for example, the ONNX opset you chose).
- The ops the runtime can run on the CPU.
- The ops the delegate or execution provider can run on its hardware. This list is usually the shortest.
Start by listing what your model contains. This script counts the ops in the ONNX file exported earlier in this tutorial.
import collections
import onnx
model = onnx.load("mobilenet_v3_small.onnx")
counts = collections.Counter(node.op_type for node in model.graph.node)
for op_type, n in counts.most_common():
print(f"{op_type:24s}{n}")Compare the output with the documentation for your target runtime and accelerator. Common trouble spots in vision models:
| Operation | Typical problem |
|---|---|
| Hard-swish, GELU, SiLU | Missing or approximated on older delegates |
| Resize with `align_corners` or unusual modes | Different results, or CPU-only |
| Non-max suppression, `TopK`, `Where` | Dynamic output sizes that accelerators cannot handle |
| Custom layers from detection or segmentation repositories | No equivalent in the target format |
| Dynamic shapes | Many delegates need fixed input sizes |
Fixes, in order of preference
- Replace the op in the model. Swap an unsupported activation for a supported one and fine-tune briefly. This keeps the whole graph on the accelerator.
- Move the op out of the model. Run non-max suppression, decoding, and sorting in app code after inference. Many detection exports are easier to deploy when they end at the raw box and score tensors.
- Raise the opset or runtime version. A newer ONNX opset or a newer runtime release may add the op. Check this before rewriting anything.
- Allow a fallback op set. TensorFlow Lite can embed full TensorFlow kernels for ops it lacks.
import tensorflow as tf
model = tf.keras.applications.MobileNetV2(
weights="imagenet", input_shape=(224, 224, 3)
)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS,
tf.lite.OpsSet.SELECT_TF_OPS,
]
tflite_model = converter.convert()
with open("mobilenet_v2_select_ops.tflite", "wb") as f:
f.write(tflite_model)CPU fallback
When a delegate cannot run some ops, the runtime splits the graph into partitions. Supported stretches run on the accelerator and the rest runs on the CPU. Every switch between them copies tensors across memory domains and synchronizes the two sides.
The cost is not only the CPU ops themselves. A single unsupported op in the middle of the network creates two copies and a synchronization, and that can make the delegated model slower than plain CPU inference. Symptoms are:
- The accelerated run is no faster than the CPU run, or is slower.
- Latency has a high variance, because the CPU partitions compete with other apps.
- Reported delegate usage covers only a small share of the nodes.
Seeing what actually ran
Never assume the delegate was used. Ask the runtime. With ONNX Runtime, request only providers that are present, then check which ones the session accepted, and enable verbose logging to see the node assignment.
import onnxruntime as ort
preferred = [
"CoreMLExecutionProvider",
"NnapiExecutionProvider",
"CUDAExecutionProvider",
"CPUExecutionProvider",
]
available = ort.get_available_providers()
providers = [p for p in preferred if p in available]
options = ort.SessionOptions()
options.log_severity_level = 0 # verbose: logs which nodes go to which provider
session = ort.InferenceSession(
"mobilenet_v3_small.onnx", sess_options=options, providers=providers
)
print("requested:", providers)
print("active:", session.get_providers())For TensorFlow Lite, the benchmark_model tool reports delegate partitioning and per-op timing. Run it on the device, not on your desktop.
adb push mobilenet_v2_int8.tflite /data/local/tmp/
adb shell /data/local/tmp/benchmark_model \
--graph=/data/local/tmp/mobilenet_v2_int8.tflite \
--use_gpu=true \
--enable_op_profiling=true \
--num_runs=100 \
--warmup_runs=10Look for log lines that say how many nodes were delegated and into how many partitions. One large partition is good. Many small partitions indicate scattered unsupported ops. Exact log wording differs between releases, so read the output you get rather than matching strings.
Other common failures
Shape and layout mismatches
PyTorch uses NCHW (channels first) and TensorFlow Lite usually uses NHWC (channels last). Converters insert transposes for you, but your app code feeds the tensor directly. Always read the shape from the model, as the interpreter example earlier did, rather than hard-coding it. A wrong layout often produces no error. It produces nonsense scores.
Dynamic shapes
Models exported with dynamic batch or image-size axes may load on the CPU and fail on a delegate. Export with a fixed shape for the device, such as 1x3x224x224, unless you have a reason to do otherwise.
Quantization surprises
- The input type is
uint8orint8, but the app passesfloat32. The runtime raises a type error, or reads garbage. - Some ops are left in float32 inside an "int8" model. Each boundary adds quantize and dequantize steps, and the accelerator may refuse the float ops.
- Accuracy drops on a few layers with wide activation ranges. Check these with a representative dataset of real images, not random noise.
Preprocessing drift
If the app resizes with a different interpolation, orders channels as BGR instead of RGB, or normalizes with different constants, accuracy falls with no error. Compare the app's output with the Python reference on the same saved image. Differences larger than small numerical noise mean the pipelines disagree.
Warm-up and first-run cost
The first inference can take far longer because the delegate compiles kernels or loads weights. Measure after warm-up, and in the app, run a dummy inference during startup instead of on the user's first photo.
Thermal throttling
A benchmark of 100 runs may look fine while a 5-minute camera session slows down as the chip heats. Benchmark for long enough to see the trend, and watch whether latency rises over time.
Memory and crashes
Large models plus camera buffers can push an app past its memory limit, and the OS then kills it with no stack trace. Reuse input buffers, release interpreters you no longer use, and check peak memory on a low-end device.
A triage routine
Change one thing at a time. If you switch the delegate, the precision, and the model together, you will not know which change helped.
Exercise
A team enables a GPU delegate for their detection model. The delegated model runs at 48 ms per frame, while the CPU-only model runs at 41 ms. The logs show that the delegate took 14 partitions. What is the most likely cause, and what are two ways to fix it?
Show answer
Unsupported ops are scattered through the graph, so the delegate gets many small partitions, and the copies between GPU and CPU cost more than the GPU saves. Fixes: replace the unsupported ops with supported ones (or move them, such as non-max suppression and decoding, into app code after inference), so the graph forms one large partition. Another fix is to use a newer runtime or opset that supports the op. If neither works, run the model on the CPU.
Exercise
Your int8 model gives sensible results in Python but nonsense scores in the app, and no error is raised. Name three things to check.
Show answer
Check the input type and quantization parameters the app passes against what the interpreter reports. Check channel order and tensor layout (RGB vs BGR, NCHW vs NHWC). Check resize interpolation and normalization constants against the training pipeline. Then compare the app's output with the Python reference on the same saved image.