Use an AMD GPU for your Mac to accelerate Deeplearning in Keras
By: Daniel Deutsch
Tags: Gpu, Amd, Keras, Machine Learning, Deep Learning

Mac + AMD Radeon RX5700 XT + Keras

Every machine learning engineer these days will come to the point where he wants to use a GPU to speed up his deeplearning calculations. I happen to get an AMD Radeon GPU from a friend. Unfortunately, I saw that there is a big difference between AMD and Nvidia GPUs, whereas only the later is supported greatly in deeplearning libraries like Tensorflow. I came across some articles and made my mac+amd GPU setup work anyways. 🚀

This can be seen as comprehension of other articles (see “additional reading”) and some additional solutions from my side during implementation.

Disclaimer

I am not associated with any of the services I use in this article.

I do not consider myself an expert. If you have the feeling that I am missing important steps or neglected something, consider pointing it out in the comment section or get in touch with me.

I am always happy for constructive input and how to improve.

This article was written on 03–10–2020.

I cannot monitor all of my articles. There is a high probability that when you read this article the tips are outdated and the processes have changed.

If you need more information on certain parts, feel free to point it out in the comments.

Table of Contents

The problem — the answer

Tensorflow, which is used at the core for Keras calculations, supports local GPU acceleration using Nvidia graphic cards via CUDA. Unfortunately, there is nothing like this for AMD yet.

The answer to this problem is PlaidML, a python library and tensor compiler that allows us to speed up vector calculations.

PlaidML is an advanced and portable tensor compiler for enabling deep learning on laptops, embedded devices, or other devices where the available computing hardware is not well supported or the available software stack contains unpalatable license restrictions. PlaidML sits underneath common machine learning frameworks, enabling users to access any hardware supported by PlaidML. PlaidML supports Keras, ONNX, and nGraph. As a component within the nGraph Compiler stack, PlaidML further extends the capabilities of specialized deep-learning hardware (especially GPUs,) and makes it both easier and faster to access or make use of subgraph-level optimizations that would otherwise be bounded by the compute limitations of the device. As a component under Keras, PlaidML can accelerate training workloads with customized or automatically-generated Tile code. It works especially well on GPUs, and it doesn’t require use of CUDA/cuDNN on Nvidia hardware, while achieving comparable performance.

Source https://github.com/plaidml/plaidml

As of the time of writing, the following Hardware and networks are validated:

Source official docs https://github.com/plaidml/plaidml/tree/plaidml-v1

As PlaidML helps with tensor calculations on Keras models, it will not speed up independent tensor calculations using Numpy for example. This can be done using OpenCL, which will not be covered in this article. Check out the “additional reading” section for more.

Prerequisites

If you want to follow along, you should have

a Mac OS

an external AMD GPU

Keras, as deep learning library

My setup

MacOS Catalina

System Version: macOS 10.15.6 (19G2021)

Kernel Version: Darwin 19.6.0

Boot Volume: Macintosh HD

Boot Mode: Normal

Secure Virtual Memory: Enabled

System Integrity Protection: Enabled

External GPU

Runnning

system_profiler SPDisplaysDataType

will give you the Graphics/Display output.

It shows my external GPU:

Radeon RX 5700 XT: Chipset Model: Radeon RX 5700 XT

Type: External GPU

Bus: PCIe

PCIe Lane Width: x4

VRAM (Total): 8 GB

Vendor: AMD (0x1002)

Device ID: 0x731f

Revision ID: 0x00c1

ROM Revision: 113-D1990103-O09

Automatic Graphics Switching: Supported

gMux Version: 4.0.29 [3.2.8]

Metal: Supported, feature set macOS GPUFamily2 v1

GPU is Removable: Yes

Library versions

keras=2.2.4=pypi_0

keras-applications=1.0.8=py_1

keras-preprocessing=1.1.0=py_0 plaidbench=0.7.0=pypi_0

plaidml=0.7.0=pypi_0

plaidml-keras=0.7.0=pypi_0

Connect external GPU to Mac

Install packages

pip install pyopencl plaidml-keras plaidbench

plaidml-setup

plaidml-setup

setup steps of plaidml-setup

plaidbench keras mobilenet

plaidbench keras mobilenet

result of running plaidbench

This shows that the GPU setup should work. Now let's get into actual implementation.

Actual implementation

For making it work you need to add this to the notebook/file:

import plaidml.keras

import os

plaidml.keras.install_backend()

os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"

There are other suggestions on how to add the backend. However, it worked for me only in this order. First installing the backend, then setting the environment. (For other suggestions see “additional reading”)

Afterward, doing

from keras import backend as K

K

should give you something like:

<module 'plaidml.keras.backend' from '/Users/XXXX/opt/anaconda3/envs/XXX/lib/python3.7/site-packages/plaidml/keras/backend.py'>

If you are not sure if the GPU is really used there are several commands available like K._get_available_gpus() . But they didn't work for me. If you are getting the backend message like the one above it should work. You will see during calculation anyways if your GPU is running or your Mac ;)

Add deep learning code

To test it you can use basic Keras example code from their docs: https://github.com/keras-team/keras/tree/master/examples

For example the neural style transfer.

Running it on the base image from my header (credits to Nana Dua) and adding some special flavor (credits to Arno Senoner), leads to: