tensorflow-metal for Python3.12 and tensorflow 2.17.x

Hi, The most recent version of tensorflow-metal is only available for macosx 12.0 and python up to version 3.11. Is there any chance it could be updated with wheels for macos 15 and Python 3.12 (which is the default version supported for tensrofllow 2.17+)? I'd note that even downgrading to Python 3.11 would not be sufficient, as the wheels only work for macos 12. Thanks.

Answered by feranick in 816854022

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:

  1. 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

  1. Decompress the whl using Keka or 7zip.

  2. Identify where your base installation folder for your packages (i.e. where the packages installed with Pip are located). The <base> 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 <base> location is:

/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/

  1. Now, create a new folder inside the site-package folder above, called: tensorflow-metal (you may need sudo rights).

  2. 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 <base>/tensorflow-plugins

  1. 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 <base>) 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")
Accepted Answer

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:

  1. 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

  1. Decompress the whl using Keka or 7zip.

  2. Identify where your base installation folder for your packages (i.e. where the packages installed with Pip are located). The <base> 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 <base> location is:

/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/

  1. Now, create a new folder inside the site-package folder above, called: tensorflow-metal (you may need sudo rights).

  2. 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 <base>/tensorflow-plugins

  1. 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 <base>) 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")

Of course, it would take Apple 10 seconds to create a Python 3.12 wheel. And yet, here we are.

tensorflow-metal for Python3.12 and tensorflow 2.17.x
 
 
Q