Post

Replies

Boosts

Views

Activity

Reply to tensorflow-metal for Python3.12 and tensorflow 2.17.x
I have figured out a way to use it with Python 3.12 (and MacOS 15). This method relies on the fact that the tensorflow-metal library is not a python library per se, but a whl package containing a MacOS library. As long as there are no API chances since it was compiled (MacOS 12), the library itself should work with any version of Python. With this in mind: Get the whl package of the tensorflow-metal library from Pypi: https://files.pythonhosted.org/packages/52/56/8373f5751011304a346f07e5423e69f809b626989d2541ae9e816ae7ced2/tensorflow_metal-1.1.0-cp311-cp311-macosx_12_0_arm64.whl Decompress the whl using Keka or 7zip. Identify where your base installation folder for your packages (i.e. where the packages installed with Pip are located). The installation folder for the stock version of Python in MacOS is: ~/Library/Python/3.9/lib/python/site-packages if you installed Python with MacPorts the location is: /opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/ Now, create a new folder inside the site-package folder above, called: tensorflow-metal (you may need sudo rights). From the decompressed folder from the whl package, copy the library into the site-folder/tensorflow-plugins folder: cp tensorflow_metal-1.1.0-cp311-cp311-macosx_12_0_arm64/tensorflow-plugins/libmetal_plugin.dylib /tensorflow-plugins This assumes you have tensorflow already installed. Run the script below, which I tested with TF 2.17.0 and tf_keras installed as well. You can try to run the script with the plugin installed and without (just remove it from ) to see a massive difference in performance, as expected. import time import tensorflow as tf import tf_keras as keras #from tensorflow import keras start_time = time.perf_counter() cifar = keras.datasets.cifar100 (x_train, y_train), (x_test, y_test) = cifar.load_data() model = keras.applications.ResNet50( include_top=True, weights=None, input_shape=(32, 32, 3), classes=100,) loss_fn = keras.losses.SparseCategoricalCrossentropy(from_logits=False) model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"]) model.fit(x_train, y_train, epochs=5, batch_size=64) total_time = time.perf_counter() - start_time print(" Total time: {0:.1f}s or {1:.1f}m or {2:.1f}h".format(total_time, total_time/60, total_time/3600),"\n")
Dec ’24