美文网首页TVM学习
【TVM系列一】开发环境搭建

【TVM系列一】开发环境搭建

作者: AIPlayer | 来源:发表于2022-07-02 14:56 被阅读0次

    一、前言

    众所周知,深度学习的计算量庞大,在追求效率与实用性的工业界,深度学习所面临的一个最大的问题就是如何在不影响模型精度的前提下将算法模型部署到目标硬件平台上进行高效的前向计算。

    和单纯研究相比,在工业界主要遇到了两个问题:

    • 深度学习框架众多,caffe / mxnet / tensorflow / pytorch训练出来的模型都彼此有不同的分发格式,对于部署有环境兼容的问题。

    • 硬件价格昂贵,一些嵌入式平台没有使用GPU的条件。同时一些人也开始在做FPGA/ASIC的深度学习加速卡。

    针对这些问题,TVM应运而生,为了解决第一个问题,TVM内部实现了自己的IR,可以将上面这些主流深度学习框架的模型转换为统一的内部表示,主要是通过NNVM进行转换。

    image.png

    为了解决第二个问题,TVM内部有多重机制来做优化。其中一个特点是,使用机器学习(结合专家知识)的方法,通过在目标硬件上跑大量trial,来获得该硬件上相关运算(例如卷积)的最优实现。这使得TVM能够做到快速为新型硬件或新的op做优化。

    本文作为TVM系列文章的开篇,先从TVM的开发环境搭建开始。

    二、开发环境搭建

    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
    

    根据提示输入yes/no,最后source .bashrc更新环境配置即可。

    2、TVM库源码编译

    从源码编译tvm分两步:

    (1)将C++代码编译成libtvm.so和libtvm_runtime.so

    (2)配置python包

    • 下载源码:
    git clone --recursive https://github.com/apache/tvm tvm
    

    编译需要的环境配置:支持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目录会生成libtvm.so与libtvm_runtime.so两个动态库文件

    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,升级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环境变量到.bashrc文件,执行source .bashrc:

    export TVM_HOME=/path/to/tvm
    export PYTHONPATH=$TVM_HOME/python:${PYTHONPATH}
    

    相关文章

      网友评论

        本文标题:【TVM系列一】开发环境搭建

        本文链接:https://www.haomeiwen.com/subject/bukkbrtx.html