在一个干净的docker环境(ubuntu16.04)中编译带 tensorrt 的 onnxruntime:
过程:
# 先 build 出 wheel
root@4ab06d738053:/tools/onnxruntime# ./build.sh \
--parallel 8 \
--use_cuda \
--cuda_version=11.1 \
--cuda_home=/usr/local/cuda \
--cudnn_home=/usr/local/cuda \
--use_tensorrt --tensorrt_home=/usr/local/lib/python3.8/dist-packages/tensorrt \
--build_shared_lib --enable_pybind \
--build_wheel --update --build \
--config Release
# 安装 wheel
root@4ab06d738053:/tools/onnxruntime# pip install build/Linux/Release/dist/onnxruntime_gpu_tensorrt-1.7.0-cp38-cp38-linux_x86_64.whl
# 查看结果,如果 import onnxruntime 有错误,要卸载 onnxruntime-gpu
root@4ab06d738053:/tools/onnxruntime# pip list | grep onnxruntime
onnxruntime-gpu 1.7.0
onnxruntime-gpu-tensorrt 1.7.0
出现的问题:
1、缺少 git 。
root@a42b2c92c7f3:/# git clone --recursive https://github.com/microsoft/onnxruntime.git
bash: git: command not found
root@a42b2c92c7f3:/# apt-get install git
2、git clone中的错误,参考 跳坑 gnutls_handshake() failed: The TLS connection was non-properly terminated. 。
fatal: unable to access 'https://github.com/NVlabs/cub.git/': gnutls_handshake() failed: The TLS connection was non-properly terminated.
root@a42b2c92c7f3:/# cd onnxruntime
root@a42b2c92c7f3:/onnxruntime# git config --global --unset https.https://github.com.proxy
root@a42b2c92c7f3:/onnxruntime# git config --global --unset http.https://github.com.proxy
root@a42b2c92c7f3:/onnxruntime# git submodule update --init --recursive
3、缺少 cmake,参考 cmake通过sh文件升级高级版本 。
2021-04-26 06:46:35,564 tools_python_utils [INFO] - flatbuffers module is not installed. parse_config will not be available
2021-04-26 06:46:35,586 build [ERROR] - Failed to resolve executable path for 'cmake'.
root@353f538bc319:/tools# sh cmake-3.19.8-Linux-x86_64.sh --prefix=/usr/local --exclude-subdir
4、安装 python-dev,参考 [Ubuntu]Could NOT find PythonLibs 。
-- Found PythonInterp: /usr/bin/python3 (found version "3.8.9")
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.9", minimum required is "3.5")
CMake Error at /usr/local/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:218 (message):
Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS)
root@353f538bc319:/tools/onnxruntime# apt-get install python3-dev
如果还是有错,参考 Cmake is not able to find Python-libraries - Stack Overflow
root@353f538bc319:/tools/onnxruntime# apt-get install python3.8 python3.8-dev python3.8-distutils python3.8-venv
5、如果有 tensorrt 的错误,不仅要装 2.3. pip Wheel File Installation, 还要装 2.2. Debian Installation 。
CMake Error in CMakeLists.txt:
Found relative path while evaluating include directories of
"custom_op_library":
"TENSORRT_INCLUDE_DIR-NOTFOUND"
root@4ab06d738053:/tools# dpkg -i nv-tensorrt-repo-ubuntu1604-cuda11.1-trt7.2.3.4-ga-20210226_1-1_amd64.deb
root@4ab06d738053:/tools# apt-key add /var/nv-tensorrt-repo-ubuntu1604-cuda11.1-trt7.2.3.4-ga-20210226/7fa2af80.pub
root@4ab06d738053:/tools# apt-get update
root@4ab06d738053:/tools# apt-get install tensorrt
6、import onnxruntime 的错误 。
>>> import onnxruntime
Inconsistency detected by ld.so: dl-version.c: 205: _dl_check_map_versions: Assertion `needed != NULL' failed!
root@4ab06d738053:/perf_eval# pip uninstall onnxruntime-gpu
7、出现 “ModuleNotFoundError: No module named 'onnxruntime.capi'”。
换个目录 import onnxruntime。
8、出现 “AttributeError: module 'onnxruntime' has no attribute 'InferenceSession'”。
把编译好的 tensorrt 版本的 onnxruntime 卸载再安装。
9、出现 “fatal error: cuda_runtime_api.h: No such file or directory”。
root@4ab06d738053:/perf_eval# export CPLUS_INCLUDE_PATH=/usr/local/cuda/include/