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: https://developer.apple.com/metal/tensorflow-plugin/
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.
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.
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.
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
For example, in my case, a 2.9.1
tensorboard does not work with 2.9.0
tensorflow-macos , but 2.9.0
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.
If you have installed
tensorflow-metal , you should uninstall it before trying again. This is important.
You should install
tensorflow-deps by using conda:
conda install -c apple tensorflow-deps
And you should install
tensorflow-metal through pip because conda does not have them.
pip install tensorflow-macos pip install tensorflow-metal
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)
$ conda install tensorflow. This is because you have installed
tensorflow-macos, which is incompatible with original
$ pip listor
$ conda listcommand to check the version of your packages. If you are trying to install
tensorflow-macosbut there is an existing
tensorflowon your environment, then you will notice these errors. Some peer dependencies could also cause this issue.
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~