Setting up MXNet on macOS with Conda


MXNet is another open source deep learning framework picking up speed after the success of TensorFlow and PyTorch. Amazon and Microsoft is acting as driving force behind it. It is main selling point is “scalable”, i.e., designed with cloud computing and distributed parallel computing in mind. Since it is not yet as popular as TF and PyTorch in the community, you will find less StackOverflow answers when you need one. But this can be fun for some people if you enjoy the process of figuring wired things out.


MXNet’s official installation guide didn’t spell it out explicitly, but as of today (05/27/2018), the support for Python 3 is still in progress. Some function is not fully tested yet. So stay with Python 2.7 if you can.


The official MXNet installation guide is only tested on macOS 10.11 and 10.12. So if you upgraded your macOS to High Sierra like me, you need to defeat the os version check the build the binary yourself. I will explain it later.


Installing MXNet with Conda and Jupyter notebook on macOS 10.12 (Sierra):

As of writing, MXNet is still not an official Conda package. So we cannot simply do conda install mxnet just yet. Here’s how I get it to work:

  1. Install nb_conda_kernels so that you don’t need to manually update your kernel list for Jupyter: conda install -c conda-forge nb_conda_kernels
  2. Create a Conda environment for mxnet: conda create -n mxnet_conda_env python=2.7 ipykernel matplotlib. Conda prefer you install packages upon creating environments to avoid conflicts. Install ipykernel

    “TIP: Install all the programs that you want in this environment at the same time. Installing 1 program at a time can lead to dependency conflicts.”

    To use a conda environment as a kernel, don’t forget to install ipykernel in this environment or it won’t show up in the kernel list.

  3. Activate the environment conda activate mxnet_conda_env and install mxnet inside. (mxnet_conda_env) $ pip install mxnet --pre (Some sources said that you need to install pip into the virtual environment and use that pip to correctly install package into the environment. But in my experience, pip correctly detected my active environment and install package into the correct env site-package path.)
  4. That’s it. You can start playing with your neuron network in Jupyter now.

Installing MXNet with Conda and Jupyter notebook on macOS 10.13 (High Sierra):

If you follow the previous guideline with pip install on High Sierra, you won’t hit any error during installation. But I got an illegal instruction: 4 error when I try to import mxnet.

The “illegal instruction” message is simply telling you that your binaries contain instructions the version of the OS that you are attempting to run them under does not understand.

To solve this problem, we have to build the binary from source (if not already installed, download and install Xcode, or install it from the App Store for macOS):

  1. Create a Conda environment following step 1 and 2 from previous section and activate it.
  2. Download the bash script for building MXNet from source:
    (mxnet_conda_env) $ curl -O https://raw.githubusercontent.com/dmlc/mxnet/master/setup-utils/install-mxnet-osx-python.sh
  3. Make the script executable:
    (mxnet_conda_env) $ chmod 744 install-mxnet-osx-python.sh
  4. !!Important: I don’t know what this step will do to your machine. Do it at your own risk. I am not liable to You for damages, including any direct, indirect, special, incidental, or consequential damages!! Use your favorite text editor to open the install-mxnet-osx-python.sh file. Go to near the bottom of the file, you will see:
    main() {
    print_intro_msg
    chk_mac_vers
    ...

    Comment out the “chk_mac_vers” line (line 544 in my case) by adding “#” in front of it so the build script skips checking your os version (original author makes it only build under macOS 10.11 or 10.12):

    main() {
    print_intro_msg
    # chk_mac_vers
    ...
  5. Run the script inside the virtual environment:
    (mxnet_conda_env) $ bash install-mxnet-osx-python.sh
  6. The MXNet test in the end of the script will fail because around line 513 of install-mxnet-osx-python.sh missed one “=” sign in the comparison statement. ($? = 0 should be $? == 0). But you will find MXNet actually works in this Conda environment and in Jupyter notebook.

Enjoy!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.