TVM源码框架安装方法

TVM源码框架安装方法

本文提供如何在各种系统上从零构建和安装TVM包的说明。它包括两个步骤:             

首先从C++代码中构建共享库(linux的libtvm.so,macOS的libtvm.dylib和windows的libtvm.dll)。             

编程语言包的设置(例如Python包)。             

实现,请从下载页面下载tvm源代码。

https://tvm.apache.org/download

Developers: Get Source from Github

还可以选择从github 上clone源repo。使用--recursive选项clone子模块很重要。

git clone --recursive https://github.com/apache/tvm tvm 

使用github工具的windows用户,可以使用git shell,再键入以下命令。  

git submodule init

git submodule update

Build the Shared Library

目标是build 共享库:

  • On Linux the target library are libtvm.so
  • On macOS the target library are libtvm.dylib
  • ·       On Windows the target library are libtvm.dll

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

最低building需求

一个支持C++ 14(g++—5或更高)的C++编译器             

CMake 3.5或更高             

强烈建议使用LLVM构建以启用所有功能。             

如果要使用CUDA,则需要CUDA toolkit版本>=8.0。如果要从旧版本升级,请确保删除旧版本并在安装后重新启动。             

在macOS上,可能需要安装自制程序https://brew.sh,便于简化安装和管理依赖项。             

使用cmake来构建库。TVM的配置可以通过以下方式进行config.cmake配置文件完成。             

首先,检查系统中的cmake。如果您没有cmake,您可以从官方网站获取最新版本             

首先创建一个构建目录,复制cmake/config.cmake文件到该目录。

mkdir build

cp cmake/config.cmake build

  • 使用 build/config.cmake 来定制编译选项
    • On macOS, for some versions of Xcode, you need to add -lc++abi in the LDFLAGS or you’ll get link errors.
    • Change set(USE_CUDA OFF) to set(USE_CUDA ON) to enable CUDA backend. Do the same for other backends and libraries you want to build for (OpenCL, RCOM, METAL, VULKAN, …).
    • To help with debugging, ensure the embedded graph runtime and debugging functions are enabled with set(USE_GRAPH_RUNTIME ON) and set(USE_GRAPH_RUNTIME_DEBUG ON)
  • TVM 需要 CPU codegen LLVM . 强烈推荐用 LLVM支持.
    • LLVM 4.0 or higher is needed for build with LLVM. Note that version of LLVM from default apt may lower than 4.0.
    • Since LLVM takes long time to build from source, you can download pre-built version of LLVM from LLVM Download Page.
      • Unzip to a certain location, modify build/config.cmake to add set(USE_LLVM /path/to/your/llvm/bin/llvm-config)
      • You can also directly set set(USE_LLVM ON) and let cmake search for a usable version of LLVM.
      • You can also use LLVM Nightly Ubuntu Build
        • Note that apt-package append llvm-config with version number. For example, set set(USE_LLVM llvm-config-10) if you installed LLVM 10 package
  • 编译TVM和相关libraries.
    • 使用Ninja build system 替代Unix Makefiles. 比用Makefiles更简洁.
·       cd build
·       cmake ..
·       make -j4
·       cd build
·       cmake .. -G Ninja
·       ninja

If everything goes well, we can go to Python Package Installation

https://tvm.apache.org/docs/install/from_source.html#python-package-installation

Building with a Conda Environment

Conda是获取运行TVM所需依赖项的一种非常方便的方法。

首先,如果系统中还没有conda,请按照conda的安装指南(https://docs.conda.io/projects/conda/en/latest/user-guide/install/

安装miniconda或anaconda。在conda环境中运行以下命令:

# Create a conda environment with the dependencies specified by the yaml

conda env create --file conda/build-environment.yaml

# Activate the created environment

conda activate tvm-build

上面的命令将安装所有必要的构建依赖项,如cmake和LLVM。可以在后面运行标准build过程。             

如果要在conda环境之外使用编译后的二进制文件,可以将LLVM设置为static linking mode set(USE_LLVM "llvm-config --link-static")。这样,生成的库就不会依赖于conda环境中的动态LLVM库。             

上面的说明展示了如何使用conda提供构建libtvm所需的构建依赖项。如果已经使用conda作为包管理器,并且希望直接将tvm作为conda包来构建和安装,则可以按照以下说明进行操作:

conda build --output-folder=conda/pkg  conda/recipe

# Run conda/build_cuda.sh to build with cuda enabled

conda install tvm -c ./conda/pkg

Building on Windows

使用cmake通过MSVC构建TVM支持。您将需要包含一个visualstudio编译器。最低要求的VS版本是Visual Studio Community 2015 Update 3。我们建议使用Conda环境进行后续构建,以获得必要的依赖关系并获得激活的tvm构建环境。然后可以运行以下命令来构建。

mkdir build

cd build

cmake -A x64 -Thost=x64 ..

cd ..

上面的命令在build目录下生成解决方案文件。可以运行以下命令来build

cmake --build build --config Release -- /m

Building ROCm support

目前,ROCm只在linux上受支持,所以所有的指令都是用linux编写的。             

Set Set(USE_ROCM ON),将ROCM_PATH设置为正确的路径。             

首先需要从ROCm安装HIP运行时。确保安装系统中安装了ROCm。             

安装最新稳定版本的LLVM(v6.0.1)和LLD,确保ld.lld可通过命令行使用。

Python Package Installation

TVM package

根据开发环境,可使用虚拟环境和包管理器(如virtualenv或conda)来管理python包和依赖项。             

安装和维护python开发环境。             

python包位于tvm/python。有两种安装方法:

Method 1

此方法推荐给可能更改代码的开发人员。             

设置环境变量PYTHONPATH,表示python在哪里可以找到库。例如,假设在目录/path/to/tvm上cloned了tvm,那么我们可以在~/.bashrc中添加以下行。一旦获取代码并重新build项目,修改将立即显现出来(无需再次调用setup安装程序)。

export TVM_HOME=/path/to/tvm

export PYTHONPATH=$TVM_HOME/python:${PYTHONPATH}

Method 2

Install TVM python bindings by setup.py:

# install tvm package for the current user

# NOTE: if you installed python via homebrew, --user is not needed during installaiton

#       it will be automatically installed to your user directory.

#       providing --user flag may trigger error during installation in such case.

export MACOSX_DEPLOYMENT_TARGET=10.9  # This is required for mac to avoid symbol conflicts with libstdc++

cd python; python setup.py install --user; cd ..

Python dependencies

Note that the --user flag is not necessary if you’re installing to a managed local environment, like virtualenv.

pip3 install --user numpy decorator attrs
pip3 install --user tornado
pip3 install --user tornado psutil xgboost

Install Contrib Libraries

Enable C++ Tests

We use Google Test to drive the C++ tests in TVM. The easiest way to install GTest is from source.

git clone https://github.com/google/googletest
cd googletest
mkdir build
cd build
cmake ..
make
sudo make install

After installing GTest, the C++ tests can be built and started with ./tests/scripts/task_cpp_unittest.sh or just built with make cpptest.              

人工智能芯片与自动驾驶
原文地址:https://www.cnblogs.com/wujianming-110117/p/14051492.html