Sub topic 1
Why Edge Inference Is Different: Latency, Memory, Power, and Privacy
Prerequisites
This chapter assumes you can read basic Python and have trained or used an image model in PyTorch or TensorFlow. You should know what a convolutional neural network (CNN) is, and how float32 and int8 differ as tensor data types. You don't need mobile development experience. Later chapters cover Android and iOS in detail.
What "edge" means here
Edge inference means running a trained model on the device that produces the data, instead of sending the data to a server. That device may be a phone, a camera, a drone, a single-board computer, or a microcontroller board. This tutorial focuses on phones and similar devices with a CPU, a GPU, and often a neural processing unit (NPU).
Training stays on servers. Only the forward pass moves to the device. The rest of the tutorial is about making that forward pass fit. This chapter explains the limits that shape every later decision.
The four pressures
A server has plenty of memory, wall power, active cooling, and a fast network. A phone has none of these in the same amount. Four pressures follow.
| Pressure | Server inference | Edge inference |
|---|---|---|
| Latency | Network round trip plus fast compute | No network, but slower compute |
| Memory | Tens of GB, shared by few models | Often 2–12 GB shared with the OS and other apps |
| Power | Wall power | Battery, with a drain the user can see |
| Heat | Fans and datacenter cooling | Passive cooling, so speed drops when hot |
| Privacy | Data must travel to the server | Data can stay on the device |
Latency
Latency is the time from input to result. On the edge, the model's compute time is the main part of it. A server request adds network time instead, which varies with signal quality and can fail altogether.
Latency matters most for live camera features. At 30 frames per second (fps), a new frame arrives every 33.3 ms. If the whole pipeline takes longer than that, you must drop frames or lower the frame rate. The pipeline includes camera capture, preprocessing, inference, postprocessing, and drawing results. The model gets only a fraction of the 33.3 ms.
Two latency numbers are worth separating from the start:
- Typical latency is the median over many runs.
- Tail latency is a high percentile, such as the 95th. A user perceives stutter from the slow runs, not the average ones.
Cold-start latency is a third number. The first inference after loading a model is often much slower, because the runtime allocates buffers and may compile the model for an accelerator. Measure it separately from steady-state speed. The benchmarking chapter shows how.
Memory
Memory limits show up in three places:
- Storage size. The model file ships inside your app or downloads later. Large files raise download times and can hit app store size limits.
- Weights in RAM. Loading the model puts its parameters in memory.
- Activations. These are the intermediate tensors produced layer by layer. For vision models on large inputs they can use more memory than the weights.
The operating system also enforces per-app memory limits. If your app exceeds its limit, the OS kills it. This is why a model that "fits" on a test phone can crash on a cheaper one.
Weight memory is easy to estimate: parameter count times bytes per parameter. The script below does this arithmetic and also computes the per-frame time budget.
def model_size_mb(num_params: int, bytes_per_param: int) -> float:
return num_params * bytes_per_param / 1e6
# Approximate parameter counts of well-known image classifiers.
models = {
"ResNet-50": 25_600_000,
"MobileNetV2": 3_500_000,
"MobileNetV3-Small": 2_500_000,
}
for name, params in models.items():
fp32 = model_size_mb(params, 4) # float32 uses 4 bytes per weight
int8 = model_size_mb(params, 1) # int8 uses 1 byte per weight
print(f"{name:18s} float32 {fp32:6.1f} MB int8 {int8:6.1f} MB")
fps = 30
budget_ms = 1000 / fps
print(f"\nFrame budget at {fps} fps: {budget_ms:.1f} ms for the whole pipeline")The output shows ResNet-50 at about 102 MB in float32 and about 26 MB in int8. MobileNetV2 is about 14 MB and 3.5 MB. These figures count weights only. Activations, runtime overhead, and the camera buffers come on top.
Power
Every inference spends battery. Power use is roughly energy per inference multiplied by how often you run it. A model that takes 20 ms once per second costs far less than the same model on every frame at 30 fps.
Power depends on where the computation runs. A CPU is flexible but usually costs more energy per operation than a GPU or NPU doing the same work. Moving memory is also expensive: reading data from DRAM often costs more energy than the arithmetic on it. Smaller models and lower-precision numbers help because they move fewer bytes.
Battery drain is a product concern, not only an engineering one. Users uninstall apps that drain the battery, and the OS may flag them.
Heat and throttling
A phone has no fan. When the chip heats up, the OS lowers clock speeds to protect hardware and keep the case comfortable to hold. This is thermal throttling.
The consequence for benchmarking is important. A model that runs at 15 ms in the first ten seconds may run at 30 ms after several minutes of continuous use. Short benchmarks overstate sustained speed. Plan for the throttled number if your feature runs for a long time, such as a camera viewfinder or video processing.
Other things that change speed on a real device:
- Background load from other apps
- Battery saver mode
- Ambient temperature and charging state
- Differences between phone models with the same chip family
Privacy
Running locally means the camera frame never leaves the phone. This helps with:
- Legal and policy constraints on faces, documents, or medical images
- User trust
- Offline operation, such as in a tunnel or at a remote site
- Lower server cost, since you don't pay for inference per request
Privacy is a benefit of on-device inference, but it isn't automatic. The model file itself is on the user's device, so a determined person can extract it. Treat the weights as readable. If the model is a trade secret, edge deployment exposes it. Also, if your app still uploads results or analytics, check what those contain.
How the pressures interact
The pressures are linked, and improving one often affects another.
For example, int8 quantization shrinks the file by about four times compared with float32. It also reduces memory traffic and can unlock fast NPU paths. The cost is a possible drop in accuracy, which you must measure. A faster accelerator can lower energy per inference, but only if the operators your model uses are supported there. Otherwise work falls back to the CPU. Later chapters cover each lever in turn.
Why a server benchmark is not enough
A common mistake is to judge a model on a desktop GPU and assume the number carries over. It does not, for several reasons:
- Floating-point operation counts (FLOPs) are a poor predictor of phone speed. Memory access patterns and operator support often dominate.
- The accelerator may not support every operator, so parts of the graph run on a slower processor.
- Thermal and power behavior only appear on the device.
- The full pipeline includes preprocessing, which can cost as much as inference for small models.
The working rule is: use parameter counts and FLOPs to shortlist candidates, then measure the shortlist on the target hardware.
Setting a budget before you choose a model
Start from the product requirement and work backward. A practical process:
- Define the target devices, including a low-end one you must still support.
- Define a latency target, such as median under 30 ms and 95th percentile under 50 ms.
- Define a size limit for the download or the app bundle.
- Define an accuracy floor on your own validation data, not only on a public benchmark.
- Decide how often the model runs, and for how long continuously.
These five numbers act as pass/fail tests for every later choice. A model that misses any one is out, even if it is excellent on the others.
Common mistakes
- Treating the first inference as representative. Warm up the model before timing it.
- Testing only on a flagship phone. The low-end device defines your real constraint.
- Ignoring the preprocessing cost, such as resizing and color conversion on the CPU.
- Running inference on every frame when every third frame would do.
- Assuming "on-device" means "private" without checking what else leaves the device.
Summary of the working model
Edge inference is a constrained optimization problem. You have limits on time, memory, energy, and heat, and in return you get lower network dependence and better data control. The rest of this tutorial gives you the tools to meet those limits: efficient architectures, quantization, export formats, runtimes, accelerators, and measurement on real hardware.
Exercise
A camera app must process live video at 30 fps. Preprocessing takes 6 ms, drawing the results takes 4 ms, and capture overhead takes 3 ms per frame. What is the largest median inference time that still keeps up with 30 fps? Why should you also worry about more than the median?
Show answer
At 30 fps the budget is 1000 / 30 = 33.3 ms per frame. Subtracting 6 + 4 + 3 = 13 ms leaves about 20 ms for inference. The median alone is not enough because slow runs cause visible stutter, so you should also check a tail percentile such as p95. Thermal throttling can also raise the sustained time above a short-test median, so you should measure after several minutes of continuous use on the real device.
Exercise
A float32 classifier has 8 million parameters. Estimate its weight size in float32 and in int8. Name one reason the real memory use will be higher than the int8 figure.
Show answer
Float32 uses 4 bytes per weight, so 8,000,000 × 4 = 32 MB. Int8 uses 1 byte per weight, so the size is about 8 MB. Real memory use is higher because activations, runtime buffers, and input and output tensors also need memory, and the app has its own overhead.