Fix/Install Tensorflow with metal plugin on M1/M2 mac (NotFoundError: Graph execution error:)

Yining Zhang
4 min readMar 2, 2023

--

TLDR: (Skip to step 5 if you have already installed metal and tensorflow) You have to downgrade tensorflow-macos and tensorflow-metal to 2.9 and 0.5.0 respectively for them to be usable. This is what most guides do not cover.

Successfully running… ‘Plugin optimizer for device_type GPU is enabled’

After a morning of struggle and diving through multiple forums, I was finally able to install and actually train an ANN model using Tensorflow and its metal plugins. Here is a simple installation guide that my past self would have needed at 9.30am.

Error

This should be the most common error that you will encounter once you have installed tensorflow-macos and tensorflow-metal.

NotFoundError: Graph execution error:

tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id:

1. Install miniconda/anaconda + xcode-select

If you have installed anaconda/miniconda previously, uninstall them just to make sure everything go smoothly (optional).

Install miniconda (recommended) or Anaconda.

Note: Choose the ARM64 version for M1/M2!!! (x86 will run with Rosetta but everything else will not work)

Use the pkg installation to be safe

Once that is complete, install xcode-select if your machine have yet to do so. (you will most likely have already installed this)

xcode-select --install

2. Check for base env

Once installation is complete, go to your terminal. You should see a ‘base py’ label (this shows that you are currently in the base Conda environment)

your terminal may look different, ‘base py’ may appear on the left

If you do not see a base py:

This means that you are still in your default environment, you will have to activate Conda by running:

conda activate

3. Creating YML file for new Tensorflow Environment

  • I will be creating the YAML file in my Desktop, feel free to do it in any other directories.
cd Desktop
touch tensorflow-apple-metal.yml
  • Copy and paste the following content into the yml you created.
name: tensorflow
channels:
- apple
- conda-forge
dependencies:
- python=3.10
- pip>=19.0
- jupyter
- scikit-learn
- scipy
- pandas
- pandas-datareader
- matplotlib
- pillow
- tqdm
- requests
- h5py
- pyyaml
- flask
- boto3
- ipykernel
- pip:
- tensorflow-macos
- tensorflow-metal
- bayesian-optimization
- gym
- kaggle

Source: Jeff Heaton. (GOAT) This informs Conda of the necessary dependencies needed for this new environment (eg. Jupyter, NumPy, Pandas).

4. Creating the new Tensorflow environment

  • cd to the directory where your YAML file is located
  • Deactivate your conda environment.
conda deactivate
  • Create the Tensorflow environment with your YAML file. (This will take a while as it is installing all the necessary dependencies into your environment)
conda env create -f tensorflow-apple-metal.yml -n tensorflow
  • Once done, activate the environment and register it
conda activate Tensorflow

python -m ipykernel install --user --name tensorflow --display-name "Python 3.10 (tensorflow)"

5. (IMPORTANT) Reinstalling the correct version of tensorflow-macos and tensorflow-metal

This is the most important step, or else you will run into some of the errors as stated below.

In order to use the metal plugin with Tensorflow, you will have to DOWNGRADE to:

tensorflow-macos 2.9

tensorflow-metal 0.5.0

https://developer.apple.com/metal/tensorflow-plugin/

Within your Tensorflow environment, you can check the current version of your tensorflow and metal.

pip show tensorflow-macos

pip show tensorflow-metal

Now, install the compatible versions. (as of March 2023, this is the latest compatible versions)

pip install --upgrade tensorflow-macos==2.9

pip install --upgrade tensorflow-metal==0.5.0

Enjoy!

Although it seems like GPU compute is slower than CPU…

Edit: Increasing your batch size will significantly boost the speed. Feel free to try out 100/200/500/1000, just make sure it doesn’t cause overfitting

https://developer.apple.com/forums/thread/694562

Credit: https://www.youtube.com/watch?v=o4-bI_iZKPA&t=5s (All credits to Jeff Heaton, but the video did not mention the downgrading step which was necessary for the install)

--

--

Yining Zhang

Tossing my thoughts into a sea of ones and zeroes…