I have the up-to-date versions of Keras and PlaidML installed. I ran the file plaidml-setup and configured plaidml to use my AMD GPU:
C:\WinPython\python-3.6.1.amd64\Scripts>plaidml-setup
PlaidML Setup (0.7.0)
(...)
Default Config Devices:
llvm_cpu.0 : CPU (via LLVM)
Experimental Config Devices:
llvm_cpu.0 : CPU (via LLVM)
opencl_amd_gfx902.0 : Advanced Micro Devices, Inc. gfx902 (OpenCL)
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_amd_gfx902.0
Default device? (1,2)[1]:2
Selected device:
opencl_amd_gfx902.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 C:\Users\jsupi\.plaidml? (y,n)[y]:y
Success!
I succesfully tested the installation by running plaidbench keras mobilenet
:
C:\WinPython\python-3.6.1.amd64\Scripts>plaidbench keras mobilenet
Running 1024 examples with mobilenet, batch size 1, on backend plaid
INFO:plaidml:Opening device "opencl_amd_gfx902.0"
Compiling network... Warming up... Running...
Example finished, elapsed: 7.484s (compile), 26.724s (execution)
-----------------------------------------------------------------------------------------
Network Name Inference Latency Time / FPS
-----------------------------------------------------------------------------------------
mobilenet 26.10 ms 11.90 ms / 84.02 fps
Correctness: PASS, max_error: 1.8053706298815086e-05, max_abs_error: 9.760260581970215e-07, fail_ratio: 0.0
Then I wanted to run some python module on my GPU. I read in this answer that I need to set os.environ["RUNFILES_DIR"]
and os.environ["PLAIDML_NATIVE_PATH"]
to correct paths, for example:
os.environ["RUNFILES_DIR"] = "/Library/Frameworks/Python.framework/Versions/3.7/share/plaidml"
os.environ["PLAIDML_NATIVE_PATH"] = "/Library/Frameworks/Python.framework/Versions/3.7/lib/libplaidml.dylib"
The problem is that I can't find anything resembling the last one in my system. I ran the Windows search function, but it couldn't find the libplaidml.dylib
file anywhere. So I tried the following:
import os
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
os.environ["RUNFILES_DIR"] = "C://Users/jsupi/.plaidml"
#os.environ["PLAIDML_NATIVE_PATH"] = "C:/Windows/WinPython/python-3.6.1.amd64/Lib/site-packages"
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import keras
from keras.datasets import mnist #to import our dataset
from keras.models import Sequential, Model # imports our type of network
from keras.layers import Dense, Flatten, Input # imports our layers we want to use
from keras.losses import categorical_crossentropy #loss function
from keras.optimizers import Adam, SGD #optimisers
from keras.utils import to_categorical #some function for data preparation
batch_size = 128
num_classes = 10
epochs = 50
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)
#Neural network with single dense hidden layer
model = Sequential()
#model.add(Input(input_shape=(28,28)))
model.add(Flatten(input_shape=(28,28)))
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
and got the error message:
Traceback (most recent call last):
File "D:\Kuba\Machine Learning\DigitRecognitionKeras.py", line 51, in <module>
model.add(Dense(128, activation='relu'))
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\engine\sequential.py", line 181, in add
output_tensor = layer(self.outputs[0])
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\engine\base_layer.py", line 431, in __call__
self.build(unpack_singleton(input_shapes))
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\layers\core.py", line 866, in build
constraint=self.kernel_constraint)
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\engine\base_layer.py", line 249, in add_weight
weight = K.variable(initializer(shape),
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\initializers.py", line 218, in __call__
dtype=dtype, seed=self.seed)
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 59, in wrapper
return func(*args, **kwargs)
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 1305, in random_uniform
rng_state = _make_rng_state(seed)
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 205, in _make_rng_state
rng_state = variable(rng_init, dtype='uint32')
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 59, in wrapper
return func(*args, **kwargs)
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 1935, in variable
_device(), plaidml.Shape(_ctx, ptile.convert_np_dtype_to_pml(dtype), *value.shape))
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 102, in _device
devices = plaidml.devices(_ctx)
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\__init__.py", line 1075, in devices
plaidml.settings.start_session()
File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\settings.py", line 77, in start_session
raise plaidml.exceptions.PlaidMLError('PlaidML is not configured. Run plaidml-setup.')
plaidml.exceptions.PlaidMLError: PlaidML is not configured. Run plaidml-setup.
Note the last line, which says that PlaidML is not configured even though I have just done that and succesfully tested it. The program runs fine if I comment out the first 3 lines (thus running it without plaidml) and write tensorflow.keras instead of keras in all the "import" lines (it seems to be necessary without plaidml).
Do you have any ideas how to resolve this issue? I have Windows 10 and Python 3.6.
UPDATE 08/11/2021: I have recently solved the problem after a suggestion made by a friend. First of all, 'libplaidml.dylib' is a Linux library file, and I have Windows, so I had to set the path to an analogous .dll file instead (I also use r"" to make sure there are no problems with backslashes):
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
os.environ["RUNFILES_DIR"] = r"C:\\Users\jsupi\.plaidml"
os.environ["PLAIDML_NATIVE_PATH"] = r"C:\\WinPython\python-3.6.1.amd64\Library\bin\plaidml.dll"
That done, I also created a virtual environment with all the necessary python libraries installed (but that's probably not necessary) and I ran the python script from the command line rather than from the GUI.
I hope I didn't forget to write any essential step here. Oh, and one thing that confused me - after the fixes described above - was how little of my GPU was used during some computations. When I switched plaidml back to using the CPU, the runtime of a script increased 100-fold, and only that did convince me the GPU had been working after all.