I have installed tensorflow-macos and while training this is my CPU usage
and GPU usage
.
Can I make Tensorflow run on GPU anyway?
I have installed tensorflow-macos and while training this is my CPU usage
and GPU usage
.
Can I make Tensorflow run on GPU anyway?
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.
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)
Uncommenting the line's added from Aman's code and rerunning makes the GPU work 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:
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.
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
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."
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.
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
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
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))