9

I have installed tensorflow-macos and while training this is my CPU usage and GPU usage .

Can I make Tensorflow run on GPU anyway?

petezurich
  • 9,280
  • 9
  • 43
  • 57
hat
  • 115
  • 1
  • 1
  • 6
  • Here is a helpful thread: https://github.com/pytorch/pytorch/issues/47702#issuecomment-948858262 It does NOT concern Tensorflow but PyTorch, but still is informative to understand what to expect from M1's GPUs for deep learning, at this stage. – tyrex Nov 01 '21 at 20:01
  • This might help! https://stackoverflow.com/questions/70354859/anyway-to-work-with-tensorflow-in-mac-with-apple-silicon-m1-m1-pro-m1-max-gp – bikram Dec 15 '21 at 22:30
  • use tensorflow-macos with tensorflow-deps==2.5.0 FYI:https://developer.apple.com/metal/tensorflow-plugin/ – suiwenfeng Jan 11 '22 at 10:53

6 Answers6

11

I've been setting up my new M1 machine today and was looking for a test such as that provided by Aman Anand already here. It successfully runs on the GPU after following the standard instructions provided in #153 using a miniforge package manager installed using Homebrew and an environment cloned from the YAML file in the #153 guide.

Process running on GPU

I also ran the smaller simpler snippet as followed which only runs on CPU, '% GPU' == 0%:

import numpy as np
import tensorflow as tf

### Aman's code to enable the GPU
#from tensorflow.python.compiler.mlcompute import mlcompute
#tf.compat.v1.disable_eager_execution()
#mlcompute.set_mlc_device(device_name='gpu')
#print("is_apple_mlc_enabled %s" % mlcompute.is_apple_mlc_enabled())
#print("is_tf_compiled_with_apple_mlc %s" % #mlcompute.is_tf_compiled_with_apple_mlc())
#print(f"eagerly? {tf.executing_eagerly()}")
#print(tf.config.list_logical_devices())

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")

model.fit(x, y, validation_data = (x2, y2), batch_size = 500, epochs = 500)

Training not using GPU

Uncommenting the line's added from Aman's code and rerunning makes the GPU work again:

Training uses GPU again

If these scripts still don't use the GPU per the activity monitor (set the update rate to 1s in view/update_frequency), go back to the #153 page to start agin from a clean slate and follow the instructions carefully, and be sure to ignore the instructions meant for Intel/X86.

My steps:

  1. install xcode (from the app store)
  2. install Homebrew (don't forget to set the PATH as recommended just after installation finishes, terminal then needs restarting or your shell profile reloading)
  3. install miniforge ("brew install miniforge")
  4. copy environment.yaml file and clone as new conda environment with the command given in #153.
  5. profit.

UPDATE 2022-01-26:

The workflow to install tensorflow on apple silicon has become much easier in the last 6 months, it still relies on miniforge but the packages are distributed through conda and pip from a standard conda env rather than having to create one from the yaml file. These instructions are very easy to follow and should have you going in under 2 minutes. The only exception being that I had to run one additional command to install openblas afterwards through conda to make it work.

My test above breaks in tensorflow 2.7 because they changed something to do with the mlcompute location for m1 but go on to say that the mlcompute no longer needs to instruct the use of the GPU with the Metal plugin and the test works again by simply removing the references to mlcompute in lines 5-10, and runs on the GPU as can be seen in activity monitor.

G.S
  • 535
  • 3
  • 8
  • 1
    I noticed that currently I need just to `pip install tensorflow-macos` and `pip install tensorflow-metal` and it is enough to enable GPU on M1. However, the speed of learning is not impressive. I checked that GPU is under 100% load so it is definitely involved then. – chm Nov 27 '22 at 08:26
5

You can check available GPU devices by

import tensorflow as tf
tf.config.list_physical_devices()

Then run your model

with tf.device('/device:GPU:0'):
    model.fit(x_train, y_train)

See also https://www.tensorflow.org/api_docs/python/tf/device

tsc
  • 51
  • 1
  • 2
3

This issue has already been fixed with the release of TensorFlow-macos 2.5. The easiest way to utilize GPU for Tensorflow on Mac M1 is to create a new conda miniforge3 ARM64 environment and run the following 3 commands to install TensorFlow and its dependencies:

conda install -c apple tensorflow-deps
python -m pip install tensorflow-macos
python -m pip install tensorflow-metal

Further instructions are on this page: https://developer.apple.com/metal/tensorflow-plugin/

"Accelerate training of machine learning models with TensorFlow right on your Mac. Install TensorFlow v2.5 and the tensorflow-metal PluggableDevice to accelerate training with Metal on Mac GPUs."

Long Le
  • 301
  • 2
  • 2
2

You can, but it's a bit of a pain as of now, it appears. One solution is to use mini-forge. If you use conda you need to uninstall that first.

  1. Install Xcode and the Command Line Tools package.
  2. Install Miniforge to get conda.
  3. Install Apple's fork of TensorFlow from conda-forge in a conda environment and other required packages.

My answer is based on this helpful guide: https://medium.com/gft-engineering/macbook-m1-tensorflow-on-jupyter-notebooks-6171e1f48060

This issue on Apple's GitHub has more discussion: https://github.com/apple/tensorflow_macos/issues/153

jeffhale
  • 3,759
  • 7
  • 40
  • 56
  • 2
    I did install miniforge and Apple version of TensorFlow, the version is '2.4.0-rc0'. But TensorFlow still running on CPU :( – hat May 03 '21 at 13:53
1

I too am presently facing the same issue. I did try following this youtube link. Still following the steps my compiler keeps failing at make -j8 which is really frustrating too. Hope there would be a solution available too.

Update as of 16 June 21

Was able to get my test environment up using both opencv2 and tensorflow2.4. Following the steps by Prabhat on medium.

Note: be careful with messing the conda and pip environments and do change your default paths where you have added/downloaded the opncv and tensorflow virtual envs.

Hope this would be helpful in installing.

For test runs I also used the github tf test-code

Rishi
  • 11
  • 4
1

You can try to run the following sample code, open the activity monitor to check if gpu's are working properly and Tensorflow is installed perfectly.

#import os
#os.environ["TF_DISABLE_MLC"] = "1"
#os.environ["TF_MLC_LOGGING"] = "1"
import tensorflow as tf
from tensorflow.python.compiler.mlcompute import mlcompute

tf.compat.v1.disable_eager_execution()
mlcompute.set_mlc_device(device_name='gpu')
print("is_apple_mlc_enabled %s" % mlcompute.is_apple_mlc_enabled())
print("is_tf_compiled_with_apple_mlc %s" % mlcompute.is_tf_compiled_with_apple_mlc())
print(f"eagerly? {tf.executing_eagerly()}")
print(tf.config.list_logical_devices())

from tensorflow.keras import datasets, layers, models

(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
               'dog', 'frog', 'horse', 'ship', 'truck']
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
                    validation_data=(test_images, test_labels))
Aman Anand
  • 41
  • 6