The Mac has long been a popular platform for developers, engineers, and researchers. Apple’s Mac-optimized version of TensorFlow 2.5+ leverages the full power of the Mac with a huge jump in performance.

M1/M2 chip contains a powerful new 8-Core CPU and up to 8-core GPU that is optimized for ML training tasks right on the Mac. In the graphs below, you can see how Mac-optimized TensorFlow 2.4 can deliver huge performance increases on both M1- and Intel-powered Macs with popular models.

Apple M1/M2 GPU Performance

Requirements

  • Mac computers with Apple silicon or AMD GPUs
  • macOS 12.3 or later
  • Python 3.7 or later

Install Miniconda on Apple Metal M1/M2

The latest Anaconda distribution support Apple M1/M2’s ARM64 architecture, in addition to existing supported architectures. All packages included in the installer have been compiled to support ARM64, meaning M1/M2 users can now enjoy the computing efficiency of M1/M2s and speed up workflows by 20%.

Miniconda is the recommended approach for installing TensorFlow with GPU support. It creates a separate environment to avoid changing any installed software in your system. This is also the easiest way to install the required software, especially for the GPU setup.

arm64 : Apple silicon

curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
sh Miniconda3-latest-MacOSX-arm64.sh

x86

curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
sh Miniconda3-latest-MacOSX-x86_64.sh

If you’re using zsh and it has not been set up to read .bashrc, you need to add the Miniconda directory to the zsh shell path environment variable. Add this to your .zshrc:

export PATH="/home/username/miniconda/bin:$PATH"

Make sure to replace /home/username/miniconda with your actual path. Save, exit the terminal, and then reopen the terminal. conda command should work.

You may need to restart your terminal or source ~/.bashrc to enable the conda command. Use conda -V to test if it is installed successfully.

Create a Conda Environment

Create a new conda environment named tfEnv with the following command.

conda create --name tfEnv python=3.10

You can deactivate and activate it with the following commands.

conda deactivate
conda activate tfEnv

Make sure it is activated for the rest of the installation.

Install base TensorFlow

TensorFlow users on Intel Macs or Macs powered by Apple’s new M1/M2 chip can now take advantage of accelerated training using Apple’s Mac-optimized version of TensorFlow 2.4 and the new ML Compute framework.


TensorFlow requires a recent version of pip, so upgrade your pip installation to be sure you’re running the latest version

pip install --upgrade pip

This guide is for the latest stable version of TensorFlow. Refer to these tables for older TensorFlow version requirements. For the CPU-only build use the pip package named tensorflow-cpu.

python -m pip install tensorflow-macos

Install TensorFlow Metal Plug-in

As the number of accelerators (GPUs, TPUs) in the ML ecosystem has exploded, there has been a strong need for seamless integration of new accelerators with TensorFlow. TensorFlow introduces the PluggableDevice architecture which offers a plugin mechanism for registering devices with TensorFlow without the need to make changes in TensorFlow code.

This PluggableDevice architecture has been designed & developed collaboratively within the TensorFlow community. It leverages the work done for Modular TensorFlow, and is built using the StreamExecutor C API. The PluggableDevice mechanism is available in TF 2.5.

Register Conda Environment

The following command registers your TensorFlow environment. Again, make sure you activate your new TensorFlow environment.

conda install -c anaconda ipykernel
python -m ipykernel install --user --name=tfEnv

conda environment registered as a Jupyter Kernal If you don’t do this it won’t show up on your list of available Kernels in Jupyter Notebook.

Verify Metal Instalation

Users do not need to make any changes to their existing TensorFlow scripts to use ML Compute as a backend for TensorFlow and TensorFlow Addons.

Verufy Apple M1/M2 GPU TensorFlow

Related Post

Install PyTorch 2.0 GPU/MPS for Mac M1/M2 with Conda