[PYTHON] GPU ~ Implementation of PlaidML on Mac ~ (as of May 2020)

For those who have completed this setting once

This is the procedure for starting jupyter from the next time onwards. We have prepared a simplified version, so we would appreciate it if you could refer to it.

[Note] Plaid ML Keras execution order

Execution environment

Execution environment 1
machine MacBook Air(Retina, 13-inch,2019)
OS MacOS Catalina version 10.15.4
memory 8 GB 2133 MHz LPDDR3
graphic Intel UHD Graphics 617 1536 MB
module version
Keras 2.2.4
plaidml 0.7.0
plaidml-keras 0.7.0
plaidbench 0.7.0
Execution environment 2
machine MacBook (Retina, 12-inch, 2017)
OS MacOS Catalina version 10.15.4
memory 8 GB 1867 MHz LPDDR3
graphic Intel HD Graphics 615 1536 MB
module version
Keras 2.2.4
plaidml 0.7.0
plaidml-keras 0.7.0
plaidbench 0.7.0

Motivation for implementing PlaidML

The number of contents that can be used to study machine learning for free in GW has increased, and I decided to start working on machine learning for image processing, which I originally wanted to do. I don't have a machine with high specs that I'm proud of, and as I wrote in the execution environment, the execution environment is a MacBook, so if you can't use the ** NVIDIA ** GPU, put it on your PC. It's a burden, but it takes a lot of time. I felt that learning was limited by the CPU alone, so I implemented PlaidML, which is rumored to be able to learn using the GPU on the MacBook.

References

I mainly referred to GitHub of Plaid ML.

About the implementation of PlaidML

What the module does

PlaidML is a framework of scientific mathematical calculation library developed by intel and others. The frameworks of scientific mathematical calculation libraries supported by Keras as standard include tensorflow and Theano. When trying to use the GPU (graphics) of a PC with these libraries, it was only supported by the GPU made by NVIDIA. On Macs with AMD GPUs, it was not possible to calculate using the GPU.

However, this PlaidML is not tied to being made by NVIDIA, it is a module to enable the use of GPU, and it is a module adopted as a framework of the back end of Keras. In this implementation, we will set up PlaidML on this Keras backend.

Where it gets stuck

** Machine learning on Mac using GPU ** about it.

Implementation

PlaidML Github QuickStart Link QuickStar builds a virtual environment using virtualenv, but I didn't. There is no particular reason.

Installation of required modules

module_install


pip3 install plaidml-keras plaidbench
pip3 install keras

PlaidML setup

Set up PlaidML. Set up whether to calculate using CPU or GPU with PlaidML.

plaidml-setup


plaidml-setup

I proceeded according to Quick Start, but I got an error here. The error statement is as follows.

error


Traceback (most recent call last):
  File "/usr/local/bin/plaidml-setup", line 5, in <module>
    from plaidml.plaidml_setup import main
  File "/usr/local/lib/python3.7/site-packages/plaidml/__init__.py", line 50, in <module>
    import plaidml.settings
  File "/usr/local/lib/python3.7/site-packages/plaidml/settings.py", line 33, in <module>
    _setup_config('PLAIDML_EXPERIMENTAL_CONFIG', 'experimental.json')
  File "/usr/local/lib/python3.7/site-packages/plaidml/settings.py", line 30, in _setup_config
    'Could not find PlaidML configuration file: "{}".'.format(filename))
plaidml.exceptions.PlaidMLError: Could not find PlaidML configuration file: "experimental.json".

There was already a discussion on GitHub about this error. If you are interested, please take a look. plaidml.exceptions.PlaidMLError: Could not find PlaidML configuration file: "experimental.json". #370

In conclusion, Since there was a Conclusion that an error occurred because the PATH did not pass, set the PATH described. .. Execute ʻexport` again and confirm that the set PATH is in place ...

PATH


export PLAIDML_NATIVE_PATH=/usr/local/lib/libplaidml.dylib
export RUNFILES_DIR=/usr/local/share/plaidml

export

Run setup again

plaidml-setup


plaidml-setup
~~
Enable experimental device support? (y,n)[n]:y #Select y

Multiple devices detected (You can override by setting PLAIDML_DEVICE_IDS).
Please choose a default device:

   1 : llvm_cpu.0
   2 : opencl_intel_uhd_graphics_617.0
   3 : metal_intel(r)_uhd_graphics_617.0

Default device? (1,2,3)[1]:3 #The bottom two are GPUs, this time select graphic 3
~~
#Whether to save the result of this selection
Save settings to /Users/linda/.plaidml? (y,n)[y]:y #Select y

Execution result (whole)
PlaidML Setup (0.7.0)

Thanks for using PlaidML!

The feedback we have received from our users indicates an ever-increasing need
for performance, programmability, and portability. During the past few months,
we have been restructuring PlaidML to address those needs.  To make all the
changes we need to make while supporting our current user base, all development
of PlaidML has moved to a branch — plaidml-v1. We will continue to maintain and
support the master branch of PlaidML and the stable 0.7.0 release.

Read more here: https://github.com/plaidml/plaidml 

Some Notes:
  * Bugs and other issues: https://github.com/plaidml/plaidml/issues
  * Questions: https://stackoverflow.com/questions/tagged/plaidml
  * Say hello: https://groups.google.com/forum/#!forum/plaidml-dev
  * PlaidML is licensed under the Apache License 2.0
 

Default Config Devices:
   llvm_cpu.0 : CPU (via LLVM)
   metal_intel(r)_uhd_graphics_617.0 : Intel(R) UHD Graphics 617 (Metal)

Experimental Config Devices:
   llvm_cpu.0 : CPU (via LLVM)
   opencl_intel_uhd_graphics_617.0 : Intel Inc. Intel(R) UHD Graphics 617 (OpenCL)
   metal_intel(r)_uhd_graphics_617.0 : Intel(R) UHD Graphics 617 (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_uhd_graphics_617.0
   3 : metal_intel(r)_uhd_graphics_617.0

Default device? (1,2,3)[1]:3

Selected device:
    metal_intel(r)_uhd_graphics_617.0

Almost done. Multiplying some matrices...
Tile code:
  function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }
Whew. That worked.

Save settings to /Users/linda/.plaidml? (y,n)[y]:y
Success!

Inference performance benchmark

Next, let's benchmark the inference performance of MobileNet.

plaidbench keras mobilenet
Running 1024 examples with mobilenet, batch size 1, on backend plaid
INFO:plaidml:Opening device "metal_intel(r)_uhd_graphics_617.0"
Compiling network...INFO:plaidml:Analyzing Ops: 266 of 413 operations complete
 Warming up... Running...
Example finished, elapsed: 2.280s (compile), 32.101s (execution)

-----------------------------------------------------------------------------------------
Network Name         Inference Latency         Time / FPS          
-----------------------------------------------------------------------------------------
mobilenet            31.35 ms                  0.00 ms / 1000000000.00 fps
Correctness: PASS, max_error: 6.440454399125883e-06, max_abs_error: 5.811452865600586e-07, fail_ratio: 0.0

Second line from the top ʻINFO: plaidml: Opening device "metal_intel (r) _uhd_graphics_617.0" ` You can see that you are benchmarking the accelerator you have set up.

Actually, the setting itself is completed so far, but let's check if the Keras backend is really changed to PlaidML.

Start jupyter on a terminal through this PATH etc.

Be careful If you start Jupyter in another terminal window or close the terminal window that you have set up until now, the settings will be reset.

Confirmation of PlaidML

On jupyter

import plaidml.keras
plaidml.keras.install_backend() #Set to backend
import keras
print(keras.backend.backend()) #Checking the Keras backend

>>> tensorflow

The keras backend is still tensorflow.

To actually set Plaidml as the backend of keras,

python


plaidml.keras.install_backend() #Set to backend

Instead, you need to set the PATH to be set on the backend.

python


export KERAS_BACKEND=plaidml.keras.backend

If you check after setting this PATH ...



#### **`python`**
```python

import keras
print(keras.backend.backend())

>>>plaidml.keras.backend

You can see that it has been changed.

#Finally I would like to study image analysis in the future.

#Related literature Dreaming of NVIDIA~ PlaidML ~

Recommended Posts

GPU ~ Implementation of PlaidML on Mac ~ (as of May 2020)
Implementation of MathJax on Sphinx
Handling of python on mac
Use Colab only as an external GPU environment (as of 2020.6 / Mac environment)
Environment construction of python3.8 on mac
GPU check of PC on jupyter notebook
Try running PlaidML image judgment on Mac
python on mac
A memo of installing Chainer 1.5 for GPU on Windows
Support for Python 2.7 runtime on AWS Lambda (as of 2020.1)
Implementation of personally optimized photo frames on Raspberry Pi