Tensorflow with Apple Silicon

My MacBook Pro 2021 has an M1 Pro chip in it. Before trying this out, I have been using original Tensorflow, which I installed from arm64 Miniforge, on CPU only. It runs okay, has correct hardware instruction, and is fast enough (in my understanding before I really unlocked the GPU acceleration on Apple Silicon).

Since original Tensorflow gives me the error "no CPU frequency: 0 Hz" all the time, I decide to find out what goes wrong. And suddenly, I find out this article.

I have seen this before, but I did not try it because it felt like an out-dated version. After many searches, I finally realize that it may provide some additional features other than original Tensorflow, and it is definitely not out-dated (as it is keeping updating).

By following the article above, I then realize a new problem: I cannot install it on an existing conda environment as it always report package inconsistency while installing. So there are a few additional steps I took to make sure that tensorflow-macos runs perfectly on my M1 Pro machine.

Follow the guide, step by step

Before trying any other advice, you should follow the guide (article above) step by step, carefully. If you did anything wrong or you got any error, you should try it again from the beginning and do not forget to add --force-reinstall flag when needed.

Uninstall existing Tensorflow

Yes, you are right. tensorflow cannot exist with tensorflow-macos at the same time (at least based on my observation), which means that you have to uninstall old tensorflow (and its dependencies) before trying to install anything like tensorflow-macos . There will be potential conflicts if you reject to do so.

Check out the peer version

Besides that, your old tensorboard or other peer Tensorflow dependencies may be the wrong one (wrong version) with your new macOS-specific Tensorflow, so I recommend you to uninstall them altogether, or create a clean environment for tensorflow-macos .

For example, in my case, a 2.9.1 tensorboard does not work with 2.9.0 tensorflow-macos , but 2.9.0 tensorboard does.

Verify it works

In order to make sure that your tensorflow-macos works properly, you do not have to run a real program at first. You can simply use a terminal python to print out the version of Tensorflow and Keras.

$ python > import tensorflow as tf > tf.__version__ > > from tensorflow import keras > keras.__version__

If this does not work for you, then your Tensorflow is definitely not working properly. You should install dependencies if the error is about a missing package; or try to re-install the entire tensorflow-macos if it gives some other weird error.

Uninstall deps before trying again

If you have installed tensorflow-macos and tensorflow-metal , you should uninstall it before trying again. This is important.

Use correct package manager

You should install tensorflow-deps by using conda:

conda install -c apple tensorflow-deps

And you should install tensorflow-macos and tensorflow-metal through pip because conda does not have them.

pip install tensorflow-macos pip install tensorflow-metal

Miniforge is the best

If you are using Anaconda, then I highly recommend you to switch to Miniforge because Anaconda is running on Rosetta, which means that it will never have native hardware instruction and native performance. Go download and install arm64 Miniforge. (You can do it with Homebrew)

Common issues

  1. Cannot find tensorflow when executing $ conda install tensorflow . This is because you have installed tensorflow-macos , which is incompatible with original tensorflow .
  2. Package inconsistency. You should use $ pip list or $ conda list command to check the version of your packages. If you are trying to install tensorflow-macos but there is an existing tensorflow on your environment, then you will notice these errors. Some peer dependencies could also cause this issue.
  3. No module named 'xxx' while importing Tensorflow. Just install it through conda or pip. You can easily find their real package names online. Your package manager may "forget" to install it for some reason.

Performance?

When I first switched to GPU mode, I realize that the performance is surprisingly worse than using CPU. Haha, it really feels weird. This is simply because the batch size is too small (e.g. around 64 or 128). In these scenarios, GPU is not necessary and may cause some overhead. Change your batch size to 1024 or 2048 (actually depending on your needs and your models – these values are not definite) will easily resolve the issue and you can feel how fast it is now. And if you feel like the training is freeze, change your batch size to the low (like 8 or 16) to see if it actually runs – if it runs, then it means that your GPU or memory might not be enough for a large batch size.

If you also encounter these issues, I feel totally understand as everyone has their first time using GPU for ML.

I hope this helps~

July 20, 2022