Sub topic 13
Enabling the TensorRT Execution Provider
This chapter assumes basic Python, NumPy, ONNX Runtime, and PyTorch knowledge. You also need an NVIDIA GPU with compatible CUDA, cuDNN, TensorRT, and ONNX Runtime packages. TensorRT support is available through the GPU-enabled ONNX Runtime package, not the CPU-only package.
How provider selection works
An ONNX Runtime execution provider (EP) implements operators for a particular hardware backend. The TensorRT EP tries to compile supported parts of the ONNX graph into a TensorRT engine. CUDA handles supported parts that TensorRT cannot compile. CPU handles any remaining operators.
Provider order expresses preference:
- TensorRT receives the first opportunity to claim supported graph nodes.
- CUDA receives nodes TensorRT does not claim.
- CPU provides a final fallback.
flowchart TD
M[ONNX model] --> T[TensorRT EP]
T -->|Supported subgraph| E[TensorRT engine]
T -->|Unsupported operators| C[CUDA EP]
C -->|Unsupported operators| P[CPU EP]
E --> O[Model output]
C --> O
P --> OCreate a TensorRT-enabled session
The provider options use TensorRT-specific names. FP16 allows TensorRT to use half-precision kernels where supported. Engine caching stores compiled engines on disk, so later process starts do not rebuild the same engine.
import onnxruntime as ort
providers = [
(
"TensorrtExecutionProvider",
{
"trt_fp16_enable": True,
"trt_engine_cache_enable": True,
"trt_engine_cache_path": "./trt_cache",
},
),
"CUDAExecutionProvider",
"CPUExecutionProvider",
]
session = ort.InferenceSession(
"resnet50.onnx",
providers=providers,
)
print("Configured providers:", session.get_providers())The cache directory must be writable. The first session creation can take much longer because TensorRT builds an engine. Later starts can reuse the cached engine when the model, TensorRT version, GPU, and relevant settings remain compatible.
session.get_providers() reports the providers configured for the session. It does not prove that every graph node was assigned to TensorRT. Enable ONNX Runtime profiling when you need to inspect actual operator placement and timing.
Check availability before deployment
A deployment process should fail clearly when TensorRT was requested but is unavailable. You can inspect the providers exposed by the installed ONNX Runtime build:
available = ort.get_available_providers()
if "TensorrtExecutionProvider" not in available:
raise RuntimeError(
f"TensorRT provider is unavailable. Found: {available}"
)Keep CUDA and CPU fallback only when degraded performance is acceptable. In a latency-sensitive service, silently falling back to CPU can turn a configuration problem into a production outage.
| Configuration | Typical purpose |
|---|---|
| TensorRT, CUDA, CPU | Best performance with compatibility fallback |
| CUDA, CPU | GPU inference without TensorRT engine building |
| CPU only | Functional testing or machines without an NVIDIA GPU |
Exercise
Why should CUDA appear after TensorRT in the provider list?
Show answer
TensorRT must receive the first opportunity to compile supported subgraphs. CUDA then handles nodes TensorRT cannot support, and CPU provides the final fallback.