Mac 的 AMD 顯卡於 Keras 加速解法 – PlaidML

Apple 近幾年所推出的 MacBook Pro / iMac / Mac mini…. 都是 AMD 的顯卡,沒有 Nivida
且 Nvidia 最近也沒什麼官方支援 Mac 的 Driver 了 ( 所以透過 eGPU 來在 Mac 上使用 NV 卡好像不是太好的選擇 )

然而 Tensorflow 之類的 Tool 都是使用 CUDA 來加速的
( Google 官方也移除了 Mac 上的 tensorflow-gpu )
讓 MacBook Pro 的 AMD User 好像覺得只能使用 CPU….
空有強大的顯卡卻無用武之地
( 本人的 MacBook Pro 是使用 Radeon Pro 系列… 另外在 Mac 上 AMD 應該沒啥驅動的問題 )

但是如果是使用 Keras 且配 AMD 卡的人就不用擔心了
因為今天要說的就是如何使用 Keras + AMD GPU ( Mac 上還有 Metal ) 的解決方法 就是使用 PlaidML
https://github.com/plaidml/plaidml 使用上很簡單
直接 pip 安裝,ex :
pip3 install plaidml-keras plaidbench

然後輸入指令
plaidml-setup
會看到類似以下資訊
Default Config Devices:
   No devices.

Experimental Config Devices:
   llvm_cpu.0 : CPU (LLVM)
   opencl_intel_intel(r)_hd_graphics_530.0 : Intel Inc. Intel(R) HD Graphics 530 (OpenCL)
   opencl_cpu.0 : Intel CPU (OpenCL)
   metal_amd_radeon_pro_460.0 : AMD Radeon Pro 460 (Metal)
   opencl_amd_amd_radeon_pro_460_compute_engine.0 : AMD AMD Radeon Pro 460 Compute Engine (OpenCL)
   metal_intel(r)_hd_graphics_530.0 : Intel(R) HD Graphics 530 (Metal)

Using experimental devices can cause poor performance, crashes, and other nastiness.

會問是否使用實驗中的設備 ( 表示可能不穩定或是有非預期錯誤發生 )
Enable experimental device support? (y,n)[n]:
選 y ( 是 ),之後會看到可選的項目
Multiple devices detected (You can override by setting PLAIDML_DEVICE_IDS).
Please choose a default device:

   1 : llvm_cpu.0
   2 : opencl_intel_intel(r)_hd_graphics_530.0
   3 : opencl_cpu.0
   4 : metal_amd_radeon_pro_460.0
   5 : opencl_amd_amd_radeon_pro_460_compute_engine.0
   6 : metal_intel(r)_hd_graphics_530.0

例如我要使用 Mac 的 Metal 與 AMD 的 Radeon Pro 460 來做為 ML 加速
Default device? (1,2,3,4,5,6)[1]: 
就輸入 4 然後我們就可以到 Keras 中使用 PlaidML 來做加速了
將 Keras 預設的 Backend 從 Tensorflow 切過去,只要一行 ( 或是可在 env 中做設定 )
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend" 以下是 PlaidML Github 上所提供的範例 Code :
#!/usr/bin/env python
import numpy as np
import os
import time

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

import keras
import keras.applications as kapp
from keras.datasets import cifar10

(x_train, y_train_cats), (x_test, y_test_cats) = cifar10.load_data()
batch_size = 8
x_train = x_train[:batch_size]
x_train = np.repeat(np.repeat(x_train, 7, axis=1), 7, axis=2)
model = kapp.VGG19()
model.compile(optimizer='sgd', loss='categorical_crossentropy',
              metrics=['accuracy'])

print("Running initial batch (compiling tile program)")
y = model.predict(x=x_train, batch_size=batch_size)

# Now start the clock and run 10 batches
print("Timing inference...")
start = time.time()
for i in range(10):
    y = model.predict(x=x_train, batch_size=batch_size)
print("Ran in {} seconds".format(time.time() - start))
在我的電腦上使用 Tensorflow 結果為
tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Running initial batch (compiling tile program)
Timing inference...
Ran in 25.52408504486084 seconds
但使用 Mac 的 AMD GPU ( PlaidML 為 Backend )
速度為
Running initial batch (compiling tile program)
INFO:plaidml:Analyzing Ops: 55 of 195 operations complete
INFO:plaidml:Analyzing Ops: 111 of 195 operations complete
Timing inference...
Ran in 3.932204008102417 seconds
可以看到獲得很大的提升
( 25 秒 vs 3 秒 ) 

2 thoughts on “Mac 的 AMD 顯卡於 Keras 加速解法 – PlaidML”

  1. Hi:
    I am currently reproducing your step, I am easily install the PlaidML-Keras, but when I trying to do “plaidml-setup” it give me the following error:
    ➜ ~ plaidml-setup
    Traceback (most recent call last):
    File “/usr/local/lib/python3.7/site-packages/plaidml/__init__.py”, line 155, in load_library
    return ctypes.cdll.LoadLibrary(libpath)
    File “/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ctypes/__init__.py”, line 434, in LoadLibrary
    return self._dlltype(name)
    File “/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ctypes/__init__.py”, line 356, in __init__
    self._handle = _dlopen(self._name, mode)
    OSError: dlopen(/usr/local/lib/python3.7/site-packages/plaidml/libplaidml.dylib, 6): image not found

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File “/usr/local/bin/plaidml-setup”, line 10, in
    sys.exit(main())
    File “/usr/local/lib/python3.7/site-packages/plaidml/plaidml_setup.py”, line 15, in main
    ctx = plaidml.Context()
    File “/usr/local/lib/python3.7/site-packages/plaidml/__init__.py”, line 858, in Context
    return plaidml.context.Context(_lib())
    File “/usr/local/lib/python3.7/site-packages/plaidml/__init__.py”, line 758, in _lib
    _impl_lib = _Library()
    File “/usr/local/lib/python3.7/site-packages/plaidml/__init__.py”, line 175, in __init__
    lib = load_library(‘libplaidml.dylib’, [‘lib’])
    File “/usr/local/lib/python3.7/site-packages/plaidml/__init__.py”, line 165, in load_library
    return ctypes.cdll.LoadLibrary(libpath)
    File “/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ctypes/__init__.py”, line 434, in LoadLibrary
    return self._dlltype(name)
    File “/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ctypes/__init__.py”, line 356, in __init__
    self._handle = _dlopen(self._name, mode)
    OSError: dlopen(/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/libplaidml.dylib, 6): image not found
    ➜ ~

    have you ever experience that?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.