最近在做模型推理优化时,踩了不少坑,特来分享一下TensorRT和ONNX Runtime的实战对比。
背景:我们团队正在将一个YOLOv5模型部署到边缘设备上,需要在推理速度和精度之间找平衡。最初的ONNX模型在CPU上推理耗时约200ms,明显不满足实时性要求。
测试环境:RTX 3080显卡,CUDA 11.6,TensorRT 8.4,PyTorch 1.12
方法:
- ONNX Runtime:直接使用onnxruntime包,加载模型后设置session_options
import onnxruntime as ort
options = ort.SessionOptions()
options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
session = ort.InferenceSession('model.onnx', options)
- TensorRT:使用torch2trt转换,注意要设置正确的input shape
import torch2trt
model_trt = torch2trt(model, [dummy_input], max_workspace_size=1<<30)
结果对比:
- ONNX Runtime:加速约3倍,耗时65ms
- TensorRT:加速约5倍,耗时40ms
踩坑总结:
- 注意模型输入shape必须固定,否则转换失败
- TensorRT需要确保所有层都支持,不支持的会回退到CPU
- 推荐先用ONNX Runtime验证精度,再考虑TensorRT优化
建议新手从ONNX Runtime开始,逐步过渡到TensorRT。

讨论