Configuring TensorFlow GPU on MacOS M2

Configuring TensorFlow GPU on MacOS M2

This article mainly introduces the configuration of TensorFlow on MacOS 12.x, aiming to help beginner machine learning users on Mac avoid some detours.

In general, configuring the Mac version of TensorFlow only requires three steps: the first step is to set up a virtual environment, it is recommended to choose Miniconda; the second step is to create a conda environment; the third step is to install TensorFlow.

1. Install Miniconda

1.1 Completely Remove Anaconda

(1) Download anaconda-clean to remove related configuration files

##  Enter in the terminal: conda install anaconda-clean --yes

(2) Delete the folders where anaconda, miniconda, and miniforge are located

##  Enter in the terminal: rm -rf ~/anaconda3 rm -rf ~/opt/anaconda3 rm -rf ~/miniforge3 rm -rf ~/miniconda3 rm -rf .conda rm -rf .condarc

(3) Open .bash_profile, and delete the anaconda path

#  Open environment configuration with vim vim .bash_profile #  After running, find the following line of code and input dd before the code to delete #  export PATH="/Users/your_username/anaconda3/bin:$PATH"

1.2 InstallMiniconda

Go to the Miniconda official website to choose to download, with the default path.https://docs.conda.io/en/latest/miniconda.html

Configuring TensorFlow GPU on MacOS M2

2. Configure Environment

2.1 Create Virtual Environment

Create the environment in Miniconda3, stored in the env folder of Miniconda3

# Enter in the terminal: conda create -n ***** python=3.9 # ***** is the name you give to this configuration environment
2.2 Activate Virtual Environment
#  Enter in the terminal: conda activate ***** #  Then close the terminal and reopen it

2.3 Miniconda Related Operation Codes

Check the current environments: conda info -e Check conda version: conda -V Check all installed packages: conda list Create a new virtual environment: conda create -n **** python=3.9   (**** is the virtual environment name) Install package:  conda install ****      (**** is the package name) Switch virtual environment: conda activate ****   (**** is the virtual environment name) Exit environment: conda deactivate Delete a virtual environment: conda remove -n **** --all  (**** is the virtual environment name)
3. Configure TensorFlow-macos
3.1 Install Dependency Package tensorflow-deps
If this package is not installed, the subsequent installation of TensorFlow will fail
conda install -c apple tensorflow-deps
3.2 Configure TensorFlow and Jupyter Notebook
Note: This step and the subsequent packages are installed in the virtual environment ***** among
conda activate ***** ##  ***** is the name you give to this configuration environment ### 1. Install tensorflow-macos, specify version 2.9, the new version cannot temporarily call GPU python -m pip install tensorflow-macos==2.9.0 -i https://pypi.tuna.tsinghua.edu.cn/simple/ ##  If there is a download error, you need to first download torch, then re-download tensorflow-macos #  pip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple/ ### 2. Install the Apple official maintained tensorflow-metal python -m pip install tensorflow-metal==0.5.0 -i https://pypi.tuna.tsinghua.edu.cn/simple/ ## 3. Install tools like jupyter conda install jupyter jupyterlab pandas matplotlib numpy
4. Test TensorFlow-macos
4.1 Bugs of New Version Packages and Solutions
At first, I followed the Apple official website to install the latest packages, but later found that GPU acceleration could not be called and kept reporting errors, but when using the CPU, the program ran normally, indicating that the problem lies in the packages:

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

Finally, I found the answer on Apple’s feedback platform:Reinstall the old version of the package

tensorflow-macos==2.9 ;

tensorflow-metal==0.5.0 ;

Configuring TensorFlow GPU on MacOS M2

4.2 Run TensorFlow-macos
So far, the configuration of TensorFlow has been completed, let’s test the running situation:
import numpy as np import tensorflow as tf x = np.random.random((10000, 5)) y = np.random.random((10000, 2)) x2 = np.random.random((2000, 5)) y2 = np.random.random((2000, 2)) inp = tf.keras.layers.Input(shape = (5,)) l1 = tf.keras.layers.Dense(256, activation = 'sigmoid')(inp) l1 = tf.keras.layers.Dense(256, activation = 'sigmoid')(l1) l1 = tf.keras.layers.Dense(256, activation = 'sigmoid')(l1) l1 = tf.keras.layers.Dense(256, activation = 'sigmoid')(l1) l1 = tf.keras.layers.Dense(256, activation = 'sigmoid')(l1) o = tf.keras.layers.Dense(2, activation = 'sigmoid')(l1) model = tf.keras.models.Model(inputs = [inp], outputs = [o]) model.compile(optimizer = "Adam", loss = "mse")
Successfully run, wish you a smooth journey in configuring TensorFlow

Configuring TensorFlow GPU on MacOS M2

Configuring TensorFlow GPU on MacOS M2

References
[1] Apple Official – TensorFlow Installation Tutorial
https://developer.apple.com/metal/tensorflow-plugin/
[2] Apple Developer Forums – tensorflow-metal
https://developer.apple.com/forums/tags/tensorflow-metal
[3] 【Guest Contribution】 How to Quickly Install TensorFlow on MAC M1 Chip?
https://mp.weixin.qq.com/s/EBnX-h4UqGV9KBJJ43f2tA

Every complicated problem must have its intricate solution

This chapter ends, I wish to rest, if you feel this article is great
[Like], [Look] shared on both sides with friends!

Leave a Comment