From .pth to HTTP endpoint
Three flavors of serving, in increasing operational sophistication:
- FastAPI + uvicorn — write your own server. Maximum flexibility, minimum infrastructure. Great for prototypes, internal tools, and small-scale production.
- TorchServe — managed inference server (AWS + Meta). Built-in batching, model versioning, metrics, multi-model serving. The right choice for "we have many models in production".
- vLLM / TGI / SGLang — LLM-specific serving. Continuous batching, PagedAttention, optimized kernels. The right choice for serving language models.
The basics any serving setup needs
- Load the model once on startup, not per request.
- Set
model.eval()+ usetorch.inference_mode()for every request. - Batch requests when latency budget allows — much better throughput.
- Avoid CPU-GPU sync inside the request — pre-allocate buffers, use non_blocking transfers.
- Have a health endpoint for orchestrators to know if the service is alive.
- Have a metrics endpoint (latency, throughput, error rate, GPU utilization).