TensorRT源码编译

发布时间 2023-07-05 18:20:07作者: 流了个火

1.参考资料

tensorrt编译 https://zhuanlan.zhihu.com/p/346307138
tensorrt相关指南 https://zhuanlan.zhihu.com/p/382728131
使用 TensorRT 加速深度学习推理 https://developer.nvidia.com/zh-cn/blog/speeding-up-deep-learning-inference-using-tensorrt/

2.源码编译

2.1.下载TensorRT

直接从官网获取 https://developer.nvidia.com/tensorrt

这里给出下载的目录,可自行按需选择
tensorrt download
https://developer.nvidia.com/nvidia-tensorrt-download
https://developer.nvidia.com/nvidia-tensorrt-8x-download

这里我们使用8.2.2.1
https://developer.nvidia.com/compute/machine-learning/tensorrt/secure/8.2.2.1/tars/tensorrt-8.2.2.1.linux.x86_64-gnu.cuda-10.2.cudnn8.2.tar.gz
解压

tar -zxvf tensorrt-8.2.2.1.http://linux.x86_64-gnu.cuda-10.2.cudnn8.2.tar.gz -C ./
mv TensorRT-8.2.1.8/ /TensorRT

2.2.TensorRT OSS编译

github: https://github.com/NVIDIA/TensorRT/tree/release/8.2
OSS 是 Open Source Software的缩写,该存储库包含 NVIDIA TensorRT 的开源软件 (OSS) 组件。 其中包括 TensorRT plugins和parsers(Caffe 和 ONNX)的源代码,以及演示 TensorRT 平台的用法和功能的示例应用程序。 这些开源软件组件是 TensorRT 通用版 (GA) 版本的子集,包括了一些扩展和bug修复

2.2.1.clone指定分支

git clone -b release/8.2 https://github.com/nvidia/TensorRT TensorRT
cd TensorRT
git submodule update --init --recursive

2.2.2.环境依赖

生成编译容器 https://github.com/NVIDIA/TensorRT/tree/release/8.2#setting-up-the-build-environment
准备与2.1下载TensorRT GA相同的资源依赖,包括
cuda-10.2 + cuDNN-8.2 (安装方式见附1、附2)
其余版本详见 https://github.com/NVIDIA/TensorRT/tree/release/8.2#prerequisites
这里注意TensorRT在2.1已经下载完成

export TRT_LIBPATH=/TensorRT/

2.2.3.编译

mkdir build && cd build
cmake .. -DTRT_LIB_DIR=/TensorRT/lib/ -DTRT_INC_DIR=/TensorRT/include -DCUDA_VERSION=10.2
# cmake CMAKE_CUDA_COMPILER="/usr/local/cuda/bin/nvcc"  .. -DTRT_LIB_DIR=/TensorRT/lib/ -DTRT_INC_DIR=/TensorRT/include -DCUDA_VERSION=10.2
make nvinfer_plugin -j$(nproc)
# make -j$(nproc)

1.安装指定版本的cuda toolkit

https://developer.nvidia.com/cuda-toolkit
https://developer.nvidia.com/cuda-10.2-download-archive?target_os=Linux&target_arch=x86_64&target_distro=CentOS&target_version=8&target_type=rpmlocal
使用rpc安装

wget https://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda-repo-rhel8-10-2-local-10.2.89-440.33.01-1.0-1.x86_64.rpm
sudo rpm -i cuda-repo-rhel8-10-2-local-10.2.89-440.33.01-1.0-1.x86_64.rpm
sudo dnf clean all
sudo dnf -y module install nvidia-driver:latest-dkms
#sudo dnf -y install cuda
sudo dnf -y install cuda-10.2.89-1 --nogpgcheck #加上 --nogpgcheck,即可绕过GPG验证成功安装

使用runfile安装

wget https://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_440.33.01_linux.run
sudo sh cuda_10.2.89_440.33.01_linux.run

注意:补丁
使用TensorRT version >= 7.2时,cuda10.2中可能有cublas报错,安装补丁可解决,下载:
https://developer.nvidia.com/cuda-10.2-download-archive?target_os=Linux&target_arch=x86_64&target_distro=CentOS&target_version=7&target_type=rpmlocal

2.安装指定版本的cudnn

cudnn入口:https://developer.nvidia.com/cudnn
所有版本:https://developer.nvidia.com/rdp/cudnn-archive
8.2版本:https://developer.nvidia.com/compute/machine-learning/cudnn/secure/8.2.4/10.2_20210831/cudnn-10.2-linux-x64-v8.2.4.15.tgz