Skip to main content
Model Deployment

Model Deployment

From Research to Production

There is a saying in ML engineering: “Getting to 95% accuracy takes a month; getting that model into production takes a year.” Training is the glamorous part. Deployment is where dreams meet reality — where you discover that your model needs 8 GB of RAM but the server has 4, that inference takes 500ms but the SLA requires 50ms, and that the model works perfectly on your test set but produces nonsense on slightly different real-world inputs. Production requires:
  • Fast inference (users will not wait more than a few hundred milliseconds)
  • Minimal dependencies (your prod server should not need a full PyTorch research installation)
  • Reproducibility (the same input must produce the same output, every time)
  • Monitoring (you need to know when the model starts performing poorly before users complain)
The honest truth: Most ML models never make it to production. The gap between a Jupyter notebook that achieves good metrics and a reliable production service is vast. The techniques in this chapter — export formats, quantization, serving frameworks, monitoring — are what separate ML engineers from ML researchers.

Export Formats

TorchScript (JIT Compilation)

TorchScript converts your Python model into a serialized format that can run without a Python interpreter. This matters for two reasons: (1) Python is slow due to the GIL, and (2) deploying a Python environment to edge devices or C++ servers is painful. TorchScript gives you a portable, optimized model file.
Use tracing for straightforward models (CNNs, fixed-architecture Transformers). Use scripting for models with data-dependent control flow (if/else, loops over variable-length sequences). When in doubt, try tracing first — it is simpler and produces more optimized code.
Pitfall — forgetting model.eval(): If you trace or script a model in training mode, BatchNorm and Dropout will be baked in with their training behavior (using batch statistics, dropping activations). This is a silent bug that produces inconsistent and degraded inference results. Always call model.eval() before export.

ONNX Export

Verify the export:

Model Optimization

Quantization (INT8)

Quantization converts model weights (and optionally activations) from 32-bit floats to 8-bit integers. This is not just about storage — INT8 arithmetic is 2-4x faster than FP32 on most hardware, and the model fits in 4x less memory. The key question is always: how much accuracy do you lose? For most well-trained models, the answer is surprisingly little (0-1%). Reduce precision for faster inference:

Model Size Comparison


Pruning

Most neural networks are overparameterized — a large fraction of weights are near zero and contribute very little to the output. Pruning removes these unimportant weights, creating a sparse model that is smaller and (on hardware that supports sparse computation) faster. The “Lottery Ticket Hypothesis” (Frankle and Carlin, 2018) showed that dense networks contain sparse sub-networks that, when trained from the same initialization, match the full network’s performance. Remove unimportant weights:

Serving with FastAPI

FastAPI is the most popular choice for serving ML models as REST APIs. It is async-native, auto-generates OpenAPI docs, and handles concurrent requests well. The key principle: load the model once at startup (not per request) and keep it in memory.
Pitfall — thread safety with GPU models: If you serve a GPU model with multiple async workers, concurrent requests can cause CUDA errors. Either (1) use a single worker with async I/O, (2) use a request queue with a dedicated inference thread, or (3) use a proper model serving framework like Triton that handles batching and concurrency correctly. For CPU-only models, FastAPI’s default async handling works fine.

GPU Serving with Triton


Docker Deployment


Edge Deployment

ONNX Runtime Mobile

TensorFlow Lite Conversion


Monitoring in Production


Deployment Checklist


Exercises

Export a ResNet model to both TorchScript and ONNX. Compare inference speeds.
Apply INT8 quantization to a model. Measure size reduction and accuracy change.
Build a complete image classification API with proper error handling and documentation.

What’s Next

Module 22: Debugging Deep Learning

Tools and techniques for diagnosing training issues and model failures.