Sub topic 17
Building and Profiling Engines with `trtexec`
Assume you know basic ONNX and TensorRT concepts and have an NVIDIA GPU with a working CUDA and TensorRT installation. The trtexec command-line tool builds TensorRT engines from ONNX models and measures their performance without requiring application code.
Build an Engine
Start with an ONNX model whose input is named input. The following command builds an FP16 engine:
trtexec --onnx=resnet50.onnx --saveEngine=resnet50.plan --fp16 \
--minShapes=input:1x3x224x224 \
--optShapes=input:8x3x224x224 \
--maxShapes=input:32x3x224x224The three shape settings define an optimization profile for a dynamic input:
--minShapesis the smallest supported shape.--optShapesis the shape TensorRT optimizes most carefully.--maxShapesis the largest supported shape.--saveEnginewrites the compiled engine to disk.--fp16permits half-precision calculations.
This profile supports batch sizes from 1 through 32. Batch size 8 is the main tuning target. Every shape used at runtime must fall inside this range.
TensorRT first parses the ONNX graph, selects implementations for its operators, applies graph optimizations, and builds an engine for the target GPU. The resulting .plan file is usually hardware and software specific.
Profile an Existing Engine
Use --loadEngine to measure an engine that has already been built:
trtexec --loadEngine=resnet50.plan \
--shapes=input:8x3x224x224 \
--warmUp=200 \
--duration=อย่างThe --duration value must be a number of seconds. For example:
trtexec --loadEngine=resnet50.plan \
--shapes=input:8x3x224x224 \
--warmUp=200 \
--duration=10The report includes host latency, enqueue time, GPU compute time, and throughput. Use the same batch size, input shape, and precision when comparing engines. A single timing result can be misleading because the first runs may include initialization and memory setup.
| Option | Purpose |
|---|---|
| `--fp16` | Enables FP16 tactics when supported |
| `--shapes` | Selects runtime dimensions for a dynamic input |
| `--warmUp` | Runs warmup iterations before measurement |
| `--duration` | Measures performance for a fixed number of seconds |
| `--dumpProfile` | Prints per-layer timing information |
Read the Profile
If the engine is unexpectedly slow, inspect individual layers:
trtexec --loadEngine=resnet50.plan \
--shapes=input:8x3x224x224 \
--dumpProfileLook for layers with unusually high GPU time or operations that prevent fusion. Compare FP32 and FP16 results, then validate accuracy with representative images before deployment.
Exercise
Why should the optimization profile's optShapes match the workload's most common batch size?
Show answer
TensorRT tunes tactics around the optShapes dimensions, so matching the normal workload can improve its latency and memory behavior.