前言
Torch2TRT是英偉達提供的開源Pytorch到TensorRT模型的轉化工具。相對于其他Pytorch模型轉TensorRT的方式,我認為這是最簡單和容易上手的方式。但是該工具并不成熟,在安裝和使用過程中有一些坑。
遇到的問題
1. fatal error: xxxxxx.h: No such file or directory
例如:
fatal error: cuda_runtime_api.h: No such file or directory
fatal error: NvInfer.h No such file or directory
上面是找不到CUDA的include文件
下面是找不到TensorRT的include文件
首先需要確定確定是否已經安裝CUDA和TensorRT,如果沒有安裝,則需要安裝CUDA和TensorRT。這點相關教程多如牛毛,不再贅述。如果已經安裝CUDA和TensorRT,可以在環境變量中指定include目錄,如CUDA一般為/usr/local/cuda/include
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/local/cuda/include
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/cuda/include
export C_INCLUDE_PATH=$C_INCLUDE_PATH:<TesnsorRT include 目錄>
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:<TesnsorRT include 目錄>
另一種方法是修改項目的setup.py文件來指定include目錄。如原文件在第9行函數def trt_inc_dir()中指定了TensorRT的include地址,我們需要修改內容指定自己電腦中TensorRT地址,例如:
def trt_inc_dir():return "/home/xxx/TensorRT-8.6.1.6/include"
如果缺少CUDA的include目錄,可在原文件第29行添加,例如:
plugins_ext_module = CUDAExtension(name='plugins',sources=['torch2trt/plugins/plugins.cpp'],include_dirs=[trt_inc_dir(),'/usr/local/cuda/include'#在這里添加],library_dirs=[trt_lib_dir(),'/usr/local/cuda/lib64'],libraries=['nvinfer'],extra_compile_args={'cxx': compile_args_cxx,'nvcc': []})
2. /usr/bin/ld: cannot find -lxxxx: No such file or directory
例如:
/usr/bin/ld: cannot find -lcudart: No such file or directory
/usr/bin/ld: cannot find -lnvinfer: No such file or directory
上面是找不到CUDA的庫文件
下面是找不到TensorRT的庫文件
首先同樣需要確定確定是否已經安裝CUDA和TensorRT。如果已經安裝,則需要修改項目的setup.py文件來指定lib目錄。如果缺少TensorRT的庫文件,則需要修改第12行的trt_lib_dir()函數指向自己的TensorRT的庫目錄,例如:
def trt_lib_dir():return "/home/xxx/TensorRT-8.6.1.6/lib"
如果缺少CUDA等其他庫文件,可以在32行附近指定庫文件目錄,例如:
plugins_ext_module = CUDAExtension(name='plugins',sources=['torch2trt/plugins/plugins.cpp'],include_dirs=[trt_inc_dir(),'/usr/local/cuda/include'],library_dirs=[trt_lib_dir(),'/usr/local/cuda/lib64'#在這里添加],libraries=['nvinfer'],extra_compile_args={'cxx': compile_args_cxx,'nvcc': []})
3. module ‘collections’ has no attribute ‘Sequence’
與python3.10 不兼容。在Python3.10中,Sequence在collections.abc下,而不在collections下。
這一般是torch2trt/converters/interpolate.py文件報錯,根據報錯信息定位torch2trt/converters/interpolate.py文件地址,可修改該文件第5行:
#import collections
import collections.abc as collections
4. incompatible function arguments. The following argument types are supported
例如:
TypeError: (): incompatible function arguments. The following argument types are supported:1. (arg0: tensorrt.tensorrt.IConvolutionLayer, arg1: tensorrt.tensorrt.DimsHW) -> NoneInvoked with: <tensorrt.tensorrt.IConvolutionLayer object at 0x7f7aeaac30d8>, ([1, 1], [1, 1])
一般為torch2trt/converters/Conv2d.py文件中的bug,根據報錯信息定位torch2trt/converters/Conv2d.py文件地址,可修改該文件第40行上下:
layer = ctx.network.add_convolution_nd(input=input_trt,num_output_maps=out_channels,kernel_shape=kernel_size,kernel=kernel,bias=bias)# 在這添加下面的8行代碼print("Before stride"+str(stride))if isinstance(stride[0], int)==False:stride=(stride[0][0],stride[1][1])print("After stride"+str(stride))print('Before padding= '+str(padding))if isinstance(padding[0], int)==False:padding=(padding[0][0],padding[1][1])print('After padding= '+str(padding)layer.stride_nd = stridelayer.padding_nd = paddinglayer.dilation_nd = dilation