Sub topic 1
Why edge inference is different: latency, memory, power, and privacy
Prerequisites
This chapter assumes you can load or train a convolutional image classifier in PyTorch or TensorFlow. You should know float32 tensors, basic image preprocessing, and how a model's input shape is set.
Edge inference runs a model on the device itself, such as a phone, camera, or embedded board, instead of on a server. Four limits shape the design:
- Latency: users expect a result in tens of milliseconds, and a network round trip adds delay.
- Memory: an app may get only a few hundred megabytes of RAM.
- Power and heat: sustained computation drains the battery and triggers thermal throttling, which slows the model down.
- Privacy: images that stay on the device avoid upload and storage risks.
Choosing an efficient architecture
Start with a model built for mobile, such as MobileNetV3 or EfficientNet-Lite. These use depthwise separable convolutions to cut multiply-add operations. Choose the smallest model that meets your accuracy target on your own data, then measure it on the device.
Quantization: float32 to int8
Quantization stores weights and activations as 8-bit integers instead of 32-bit floats. The model becomes about four times smaller, and mobile CPUs and NPUs often run it faster. Post-training quantization calibrates value ranges with about 100 representative, preprocessed images. If accuracy drops too far, use quantization-aware training.
Pruning and knowledge distillation
Pruning removes weights or channels that contribute little. Knowledge distillation trains a small student model to match a large teacher. Both shrink a model but need extra training work, so apply them only when architecture choice and quantization are not enough.
Exporting and converting a model
Export the trained model to a portable format such as TensorFlow Lite or ONNX. The export needs a fixed input shape and operators that the target runtime supports.
import torch
import torchvision
model = torchvision.models.mobilenet_v3_small(weights="DEFAULT").eval()
dummy = torch.randn(1, 3, 224, 224)
torch.onnx.export(
model, dummy, "mobilenet_v3_small.onnx",
input_names=["input"], output_names=["logits"], opset_version=17,
)Exercise
Exercise
A model runs in 40 ms on a laptop but takes 300 ms on a phone after several minutes of use. Name two device limits that could explain the slowdown.
Show answer
Thermal throttling, which lowers clock speed under sustained load, and a slower CPU with no hardware delegate. Memory pressure that forces swapping or a smaller thread pool would also fit.