PyTorch makes experimentation fast, but production inference demands more than loading a checkpoint behind an HTTP endpoint. You need a reproducible runtime, predictable preprocessing, latency and cost targets, observability, security controls, and a release process that can recover safely when data or traffic changes.
This guide explains how to deploy PyTorch models in production for Indian startups, research teams, and enterprise applications. It covers CPU and GPU serving, batch and real-time workloads, containerisation, optimisation, and the operational decisions that matter after launch.
Start with a production contract
Before exporting the model, define what the service must guarantee. A useful production contract includes:
- Input and output schema: Specify fields, data types, image dimensions, tokenisation rules, units, and error responses.
- Performance targets: Set p50 and p95 latency, throughput, concurrency, and maximum payload size.
- Quality thresholds: Record accuracy, precision, recall, calibration, or task-specific metrics on a fixed evaluation set.
- Availability and cost: Decide the service-level objective and a monthly inference budget in rupees, including GPU, storage, egress, and observability costs.
- Data handling: Classify personally identifiable information, define retention, and document whether requests may be logged.
Treat preprocessing as part of the model. A model trained with one resize policy, normalisation range, tokenizer, or label mapping can fail silently when production code uses another. Package preprocessing, postprocessing, model weights, and configuration together rather than maintaining them as undocumented application logic.
Choose the right PyTorch runtime
Keep the original PyTorch module for development, but choose a deployment representation deliberately:
- TorchScript: Useful when you want a PyTorch-native, self-contained artefact and a stable execution graph. Test scripting or tracing against representative inputs; tracing can miss data-dependent control flow.
- ONNX: A practical choice when you need broader runtime support or hardware-specific acceleration. Validate operators, dynamic shapes, and numerical differences after export.
- TorchInductor and `torch.compile`: Useful for optimising supported workloads while remaining in the PyTorch ecosystem. Benchmark the compiled model under realistic warm and cold conditions.
- Quantisation: FP16 or BF16 can improve GPU throughput, while INT8 can reduce CPU cost and memory. Measure quality loss rather than assuming quantisation is safe.
Always save a versioned manifest containing the model commit, weights checksum, Python and PyTorch versions, CUDA or driver requirements, preprocessing configuration, and evaluation results. For larger models, use a serving layer such as TorchServe, NVIDIA Triton, or a managed inference platform when batching, model repositories, and GPU scheduling would otherwise become application code.
Build a reliable inference service
A small FastAPI service is suitable for many teams, provided it is designed for inference rather than generic web traffic. Load the model once during worker startup, call model.eval(), and disable gradient tracking:
from fastapi import FastAPI
import torch
app = FastAPI()
model = torch.jit.load("model.pt", map_location="cpu").eval()
@app.post("/v1/predict")
def predict(payload: dict):
tensor = preprocess(payload)
with torch.inference_mode():
output = model(tensor)
return postprocess(output)In a real service, add schema validation, authentication, request IDs, structured logs, timeouts, and bounded concurrency. Do not create a model instance per request. For GPU inference, control access to the device and avoid unbounded queues that cause memory exhaustion. A health endpoint should distinguish liveness—the process is running—from readiness—the model is loaded and able to accept traffic.
Use asynchronous request handling for I/O, but remember that model execution is usually CPU- or GPU-bound. Benchmark worker counts: too many processes can duplicate weights and exhaust RAM or VRAM. For variable-length requests, dynamic batching can increase throughput, but enforce a maximum batch size and waiting time so latency remains predictable.
Package and deploy reproducibly
Containerise the service with a pinned base image and locked dependencies. Separate build-time files from runtime secrets, run as a non-root user, and keep the image small. A production container should include:
- A versioned model artefact retrieved from trusted object storage or a registry.
- Health checks and graceful shutdown handling.
- CPU, memory, and GPU resource limits.
- No hard-coded API keys or cloud credentials.
- A software bill of materials and vulnerability scanning in CI.
For a first deployment, a managed container service or Kubernetes can work well. Teams operating GPU fleets may use Kubernetes scheduling, node pools, and a model server such as Triton. If you already use Google Cloud, compare the operational overhead of a managed endpoint with deploying deep learning models on GKE. The right choice is the smallest platform that meets your latency, scaling, and compliance requirements—not the most elaborate architecture.
Optimise latency and cost
Profile before optimising. Measure preprocessing, data transfer, model execution, serialisation, queue time, and cold-start time separately. Common improvements include:
- Move tensors to the target device once and avoid unnecessary CPU-GPU transfers.
- Use pinned memory and non-blocking transfers where they improve measured throughput.
- Enable mixed precision after validating numerical quality.
- Resize or crop inputs at the edge when the application permits it.
- Use dynamic batching for compatible requests.
- Cache deterministic results carefully, with privacy and freshness rules.
- Keep model weights warm for latency-sensitive endpoints; use scale-to-zero only when cold starts are acceptable.
For mobile, low-connectivity, or edge use cases, export and benchmark on the actual device rather than a developer laptop. Quantisation and operator support vary across runtimes; the AI model optimisation guide for mobile devices provides a useful comparison point.
Monitor the model, not just the server
Infrastructure metrics cannot tell you whether predictions remain useful. Monitor four layers:
- Service: request rate, error rate, p50/p95/p99 latency, queue depth, CPU, RAM, VRAM, and GPU utilisation.
- Data: missing fields, schema violations, input ranges, language mix, image quality, and distribution drift.
- Predictions: confidence distributions, class balance, abstention rate, and business outcomes.
- Quality: delayed labels, sampled human review, and segment-level performance by geography, device, language, or customer type.
Use dashboards and alerts with actionable thresholds. Never log raw sensitive payloads by default. Hash or redact identifiers, sample safely, encrypt logs, and define retention. For Indian deployments, review applicable contractual, sectoral, and organisational requirements before sending data to external monitoring or cloud regions.
Release safely and plan for rollback
Treat model releases like software releases. Keep the previous artefact available and make the model version visible in every response or trace. A practical rollout sequence is:
1. Run unit, integration, export, and load tests in CI.
2. Replay a representative, privacy-safe test set.
3. Deploy to a staging environment with production-like hardware.
4. Send shadow traffic without exposing new predictions to users.
5. Use canary or percentage-based rollout with automated rollback thresholds.
6. Compare latency, errors, drift, and quality before expanding traffic.
Rollback should be a configuration change, not a rebuild under pressure. Test it before launch. For teams building agentic applications around models, the same principles apply; see this guide to deploying open-source AI agents in production.
Production checklist
Before launch, confirm that you have:
- A versioned artefact and reproducible container.
- Tested preprocessing and postprocessing.
- Load results for expected and peak traffic.
- Authentication, rate limits, payload limits, and secret management.
- Liveness and readiness checks.
- Metrics, logs, traces, drift checks, and quality review.
- A documented rollback path and an owner on call.
FAQ
Should I use Flask or FastAPI?
Either can serve a model. FastAPI offers typed validation and strong performance, while Flask is simple and mature. The bottleneck is usually inference, batching, or hardware—not the web framework.
Is ONNX always faster than PyTorch?
No. Speed depends on operators, hardware, shapes, precision, and runtime configuration. Benchmark the exported model on production-like inputs.
Do I need a GPU?
Not necessarily. CPU inference is often more economical for small models, low traffic, or strict idle-cost constraints. Use a GPU when measured throughput or latency justifies its total cost.
How often should I retrain?
Do not use a fixed calendar alone. Retrain when quality, data distribution, business conditions, or safety thresholds indicate that the current model is no longer adequate.