TensorRT Backend Guide
The TensorRT backend provides highly optimized inference using NVIDIA’s TensorRT engine. It offers the best performance for production deployments on NVIDIA GPUs and seamlessly integrates TensorRT Model Optimizer for advanced quantization workflows.
Overview
The TensorRT backend:
- High Performance: Maximum inference speed on NVIDIA GPUs
- Dynamic Shapes: Supports optimization profiles for variable input sizes
- Quantization: INT8, FP8, INT4, FP16/BF16 autocast, and mixed precision support
- CUDA Graphs: Optional CUDA graph capture for reduced CPU overhead
- Model Optimizer Integration: Advanced quantization via TensorRT Model Optimizer
- Flexible Export: Supports both Dynamo and script-based ONNX export
Quick Start
Basic Usage
With FP16 Precision
With CUDA Graphs
Configuration Options
TensorRTBackendConfig
use_dynamo
Use torch.dynamo for ONNX export (recommended).
When to use:
True(default): Better compatibility with modern PyTorch modelsFalse: Legacy models or when Dynamo export fails
workspace_size
Maximum memory workspace for TensorRT engine building.
Guidelines:
- Default: TensorRT chooses automatically
- Larger workspace → More optimization opportunities → Longer build time
- Recommended: 1-4GB for most models
opset_version
ONNX opset version for export.
Guidelines:
- Default: Latest stable opset
- Specify only if you need a particular opset for compatibility
optimization_level
TensorRT builder optimization level (0-5).
Levels:
0: No optimization3: Default (balanced)5: Maximum optimization (longer build time)
compatibility_level
Hardware compatibility level for the engine.
Options:
None: Optimized for current GPU- Specific level: Portable across compatible GPUs
timing_cache
Path to timing cache for faster subsequent builds.
Benefits:
- Faster engine rebuilds
- Reuse timing information across builds
- Especially useful during development
profiles
Optimization profiles for dynamic shapes.
See Optimization Profiles section for details.
device
Device for TensorRT engine.
quantization_config
TensorRT backend supports multiple quantization methods through TensorRT Model Optimizer integration. Use ONNXAutoCastConfig for FP16/BF16 mixed precision, ONNXQuantizationConfig for ONNX INT8/FP8/INT4 quantization, and TorchQuantizationConfig for ModelOpt PyTorch quantization presets.
For current ModelOpt preset names and version-specific support, use the Model Optimizer documentation for the installed version instead of copying preset lists into AITune docs.
enable_tf32
Enable TF32 tensor cores on Ampere+ GPUs.
Benefits:
- Faster FP32 operations on Ampere and newer GPUs
- No accuracy loss for most models
- Recommended to keep enabled
use_cuda_graphs
Enable CUDA graph capture for inference.
Benefits:
- Reduced CPU overhead
- Better performance for small models
- Automatic re-capture on shape changes
Limitations:
- First inference is slower (graph capture)
- Shape changes trigger re-capture
- Not beneficial for very large models
Optimization Profiles
Optimization profiles define the range of input shapes TensorRT will optimize for. They are essential for models with dynamic input sizes.
Profile Modes
SINGLE (Default)
Automatically generates a single profile from recorded samples:
- Min shape: Minimum observed across all samples
- Opt shape: Maximum observed across all samples
- Max shape: Maximum observed across all samples
SAMPLES_USED
Generates one profile per unique input shape:
Important: Increase max_num_samples_stored:
Use case: When you have distinct input shape categories that need separate optimization.
Custom Profiles
Define exact optimization profiles:
These profiles assume the module defines forward(input), so input is the path of its top-level tensor parameter:
Finding Input Paths
Input tensor paths are shown in tuning logs. Top-level paths are forward parameter names; nested paths include all dictionary keys, sequence indices, or attributes:
Use the string shown in the Path column as the profile key. For example, the top-level path input uses "input",
while a nested dictionary path uses inputs["tokens"].
Best Practices for Profiles
- Min < Opt < Max: Ensure min ≤ opt ≤ max for all dimensions
- Opt = Typical: Set opt to your most common input size
- Range Coverage: Ensure your runtime inputs fall within [min, max]
- Multiple Profiles: Use for distinct size categories, not slight variations
- Test Runtime Shapes: Verify your production shapes are covered
Troubleshooting
Issue: ONNX export fails
Solution: Try disabling Dynamo export:
Issue: Engine build fails due to memory
Solution: Reduce workspace size:
Issue: Runtime shape not supported
Error: Input shape X exceeds max profile shape Y
Solution: Update profiles to cover your runtime shapes:
Issue: Slow first inference
Cause: This is expected when using CUDA graphs (graph capture overhead).
Solution: Warmup with a few inference calls before measuring performance.
Issue: INT8 accuracy drop
Solution: Try different quantization algorithms:
Best Practices
- Use FP16: Use
ONNXAutoCastConfig(precision="fp16")for FP16 mixed precision without a full quantization pass - Enable TF32: Keep
enable_tf32=Trueon Ampere+ GPUs - Profile Carefully: Ensure optimization profiles cover all runtime shapes
- Timing Cache: Use timing cache during development for faster iteration
- CUDA Graphs: Enable for latency-sensitive small models
- Workspace Size: Start with 1-2 GB and, increase if the build fails
- Quantization: Validate accuracy with a representative test set
Next Steps
- Learn about Torch-TensorRT JIT Backend
- Learn about Torch-TensorRT AOT Backend
- Explore Tune Strategies
- Review Deployment Guide