avatarNikos Kafritsas

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3230

Abstract

lass="hljs-keyword">install</span> tensorflow-metal</pre></div><p id="71ed">Optionally, install the Jupyter notebook or lab:</p><div id="5d3f"><pre>$ conda <span class="hljs-keyword">install </span>-c conda-forge <span class="hljs-keyword">jupyter </span><span class="hljs-keyword">jupyterlab</span></pre></div><p id="1625">To make sure everything works, and the TensorFlow installation actually uses the GPU, open the Python prompt and execute:</p><div id="aca0"><pre>import tensorflow <span class="hljs-keyword">as</span> <span class="hljs-keyword">tf</span> <span class="hljs-keyword">tf</span>.version <span class="hljs-keyword">tf</span>.config.list_physical_devices()</pre></div><figure id="629b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*uw2U-HnKYJABFkeqwK4XCg.png"><figcaption></figcaption></figure><p id="aae1">If you see the GPU in the list of devices, it means it is accessible by Tensorflow.</p><p id="8080">Next, run a simple MNIST example to make sure everything works as expected:</p><div id="5d79"><pre><span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf <span class="hljs-title">mnist</span> = tf.keras.datasets.mnist</pre></div><div id="c7c0"><pre>(x<span class="hljs-emphasis">train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x</span>test / 255.0</pre></div><div id="ac58"><pre>model = <span class="hljs-keyword">tf</span>.keras.models.Sequential([ <span class="hljs-keyword">tf</span>.keras.layers.Flatten(input_shape=(<span class="hljs-number">28</span>, <span class="hljs-number">28</span>)), <span class="hljs-keyword">tf</span>.keras.layers.Dense(<span class="hljs-number">128</span>, activation=’relu’), <span class="hljs-keyword">tf</span>.keras.layers.Dropout(<span class="hljs-number">0.2</span>), <span class="hljs-keyword">tf</span>.keras.layers.Dense(<span class="hljs-number">10</span>) ])</pre></div><div id="6db3"><pre><span class="hljs-attr">loss_fn</span> = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=<span class="hljs-literal">True</span>)</pre></div><div id="5185"><pre>model.compile(<span class="hljs-attribute">optimizer</span>=’adam’, <span class="hljs-attribute">loss</span>=loss_fn, metrics=[‘accuracy’])</pre></div><div id="eae0"><pre>model.fit(x_train, y_train, <span class="hljs-attribute">epochs</span>=10)</pre></div><h1 id="cf07">Addressing a Few common errors</h1><h2 id="e512">1. Incorrect python path</h2><p id="7878">It is imperative to make sure that you have the correct python path regarding the MiniForge environment.</p><p id="5862">If not, you may come across this message:</p><div id="4911"><pre>Collecting <span class="hljs-keyword">package</span> <span class="hljs-title">metadata (repodata.json): </span> done Solving environment: </pre></div><div id="424c"><pre><span class="hljs-attribute">failed ResolvePackageNotFound</span><span class="hljs-punctuation">: </span> — tensorflow-deps</pre></div><p id="365c">This error will also occur if you initialize a new <code>conda</code> environment without specifying a python version, because the latest available versions (e.g. 3.9.7) may not have this package available.</p><p id="22

Options

72">The safest option is to use 3.9.0 (which was aforementioned above).</p><h2 id="b3ea">2. Error building grpcio when installing tensorflow-deps</h2><p id="9cb3">Most probably, you will encounter this error. To fix it simply install:</p><div id="5079"><pre><span class="hljs-symbol"></span> pip install tensorflow-macos --<span class="hljs-keyword">no</span>-dependencies</pre></div><h2 id="b51d">3. Numpy not recognized</h2><p id="e7b6">Installing <code>tensorflow-deps</code> sometimes also installs a non-conda-forge NumPy version which does not work properly. In some cases, when trying to import this NumPy version, you may see the following message:</p><div id="8d82"><pre><span class="hljs-symbol">IMPORTANT: </span>PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!</pre></div><div id="5402"><pre><span class="hljs-attribute">Importing</span> the numpy C-extensions failed. This <span class="hljs-literal">error</span> can happen for</pre></div><div id="c7ca"><pre><span class="hljs-attribute">many reasons</span></pre></div><p id="6f13">To fix this, install the <code>openblas</code> package:</p><div id="e167"><pre> conda <span class="hljs-keyword">install</span> -c conda-forge openblas</pre></div><h2 id="6abd">4. Kernel dies when fitting the model</h2><p id="b49e">This is the most common error, which occurs when training the model.</p><p id="c356">Specifically, the execution crashes, and an <code>NSInvalidArgumentException</code><i> </i>is thrown. Under the hood, TensorFlow uses <code>tensorflow-metal</code> which was built using <code>MPSGraph</code> inference enhancements capabilities for the GPU.</p><p id="2cdb">To fix this, use the previous version Tensorflow, as well as the previous version of <code>tensorflow_metal</code>. For instance, if the current version of Tensorflow is 2.6 and <code><i>tensorflow-metal</i></code> is 0.2, try:</p><div id="9f71"><pre>conda create <span class="hljs-comment">--name tensorflow_m1 python==3.9</span> conda <span class="hljs-built_in">activate</span> tensorflow_m1</pre></div><div id="c230"><pre>conda install -c apple <span class="hljs-attribute">tensorflow-deps</span>==2.5.0 pip install <span class="hljs-attribute">tensorflow-macos</span>==2.5.0 pip install <span class="hljs-attribute">tensorflow-macos</span>==2.5.0 --no-dependencies pip install <span class="hljs-attribute">tensorflow-metal</span>==0.1.2</pre></div><h1 id="b03e">Bonus Tips</h1><ol><li>In order to enforce consistency and interoperability, it is important to download packages using only the <code>conda-forge</code> channel, even if the package is located in the default channel as well.</li><li>Not all libraries can be found in MiniForge. Thus, it is a good practice to keep a separate Anaconda or Miniconda environment for corner cases.</li></ol><h1 id="e42a">Closing Remarks</h1><p id="3fac">As a Mac user, having the ability to use one of the most popular deep learning frameworks at its full capacity is of paramount importance.</p><p id="61c5">Traditionally, they resort to third-party tools (e.g. Colab) which is not always desirable. I hope this guide is found beneficial for many Mac developers and helps them save time during the process.</p></article></body>

Installing Tensorflow on Apple M1 With the New Metal Plugin

How to enable GPU acceleration on Mac M1 and achieve a smooth installation

Photo by Wesson Wang on Unsplash

Since Apple abandoned Nvidia support, the advent of the M1 chip sparked new hope in the ML community. The chip uses Apple Neural Engine, a component that allows Mac to perform machine learning tasks blazingly fast and without thermal issues.

When Apple with M1 was released, the integration with Tensorflow was very difficult. The process involved downloading, among other packages, a pre-configured environment.yml file with specific dependencies in such a way that no dependency conflicts will arise.

Unfortunately, that was not always the case. This article discusses how to install Tensorflow on Miniforge by using the Metal plugin, a process that is more straightforward and less prone to errors.

Step 1: Install Xcode

The first component to install is Xcode, which can easily be downloaded from the App Store. Additionally, install the Command Line Tools:

$ xcode-select --install

Step 2: Install MiniForge

MiniForge is a minimalistic conda installer which uses by default the conda-forge channel and supports, among others, the aarch64 architecture (including Apple M1).

In other words, it is Apple-friendly. To download it, simply go to this page and download the installer for Apple Silicon. Installation is easy:

$ bash Miniforge3-MacOSX-arm64.sh

If you already have a pre-existing conda distribution, e.g. Anaconda or MiniConda, there is no need to uninstall it in order to use MiniForge. For those familiar with the conda ecosystem, only one conda distro can be “functional” at a given time. Check this article to learn how to manage effectively many conda distributions simultaneously!

Step 3: Setup Environment and Install Tensorflow base and tensorflow-metal plugin

Let’s create a new environment, called tensorflow_m1:

$ conda create --name tensorflow_m1 python==3.9
$ conda activate tensorflow_m1

Then, install the following:

$ conda install -c apple tensorflow-deps
$ pip install tensorflow-macos
$ pip install tensorflow-metal

Optionally, install the Jupyter notebook or lab:

$ conda install -c conda-forge jupyter jupyterlab

To make sure everything works, and the TensorFlow installation actually uses the GPU, open the Python prompt and execute:

import tensorflow as tf
tf.__version__
tf.config.list_physical_devices()

If you see the GPU in the list of devices, it means it is accessible by Tensorflow.

Next, run a simple MNIST example to make sure everything works as expected:

import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
 tf.keras.layers.Flatten(input_shape=(28, 28)),
 tf.keras.layers.Dense(128, activation=’relu’),
 tf.keras.layers.Dropout(0.2),
 tf.keras.layers.Dense(10)
])
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer=’adam’,
 loss=loss_fn,
 metrics=[‘accuracy’])
model.fit(x_train, y_train, epochs=10)

Addressing a Few common errors

1. Incorrect python path

It is imperative to make sure that you have the correct python path regarding the MiniForge environment.

If not, you may come across this message:

Collecting package metadata (repodata.json): 
done Solving environment: 
failed ResolvePackageNotFound: 
      — tensorflow-deps

This error will also occur if you initialize a new conda environment without specifying a python version, because the latest available versions (e.g. 3.9.7) may not have this package available.

The safest option is to use 3.9.0 (which was aforementioned above).

2. Error building grpcio when installing tensorflow-deps

Most probably, you will encounter this error. To fix it simply install:

$ pip install tensorflow-macos --no-dependencies

3. Numpy not recognized

Installing tensorflow-deps sometimes also installs a non-conda-forge NumPy version which does not work properly. In some cases, when trying to import this NumPy version, you may see the following message:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons

To fix this, install the openblas package:

$ conda install -c conda-forge openblas

4. Kernel dies when fitting the model

This is the most common error, which occurs when training the model.

Specifically, the execution crashes, and an NSInvalidArgumentException is thrown. Under the hood, TensorFlow uses tensorflow-metal which was built using MPSGraph inference enhancements capabilities for the GPU.

To fix this, use the previous version Tensorflow, as well as the previous version of tensorflow_metal. For instance, if the current version of Tensorflow is 2.6 and tensorflow-metal is 0.2, try:

$ conda create --name tensorflow_m1 python==3.9
$ conda activate tensorflow_m1
$ conda install -c apple tensorflow-deps==2.5.0
$ pip install tensorflow-macos==2.5.0
$ pip install tensorflow-macos==2.5.0 --no-dependencies
$ pip install tensorflow-metal==0.1.2

Bonus Tips

  1. In order to enforce consistency and interoperability, it is important to download packages using only the conda-forge channel, even if the package is located in the default channel as well.
  2. Not all libraries can be found in MiniForge. Thus, it is a good practice to keep a separate Anaconda or Miniconda environment for corner cases.

Closing Remarks

As a Mac user, having the ability to use one of the most popular deep learning frameworks at its full capacity is of paramount importance.

Traditionally, they resort to third-party tools (e.g. Colab) which is not always desirable. I hope this guide is found beneficial for many Mac developers and helps them save time during the process.

Data Science
TensorFlow
Python
Machine Learning
iOS
Recommended from ReadMedium