Home > Software design >  What is the proper way to install TensorFlow on Apple M1 in 2022
What is the proper way to install TensorFlow on Apple M1 in 2022

Time:07-14

I am facing 4 problems when I tried to install TensorFlow on Apple M1:

  1. Conda has supported M1 since 2022.05.06 but most of articles I googled talk about using Miniforge, e.g. So I feel they are all kind of outdated.

    1. How To Install TensorFlow on M1 Mac (The Easy Way)
    2. AI - Apple Silicon Mac M1 natively supports TensorFlow 2.8 GPU acceleration
    3. How to Setup TensorFlow on Apple M1 Pro and M1 Max (works for M1 too)
    4. How To Install TensorFlow 2.7 on MacBook Pro M1 Pro With Ease
  2. I used the latest conda 4.13 to setup my python environment(3.8, 3.9 and 3.10) successfully but when I tried to install tensorflow I got the error "No matching distribution found for tensorflow" (all failed).

    ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)
    ERROR: No matching distribution found for tensorflow 
    

The answers in Could not find a version that satisfies the requirement tensorflow didn't help. I can't find useful information on https://www.tensorflow.org/ too, actually https://www.tensorflow.org/install just said pip install tensorflow.

  1. I tried to run pip install tensorflow-macos and it succeeded. I read from the above "works for M1 too" article mentioned "Apple's fork of TensorFlow is called tensorflow-macos" although I can't find much information about that. For example, https://www.tensorflow.org/ does not mention that. I also found from https://developer.apple.com/forums/thread/686926 that someone hit that "ERROR: No matching distribution found for tensorflow-macos" (but I didn't).

  2. All the articles I googled, including above 4 articles and this Tensorflow on macOS Apple M1, all say I also need to run the following 2 commands

    conda install -c apple tensorflow-deps

    pip install tensorflow-metal

But do I really need to that? I can't find this information from https://www.tensorflow.org/. What are these 2 packages tensorflow-deps and tensorflow-metal ?

CodePudding user response:

First of all, TensorFlow does not support officially the Mac M1. They don't distribute packages precompiled for the Mac M1 (and its specific arm64 arch), hence the tensorflow-macos package, which is maintained by Apple. TensorFlow distributes, as far as I know, official wheels only for x86 (Linux, Windows, Mac), and the Raspberry PI (arm64).

Apple is using a specific plugin in Tensorflow to make the framework compatible with Metal, the graphic stack of MacOS. To put it in a other way, they are leveraging the PluggableDevice API of Tensorflow to write code that translates the TensorFlow operations to code that the GPU of the M1 understands.

Those two packages contain respectively:

  • tensorflow-deps the dependencies to run Tensorflow on arm64, i.e python, numpy, grpcio and h5py. This is more of a convenience package, I believe.
  • tensorflow-metal: a plugin to make tensorflow able to run on metal, the shader API of MacOS (comparable to the low level APIs of Vulkan or DirectX12 on other platforms). You can think of it as a replacement of CUDA, if you are used to run TensorFlow on Nvidia GPUs.

Without the tensorflow-metal package, TensorFlow won't be able to leverage the GPU of the M1, but will still be able to run code on the CPU.

CodePudding user response:

Distilling the official directions from Apple (as of 13 July 2022), one would create an environment using the following YAML:

tf-metal-arm64.yaml

name: tf-metal
channels:
  - apple
  - conda-forge
dependencies:
  - python=3.9  ## specify desired version
  - pip
  - tensorflow-deps

  ## uncomment for use with Jupyter
  ## - ipykernel

  ## PyPI packages
  - pip:
    - tensorflow-macos
    - tensorflow-metal  ## optional, but recommended

Edit to include additional packages.

Creating environment

Before creating the environment we need to know what the base architecture is. Check this with conda config --show subdir.

Native (osx-arm64) base

If you have installed a native osx-arm64 Miniforge variant (I recommend Mambaforge), then you can create with:

mamba env create -n my_tf_env -f tf-metal-arm64.yaml

Note: If you don't have Mamba, then substitute conda for mamba; or install it for much faster solving: conda install -n base mamba.

Emulated (osx-64) base

If you do not have a native base, then you will need to override the subdir setting:

## create env
CONDA_SUBDIR=osx-arm64 mamba env create -n my_tf_env -f tf-metal-arm64.yaml

## activate
mamba activate my_tf_env

## permanently set the subdir
conda config --env --set subdir osx-arm64

Be sure to always activate the environment before installing or updating packages.

CodePudding user response:

The two answers I got have helped better understand how to install TensorFlow on m1. But I would like share my experience too.

  1. About tensorflow-deps. I do need it, without it pip failed to installed grpcio, and thus actually failed to install tensorflow-macos. When I first asked the question I didn't pay enough attention to output of pip install tensorflow-macos.

  2. About tensorflow-macos package, actually https://blog.tensorflow.org/2020/11/accelerating-tensorflow-performance-on-mac.html has the full information. BTW that article, published 2020-11-18, said "In the near future, we’ll be making updates like this even easier for users to get these performance numbers by integrating the forked version into the TensorFlow master branch." But according to Lescurel's answer it seems they have not.

  3. I didn't know the concept of PluggableDevice (as in Lescurel's), so even when I visited https://github.com/apple/tensorflow_macos I was still confused. Take a look at that article if you do not know that either, basically it will let TensorFlow support new devices.

  4. For 4 articles I listed, "works for M1 too" is the most helpful. It actually explained why I need tensorflow-deps & tensorflow-metal. But part of reasons I did not pay enough attention beforehand were: a) I want to use conda, not miniforge, all these package manager tools scare me a little bit (come from nodejs background). The answer from merv also suggested another one mamba, but I think I will pass. b) I don't use homebrew, basically all the articles talking about installing ts on m1 mentioned installing homebrew first. But I use macport, for the reason I mentioned here (again I am bit scared of these these package manager tools)

  5. Using environment.yaml like the one in merv's answer is a reliable way to install tensorflow!

  • Related