[macOS][M1] Building TVM on an M1 Mac

Initial Setup

I have been experimenting with running TVM on a new Apple Silicon Mac and wanted to start recording workarounds and instructions here as we start to gain support for running on M1 Macs.

I found some useful notes on getting setup with standard Mac tools here, currently homebrew doesn’t fully work on macOS aarm64 but you can install it and many formulas are already working.

You can easily setup XCode and Homebrew using these instructions: https://github.com/mikelxc/Workarounds-for-ARM-mac.

Python

Currently many popular Python packages including NumPy don’t work out of the box on the M1.

I found the most robust way to get started right now is using a special version of Miniconda being built for M1 by Conda Forge. You can read more about how to install it here: https://conda-forge.org/blog/posts/2020-10-29-macos-arm64/.

You can then use this to do:

conda install numpy decorator attrs cython

and any other TVM dependencies you need.

LLVM

You can also get a fresh build of LLVM-11 from Conda.

conda install llvmdev

You can then set USE_LLVM to llvm-config which should be installed in Conda bin path.

CMake

You can also install CMake via Conda.

conda install cmake

As this issue discusses currently CMake will default to building a fat-binary build on macOS architectures on Big Sur (from my understanding) resulting in TVM’s build attempting to link an x86-64 version of TVM to an ARM64 version of LLVM. By default I was getting linking errors.

You can configure CMake with below flags to enable building only arm64 libraries when you are done you should be able to use lipo to confirm that the binaries are correct.

cmake .. -G Ninja -DCMAKE_OSX_ARCHITECTURES=arm64

TVM

For some reason using ctypes causes an allocator segfault using the version of Python bundled by Conda and most recent Big Sur. If you instead install cython and run make cython in the root directory of TVM everything works as expected and you can make progress.

If anyone else has refinements to this process please post below and we can try and build a working guide which can be upstreamed as docs at some point.

9 Likes

This is awesome!!

Another cool finding: it looks like llvmdev has been supported on osx-arm64: https://anaconda.org/conda-forge/llvmdev/files. So we don’t need rosetta to translate llvm

1 Like

CC @tgall_foo who may have been looking at this too.

You’re really catching up! You should post this to the Reddit or somewhere to let more people know TVM already works on M1 Mac :smiley:

1 Like

Hi!

I’ve been working on TVM and deps on Apple Silicon for a bit. Really the major source of frustration for which there is no initial work around is the lack of gcc for Apple Silicon. This is the most importance dependency that needs to be addressed.

The ETA is effectively next year sometime and is a direct function of who all will jump in and help out.

Maybe this would be a good lightning talk for the tvm conf?

Once GCC is in place, native MacOS via brew or other paths will be able to build quite a number of deps pretty smoothly, like numpy etc.

The other path to success is Linux within a VM on Apple Silicon. This will provide a “just works” experience however access to offload will be the interesting bit. Parallels for instance hasn’t shipped their alpha so we don’t know what that might look like, can we make use of Metal? Who knows. Virtualbox is only just talking about Apple Silicon support, far as I can tell they’ve done nothing. That is all I’ve looked into thus far.

There is as of the moment no plans to support dual boot between say Linux and MacOS on Apple Silicon.

Another path, “We” could certainly get more deps building with clang instead of gcc. This would probably be better for the sake of the universe, yet not an easy task.

I have TVM built on my Apple Silicon hardware using clang-12. I hadn’t looked at the conda route @jroesch, so pretty cool!

Thanks for the great post! I Just got my M1 chip Mac and tried to build TVM on it. I download the Conda-forge. When I run Conda install llvmdev, it keeps getting me this error “Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.” How can I solve it ? Thank you!

1 Like

Have you tried not using conda? It sounds like the package might just be broken and needs to be solved.

I have an m1 and I think I just downloaded llvm using homebrew

Hi! Thanks for responding. I installed llvm via brew and it works but there is still problems to run cmake in the build path. I am pretty new to Tvm and trying to run microtvm. Can I add your WeChat and study for more details? WeChat ID: JOHN_LZH Thank you so much for the help!

I don’t have a wechat but feel free to dm me on the forum.

I’ll try to put together a guide on doing a fresh dev install of TVM tomorrow.

1 Like

That’s fine. Thank you so much. Right now I went through all the installation but there are some assertion error occur. However, the Tvm python package works fine which I have on clue why that happened.

I installed LLVM via brew install llvm, and tried to build tvm both with and without -DCMAKE_OSX_ARCHITECTURES=arm64. After I got the two shared library, I tried import tvm in python. However, I got the following error: OSError no suitable image found. Did find: /xx/xxx/libtvm.dylib: mach-o, but wrong architecture. Anyone encountered this problem?

I ran into the same issue. The problem is that you probably installed the wrong architecture of python. I installed python from their website and it had x86_64 architecture. Try installing Python from homebrew.

brew install python@3.8