TensorRT入门(Windows上安装与VisualStudio项目配置)

  • 下载地址:NVIDIA TensorRT 8.x Download,选择对应的版本下载下来
  • 解压出来得到 TensorRT-8.x.x.x,并放置到一个目录下作为安装目录 <install_path></install_path>
  • <install_path>/lib</install_path>写入 PATH变量,如 D:\deploy_tools\TensorRT-8.4.0.6\lib
  • 打开 <install_path>/python</install_path>目录,里面有很多 whl文件,选择python对应版本的 whl使用 pip进行安装
cd /d D:\deploy_tools\TensorRT-8.4.0.6\python
pip install tensorrt-8.4.0.6-cp37-none-win_amd64.whl

这里安装了python3.7版本的,如果是使用其他的python版本应当替换文件名。

完成以上步骤安装就可以了。这个时候可以使用 <install_path>/sample/sampleMNIST</install_path>这个项目试验一下,打开文件夹下面的 .sln文件,使用Visual Studio生成一下文件即可。生成得到的可执行文件放在了 <install_path>/bin</install_path>文件夹下。可以尝试执行一下,得到类似如下的信息就表示安装成功了。

&&&& RUNNING TensorRT.sample_mnist [TensorRT v8400] # sample_mnist.exe
[03/02/2022-16:35:11] [I] Building and running a GPU inference engine for MNIST
[03/02/2022-16:35:11] [I] [TRT] [MemUsageChange] Init CUDA: CPU +246, GPU +0, now: CPU 3815, GPU 2013 (MiB)
[03/02/2022-16:35:12] [I] [TRT] [MemUsageSnapshot] Begin constructing builder kernel library: CPU 3877 MiB, GPU 2013 MiB
[03/02/2022-16:35:12] [I] [TRT] [MemUsageSnapshot] End constructing builder kernel library: CPU 3890 MiB, GPU 2013 MiB
[03/02/2022-16:35:12] [W] [TRT] TensorRT was linked against cuBLAS/cuBLAS LT 10.2.2 but loaded cuBLAS/cuBLAS LT 10.0.0
[03/02/2022-16:35:12] [I] [TRT] [MemUsageChange] Init cuBLAS/cuBLASLt: CPU +113, GPU +34, now: CPU 3957, GPU 2047 (MiB)
[03/02/2022-16:35:12] [I] [TRT] [MemUsageChange] Init cuDNN: CPU +206, GPU +68, now: CPU 4163, GPU 2115 (MiB)
[03/02/2022-16:35:12] [W] [TRT] TensorRT was linked against cuDNN 8.3.2 but loaded cuDNN 8.0.5
[03/02/2022-16:35:12] [I] [TRT] Local timing cache in use. Profiling results in this builder pass will not be stored.

[03/02/2022-16:35:13] [I] [TRT] Detected 1 inputs and 1 output network tensors.

[03/02/2022-16:35:13] [I] [TRT] Total Host Persistent Memory: 10784
[03/02/2022-16:35:13] [I] [TRT] Total Device Persistent Memory: 1024
[03/02/2022-16:35:13] [I] [TRT] Total Scratch Memory: 0
[03/02/2022-16:35:13] [I] [TRT] [MemUsageStats] Peak memory usage of TRT CPU/GPU memory allocators: CPU 1 MiB, GPU 886 MiB
[03/02/2022-16:35:13] [I] [TRT] [BlockAssignment] Algorithm ShiftNTopDown took 0.0757ms to assign 3 blocks to 11 nodes requiring 57860 bytes.

[03/02/2022-16:35:13] [I] [TRT] Total Activation Memory: 57860
[03/02/2022-16:35:13] [I] [TRT] [MemUsageChange] TensorRT-managed allocation in building engine: CPU +0, GPU +4, now: CPU 0, GPU 4 (MiB)
[03/02/2022-16:35:13] [I] [TRT] [MemUsageChange] Init CUDA: CPU +0, GPU +0, now: CPU 4238, GPU 2121 (MiB)
[03/02/2022-16:35:13] [I] [TRT] Loaded engine size: 1 MiB
[03/02/2022-16:35:13] [I] [TRT] [MemUsageChange] TensorRT-managed allocation in engine deserialization: CPU +0, GPU +1, now: CPU 0, GPU 1 (MiB)
[03/02/2022-16:35:13] [I] [TRT] [MemUsageChange] TensorRT-managed allocation in IExecutionContext creation: CPU +0, GPU +0, now: CPU 0, GPU 1 (MiB)
[03/02/2022-16:35:13] [I] Input:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@=   ++++#++=*@@@@@
@@@@@@@@#.            *@@@@@
@@@@@@@@=             *@@@@@
@@@@@@@@.   .. ...****%@@@@@
@@@@@@@@: .%@@#@@@@@@@@@@@@@
@@@@@@@%  -@@@@@@@@@@@@@@@@@
@@@@@@@%  -@@*@@@*@@@@@@@@@@
@@@@@@@#  :#- ::. ::=@@@@@@@
@@@@@@@-             -@@@@@@
@@@@@@%.              *@@@@@
@@@@@@#     :==*+==   *@@@@@
@@@@@@%---%%@@@@@@@.  *@@@@@
@@@@@@@@@@@@@@@@@@@+  *@@@@@
@@@@@@@@@@@@@@@@@@@=  *@@@@@
@@@@@@@@@@@@@@@@@@*   *@@@@@
@@@@@%+%@@@@@@@@%.   .%@@@@@
@@@@@*  .******=    -@@@@@@@
@@@@@*             .#@@@@@@@
@@@@@*            =%@@@@@@@@
@@@@@@%#+++=     =@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@

[03/02/2022-16:35:13] [I] Output:
0:
1:
2:
3:
4:
5: **********
6:
7:
8:
9:

&&&& PASSED TensorRT.sample_mnist [TensorRT v8400] # sample_mnist.exe

在这里遇到一个问题,我这边电脑是安装了cuda10的,并且cuda的路径是在 PATH中的,但是当我第一次执行 sample_mnist.exe文件的时候发现报错 Unable to load library:cublas64_10.dll。也就是说没有找到 cublas64_10.dll,于是我进入了 <path_cuda>/v10.0/bin</path_cuda>目录下,发现有一个 cublas64_100.dll文件,应该就是需要的 cublas64_10.dll文件,于是复制了一个新的 cublas64_100.dll,并且重命名为了 cublas64_10.dll。就不报这个错误了。

&&&& RUNNING TensorRT.sample_mnist [TensorRT v8400] # sample_mnist.exe
[03/02/2022-16:40:37] [I] Building and running a GPU inference engine for MNIST
[03/02/2022-16:40:37] [I] [TRT] [MemUsageChange] Init CUDA: CPU +254, GPU +0, now: CPU 5106, GPU 2013 (MiB)
[03/02/2022-16:40:37] [I] [TRT] [MemUsageSnapshot] Begin constructing builder kernel library: CPU 5171 MiB, GPU 2013 MiB
[03/02/2022-16:40:37] [I] [TRT] [MemUsageSnapshot] End constructing builder kernel library: CPU 5175 MiB, GPU 2013 MiB
[03/02/2022-16:40:37] [E] [TRT] 6: [libLoader.h::nvinfer1::rt::DynamicLibrary::DynamicLibrary::49] Error Code 6: Internal Error (Unable to load library: cublas64_10.dll)
[03/02/2022-16:40:37] [E] [TRT] 2: [builder.cpp::nvinfer1::builder::Builder::buildSerializedNetwork::619] Error Code 2: Internal Error (Assertion engine != nullptr failed. )
&&&& FAILED TensorRT.sample_mnist [TensorRT v8400] # sample_mnist.exe

这里的8.4.0.6版本的TensorRT需要cuda10.2+cudnn8的环境。很遗憾我这边电脑之前装的是cuda10.0+cudnn7的环境,还缺 cudnn64_8.dll,我发现 <path_cuda>/v10.0/bin</path_cuda>里面有一个 cudnn64_7.dll,尝试了一下将 cudnn64_7.dll复制一个重命名为 cudnn64_8.dll也是可以的,在运行的时候会有信息提醒,但是可以运行下去。

sample_mnist是使用caffe框架模型进行的,配置好以上信息应该是没有问题了,但是如果使用其他框架的模型,如tensorflow,则可能出现无法加载 cublasLt64_10.dll的情况。不太清楚是不是由于cuda版本的原因,在网上下载了一个这个文件,也是放到 <path_cuda>/v10.0/bin</path_cuda>就可以了。在这个地址下载就好:dll4free

另外一个小问题是发现这个 sampleMNIST项目打了断点也没有调试,还是直接运行完了项目,打开项目 &#x9879;&#x76EE;&#x5C5E;&#x6027;-> &#x94FE;&#x63A5;&#x5668; -> &#x8C03;&#x8BD5; -> &#x751F;&#x6210;&#x8C03;&#x8BD5;&#x4FE1;&#x606F;中的false修改为优化以便于调试(/DEBUG)就好了。这样就可以慢慢研究tensorRT是如何进行的了。

如果需要使用到Tensorflow或者PyTorch框架的话,需要使用到 uff, graphsurgeononnx_graphsurgeon的库文件,需要额外安装一下对应的 .whl文件:

python3 -m pip install <install_path>\graphsurgeon\graphsurgeon-0.4.5-py2.py3-none-any.whl
python3 -m pip install <install_path>\uff\uff-0.6.9-py2.py3-none-any.whl
python3 -m pip install <install_path>\onnx_graphsurgeon\onnx_graphsurgeon-0.3.12-py2.py3-none-any.whl
</install_path></install_path></install_path>

在Visual Studio项目中创建自己的项目可以尝试将 samples文件夹下的项目复制一个后来修改,也可以从零开始创建。无非就是将需要的 includelib添加进去。

  • <install_path>/lib</install_path>$(CUDA_PATH)\lib\x64添加到 VC++ Directories > Executable Directories
  • <install_path>/include</install_path> 添加到 C/C++ > General > AdditionalDirectories
  • Linker > Input > Additional Dependencies中添加以下所有的库文件:
  • nvinfer.lib
  • nvinfer_plugin.lib
  • nvonnxparser.lib
  • nvparsers.lib
  • cudnn.lib
  • cublas.lib
  • cudart.lib
  • (Optional) 完成以上配置后将 sample_mnist.cpp文件内容复制到项目中发现有一些include问题,这个时候如果需要使用到他们的一些函数作为支持的话,还需要在 C/C++ > General > AdditionalDirectories中添加:
  • <install_path>/samples/common</install_path>
  • <install_path>/samples/common/windows</install_path>
  • $(CUDA_PATH)\include
  • (Optional)使用 sample_mnist.cpp时候,由于使用到了 logger.hgetopt.h中的相关函数实现,需要将 logger.cpp以及 getopt.c文件添加到项目的 &#x6E90;&#x6587;&#x4EF6;中。
  • (Optional)编译 sample_mnist.cpp的时候报 localtime不安全的问题,在 C/C++ > 预处理器 > 预处理器定义中添加 _CRT_SECURE_NO_WARNINGS即可
  • (Optional)编译 sample_mnist.cpp的时候报 无法从const char* 转化为LPCWSTR的问题,将 常规 > 字符集修改为 <从父级项目或默认设置继承><!--从父级项目或默认设置继承-->,点击应用后应该显示一个空的格子,既不要是Unicode,也不要是多字节字符。

Original: https://blog.csdn.net/sinat_18131557/article/details/123237148
Author: sinat_18131557
Title: TensorRT入门(Windows上安装与VisualStudio项目配置)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/497464/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球