Sub topic 3
How TensorRT optimizes a network into an engine
This chapter assumes you can run an ONNX model with ONNX Runtime, that you have an NVIDIA GPU with CUDA drivers, and that TensorRT is installed.
How TensorRT optimizes a network into an engine
TensorRT is NVIDIA's inference optimizer. Its builder reads a network graph, fuses neighboring layers into single GPU kernels, and picks the fastest kernel for each operation on your GPU. It saves the result to a serialized file called an engine. The TensorRT runtime loads that engine and runs it with little overhead.
Enabling the TensorRT execution provider
ONNX Runtime can pass supported subgraphs to TensorRT through its TensorRT execution provider. Operations TensorRT does not support fall back to CUDA or CPU.
import onnxruntime as ort
providers = [
("TensorrtExecutionProvider", {
"trt_fp16_enable": True,
"trt_engine_cache_enable": True,
"trt_engine_cache_path": "./trt_cache",
}),
"CUDAExecutionProvider",
"CPUExecutionProvider",
]
sess = ort.InferenceSession("resnet50.onnx", providers=providers)
print(sess.get_providers())Check the printed list. If TensorrtExecutionProvider is missing, ONNX Runtime could not load TensorRT and used the next provider instead.
Engine build time and engine caching
Building an engine can take minutes. With trt_engine_cache_enable set, the first run saves the engine to trt_engine_cache_path, and later runs load it from there. Changing the model, the shapes, or the precision settings forces a rebuild.
Dynamic batch sizes and optimization profiles
A dynamic batch axis needs an optimization profile. The profile sets the minimum, optimal, and maximum shapes the engine must accept. TensorRT tunes kernels for the optimal shape and accepts any shape in the range.
FP16 and INT8: speed gains and accuracy checks
FP16 halves memory traffic and often speeds up inference with a small accuracy loss. INT8 is faster still, but it needs calibration data to choose scale values. After either change, rerun your parity check and compare top-1 predictions on a validation set.
Building and profiling engines with trtexec
trtexec is the command-line tool that builds and benchmarks engines.
trtexec --onnx=resnet50.onnx --saveEngine=resnet50.plan --fp16 \
--minShapes=input:1x3x224x224 \
--optShapes=input:8x3x224x224 \
--maxShapes=input:32x3x224x224Its output reports latency and throughput. Compare these numbers with your own benchmark.
Deployment: version pinning, engine portability, and fallbacks
An engine is tied to the GPU model and the TensorRT version that built it. Pin the TensorRT, CUDA, and ONNX Runtime versions in your deployment, and rebuild engines on each target machine. Keep the ONNX file as the source of truth. Keep the CUDA and CPU providers in your list so the service still runs if TensorRT fails to load.
Exercise
Exercise
Why should CUDAExecutionProvider and CPUExecutionProvider come after TensorrtExecutionProvider in the providers list?
Show answer
They run any nodes that TensorRT cannot handle, so the model still works when TensorRT rejects part of the graph or is unavailable.