Skip to content

Instantly share code, notes, and snippets.

@LogCreative
Last active November 20, 2022 14:31
Show Gist options
  • Select an option

  • Save LogCreative/8b8f0d956756cf710c01185eacc05d27 to your computer and use it in GitHub Desktop.

Select an option

Save LogCreative/8b8f0d956756cf710c01185eacc05d27 to your computer and use it in GitHub Desktop.
Install TVM on macOS M1 chip.

Install TVM on macOS M1 chip

TVM is an open source machine learning compiler framework. The official guide gives some information about how to install it for macOS M1 chip. But there are still some pitfalls when installing. The setup for this tutorial is on MacBook Pro M1, macOS Ventura (13.0).

Download the source code

It is not recommended to just use pip to install tvm since the version is not compiled for apple chip. To maximize the availability, compile the source and then install it. You could download the released version of TVM source from download page, I use 0.10.0 in this tutorial.

Though in theory, you could git clone the source code from the offical GitHub repository. But it is tedious to download the submodules of this project -- they are just too many.

Unzip the file and enter the directory in the Terminal.

Build the shared library

Remember, you need to use Clang (LLVM) to compile the library! Don't use GCC if you don't want to have any trouble in the future. It is not recommended to install TVM on Windows since some features will be forbidden, and for the most of the time, the compiling process is broken.

Install the dependencies in the official guide, and then modify the CMake file build/config.cmake:

# For using PyTorch, add parametters to LLVM.
set(USE_LLVM "/opt/homebrew/opt/llvm/bin/llvm-config --link-static")

# For using PyTorch.
set(HIDE_PRIVATE_SYMBOLS ON)

# Link zstd, otherwise you will meet compiling error.
link_directories("/opt/homebrew/Cellar/zstd/1.5.2/lib")

The last line is not mentioned in the official guide. Removing it will cause the compiling error:

ld: library not found for -lzstd

The solution is reference to https://stackoverflow.com/a/67877734.

You may see some errors down the line, but it is not crucial since the Ventura update is still not available.

ld: warning: object file (/opt/homebrew/Cellar/llvm/15.0.3/lib/libLLVMXRay.a(FDRRecords.cpp.o)) was built for newer macOS version (13.0) than being linked (12.3)

Python package installation

The offical guide requires us to use anaconda, then we use it. Notice you should use Python 3.8 in your environment.

I use Method 2 when installing tvm package, since I am not going to develop tvm source code.

You may meet some errors like the following, it doesn't matter as well.

error: Could not find suitable distribution for Requirement.parse('tvm==0.10.0')

The warning in the beginning could also be ignored.

WARNING: Cython is not installed, will compile without cython module

Remember to install the dependencies afterwards.

pip install numpy attrs cloudpickle decorator psutil scipy synr==0.6.0 tornado

brew install openblas gfortran

pip install pybind11 cython pythran

export OPENBLAS=/opt/homebrew/opt/openblas/lib/

pip install scipy --no-use-pep517

pip install 'xgboost<1.6.0'

Finally, install pytorch in my conda tvm environment, reference to the PyTorch Installation Page.

conda install pytorch torchvision torchaudio -c pytorch

There is no NVIDIA graphics card on MacBook. If you want to install TVM with CUDA, you need to modify config.cmake refering to the official guide.

Test TVM

If everything works well, you could test TVM with PyTorch by the offical demo jupyter notebook. Remember you should run the notebook in tvm environment.

@LogCreative

Copy link
Copy Markdown
Author

Installation guide for other platform other than PyTorch: https://tvm.apache.org/docs/how_to/compile_models/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment