1、在make的时候遇到问题,无法找到cuda的头文件:
error: cuda_runtime_api.h: No such file or directory
#include "cuda_runtime_api.h"
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
解决办法,将cuda_runtime_api.h的路径加入到.bashrc中的CPATH中,如下操作
export CPATH="/usr/local/cuda/targets/x86_64-linux/include:/mnt/TSB2T01/sda15/TensorRT-7.2.1.6/include:/usr/include/c++/7.5.0:$CPATH"
source .bashrc之后,问题解决。
2、最近编译一个torch的pth转TensorRT的trt工具的时候遇到的错误:
/usr/bin/ld: 找不到 -lnvinfer
/usr/bin/ld: 找不到 -lcudart
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/dbnet.dir/build.make:120:dbnet] 错误 1
make[1]: *** [CMakeFiles/Makefile2:95:CMakeFiles/dbnet.dir/all] 错误 2
make: *** [Makefile:103:all] 错误 2
遇到这种错误,第一反应是LD_LIBRARY_PATH没有添加相关的路径,于是,检查了cuda和TensorRT的LD_LIBRARY_PATH,发现都是对的,没什么问题。
后来翻看Makefile,发现作者已经在Makefile中以-Wl,-rpath的方式指定了动态库的链接地址,将动态库的链接地址改到我们自己的cuda的动态库的地址,再把TensorRT的库全复制到cuda存库的地址,问题解决。
3、在使用转换成功的onnx文件转trt的时候遇到报错:
[TensorRT] ERROR: Network must have at least one output
[TensorRT] ERROR: Network validation failed.
查阅相关的博客,发现只要在with open(onnx_file_path, 'rb') as model:之后加两行:
last_layer = network.get_layer(network.num_layers - 1)
network.mark_output(last_layer.get_output(0))
问题解决。
import tensorrt as trt
def ONNX_build_engine(onnx_file_path):
'''
通过加载onnx文件,构建engine
:param onnx_file_path: onnx文件路径
:return: engine
'''
# 打印日志
G_LOGGER = trt.Logger(trt.Logger.WARNING)
explicit_batch = 1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)
with trt.Builder(G_LOGGER) as builder, builder.create_network(explicit_batch) as network, trt.OnnxParser(network, G_LOGGER) as parser:
builder.max_batch_size = 100
builder.max_workspace_size = 1 << 20
print('Loading ONNX file from path {}...'.format(onnx_file_path))
with open(onnx_file_path, 'rb') as model:
print('Beginning ONNX file parsing')
parser.parse(model.read())
last_layer = network.get_layer(network.num_layers - 1)
network.mark_output(last_layer.get_output(0))
print('Completed parsing of ONNX file')
print('Building an engine from file {}; this may take a while...'.format(onnx_file_path))
engine = builder.build_cuda_engine(network)
print("Completed creating Engine")
# 保存trt文件
with open('my.engine', "wb") as f:
f.write(engine.serialize())
return engine
if __name__=='__main__':
ONNX_build_engine('./my.onnx')
4、在加了问题2的两行之后,出现了新的问题:
python3: ../builder/Network.cpp:1205: virtual nvinfer1::ILayer* nvinfer1::Network::getLayer(int) const: Assertion `layerIndex >= 0' failed.
经查阅其他人博客,在这位博主的博客中找到问题所在https://blog.csdn.net/qq_23116521/article/details/107617442
trt7若缺少explicit_batch = 1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH),会产生上面的报错,按照问题2中的代码加上这一行,问题解决。
5、在onnx->trt的过程中遇到以下问题:
[TensorRT] ERROR: Try increasing the workspace size with IBuilderConfig::setMaxWorkspaceSize() if using IBuilder::buildEngineWithConfig, or IBuilder::setMaxWorkspaceSize() if using IBuilder::buildCudaEngine.
出现此问题后,无法正常转成engine,根据提示,在转换脚本中做如下修改:
builder.max_workspace_size = 1 << 30
再次运行转换脚本,问题解决。