Tensorflow on MacBook Pro M1 Monterey 12.2 ml computer

Hello everyone,

I got this error message when I try to train a model.

2022-02-07 11:49:22.913153: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
2022-02-07 11:49:23.231884: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz

When I add from tensorflow.python.compiler.mlcompute import mlcompute to my code :

ModuleNotFoundError: No module named 'tensorflow.python.compiler.mlcompute'

I tried to reinstall tensorflow by following these instructions but sees not to work properly.

Any ideas ? thanks.

pip list output

----------------------- --------- absl-py         1.0.0 astunparse       1.6.3 brotlipy        0.7.0 cached-property     1.5.2 cachetools       5.0.0 certifi         2021.10.8 cffi          1.15.0 charset-normalizer   2.0.9 colorama        0.4.4 conda          4.11.0 conda-package-handling 1.7.3 cryptography      36.0.0 cycler         0.11.0 flatbuffers       2.0 fonttools        4.29.1 gast          0.4.0 google-auth       2.6.0 google-auth-oauthlib  0.4.6 google-pasta      0.2.0 grpcio         1.43.0 h5py          3.1.0 idna          3.1 importlib-metadata   4.10.1 keras          2.7.0 Keras-Preprocessing   1.1.2 kiwisolver       1.3.2 libclang        13.0.0 Markdown        3.3.6 matplotlib       3.5.1 munkres         1.1.4 numpy          1.19.5 oauthlib        3.2.0 opt-einsum       3.3.0 packaging        21.3 Pillow         9.0.1 pip           21.3.1 protobuf        3.19.4 pyasn1         0.4.8 pyasn1-modules     0.2.8 pycosat         0.6.3 pycparser        2.21 pyOpenSSL        21.0.0 pyparsing        3.0.7 PySocks         1.7.1 python-dateutil     2.8.2 requests        2.26.0 requests-oauthlib    1.3.1 rsa           4.8 ruamel-yaml-conda    0.15.80 scipy          1.8.0 setuptools       59.4.0 six           1.15.0 tensorboard       2.8.0 tensorboard-data-server 0.6.1 tensorboard-plugin-wit 1.8.1 tensorflow-estimator  2.7.0 tensorflow-macos    2.7.0 tensorflow-metal    0.3.0 termcolor        1.1.0 tornado         6.1 tqdm          4.62.3 typing_extensions    4.0.1 unicodedata2      14.0.0 urllib3         1.26.7 Werkzeug        2.0.2 wheel          0.35.1 wrapt          1.13.3 zipp          3.7.0```

Hi @saltyrainbow!

You don't need to use mlcompute anymore so you can remove that import and references to it, and your code should work. You can just import tensorflow and use it as you would with any other machine. The pluggable design and the tensorflow-metal plugin take care that the computations on MacOS respect the device placement the user asks from Tensorflow i.e. the computations are by default sent to the GPU.

Let me know if there are any other issues

Thank you very much for your answer. At this point I am getting the same issue.

I though creating an env with conda and install tensorflow whithin it should work. Unfortunately I'm not able to install tensorflow-macos and tensorflow-metal in my env. I got this error :

(tf) saltyrainbow@MacBook-Pro ~ % python -m pip install tensorflow-macos
ERROR: Could not find a version that satisfies the requirement tensorflow-macos (from versions: none)
ERROR: No matching distribution found for tensorflow-macos

I'm very new to datascience, so maybe I didn't follow correctly the installation steps.

I reinstalled everything and tried to run a code, and I didn't notice that I get also this : 2022-02-09 12:40:41.585013: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.

Sorry for that. In fact, I assumed that it didn't work because I got really weird accuracy results.

I've found something interesting. Without changing any parameters, sometimes i got a really good model :

Metal device set to: Apple M1

systemMemory: 16.00 GB
maxCacheSize: 5.33 GB

2022-02-09 13:18:04.110848: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2022-02-09 13:18:04.110957: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
2022-02-09 13:18:04.304541: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz
Epoch 1/20
2022-02-09 13:18:04.469609: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
12/16 [=====================>........] - ETA: 0s - loss: 0.0000e+00 - accuracy: 0.29442022-02-09 13:18:04.824160: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
16/16 [==============================] - 1s 18ms/step - loss: 3.1066e-04 - accuracy: 0.2627 - val_loss: 0.0000e+00 - val_accuracy: 0.2002
Epoch 2/20
16/16 [==============================] - 0s 11ms/step - loss: 2.7158e-04 - accuracy: 0.4070 - val_loss: 0.0000e+00 - val_accuracy: 0.0189
Epoch 3/20
16/16 [==============================] - 0s 11ms/step - loss: 2.7422e-04 - accuracy: 0.5366 - val_loss: 0.0000e+00 - val_accuracy: 0.0300
Epoch 4/20
16/16 [==============================] - 0s 11ms/step - loss: 2.7614e-04 - accuracy: 0.4945 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 5/20
16/16 [==============================] - 0s 11ms/step - loss: 2.7924e-04 - accuracy: 0.4931 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 6/20
16/16 [==============================] - 0s 11ms/step - loss: 2.4457e-04 - accuracy: 0.4933 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 7/20
16/16 [==============================] - 0s 11ms/step - loss: 3.3170e-04 - accuracy: 0.4931 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 8/20
16/16 [==============================] - 0s 11ms/step - loss: 3.5839e-04 - accuracy: 0.4931 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 9/20
16/16 [==============================] - 0s 11ms/step - loss: 3.7550e-04 - accuracy: 0.4931 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 10/20
16/16 [==============================] - 0s 11ms/step - loss: 3.7018e-04 - accuracy: 0.4933 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 11/20
16/16 [==============================] - 0s 13ms/step - loss: 5.4659e-04 - accuracy: 0.4933 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 12/20
16/16 [==============================] - 0s 11ms/step - loss: 6.4239e-04 - accuracy: 0.4931 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 13/20
16/16 [==============================] - 0s 11ms/step - loss: 7.3958e-04 - accuracy: 0.4931 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 14/20
16/16 [==============================] - 0s 11ms/step - loss: 0.0013 - accuracy: 0.4434 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
Epoch 15/20
16/16 [==============================] - 0s 11ms/step - loss: 6.7687e-04 - accuracy: 0.4407 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 16/20
16/16 [==============================] - 0s 11ms/step - loss: 5.9926e-04 - accuracy: 0.3167 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
Epoch 17/20
16/16 [==============================] - 0s 11ms/step - loss: 0.0022 - accuracy: 0.3664 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 18/20
16/16 [==============================] - 0s 11ms/step - loss: 0.0017 - accuracy: 0.2534 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
Epoch 19/20
16/16 [==============================] - 0s 11ms/step - loss: 0.0011 - accuracy: 0.2604 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
Epoch 20/20
16/16 [==============================] - 0s 11ms/step - loss: 0.0019 - accuracy: 0.2534 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
71/71 [==============================] - 0s 6ms/step - loss: 0.0064 - accuracy: 0.9996
[0.00637961458414793, 0.9995548129081726]

And sometimes I got this weird evaluation (without any changes in my code) :

[0.0023984466679394245, 0.0]
Tensorflow on MacBook Pro M1 Monterey 12.2 ml computer
 
 
Q