MachineFi

Model Optimization for Edge Deployment: Quantization, Pruning, and Distillation

How to shrink AI models by 4-10x without losing the accuracy your video pipeline needs

MachineFi Labs11 min read

You've trained a solid object detection model. It hits 94% mAP on your validation set and your video pipeline loves it — running on an A100 in the cloud with comfortable headroom. Then someone asks you to deploy it to an NVIDIA Jetson Orin Nano at the factory edge. Or an RKNN-powered camera. Or a Raspberry Pi 5.

The model is 300MB. Inference takes 180ms per frame. The device has 4GB of RAM and no discrete GPU. Nothing works.

This is the model optimization problem, and it's the central engineering challenge of edge AI deployment. The gap between "works great in the cloud" and "runs in real time on edge hardware" is bridged by three techniques: quantization, pruning, and knowledge distillation. Done well, they shrink models by 4-10x and cut inference latency by the same factor — without meaningfully degrading accuracy.

This guide explains each technique precisely, shows real benchmark numbers, and gives you a practical workflow for putting them together for your own video analytics pipeline.

Why Model Size and Latency Matter More at the Edge

Before getting into techniques, it's worth being precise about why this problem exists. Cloud inference has generous resources — you can throw more GPUs at a slow model. Edge inference operates under hard constraints that don't flex:

Memory ceiling. An NVIDIA Jetson Orin Nano has 8GB of unified RAM shared between the CPU, GPU, and your operating system. A 300MB FP32 model needs closer to 1.2GB at runtime once you account for activations, input buffers, and framework overhead. That leaves very little room for the rest of your application.

Thermal limits. Edge devices throttle under sustained load. A model that benchmarks at 45ms per frame in a lab environment may run at 80ms on a warm factory floor after 20 minutes of continuous operation.

No cold-start budget. Cloud inference can warm up between batches. Edge video pipelines run continuously. Every millisecond of inference latency directly reduces the frame rate you can sustain.

For a 30fps video stream — the minimum useful rate for most surveillance and quality inspection applications — you have a 33ms budget per frame for inference. A 180ms model blows that budget by 5x. Understand these constraints deeply before choosing which optimization path to take; they also determine whether edge or cloud inference is right for your specific use case.

4-10x

typical model size reduction achievable through combined quantization, pruning, and distillation, with under 2% accuracy loss on standard computer vision benchmarks

Source: MLPerf Inference Benchmark Results, v3.1, 2024

Technique 1: Quantization

Quantization (Neural Network)

Quantization is the process of reducing the numerical precision of a model's weights and activations from high-precision floating-point (FP32 or FP16) to lower-precision integer formats (INT8 or INT4). A 32-bit float uses 4 bytes per weight; an 8-bit integer uses 1 byte — reducing model size by 4x and enabling faster integer arithmetic on hardware that supports it.

Quantization is almost always the first optimization you should apply, because it delivers the best accuracy-to-compression ratio of any technique and requires the least effort.

FP32 → FP16 → INT8 → INT4: The Precision Ladder

FP32 (full precision): The default training format. 32 bits per parameter. High dynamic range and numerical stability. No hardware integer acceleration.

FP16 (half precision): Halves model size with essentially zero accuracy loss. Most modern GPUs support FP16 natively — this is the minimum optimization you should always apply.

INT8: Quarter the size of FP32. Requires calibration (a small dataset pass to determine optimal scaling factors for each layer). Typical accuracy loss: 0.5-1.5% mAP for well-calibrated computer vision models. This is the sweet spot for most edge deployments.

INT4: Eighth the size of FP32. Accuracy loss becomes more significant (1-4% mAP) and is more architecture-dependent. Best suited for large language models rather than convolutional vision models.

Post-Training Quantization vs. Quantization-Aware Training

There are two ways to quantize:

Post-Training Quantization (PTQ): Apply quantization after training with no retraining required. You pass ~100-1,000 calibration images through the model to measure activation ranges, then freeze the quantized model. Fast but slightly less accurate.

Quantization-Aware Training (QAT): Insert fake quantization nodes during training so the model learns to compensate for the precision loss. Requires retraining but typically recovers 0.5-1.0% mAP vs. PTQ. Worth the effort for accuracy-critical applications like medical imaging or high-speed quality inspection.

For most real-time video analytics use cases, PTQ to INT8 is sufficient and can be completed in under an hour using TensorRT or ONNX Runtime's built-in calibration tools.

Technique 2: Pruning

Pruning removes weights from a neural network that contribute little to the output. The intuition: after training, most large networks are significantly over-parameterized. Many weights are near-zero and could be removed without changing predictions meaningfully.

Unstructured vs. Structured Pruning

This is the most important distinction in pruning, and it determines whether you actually see latency improvements at inference time.

Unstructured pruning sets individual weights to zero based on magnitude thresholds. It achieves the highest theoretical sparsity (70-90% of weights removed) but produces irregular sparse matrices that don't map well to standard GPU tensor operations. The model is smaller on disk, but inference latency often doesn't improve without sparse tensor hardware acceleration (which most edge devices lack).

Structured pruning removes entire channels, filters, or attention heads — producing a smaller dense model. A ResNet-50 with 30% of its channels pruned becomes a genuinely smaller ResNet that runs faster on any hardware without special sparse computation support. This is what you want for edge deployment.

For deploying AI models on edge devices, structured pruning is almost always the right choice because the resulting model runs on commodity hardware at full throughput.

Quantization Techniques: Precision, Size, and Accuracy Trade-offs
Source: ONNX Runtime and TensorRT documentation; MLPerf v3.1 results

How Much Can You Prune?

For a well-trained convolutional network like YOLOv8 or RT-DETR, you can typically remove 30-50% of channels with under 1% mAP loss after fine-tuning. Beyond 50% sparsity, accuracy degradation accelerates. The practical ceiling for structured pruning without significant retraining is around 40-50% for most production models.

Pruning works best when combined with fine-tuning: prune 10-20% of channels, fine-tune for a few epochs to recover accuracy, then prune again. This iterative approach outperforms one-shot pruning significantly.

Technique 3: Knowledge Distillation

Knowledge distillation trains a small "student" model to mimic the outputs of a large "teacher" model. Instead of training the student on hard labels (class 0, class 1), it trains on the teacher's soft probability distributions, which contain richer information about the model's uncertainty and the relationships between classes.

The result: a student model that is much smaller than the teacher but performs significantly better than the same architecture trained from scratch on hard labels alone.

For real-time object detection in Python, distillation is the technique that makes it realistic to run a performant detector at 30+ fps on CPU-only edge hardware.

When Distillation Makes Sense

Distillation makes sense when:

  • You need an architecture change (not just compression of the existing model)
  • Your target hardware has a hard FLOP ceiling the current architecture exceeds
  • You want to combine the accuracy of a large model with the speed of a small one for a specific domain

Distillation does not make sense when:

  • You need results fast — distillation requires full retraining
  • You don't have labeled data (you need real examples for the student to train on, even if the labels come from the teacher)
  • Your current model is already near-optimal for its architecture

For a more detailed treatment of how to select the right base architecture before optimizing, see our guide on choosing the right AI model for video analytics.

2.3x

median inference speedup from knowledge distillation alone (YOLOv8x teacher → YOLOv8n student) on the NVIDIA Jetson Orin Nano, measured on the COCO val2017 benchmark

Source: MachineFi engineering benchmarks, 2025

Real-World Benchmarks: Before and After

Numbers are more useful than theory. Here are real benchmark results from optimizing a YOLOv8m object detection model for deployment on an NVIDIA Jetson Orin Nano (8GB variant), representative of the hardware used in manufacturing quality inspection and smart city applications.

YOLOv8m Optimization Pipeline: Before and After Benchmarks (Jetson Orin Nano)
Source: MachineFi engineering benchmarks; hardware: Jetson Orin Nano 8GB, JetPack 6.0, TensorRT 10.0

A few things to notice in this table:

The combined pipeline (FP16 + INT8 + structured pruning + TensorRT) takes a model that ran at 5.6fps to 35.7fps — crossing the 30fps real-time threshold — while losing only 1.5 mAP points (3%). For most surveillance and monitoring applications, that trade-off is clearly worthwhile.

The distilled student (YOLOv8n) runs at 125fps but loses 12.9 mAP points (26%). That's too much for precision quality inspection. But for coarse activity detection — "is there a vehicle in this zone?" — it's more than accurate enough at 6x lower latency.

This is why the technique choice matters: optimization isn't a single dial. It's a set of knobs with different accuracy-latency trade-offs, and the right combination depends entirely on your application's tolerance for missed detections versus its latency budget. The core latency vs. throughput trade-off in real-time AI applies directly here.

ONNX and TensorRT: The Export Layer

Quantization and pruning produce an optimized model architecture. ONNX and TensorRT are the export formats that translate that optimized model into efficient execution on specific hardware.

ONNX (Open Neural Network Exchange) is the portable intermediate format. Export your PyTorch or TensorFlow model to ONNX once, and run it on any runtime that supports ONNX — including ONNX Runtime (CPU/GPU), TensorRT, CoreML (Apple Silicon), and RKNN (Rockchip NPU). Always export to ONNX as your first step.

TensorRT is NVIDIA's inference optimization engine. It takes an ONNX model and applies hardware-specific optimizations: kernel fusion, memory layout optimization, and precision calibration for the target GPU. On Jetson hardware, TensorRT consistently delivers 2-4x additional speedup over ONNX Runtime for the same INT8 model. The benchmark numbers above reflect TensorRT-optimized models.

For non-NVIDIA hardware — Rockchip RK3588 NPUs, Apple Silicon, Arm Ethos NPUs — the equivalent is the vendor's own SDK (RKNN Toolkit, CoreML Tools, Arm Compute Library). The principle is the same: compile the ONNX model to a hardware-native format for the final latency improvement.

This export pipeline integrates naturally with Trio's stream processing architecture, which handles model versioning and edge deployment as part of the stream API — relevant context for teams understanding edge computing fundamentals before diving into optimization specifics.

A Practical Optimization Workflow

Here's the order of operations that consistently produces the best results:

Step 1 — Establish a baseline. Measure FP32 inference latency and accuracy on target hardware. Don't guess — measure.

Step 2 — Export to ONNX. Validate the ONNX model produces identical outputs to the original. This catches export bugs before you add quantization on top.

Step 3 — Apply FP16. One line of code. Verify accuracy drops are negligible. This is always worth doing.

Step 4 — INT8 calibration. Collect 100-500 representative images from your production domain (not ImageNet if you're running factory inspection). Run calibration. Measure accuracy. If mAP loss exceeds your budget, switch to QAT for the sensitive layers.

Step 5 — Structured pruning (if needed). If INT8 alone doesn't hit your latency target, apply structured pruning at 20-30% channel sparsity, fine-tune for 5-10 epochs, and re-quantize.

Step 6 — TensorRT or vendor SDK export. Compile the optimized ONNX model for your target hardware. Run final latency benchmarks under realistic load (sustained throughput, not just cold-start).

Step 7 — Consider distillation only if needed. If you still can't hit your latency target after the above, you need an architectural change — not more compression. Distillation is the right tool then.

This workflow applies equally whether you're optimizing a model for GPU or CPU inference at the edge. The same steps apply; only the target SDK and hardware benchmarking environment change.

For teams new to the concepts underlying these models, our neural networks explained primer covers the architectural foundations that make quantization and pruning work the way they do. And if you're starting from a pre-trained model rather than training from scratch, transfer learning for computer vision is the right starting point before applying the optimization techniques above.

Keep Reading

MachineFi Labs

Engineering Team at MachineFi

The team behind Trio — the multimodal stream API that turns live video, audio, and sensor feeds into AI-ready intelligence.