Sub topic 12
How TensorRT Optimizes a Network into an Engine
TensorRT is NVIDIA’s inference optimizer and runtime. It takes a model graph, such as an ONNX graph, and builds an optimized engine. This chapter assumes basic Python and NumPy, familiarity with PyTorch or another deep learning framework, and access to an NVIDIA GPU with CUDA drivers.
From model graph to engine
An ONNX file describes operations such as convolutions, matrix multiplications, and activations. TensorRT analyzes this graph before inference begins. Its builder chooses efficient implementations, called tactics, for supported operations.
A tactic might use a particular CUDA kernel, tensor layout, or algorithm. TensorRT measures or estimates the available tactics and selects a combination that fits the target GPU and the requested precision. It can also:
- Fuse compatible operations, such as convolution, batch normalization, and activation.
- Remove unnecessary operations.
- Rearrange tensors to reduce memory movement.
- Select FP16 or INT8 implementations when enabled.
- Compile the selected graph into an executable engine.
The resulting engine is a serialized binary file. The runtime loads that file and creates an execution context for inference.
Build time versus run time
Building an engine can take seconds or minutes because TensorRT may test multiple tactics. This cost normally happens during image creation, deployment preparation, or an offline optimization step. Production requests should load a previously built engine instead of rebuilding it.
The engine is usually tied to the environment where it was built. GPU architecture, TensorRT version, CUDA version, and enabled plugins can affect portability. Treat the engine as a deployment artifact that must be tested with its target stack.
| Stage | Main responsibility | Typical frequency |
|---|---|---|
| Builder | Optimize the graph and select tactics | Once per deployment configuration |
| Runtime | Load the engine and execute inference | Once per process, then many requests |
Shapes and optimization profiles
TensorRT needs shape information to select tactics. For a dynamic input, an optimization profile defines the minimum, preferred, and maximum shapes. TensorRT tunes most aggressively for the preferred shape, while supporting shapes within the allowed range.
For example, this command supports batch sizes from 1 to 32 and tunes for batch size 8:
trtexec --onnx=resnet50.onnx --saveEngine=resnet50.plan --fp16 \
--minShapes=input:1x3x224x224 \
--optShapes=input:8x3x224x224 \
--maxShapes=input:32x3x224x224A request outside the profile fails unless another compatible profile exists. Very wide ranges can also reduce optimization quality or increase engine size.
Common failure modes
Unsupported operators may prevent a complete build or require a TensorRT plugin. Incorrect input names or profile dimensions cause shape errors. An engine can also build successfully but produce different results after reduced-precision optimization. Always compare its outputs with a trusted reference model.
Exercise
Why does TensorRT need an optimization profile for a dynamic batch dimension?
Show answer
The profile gives TensorRT minimum, preferred, and maximum shapes so it can select tactics and validate runtime requests.