構建編譯TVM方法


構建編譯TVM方法

本文提供如何在各種系統上構建和安裝TVM包的說明。它包括兩個步驟:

  1. 1.     首先從C代碼構建共享庫( libtvm.so for linux, libtvm.dylib for macOS and libtvm.dll for windows)。
  2. 2.     語言包的設置(例如Python包)。

TVM源碼下載鏈接

https://github.com/apache/tvm/

 

Developers: Get Source from Github

You can also choose to clone the source repo from github. It is important to clone the submodules along, with --recursive option.

可以選擇從github中repo克隆源。克隆子模塊很重要,--recursive選項

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

For windows users who use github tools, you can open the git shell, and type the following command.

對於使用github工具的windows用戶,可以打開git shell,然后鍵入以下命令。

git submodule init

git submodule update

Build the Shared Library

目標是建立共享庫:

  • 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要求是

  • ·        支持c14(g-5或更高版本)的最新c++編譯器
  • ·        CMake 3.5或更高
  • ·        強烈建議使用LLVM構建,啟用所有功能。

如果要使用CUDA,需要CUDA toolkit version >= 8.0。如果從老版本更新,確信刪除了老版本,並且需要安裝后reboot重啟。

  • 在macOS上,可能需要安裝Homebrew https://brew.sh管理依賴項。

使用cmake來構建庫,TVM的配置可以通過以下config.cmake方式進行。

  • ·        檢查系統中的cmake。如果沒有cmake,可以從官方網站找最新版本
  • ·        創建一個構建目錄,復制cmake/config.cmake到目錄
  • mkdir build
  • cp cmake/config.cmake build
  • cd build
  • cmake ..
  • make -j4
  • You can also use Ninja build system instead of Unix Makefiles. It can be faster to build than using Makefiles.
  • cd build
  • cmake .. -G Ninja
  • ninja
  • 編輯build/config.cmake自定義編譯選項
    • 在macOS上,對於某些版本的Xcode,需要添加-lc++abi否則會出現鏈接錯誤
    • 改變set(USE_CUDA OFF)到 設置(使用_CUDA開)啟用CUDA后端。對其它后端和庫執行相同的操作(OpenCL、RCOM、METAL、VULKAN…)。
    • 若要幫助調試,確保使用啟用了set(USE_GRAPH_EXECUTOR ON)和設置(使用_PROFILER開)嵌入式圖形執行器和調試函數。
  • TVM需要LLVM用於CPU codegen。強烈建議構建LLVM支持。
    • 使用LLVM構建需要LLVM 4.0或更高版本。注意,默認apt的LLVM版本可能低於4.0。
    • 由於LLVM從源代碼構建需要很長時間,所以可以從LLVM下載頁面https://apt.llvm.org/ .
      • 解壓縮到某個位置,修改build/config.cmake添加 set(USE_LLVM /path/to/your/llvm/bin/llvm-config)
      • 也可以直接設置set(USE_LLVM ON),讓cmake搜索LLVM的可用版本。
    • 也可以使用LLVM預編譯Ubuntu構建https://releases.llvm.org/download.html
      • 注意apt包appendllvm-config帶版本編號。用於示例,集合 set(USE_LLVM llvm-config-10),如果安裝了LLVM 10包。
  • 然后可以建立tvm和相關的庫。

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

Building with a Conda Environment

Conda是獲取運行TVM所需依賴項的一種非常方便的方法。ollow the conda’s installation guide,如果系統中還沒有conda,安裝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。然后可以在最后一節中運行標准生成過程。

如果要在conda環境之外使用編譯后的二進制文件,可以將LLVM設置為靜態鏈接模式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。建議:Building with a Conda Environment以獲得必要的依賴關系,獲得激活的tvm構建環境。然后可以運行以下命令來構建:

mkdir build

cd build

cmake -A x64 -Thost=x64 ..

cd ..

The above command generates the solution file under the build directory. You can then run the following command to build

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

Building ROCm support

Currently, ROCm is supported only on linux, so all the instructions are written with linux in mind.

  • Set set(USE_ROCM ON), set ROCM_PATH to the correct path.
  • You need to first install HIP runtime from ROCm. Make sure the installation system has ROCm installed in it.
  • Install latest stable version of LLVM (v6.0.1), and LLD, make sure ld.lld is available via command line.

Python Package Installation

TVM package

Depending on your development environment, you may want to use a virtual environment and package manager, such as virtualenv or conda, to manage your python packages and dependencies.

to install and maintain your python development environment.

The python package is located at tvm/python There are two ways to install the package:

Method 1

This method is recommended for developers who may change the codes.

Set the environment variable PYTHONPATH to tell python where to find the library. For example, assume we cloned tvm on the directory /path/to/tvm then we can add the following line in ~/.bashrc. The changes will be immediately reflected once you pull the code and rebuild the project (no need to call setup again)

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.

  • Necessary dependencies:

pip3 install --user numpy decorator attrs

  • If you want to use RPC Tracker

pip3 install --user tornado

  • If you want to use auto-tuning module

pip3 install --user tornado psutil xgboost cloudpickle

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.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM