How to set up GPU-accelerated TensorFlow on M2 Mac and Asus ROG Zephyrus G14
Published:
Computer specs
| Manufacturer | Year | CPU | System memory | GPU | GPU memory | Operating system |
|---|---|---|---|---|---|---|
| Asus ROG Zephyrus G14 | 2022 | AMD Ryzen 9 6900HS | 16 GB | AMD Radeon RX 6700S GDDR | 8 GB | Windows 11 |
| Apple Macbook Pro | 2022 | Apple M2 | 16 GB | Apple M2 | 16 GB (?) | MacOS Ventura |
Setup instructions for Mac
Install Python 3.10 via Homebrew:
brew install python@3.10Perform the steps at “Get started with tensorflow-metal”.
Setup instructions for Asus ROG Zephyrus G14
Download and install the latest release of Python 3.10 from python.org. I used Python 3.10.11 directly from the python.org FTP server.
Install
virtualenvviapipand create a virtual environment to contain the experiements. This is might not be strictly necessary, but I prefer a clean development environment.pip install virtualenv virtualenv venv venv\scripts\activate.ps1Update
pip.python -m pip install --upgrade pipInstall
tensorflow-cpuandtensorflow-directml-plugin:pip install tensorflow-cpu tensorflow-directml-plugin
Note: DirectML allows TensorFlow to use the AMD GPU via DirectX 12. It’s TensorFlow plugin apparently requires the use of the tensorflow-cpu plugin. No idea what happens if you use the tensorflow package instead.
Appendix: test script with runtime results
From “Get started with tensorflow-metal”:
import tensorflow as tf
cifar = tf.keras.datasets.cifar100
(x_train, y_train), (x_test, y_test) = cifar.load_data()
model = tf.keras.applications.ResNet50(
include_top=True,
weights=None,
input_shape=(32, 32, 3),
classes=100,)
loss_fn = tf.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)
| Manufacturer | Processor | Total runtime |
|---|---|---|
| Asus | AMD Ryzen 9 6900HS (GPU component) | 316 seconds |
| Asus | AMD Radeon RX 6700S GDDR (dedicated GPU) | 134 seconds |
| Apple | M2 GPU | 390 seconds |
Revisions:
- 2023-10-05: reference the correct Python version in the Asus ROG Zephyrus G14 guide.
