TensorFlow 2.4 on Apple Silicon M1: installation under Conda environment | by Fabrice Daniel | Towards Data Science

Updated Instructions for TensorFlow 2.4 alpha 3. See details at the end of the article.
—
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 3.
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
Install Xcode Command Line Tools by downloading it from Apple Developer or by typing:
xcode-select --install
Install miniforge for arm64 (Apple Silicon) from miniforge github.
Miniforge enables installing python packages natively compiled for Apple Silicon including scikit-learn.
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
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 tf24conda activate tf24conda install -y python==3.8.6conda install -y pandas matplotlib scikit-learn jupyterlab
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 3. 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 packagespip install --force pip==20.2.4 wheel setuptools cached-property six packaging# Install all the packages provided by Apple but TensorFlowpip 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# Install additional packagespip install absl-py astunparse flatbuffers gast google_pasta keras_preprocessing opt_einsum protobuf tensorflow_estimator termcolor typing_extensions wrapt wheel tensorboard typeguard# Install TensorFlowpip install --upgrade --force --no-dependencies tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whlpip install --upgrade --force --no-dependencies tensorflow_addons_macos-0.1a3-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.
—
This article is updated with instructions for TensorFlow 2.4 alpha 3. Previously, I mentioned that initial instructions were only for alpha 1 release and that “Any new release can require a different process, you will be able to adapt it by checking install_venv.sh content.”.
So you can use these instructions for installing TF 2.4 Alpha 3. Alternatively you can also use this process proposed on ATF GitHub.
Some people reported errors like
ERROR: numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl is not a supported wheel on this platform
Apple does not propose an install that works everywhere in any situation. It’s an Alpha version. It means the instructions and prerequisites must be carefully followed. You must be on a mac M1, not Intel. Not running a terminal with rosetta2, only keep it native arm64 in any situation. You must install miniforge not anaconda and you must install the exact python version required. And of course if the release increases to alpha 4, you will need to adapt the instructions by yourself (by reading install_venv.sh). If you still have any trouble or want to do any custom thing, please directly ask your questions here to Apple.