Enabling the use of the GPU on your Mac M1 with the tensorflow-metal plugin can be challenging because there is a lot of conflicting documentation and older forum questions and replies.
I’ve written this article for a Mac M1 running on macOS Sequoia 15.1.1. As of December 2024, you should pair Python 3.11 with TensorFlow 2.15.1 and TensorFlow metal 1.1.0. The libraries are not available if trying to use Python 3.12.
Prerequisites
- Mac M1 with Apple Silicon
- xCode command line tools (xcode-select –install)
Install brew package manager
Install brew from the terminal per the official brew instructions.
# install brew /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" # add brew environment variables to current shell eval "$(/opt/homebrew/bin/brew shellenv)"
Install pyenv
Instead of using brew to directly install Python, use brew to install pyenv. This will provide more flexibility in managing multiple Python versions from the same machine.
brew install pyenv pyenv-virtualenv
Configure zsh settings for pyenv
To persist the proper shell settings, add the following lines to ~/.zprofile
# brew variables and PATH eval "$(/opt/homebrew/bin/brew shellenv)" command -v brew >/dev/null 2>&1 || export PATH=/opt/homebrew/bin:$PATH # for pyenv and pyenv-virtualenv command -v pyenv >/dev/null 2>&1 || export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
reload terminal with new settings.
exec "$SHELL"
Install specific Python version with pyenv
As of the writing of this article, you need to use Python 3.11 (not 3.12) in order to get TensorFlow Python pip modules.
pyenv install --list | grep 3\.11 pyenv install 3.11.10 # create virtual environment for 3.11 pyenv virtualenv 3.11.10 py3.11.10 pyenv virtualenvs
TensorFlow GPU smoke test
Now create a directory specifically for the Python 3.11 environment, install specific versions of the TensorFlow libraries, and then run a simple test for GPU detection.
# create directory that uses 3.11 virtual environment mkdir tflow-macm1-py311 && cd $_ pyenv local py3.11.10 cat .python-version # validate that python version is 3.11 python --version python -m pip install --upgrade pip # install exact versions of Tensorflow modules (newer versions will not work) pip install tensorflow==2.15.1 tensorflow-macos==2.15.1 tensorflow-metal==1.1.0 # show modules that are installed pip list | grep tensorflow-m tensorflow-macos 2.15.1 tensorflow-metal 1.1.0 # should list both CPU and GPU !!! # if only CPU is output, there is a problem % python -c "import tensorflow as tf; print(tf.config.list_physical_devices())" [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
TensorFlow example exercising the GPU
As a final test, run a very simple Python script that uses the TensorFlow metal libraries to exercise the GPU. The official Apple Tensorflow documentation page has a simple Python program we can use.
# get simple example of exercising Tensorflow library wget https://raw.githubusercontent.com/fabianlee/blogcode/refs/heads/master/tensorflow/macm1/tensorflow-sample.py3 # run sample, should say 'Plugin optimizer for device_type GPU is enabled' python tensorflow-sample.py3
Validate GPU utilization
The sample program above will take several minutes to complete, pulling up the Mac OS “Activity Monitor” application should show you high % GPU utilization of the Python process as shown below.
And pulling up the Window > GPU History will show a spike in GPU resources as the script is running.
If you wanted to see how long it takes to run this Tensorflow example without GPU utilization, you can uninstall just the metal library.
pip uninstall tensorflow-metal -y
Rerunning the sample Python program will now reflect estimated completion times without the GPU (only CPU), and will take ~3x the amount of time. The Activity Monitor will show very high CPU utilization, and no GPU utilization.
REFERENCES
Apple docs, Tensorflow Plugin setup
Bhanuka Manesha gist, setup Tensorflow project with GPU support
freecodecamp, installing xcode command line tools
Apple forum, installing xcode from CLI
Glen Yu, TensorFlow on M1 Pro using pyenv