TensorFlow 2.4 on Apple Silicon M1: installation under Conda environment

Install arm64 TensorFlow alpha and other ML packages

Photo by the author

The previous article was about the Machine Learning packages that works natively on Apple Silicon. I also explained how TensorFlow and scikit-learn can be installed on a Mac M1.

In this article ATF 2.4 stand for TensorFlow 2.4 for Apple Silicon currently available from github in release 0.1 alpha 1.

With ATF 2.4, standard installation requires creating a python environment while nearly no other package like scikit-learn can be installed from pip. This is making this environment quite useless for machine learning engineers except for small testing.

At the time of writing this article ATF 2.4 is not free of bugs. It cannot yet be used in a professional context. But it’s already possible to start working on personal Machine Learning projects with a Mac M1. Here I describe step by step how to install a full environment under Conda with every packages natively compiled for Apple Silicon:

  • ATF 2.4 (TensorFlow 2.4 for Apple Silicon)
  • numpy
  • scikit-learn
  • pandas
  • matplotlib
  • JupyterLab

Step 1: Xcode Command Line Tools

Install Xcode Command Line Tools by downloading it from Apple Developer or by typing:

xcode-select --install

Step 2: miniforge

Install miniforge for arm64 (Apple Silicon) from miniforge github.

Miniforge enables installing python packages natively compiled for Apple Silicon including scikit-learn.

Step 3: Download ATF 2.4

Download TensorFlow 2.4 from Apple github, untar it but don’t install it by using the provided script. Go under the arm64 directory:

cd tensorflow_macos/arm64

Step 4: create Conda environment

Don’t forget to open a new session or to source your .zshrc after miniforge install and before going through this step.

Create an empty Conda environment, then activate it and install python 3.8 (as required for ATF 2.4) and all the needed packages. Please note numpy is unnecessary here as pandas already install it, but it will be overwritten in the last step with the version provided by Apple.

conda create --name tf24
conda activate tf24
conda install -y python==3.8.6
conda install -y pandas matplotlib scikit-learn jupyterlab

Step 5: install all the ATF 2.4 packages

Now manually install ATF 2.4 packages exactly like install_venv.sh does but under your Conda environment.

Please note the following instruction corresponds to the second ATF 2.4 release, namely 0.1 alpha 1. Any new release can require a different process, you will be able to adapt it by checking install_venv.sh content.

# Install specific pip version and some other base packages
pip install --force pip==20.2.4 wheel setuptools cached-property six
# Install all the packages provided by Apple but TensorFlow
pip install --upgrade --no-dependencies --force numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl h5py-2.10.0-cp38-cp38-macosx_11_0_arm64.whl tensorflow_addons-0.11.2+mlcompute-cp38-cp38-macosx_11_0_arm64.whl
# Install additional packages
pip install absl-py astunparse flatbuffers gast google_pasta keras_preprocessing opt_einsum protobuf tensorflow_estimator termcolor typing_extensions wrapt wheel tensorboard typeguard
# Install TensorFlow
pip install --upgrade --force --no-dependencies tensorflow_macos-0.1a1-cp38-cp38-macosx_11_0_arm64.whl

Now you can run JupyterLab and start working.

Note that as pip installation can lead to inconsistencies with Conda packages previously installed, and especially because numpy is replaced by the 1.18.5 version shipped with ATF 2.4, there is no guarantee that it will work in every situation.

For now, I’ve successfully trained several MLP and Convnet models while LSTM still have an issue with the evaluation on test set. I also trained RandomForest models and plot confusion matrix everything from JupyterLab without any issue.

In the next article I will go through TensorFlow 2.4 benchmark on Mac M1.

Thank you for reading.


TensorFlow 2.4 on Apple Silicon M1 : installation under Conda environment was originally published in Towards Data Science on Medium, where people are continuing the conversation by highlighting and responding to this story.