From PyTorch model to deployed inference
You've trained a model. Now you need it to run somewhere — a server, a phone, an embedded device, an edge GPU. Three main paths:
- torch.compile — fastest path on PyTorch-native targets. JIT-compiles your forward pass into a fused kernel. Works on the same device PyTorch supports.
- ONNX — export to a vendor-neutral graph format that runs on ONNX Runtime, TensorRT, OpenVINO, CoreML, etc. Portable.
- TorchScript — older PyTorch-native serialization. Mostly superseded by torch.compile for new code, still common in legacy.
Tip: For modern inference, the realistic tooling is:
torch.compile on PyTorch servers, ONNX Runtime on cross-platform deployments, vLLM or TGI for LLM serving, Core ML for iOS, TensorFlow Lite for Android, llama.cpp for local quantized LLM inference.Quantization
Reduce weight precision (FP32 → INT8 or INT4) for smaller models and faster inference, often with minimal accuracy loss. Three flavors: post-training quantization (cheap, no retraining), quantization-aware training (better accuracy, more complex), weight-only quantization (used in LLM deployment, e.g. AWQ, GPTQ).
Distillation
Train a small student model to mimic a large teacher's predictions. Common in production to reduce serving cost. The student often achieves 90-95% of teacher accuracy at 5-10% of the parameter count.
Principle: Inference is its own engineering discipline. The model that wins your eval is rarely the model that goes to production unmodified — quantization, distillation, and graph optimization usually trim it before deployment.