Training a neural network gets all the glory, but running one — inference — is where most AI actually lives. Every photo your phone enhances, every voice command your speaker understands, every product your camera counts is an inference: a trained model taking in new data and producing an answer. And inference has a hard requirement that training does not: it must be fast enough, cheap enough, and small enough for the device it runs on.
That requirement is why inference optimization matters so much. A model that is brilliant in a research lab but takes ten seconds per image on a small device is useless for a doorbell camera. A model that needs a server rack to run cannot live in a drone. Optimization is the craft of making models practical: shrinking them, speeding them up, and trimming their appetite for memory and power, while keeping their answers good enough for the job.
This guide explains the main inference optimization techniques in plain language, with a sense of when each one helps and what it costs. You do not need to be a machine learning engineer to follow it. If you have run a model on a small board — for example, following our guide on how to run AI models on Raspberry Pi — you already have the context you need, and these techniques are the next step to making those projects genuinely fast.
What Inference Actually Costs
To optimize inference, it helps to know where the time and memory go. A neural network is mostly a long sequence of multiplications and additions, organized into layers. Running a model means pushing input data through those layers, one after another, with each layer’s output feeding the next. The cost comes from three places: the arithmetic itself, the memory needed to hold the model’s weights and the intermediate results, and the movement of data between memory and processor.
On a small device, memory movement is often the real bottleneck, not raw arithmetic. Processors can multiply numbers quickly, but fetching weights from memory takes time and energy, and a model with hundreds of millions of weights simply has a lot to fetch. This is why shrinking a model often speeds it up more than a faster processor would: fewer weights mean less to move, and less to move means faster answers and lower power draw.
Latency and throughput are two different goals, and confusing them leads to bad decisions. Latency is how long one input takes to get an answer — critical for a doorbell or a robot, where a late answer is a wrong answer. Throughput is how many inputs you can process per second — critical for a server analyzing a backlog of images, where individual speed matters less than total volume. Some techniques improve both, but others trade one for the other: batching, for example, raises throughput while making each individual answer slower. Know which one your project needs before you optimize.
Accuracy is the third corner of the triangle. Nearly every optimization trades a little accuracy for speed or size, and the art is in knowing how much accuracy your application can afford to lose. A wildlife camera that misidentifies one bird in twenty is still useful; a medical screening tool with the same error rate is not. Always measure accuracy on your own data after optimizing, never on the vendor’s demo data alone.
Quantization: Smaller Numbers, Faster Math
Quantization is the single most effective inference optimization, and it is where most people should start. A typical trained model stores its weights as 32-bit floating-point numbers — precise, but heavy. Quantization converts those weights to 8-bit integers, cutting the model to roughly a quarter of its size. Integer arithmetic is also faster on most device processors, and many chips have dedicated instructions for it, so a quantized model can run several times faster than the original.
The catch is precision. Rounding every weight to one of 256 values loses information, and for some models that loss changes answers. In practice, well-designed models tolerate 8-bit quantization with only a small accuracy drop, often a percentage point or two on standard benchmarks. The drop matters more for models that were already marginal, and less for models with accuracy to spare. The only reliable way to know is to test: quantize, then run your evaluation set and compare.
There are two main ways to quantize. Post-training quantization takes a finished model and converts it, with no retraining. It is quick and easy, and it is the right first step. Quantization-aware training goes further: the model is trained while simulating quantization, so it learns weights that survive rounding better. This takes more effort and access to training data, but it recovers accuracy that post-training quantization loses, and it is the standard approach for production models on devices.
Frameworks make both approaches accessible. PyTorch’s quantization documentation walks through post-training and quantization-aware workflows, and TensorFlow Lite offers similar tooling with ready-made quantized models. If you are deploying to a phone or a single-board computer, check whether your target runtime prefers a particular quantized format before you invest time converting — compatibility saves more time than any single optimization.
Mixed precision is a refinement worth knowing. Instead of quantizing everything to 8 bits, you keep sensitive layers — often the first and last — at higher precision, and quantize the rest. This recovers much of the lost accuracy for a small cost in size and speed, and most quantization tools support it as an option. When a fully quantized model disappoints, mixed precision is the first thing to try before giving up on quantization.

Pruning: Removing What the Model Does Not Need
Neural networks are usually overbuilt. Training produces many weights that contribute little to the final answer, and pruning removes them. The simplest form, weight pruning, sets the smallest weights to zero. The model becomes sparse — full of zeros — and sparse models can be stored compactly and, on hardware that supports sparsity, run faster.
Structured pruning goes further by removing whole units: entire filters, channels, or attention heads that matter least. This is more useful in practice, because removing a whole channel genuinely shrinks the computation, whereas scattered zeros only help if the hardware and software know how to skip them. Most device runtimes handle structured pruning well and weight sparsity less well, so structured pruning is usually the better investment for edge deployment.
Pruning is rarely a one-shot operation. The standard recipe is to prune a little, retrain briefly to let the remaining weights compensate, and repeat. This prune-and-fine-tune loop can remove a large fraction of a model’s weights while keeping accuracy close to the original. Like quantization, pruning demands measurement: prune too aggressively and accuracy falls off a cliff, and the cliff’s location varies by model and task.
Pruning and quantization combine well, and production pipelines often use both: prune first to remove dead weight, then quantize what remains. The order matters, because quantizing a pruned model is cleaner than pruning a quantized one. Together they can shrink a model to a tenth of its original size, which is often the difference between a model that fits on a device and one that does not.
Distillation: Teaching a Small Model to Act Big
Knowledge distillation takes a different approach: instead of shrinking a big model, you train a small one to imitate it. The big model, the teacher, is trained normally. Then the small model, the student, is trained not just on the correct answers but on the teacher’s full output — its confidence across all categories, including its near-misses. Those soft outputs carry information about how categories relate, and the student learns more from them than from hard labels alone.
Distillation is powerful because the student can be designed from scratch for the target device: the right size, the right architecture, the right speed. It is also flexible, because the teacher and student need not share an architecture at all. A large transformer can teach a small convolutional network, or an ensemble of models can teach a single efficient one.
The cost is training effort. Distillation needs the teacher, the student, and a training run, plus the data to train on. For teams that already train models, this is a natural step. For beginners using ready-made models, distillation is less accessible — you cannot easily distill a model you downloaded without its training data and pipeline. In that case, quantization and pruning are the more practical tools, and distillation is something to appreciate from the outside until you start training your own models.
Runtime Optimization: TensorRT, ONNX, and Delegates
The model file is only half the story; the runtime that executes it matters just as much. A runtime takes a model, plans how to run it on your hardware, and executes the plan. Good runtimes fuse operations — combining several small steps into one kernel so data does not travel to memory and back between them — and they choose the fastest available implementation for each operation on your specific chip.
On NVIDIA hardware, TensorRT is the standard optimizer: it takes a trained model, applies fusion, quantization, and kernel selection, and produces an engine tuned for a specific GPU. On other hardware, ONNX Runtime plays a similar role, running models in the open ONNX format with execution providers for CPUs, GPUs, and accelerators. TensorFlow Lite has delegates that hand parts of a model to specialized hardware, such as a phone’s neural processor or a Coral accelerator.
The practical lesson is to match the model format to the runtime your device supports best, rather than assuming one format is universally fastest. A model that flies under TensorRT on a Jetson board may crawl under a generic runtime on the same board, and vice versa. Benchmark the combination you will actually ship, not the model in isolation.
Graph optimization is another runtime trick worth knowing. Runtimes can remove operations that do nothing, fold constants into weights, and reorder operations to reduce memory traffic, all without changing the model’s answers. These optimizations are free accuracy-wise — they change how the model runs, not what it computes — which makes them the safest optimizations of all. If your runtime offers an optimization level setting, turning it up is usually a pure win.

Batching, Caching, and System-Level Tricks
Not all optimization lives inside the model. System-level techniques can matter as much as model-level ones, and they are often simpler to apply.
Batching means processing several inputs together instead of one at a time. Processors are more efficient when they work on batches, because the model’s weights are fetched once and reused across the batch. This raises throughput substantially, which is why servers batch aggressively. But batching delays each individual answer until its batch is full, so it hurts latency. Use batching for offline or server workloads, not for interactive ones.
Caching means remembering answers. If your camera watches a mostly static scene, you can skip inference when the frame has not changed meaningfully, or reuse a recent result. If your application classifies the same inputs repeatedly — the same product photos, the same documents — a cache of past results avoids recomputation entirely. Caching is unglamorous and extremely effective, and it is often the first optimization a production system reaches for.
Early exits are a cleverer trick: some models are built with intermediate outputs, so easy inputs can leave the network early with a confident answer, while hard inputs continue through the full model. A clear photo of a dog exits early; a blurry partial view goes deeper. This adapts compute to difficulty automatically, and it works well for applications where most inputs are easy.
Finally, do not forget the pipeline around the model. Resizing images on the processor before inference, decoding video efficiently, and avoiding unnecessary copies between libraries can each save more time than a fancy model tweak. Profile the whole pipeline, not just the model, because the bottleneck is often somewhere boring — and boring bottlenecks are the easiest to fix.
Benchmarking: Measuring What Actually Matters
Optimization without measurement is guessing. A proper benchmark measures latency, throughput, memory use, and accuracy on your device, with your model, on data like your real inputs. Published benchmark numbers are a starting point, not a verdict: they use different hardware, different inputs, and different definitions of speed.
Measure latency as a distribution, not a single number. The average hides the slow cases that users actually notice, so record the median and the 95th percentile as well. A model that averages fifty milliseconds but spikes to a full second every tenth frame will feel broken, even though its average looks fine. Spikes often come from thermal throttling, memory pressure, or background processes, and finding them requires running the benchmark for minutes, not seconds.
Measure accuracy after every optimization, on a held-out set you did not use for tuning. It is easy to overfit your optimization choices to a small test set, especially when trying many variants. Keep a separate evaluation set, run it at the end, and trust that number over any intermediate one.
And measure power if your device runs on a battery. A model that is twice as fast but draws three times the power may be a worse choice for a field device than a slower, thriftier one. Power measurement needs hardware — a USB power meter is enough for small boards — but it transforms optimization from guesswork into engineering. You can explore more of this field in the Edge AI & AI Infrastructure category, and the TechBist blog carries new explainers as the tooling evolves.

Putting It Together: A Sensible Order
When you face a model that is too slow or too big, work in this order. First, profile: find whether the bottleneck is the model, the pipeline, or the hardware. Second, apply free wins: runtime optimization, pipeline fixes, and caching. Third, quantize, and measure the accuracy cost. Fourth, prune if the model is still too large, with fine-tuning between rounds. Fifth, consider distillation if you train your own models and need a fundamentally smaller architecture. Sixth, add hardware acceleration only when measurement shows the optimized model still misses its target.
This order matters because it spends the cheapest effort first and the most expensive effort last. Too many projects start with the hardest technique — training a custom small model — when quantization and a runtime change would have solved the problem in an afternoon. Optimization is a ladder, and most projects only need the first few rungs.
The field keeps moving: new runtimes, new chips, and new techniques appear every year, and the best combination for a given device changes with them. But the principles are stable: shrink what you move, skip what you do not need, measure everything, and never trade away accuracy you cannot afford. Master those, and any new tool becomes a variation on something you already understand. Return to the TechBist homepage for more guides in this series whenever you are ready for the next one.
Frequently Asked Questions
What is the easiest way to speed up AI inference?
Start with quantization to 8-bit integers and a runtime optimized for your hardware. Together these often double or triple speed with a small accuracy cost, and both are supported by standard tools without retraining.
Does quantization always reduce accuracy?
Not always, but usually slightly. Well-designed models often lose only a percentage point or two at 8-bit precision. Quantization-aware training and mixed precision can recover most of the loss. Always measure accuracy on your own data after quantizing.
What is the difference between pruning and quantization?
Pruning removes weights or units the model does not need, making the model smaller and sparser. Quantization keeps all the weights but stores them with fewer bits. The two techniques address different costs and are commonly used together.
Is TensorRT only for NVIDIA hardware?
Yes, TensorRT is NVIDIA’s optimizer for NVIDIA GPUs. For other hardware, ONNX Runtime and TensorFlow Lite provide similar optimization with execution providers for CPUs, mobile chips, and accelerators.
Should I optimize for latency or throughput?
It depends on the application. Interactive systems like robots and doorbells need low latency — a fast answer to each input. Batch and server systems need high throughput — many inputs per second. Some techniques help both, but batching, for example, helps throughput at the cost of latency.
1 thought on “AI Inference Optimization Techniques”