为什么选择TVM
为提升深度学习模型的推理效率,设备平台制造商针对自己的平台推出优化的推理引擎,例如NAVIDA的tensorRT,Intel的OpenVINO,Tencent针对移动端应用推出NCNN等。目前,深度学习模型应用广泛,在服务端和移动端都有应用,甚至于特殊的嵌入式场景想,它们都有加速模型推理的需求。
TVM介是从深度学习编译器的角度来做推理引擎,目前技术领域还比较新,具体技术细节以后有机会会深入学习,这里主要想体验一下使用TVM做深度模型推理,重点是推理效率的提升,所以尝试安装下TVM测试下。
相关版本
```bash
gcc版本 6.4.0
cmake 3.16.4
llvm 我选择的版本为llvm-9.0.0
```
-
gcc 安装
- 查看系统版本
cat /etc/redhat-release
- 查看默认的gcc版本
gcc --version
- 查看默认动态库
strings /usr/lib64/libstdc++.so.6 | grep GLIBC GLIBCXX_3.4 GLIBCXX_3.4.1 GLIBCXX_3.4.2 GLIBCXX_3.4.3 GLIBCXX_3.4.4 GLIBCXX_3.4.5 GLIBCXX_3.4.6 GLIBCXX_3.4.7 GLIBCXX_3.4.8 GLIBCXX_3.4.9 GLIBCXX_3.4.10 GLIBCXX_3.4.11 GLIBCXX_3.4.12 GLIBCXX_3.4.13 GLIBCXX_3.4.14 GLIBCXX_3.4.15 GLIBCXX_3.4.16 GLIBCXX_3.4.17 GLIBCXX_3.4.18 GLIBCXX_3.4.19 GLIBC_2.3 GLIBC_2.2.5 GLIBC_2.14 GLIBC_2.4 GLIBC_2.3.2 GLIBCXX_DEBUG_MESSAGE_LENGTH
- 下载gcc源码
cd /usr/local/src/ wget http://ftp.gnu.org/gnu/gcc/gcc-6.4.0/gcc-6.4.0.tar.xz tar -xf gcc-6.4.0.tar.xz -C /usr/src cd /usr/src/gcc-6.4.0 # 自动下载mpfr-2.4.2.tar.bz2、gmp-4.3.2.tar.bz2、mpc-0.8.1.tar.gz和isl-0.15.tar.bz2软件包 ./contrib/download_prerequisites ./configure -enable-checking=release -enable-languages=c,c++ -disable-multilib make -j4 make install
- 配置gcc
find / -name "libstdc++.so*" # /usr/src/gcc-6.4.0/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.22 cd /usr/lib64 cp /usr/src/gcc-6.4.0/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.22 libstdc++.so.6.0.22 mv libstdc++.so.6 libstdc++.so.6.old ln -sv libstdc++.so.6.0.22 libstdc++.so.6 ####### strings /usr/lib64/libstdc++.so.6 | grep GLIBC GLIBCXX_3.4 GLIBCXX_3.4.1 GLIBCXX_3.4.2 GLIBCXX_3.4.3 GLIBCXX_3.4.4 GLIBCXX_3.4.5 GLIBCXX_3.4.6 GLIBCXX_3.4.7 GLIBCXX_3.4.8 GLIBCXX_3.4.9 GLIBCXX_3.4.10 GLIBCXX_3.4.11 GLIBCXX_3.4.12 GLIBCXX_3.4.13 GLIBCXX_3.4.14 GLIBCXX_3.4.15 GLIBCXX_3.4.16 GLIBCXX_3.4.17 GLIBCXX_3.4.18 GLIBCXX_3.4.19 GLIBCXX_3.4.20 GLIBCXX_3.4.21 GLIBCXX_3.4.22 GLIBC_2.3 GLIBC_2.2.5 GLIBC_2.14 GLIBC_2.17 GLIBC_2.3.2 GLIBCXX_DEBUG_MESSAGE_LENGTH
- 查看系统版本
-
cmake 安装
https://cmake.org/download/# 解压Cmake压缩包 tar -zxvf cmake-3.16.4.tar.gz # 进入Cmake文件夹 cd cmake-3.16.4 # 运行bootstrap ./bootstrap # 编译gmake gmake # 进行安装Cmake gmake install
Step1 llvm安装
llvm源码下载
llvm安装教程
下载 Sources下除LLVM Test Suite外的所有源码
- 一键下载
llvm 为了防止污染源码,不能在源码目录直接安装,在 llvm 同级目录下新建一个 build 目录,进入再安装# LLVM 源码下载 wget https://releases.llvm.org/9.0.0/clang-tools-extra-9.0.0.src.tar.xz wget https://releases.llvm.org/9.0.0/compiler-rt-9.0.0.src.tar.xz wget https://releases.llvm.org/9.0.0/libunwind-9.0.0.src.tar.xz wget https://releases.llvm.org/9.0.0/libcxxabi-9.0.0.src.tar.xz wget https://releases.llvm.org/9.0.0/libcxx-9.0.0.src.tar.xz wget https://releases.llvm.org/9.0.0/openmp-9.0.0.src.tar.xz wget https://releases.llvm.org/9.0.0/lldb-9.0.0.src.tar.xz wget https://releases.llvm.org/9.0.0/polly-9.0.0.src.tar.xz wget https://releases.llvm.org/9.0.0/lld-9.0.0.src.tar.xz wget https://releases.llvm.org/9.0.0/cfe-9.0.0.src.tar.xz wget https://releases.llvm.org/9.0.0/llvm-9.0.0.src.tar.xz # 放在llvm/tools 目录下的组件 tar Jxvf llvm-9.0.0.src.tar.xz mv llvm-9.0.0.src llvm tar Jxvf cfe-9.0.0.src.tar.xz mv cfe-9.0.0.src clang mv clang llvm/tools tar Jxvf lld-9.0.0.src.tar.xz mv lld-9.0.0.src lld mv lld llvm/tools tar Jxvf polly-9.0.0.src.tar.xz mv polly-9.0.0.src polly mv polly llvm/tools tar Jxvf lldb-9.0.0.src.tar.xz mv lldb-9.0.0.src lldb mv lldb llvm/tools # 放在 llvm/projects 目录下的组件 tar Jxvf openmp-9.0.0.src.tar.xz mv openmp-9.0.0.src openmp mv openmp llvm/projects tar Jxvf libcxx-9.0.0.src.tar.xz mv libcxx-9.0.0.src libcxx mv libcxx llvm/projects tar Jxvf libcxxabi-9.0.0.src.tar.xz mv libcxxabi-9.0.0.src libcxxabi mv libcxxabi llvm/projects tar Jxvf libunwind-9.0.0.src.tar.xz mv libunwind-9.0.0.src libunwind mv libunwind llvm/projects tar Jxvf compiler-rt-9.0.0.src.tar.xz mv compiler-rt-9.0.0.src compiler-rt mv compiler-rt llvm/projects # clang 的子组件 tar Jxvf clang-tools-extra-9.0.0.src.tar.xz mv clang-tools-extra-9.0.0.src extra mv extra llvm/tools/clang/tools # 删除下载的tar.xz rm -rf *.tar.xz
如果出现以下报错:cmake -G "Unix Makefiles" \ -DLLVM_ENABLE_ASSERTIONS=On \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_C_COMPILER=/usr/local/bin/gcc \ -DCMAKE_CXX_COMPILER=/usr/local/bin/c++ \ -DLIBCXXABI_LIBC_INCLUDES= /path/llvm/projects/libcxxabi/include \ -DLIBCXX_LIBCXX_INCLUDES= /path/llvm/projects/libcxx/include \ ../llvm make -j4 make install
执行CMake Error at tools/lldb/scripts/cmake_install.cmake:41 (file): file INSTALL cannot find "/usr/local/src/llvm/llvm/build/./lib/python2.7": No such file or directory. Call Stack (most recent call first): tools/lldb/cmake_install.cmake:50 (include) tools/cmake_install.cmake:51 (include) cmake_install.cmake:68 (include)
如果出现以下报错:cp -r lib64/python2.7 lib/python2.7
执行CMake Error at /usr/local/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message): Could NOT find SWIG (missing: SWIG_EXECUTABLE SWIG_DIR) Call Stack (most recent call first): /usr/local/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:393 (_FPHSA_FAILURE_MESSAGE) /usr/local/share/cmake-3.16/Modules/FindSWIG.cmake:64 (FIND_PACKAGE_HANDLE_STANDARD_ARGS) tools/lldb/scripts/CMakeLists.txt:16 (find_package)
如果报以下错yum install swig
执行CMake Error at /usr/local/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message): Could NOT find LibEdit (missing: libedit_INCLUDE_DIRS libedit_LIBRARIES) Call Stack (most recent call first): /usr/local/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:393 (_FPHSA_FAILURE_MESSAGE) tools/lldb/cmake/modules/FindLibEdit.cmake:54 (find_package_handle_standard_args) tools/lldb/cmake/modules/LLDBConfig.cmake:94 (find_package) tools/lldb/CMakeLists.txt:20 (include)
yum install libedit.i686 libedit-devel.i686
Step2 TVM安装
git clone --recursive https://github.com/apache/incubator-tvm tvm
git submodule init
git submodule update
- 定制config.cmake
cd tvm $$ mkdir build
cp cmake/config.cmake build
cd build
编辑build/config.cmake文件,里面有一些功能开关,这些配置有:
USE_CUDA,NVIDIA的GPU计算;
USE_ROCM,通用的GPU计算,AMD提出,目的很显然...;
USE_SDACCEL,FPGA计算;
USE_AOCL,Intel FPGA SDK for OpenCL (AOCL) runtime;
USE_OPENCL,异构平台编写程序的框架,异构平台可由CPU、GPU、DSP、FPGA或其他类型的处理器与硬件加速器所组成;
USE_METAL,iOS上的GPU计算;
USE_VULKAN,新一代的openGL,Android 7.x开始支持(iOS不支持,因为有自己的metal2);
USE_OPENGL,2D/3D渲染库标准,显卡厂家负责实现和支持;
USE_SGX, Intel SGX ;
USE_RPC,远程调用,电脑和手机可以通过网络联调;
USE_STACKVM_RUNTIME,embed stackvm into the runtime;
USE_GRAPH_RUNTIME,enable tiny embedded graph runtime;
USE_GRAPH_RUNTIME_DEBUG,enable additional graph debug functions;
USE_LLVM,llvm support;
USE_BLAS,API标准,规范发布基础线性代数操作的数值库(如矢量或矩阵乘法),不同的实现有openblas, mkl, atlas, apple
USE_RANDOM,contrib.random运行时;
USE_NNPACK,
USE_CUDNN,
USE_CUBLAS,
USE_MIOPEN,
USE_MPS,
USE_ROCBLAS,
USE_SORT,使用contrib sort;
USE_ANTLR,
USE_VTA_TSIM,
USE_RELAY_DEBUG,Relay debug模式
cmake .. -DCMAKE_INSTALL_PREFIX=./install //如果需要gdb跟踪源码的话需要加-DCMAKE_BUILD_TYPE=Debug
make -j4
make install
error:
git submodule init
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = "zh_CN.utf8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
解决方法:
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
tvm系统配置
vim ~/.bashrc
export TVM_PATH=/yourpath/to/tvm # 你自己tvm
export PYTHONPATH=$TVM_PATH/python:$TVM_PATH/topi/python:$TVM_PATH/nnvm/python:${PYTHONPATH}
source ~/.bashrc
安装TVM python bindings by setup.py
cd tvm
cd python
python3 setup.py install --user
cd ..
cd topi/python
python setup.py install --user
测试
python3
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tvm
>>> print(tvm.__version__)
0.7.dev0
>>>
Reference
PyTorch模型转TVM模型全流程记录
安装TVM
TVM学习(1)–搭建环境
--------------------------------------------------------- END --------------------------------------------------------