Sub topic 18
Deployment: version pinning, engine portability, and fallbacks
Assume you know basic Python, ONNX Runtime sessions, CUDA providers, and TensorRT engines. This chapter focuses on shipping an optimized model reliably, rather than building or training the model.
Pin the software and hardware contract
An ONNX file is usually more portable than a TensorRT engine. ONNX Runtime can execute the same graph with different providers. A TensorRT engine, however, is compiled for a particular GPU architecture and depends on compatible TensorRT, CUDA, and driver versions.
Record these items with every release:
- ONNX opset and model hash.
- ONNX Runtime version.
- TensorRT and CUDA versions.
- NVIDIA driver version.
- GPU model and compute capability.
- Input shapes, batch limits, and precision mode.
- Preprocessing and postprocessing rules.
Pin Python packages in a lock file or requirements file. Build the TensorRT engine in an environment that matches production. Rebuild the engine when you change the GPU family or make a relevant software upgrade.
onnxruntime-gpu==1.20.1
numpy==2.1.3The exact versions depend on your CUDA and driver matrix. Test the complete combination instead of assuming that individually compatible packages will work together.
Treat fallback as a monitored safety net
Provider order determines preference. In this example, TensorRT receives the first opportunity to execute the graph. CUDA handles supported parts that TensorRT does not take. CPU is the final fallback.
import onnxruntime as ort
providers = [
(
"TensorrtExecutionProvider",
{
"trt_engine_cache_enable": True,
"trt_engine_cache_path": "./trt_cache",
"trt_fp16_enable": True,
},
),
"CUDAExecutionProvider",
"CPUExecutionProvider",
]
session = ort.InferenceSession(
"resnet50.onnx",
providers=providers,
)
active = session.get_providers()
print("providers:", active)
if "TensorrtExecutionProvider" not in active:
print("warning: TensorRT is unavailable")
if active == ["CPUExecutionProvider"]:
raise RuntimeError("GPU execution is unavailable")A fallback can keep a service alive while making it far slower. Log the active providers at startup and expose them as a health or metrics value. Alert when production uses CPU unexpectedly.
Decide when an engine can be reused
Cache an engine only when its cache key includes the important deployment inputs:
- Model and ONNX graph hash.
- TensorRT and ONNX Runtime versions.
- GPU identity or architecture.
- Precision mode.
- Optimization profile and shape limits.
Keep separate caches for different GPU types. A cache hit reduces startup time, but a stale or incompatible engine should be discarded and rebuilt. Test that the rebuilt engine passes numerical parity and latency checks before accepting traffic.
Exercise
A service starts successfully, but p95 latency becomes ten times slower after deployment. What should you check first?
Show answer
Check the logged active providers and runtime warnings. The service may have fallen back from TensorRT or CUDA to the CPU because of an incompatible engine, provider version, GPU, or driver.