# Model Compression and Fast Inference

> Quantization, pruning, distillation and export to ONNX or TensorRT to cut latency, memory and serving cost. Free notes, key concepts, resources and a mini...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/model-optimization-inference
Last updated: 2026-11-28

Quantization, pruning, distillation and export to ONNX or TensorRT to cut latency, memory and serving cost.

Advanced · about 8 hours · difficulty 4/5

Learn first: [Serving Models with FastAPI and BentoML](https://www.internhack.in/roadmaps/tracks/machine-learning/model-serving), [Transfer Learning and Fine-Tuning](https://www.internhack.in/roadmaps/tracks/machine-learning/transfer-learning-vision)

Serving cost often exceeds training cost over a model's lifetime, and many devices cannot run a full-size model at all. Making models smaller and faster is a core production skill.

### Techniques

- **Quantization**: store weights (and sometimes activations) in int8 or 4-bit instead of float32. **Post-training quantization** is quick; **quantization-aware training** recovers more accuracy. For LLMs, formats such as GPTQ, AWQ and GGUF are common.
- **Pruning**: remove weights or whole channels with little impact. Structured pruning gives real speedups; unstructured sparsity needs hardware support.
- **Knowledge distillation**: train a small student model to mimic a large teacher's soft predictions. DistilBERT is a well-known example.
- **Architecture choice**: an efficient model (MobileNet, a smaller transformer) often beats compressing a huge one.

### Export and runtimes

- **ONNX** is a portable model format; **ONNX Runtime** runs it fast on CPUs and GPUs.
- **TensorRT** compiles models for NVIDIA GPUs; **OpenVINO** targets Intel hardware; **Core ML** and **LiteRT** target mobile.
- `torch.compile` and `torch.export` optimize within PyTorch.

### Measure everything

Benchmark latency (p50 and p99), throughput, memory and accuracy before and after each change, on the hardware you will actually deploy to. A 4x smaller model that loses 5 points of accuracy may or may not be worth it; that is a product decision, so present the trade-off clearly.

**Good enough to move on** when you can quantize and export a model to ONNX, benchmark it against the original, and explain the accuracy and speed trade-off.

## Key concepts

- **Post-training quantization**
- **Quantization-aware training**
- **Pruning**
- **Knowledge distillation**
- **ONNX and ONNX Runtime**
- **TensorRT and OpenVINO**
- **Latency benchmarking**

## Mini project

Export a fine-tuned image or text classifier to ONNX, apply int8 dynamic quantization, and build a table comparing file size, CPU latency, throughput and accuracy for PyTorch, ONNX and quantized ONNX versions.

## Check yourself

What does knowledge distillation transfer from the teacher that plain training on hard labels does not, and why might structured pruning speed up inference more than unstructured pruning?

## Free resources

- [ONNX Runtime documentation](https://onnxruntime.ai/docs/) (Microsoft)
- [PyTorch tutorials](https://pytorch.org/tutorials/) (PyTorch)
- [Hugging Face Optimum documentation](https://huggingface.co/docs/optimum/index) (Hugging Face)

Previous: [Distributed Training](https://www.internhack.in/roadmaps/tracks/machine-learning/distributed-training) · Next: [Cloud ML Platforms](https://www.internhack.in/roadmaps/tracks/machine-learning/cloud-ml-platforms)

## More in Scaling Up: Distributed Training and Efficient Inference

- [Distributed Training](https://www.internhack.in/roadmaps/tracks/machine-learning/distributed-training)
- [Model Compression and Fast Inference](https://www.internhack.in/roadmaps/tracks/machine-learning/model-optimization-inference)
- [Cloud ML Platforms](https://www.internhack.in/roadmaps/tracks/machine-learning/cloud-ml-platforms)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
