一、前言
眾所周知夜焦,深度學(xué)習(xí)的計(jì)算量龐大,在追求效率與實(shí)用性的工業(yè)界含友,深度學(xué)習(xí)所面臨的一個(gè)最大的問題就是如何在不影響模型精度的前提下將算法模型部署到目標(biāo)硬件平臺(tái)上進(jìn)行高效的前向計(jì)算吮便。
和單純研究相比隙轻,在工業(yè)界主要遇到了兩個(gè)問題:
深度學(xué)習(xí)框架眾多恨诱,caffe / mxnet / tensorflow / pytorch訓(xùn)練出來的模型都彼此有不同的分發(fā)格式晕窑,對(duì)于部署有環(huán)境兼容的問題。
硬件價(jià)格昂貴睹栖,一些嵌入式平臺(tái)沒有使用GPU的條件硫惕。同時(shí)一些人也開始在做FPGA/ASIC的深度學(xué)習(xí)加速卡。
針對(duì)這些問題野来,TVM應(yīng)運(yùn)而生恼除,為了解決第一個(gè)問題,TVM內(nèi)部實(shí)現(xiàn)了自己的IR曼氛,可以將上面這些主流深度學(xué)習(xí)框架的模型轉(zhuǎn)換為統(tǒng)一的內(nèi)部表示豁辉,主要是通過NNVM進(jìn)行轉(zhuǎn)換。
為了解決第二個(gè)問題舀患,TVM內(nèi)部有多重機(jī)制來做優(yōu)化徽级。其中一個(gè)特點(diǎn)是,使用機(jī)器學(xué)習(xí)(結(jié)合專家知識(shí))的方法聊浅,通過在目標(biāo)硬件上跑大量trial餐抢,來獲得該硬件上相關(guān)運(yùn)算(例如卷積)的最優(yōu)實(shí)現(xiàn)。這使得TVM能夠做到快速為新型硬件或新的op做優(yōu)化狗超。
本文作為TVM系列文章的開篇弹澎,先從TVM的開發(fā)環(huán)境搭建開始。
二努咐、開發(fā)環(huán)境搭建
1、Anconda安裝
- 下載32位anconda
wget https://repo.anaconda.com/archive/Anaconda3-5.3.1-Linux-x86.sh
- 安裝
chmod +x Anaconda3-5.31-Linux-x86.sh
./Anaconda3-5.31-Linux-x86.sh
根據(jù)提示輸入yes/no殴胧,最后source .bashrc更新環(huán)境配置即可渗稍。
2、TVM庫(kù)源碼編譯
從源碼編譯tvm分兩步:
(1)將C++代碼編譯成libtvm.so和libtvm_runtime.so
(2)配置python包
- 下載源碼:
git clone --recursive https://github.com/apache/tvm tvm
編譯需要的環(huán)境配置:支持C++14的c++編譯器g++ >= g++-5团滥,CMake >=3.5
sudo apt-get update
sudo apt-get install -y python3 python3-dev python3-setuptools gcc libtinfo-dev zlib1g-dev build-essential cmake libedit-dev libxml2-dev
遇到問題:
W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: http://storage.googleapis.com/bazel-apt stable InRelease: The following signatures were invalid: KEYEXPIRED 1590239453 KEYEXPIRED 1590239453 KEYEXPIRED 1590239453
W: Failed to fetch http://storage.googleapis.com/bazel-apt/dists/stable/InRelease The following signatures were invalid: KEYEXPIRED 1590239453 KEYEXPIRED 1590239453 KEYEXPIRED 1590239453
W: Some index files failed to download. They have been ignored, or old ones used instead.
解決:
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
- 下載llvm竿屹,用于CPU后端代碼生成:
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 10
遇到問題:
apt-get install -y clang-12 lldb-12 lld-12 clangd-12
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
解決:
sudo rm /var/lib/dpkg/lock
- 編譯tvm:
mkdir build && cp cmake/config.cmake build
修改cmake/config.cmake中的配置,將set(USE_LLVM OFF)改為ON灸姊,然后就可以編譯了:
cd build && cmake .. && make -j4
編譯完成后在build目錄會(huì)生成libtvm.so與libtvm_runtime.so兩個(gè)動(dòng)態(tài)庫(kù)文件
3拱燃、TVM python軟件包使用配置
安裝xgboost遇到問題:
Command "/home/zgs/anaconda3/envs/py36/bin/python3 -u -c "
import setuptools, tokenize;__file__='/tmp/pip-install-5hsr7l_p/xgboost/setup.py';
f=getattr(tokenize, 'open', open)(__file__);
code=f.read().replace('\r\n', '\n');f.close();
exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-53aqs7l9/install-record.txt --single-version-externally-managed --compile"
failed with error code 1 in /tmp/pip-install-5hsr7l_p/xgboost/
解決:
pip install --upgrade --force pip
pip install setuptools==33.1.1
xgboost需要cmake版本 > 3.11,升級(jí)cmake:
wget https://github.com/Kitware/CMake/releases/download/v3.20.5/cmake-3.20.5.tar.gz
./configure && make && sudo make install
ln -sf /usr/local/bin/cmake /usr/bin/cmake
ln -sf /usr/local/bin/ctest /usr/bin/ctest
ln -sf /usr/local/bin/cpack /usr/bin/cpack
遇到問題:
CMake Error at /usr/local/share/cmake-3.5/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
system variable OPENSSL_ROOT_DIR (missing: OPENSSL_LIBRARIES
OPENSSL_INCLUDE_DIR)
Call Stack (most recent call first):
/usr/local/share/cmake-3.5/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
/usr/local/share/cmake-3.5/Modules/FindOpenSSL.cmake:370 (find_package_handle_standard_args)
CMakeLists.txt:61 (find_package)
解決:
sudo apt-get install libssl-dev
安裝python依賴包:
pip3 install --user numpy decorator attrs
# For RPC tracker
pip3 install --user tornado
# For auto-tuning module
pip3 install --user tornado psutil xgboost cloudpickle
添加tvm環(huán)境變量到.bashrc文件力惯,執(zhí)行source .bashrc:
export TVM_HOME=/path/to/tvm
export PYTHONPATH=$TVM_HOME/python:${PYTHONPATH}