How to set up GPU-accelerated TensorFlow on M2 Mac and Asus ROG Zephyrus G14

1 minute read

Published:

Computer specs

ManufacturerYearCPUSystem memoryGPUGPU memoryOperating system
Asus ROG Zephyrus G142022AMD Ryzen 9 6900HS16 GBAMD Radeon RX 6700S GDDR8 GBWindows 11
Apple Macbook Pro2022Apple M216 GBApple M216 GB (?)MacOS Ventura

Setup instructions for Mac

  1. Install Python 3.10 via Homebrew:

     brew install python@3.10
    
  2. Perform the steps at “Get started with tensorflow-metal”.

Setup instructions for Asus ROG Zephyrus G14

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

  2. Install virtualenv via pip and 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.ps1
    
  3. Update pip.

     python -m pip install --upgrade pip
    
  4. Install tensorflow-cpu and tensorflow-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)
ManufacturerProcessorTotal runtime
AsusAMD Ryzen 9 6900HS (GPU component)316 seconds
AsusAMD Radeon RX 6700S GDDR (dedicated GPU)134 seconds
AppleM2 GPU390 seconds

Revisions:

  • 2023-10-05: reference the correct Python version in the Asus ROG Zephyrus G14 guide.