pyopencl.RuntimeError: clBuildProgram failed: invalid build options - python

I am new to OpenCL and have some problems with setting up a OpenCL program. To illustrate my problem please look at the code (taken from https://github.com/benshope/PyOpenCL-Tutorial):
# Use OpenCL To Add Two Random Arrays (This Way Hides Details)
import pyopencl as cl # Import the OpenCL GPU computing API
import pyopencl.array as pycl_array # Import PyOpenCL Array (a Numpy array plus an OpenCL buffer object)
import numpy as np # Import Numpy number tools
context = cl.create_some_context() # Initialize the Context
queue = cl.CommandQueue(context) # Instantiate a Queue
a = pycl_array.to_device(queue, np.random.rand(50000).astype(np.float32))
b = pycl_array.to_device(queue, np.random.rand(50000).astype(np.float32))
# Create two random pyopencl arrays
c = pycl_array.empty_like(a) # Create an empty pyopencl destination array
program = cl.Program(context, """
__kernel void sum(__global const float *a, __global const float *b, __global float *c)
{
int i = get_global_id(0);
c[i] = a[i] + b[i];
}""").build() # Create the OpenCL program
program.sum(queue, a.shape, None, a.data, b.data, c.data) # Enqueue the program for execution and store the result in c
print("a: {}".format(a))
print("b: {}".format(b))
print("c: {}".format(c))
# Print all three arrays, to show sum() worked
If I execute the script I get the following error:
"C:\Program Files\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\python.exe" D:/python/openCL/020_array_sum.py
Traceback (most recent call last):
File "D:/python/openCL/020_array_sum.py", line 20, in <module>
}""").build() # Create the OpenCL program
File "C:\Program Files\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\site-packages\pyopencl\__init__.py", line 166, in build
options=options, source=self._source)
File "C:\Program Files\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\site-packages\pyopencl\__init__.py", line 206, in _build_and_catch_errors
raise err
pyopencl.RuntimeError: clBuildProgram failed: invalid build options -
(options: -I c:\program files\winpython-64bit-2.7.6.3\python-2.7.6.amd64\lib\site-packages\pyopencl\cl)
(source saved as c:\appdata\local\temp\tmp0bj_ij.cl)
Process finished with exit code 1
As far as I understood it, this is caused by the build() function, but I do not understand why. In one forum they suggested to define the kernel with only one " instead of """. This also did not help.
I use WinPython-64bit-2.7.6.3 and pycharm-community-3.1.1. For the openCL i have installed: AMD-APP-SDK-v2.9-Windows-641, Mako-0.9.1.win-amd64-py2.7, pytools-2014.1.2.win-amd64-py2.7 and pyopencl-2013.2.win-amd64-py2.7.
My graphics card is a Radeon HD 7850 and I have a AMD PhenomII processor.
P.S.: When I compile in Spyder, the error message reads:
>>> runfile('D:/python/openCL/020_array_sum.py', wdir=r'D:/python/openCL')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "D:/python/openCL/020_array_sum.py", line 20, in <module>
}""").build() # Create the OpenCL program
File "C:\Program Files\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\site-packages\pyopencl\__init__.py", line 166, in build
options=options, source=self._source)
File "C:\Program Files\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\site-packages\pyopencl\__init__.py", line 206, in _build_and_catch_errors
raise err
pyopencl.RuntimeError: clBuildProgram failed: invalid build options -
(options: -I c:\program files\winpython-64bit-2.7.6.3\python-2.7.6.amd64\lib\site-packages\pyopencl\cl)
(source saved as c:\users\andreas\appdata\local\temp\tmpzrgacv.cl)
Edit: I have now also tested it on another PC: same error. It also has an Nvidia graphics card. What both have in commen, is that they are only specified with OpenCL 1.1. Could it be, that I need OpenCL 1.2?

I think I found the problem. I changed the installtion directory of WinPython such that the path no longer includes spaces, now C:\WinPython-64bit-2.7.6.3. Then it worked. Thanks again for all your suggestions and your time.

This worked for me, I disabled caching of built kernels. My solution is useful if you are using pyopencl
import os
os.environ['PYOPENCL_NO_CACHE'] = '1'

Related

Different Python packages use different versions of the same DLL

For my project I use pyenchant library for spell checking and win32com.client.dispatch to call SAPI.SpVoice for automatic voiceovers. I have some Speech2Go packages installed and all worked fine until I decided it's time to move my project to Python 3.9. The only thing preventing me to do it is the new version of pyenchant, which uses the same DLL as Speech2Go, but the different version. By trial and error I deduced that the DLL in question is libstdc++-6.dll and I cannot force one of these 2 programs to use the same version neither I can rename the DLL obviously.
But it turned out that this code does not work in Python 3.9:
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
def speaktest(text):
voices = speaker.GetVoices("Language = 409", 'Gender = Male')
for voice in voices:
desc = voice.GetDescription()
if "Eric" in desc:
speaker.Voice = voice
speaker.Speak(text)
break
if __name__ == '__main__':
speaktest("Hello")
import enchant
from enchant.checker import SpellChecker
After speaker.Speak(text) is called, provided a Speech2Go voice is selected (named "Eric" in my case, because that's what I have installed), I cannot load pyenchant because of this error:
Traceback (most recent call last):
File "C:\Users\User\AppData\Roaming\JetBrains\PyCharm2021.2\scratches\scratch_9.py", line 21, in <module>
import enchant
File "C:\Plan Z Editor XXL\venv\lib\site-packages\enchant\__init__.py", line 81, in <module>
from enchant import _enchant as _e
File "C:\Plan Z Editor XXL\venv\lib\site-packages\enchant\_enchant.py", line 162, in <module>
e = ctypes.cdll.LoadLibrary(enchant_lib_path)
File "C:\python39\lib\ctypes\__init__.py", line 452, in LoadLibrary
return self._dlltype(name)
File "C:\python39\lib\ctypes\__init__.py", line 374, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 127] The specified procedure could not be found
Process finished with exit code 1
If I call speaktest after importing pyenchant, this happens:
Traceback (most recent call last):
File "C:\Users\User\AppData\Roaming\JetBrains\PyCharm2021.2\scratches\scratch_9.py", line 22, in <module>
speaktest("Hello")
File "C:\Users\User\AppData\Roaming\JetBrains\PyCharm2021.2\scratches\scratch_9.py", line 16, in speaktest
speaker.Speak(text)
File "C:\Users\USER\AppData\Local\Temp\gen_py\3.9\C866CA3A-32F7-11D2-9602-00C04F8EE628x0x5x4\ISpeechVoice.py", line 70, in Speak
return self._oleobj_.InvokeTypes(12, LCID, 1, (3, 0), ((8, 1), (3, 49)),Text
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147221164), None)
Process finished with exit code 1
What CAN be a working solution is to use multiprocessing to handle the speaking and making sure multiprocessing.freeze_support() is called before importing pyenchant, thus making different Python instances load different versions of libstdc++-6.dll, but since I already have a code base around both pyEnchant and SAPI.SpVoice and it works the way it is, it's not a good idea to rewrite all the code just because these 2 libraries conflict with each other.
That's why I'm here asking whether there is a much simpler method of resolving this conflict without rewriting a large chunk of code?

MultiprocessIterator throws error when changing batch_size

I want to train a Faster R-CNN with ChainerCV. As a first test I mostly copied the provided example, I only changed the lines corresponding the dataset to use my custom dataset. I checked if my dataset is fully functional with all operations discribed in this tutorial.
If I run the script without changes everything works perfect, but if I change the batch_size I get an error. I tried increasing the shared_mem from 100 MB to 1000 MB, but the error didn’t disappear.
Error when setting the batch_size=2:
Exception in main training loop: all the input array dimensions except for the concatenation axis must match exactly
Traceback (most recent call last):
File "/home/cv/anaconda3/envs/chainer/lib/python3.6/site-packages/chainer/training/trainer.py", line 315, in run
update()
File "/home/cv/anaconda3/envs/chainer/lib/python3.6/site-packages/chainer/training/updaters/standard_updater.py", line 165, in update
self.update_core()
File "/home/cv/anaconda3/envs/chainer/lib/python3.6/site-packages/chainer/training/updaters/standard_updater.py", line 171, in update_core
in_arrays = self.converter(batch, self.device)
File "/home/cv/anaconda3/envs/chainer/lib/python3.6/site-packages/chainer/dataset/convert.py", line 134, in concat_examples
[example[i] for example in batch], padding[i])))
File "/home/cv/anaconda3/envs/chainer/lib/python3.6/site-packages/chainer/dataset/convert.py", line 164, in _concat_arrays
return xp.concatenate([array[None] for array in arrays])
Will finalize trainer extensions and updater before reraising the exception.
Traceback (most recent call last):
File "/home/cv/ChainerCV/faster_rcnn/train.py", line 131, in <module>
main()
File "/home/cv/ChainerCV/faster_rcnn/train.py", line 126, in main
trainer.run()
File "/home/cv/anaconda3/envs/chainer/lib/python3.6/site-packages/chainer/training/trainer.py", line 329, in run
six.reraise(*sys.exc_info())
File "/home/cv/anaconda3/envs/chainer/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/home/cv/anaconda3/envs/chainer/lib/python3.6/site-packages/chainer/training/trainer.py", line 315, in run
update()
File "/home/cv/anaconda3/envs/chainer/lib/python3.6/site-packages/chainer/training/updaters/standard_updater.py", line 165, in update
self.update_core()
File "/home/cv/anaconda3/envs/chainer/lib/python3.6/site-packages/chainer/training/updaters/standard_updater.py", line 171, in update_core
in_arrays = self.converter(batch, self.device)
File "/home/cv/anaconda3/envs/chainer/lib/python3.6/site-packages/chainer/dataset/convert.py", line 134, in concat_examples
[example[i] for example in batch], padding[i])))
File "/home/cv/anaconda3/envs/chainer/lib/python3.6/site-packages/chainer/dataset/convert.py", line 164, in _concat_arrays
return xp.concatenate([array[None] for array in arrays])
ValueError: all the input array dimensions except for the concatenation axis must match exactly
System info:
__Hardware Information__
Machine : x86_64
CPU Name : skylake
Number of accessible CPU cores : 8
__OS Information__
Platform : Linux-4.15.0-45-generic-x86_64-with-debian-stretch-sid
Release : 4.15.0-45-generic
System Name : Linux
Version : #48~16.04.1-Ubuntu SMP Tue Jan 29 18:03:48 UTC 2019
OS specific info : debianstretch/sid
glibc info : glibc 2.10
__CUDA Information__
Found 1 CUDA devices
id 0 b'GeForce GTX 1080' [SUPPORTED]
compute capability: 6.1
pci device id: 0
pci bus id: 1
Summary:
1/1 devices are supported
CUDA driver version : 10000
__Conda Information__
conda_build_version : 3.17.6
conda_env_version : 4.6.3
platform : linux-64
python_version : 3.7.1.final.0
EDIT: When running the example with batch_size=2 the error also occurs.
While trying to fix the error I got another error.
ValueError: Currently only batch size 1 is supported.
Waiting seems to be the solution.
Current Faster-RCNN implementation does not support multi batch training, but you can rewrite it to support it like code below.
https://github.com/knorth55/chainer-light-head-rcnn/blob/master/light_head_rcnn/links/model/light_head_rcnn_train_chain.py
Another option is using Faster-RCNN with FPN in ChainerCV.
The latest version of ChainerCV has Faster-RCNN with FPN which supports multi batch training.
https://github.com/chainer/chainercv/blob/master/examples/fpn/train_multi.py
self.converter assumes that the first argument of batch is composed of inputs that have the same shape. For example, if you use image dataset, all images are supposed to have the shape of (C, H, W).
So, could you check your dataset returns images of the same shape?
And if your dataset has various shapes of images, how about using TransformDataset like https://github.com/chainer/chainercv/blob/df63b74ef20f9d8c830e266881e577dd05c17442/examples/faster_rcnn/train.py#L86?

Cannot use fortran code from f2py compile function

I have the following Fortran code:
!routines.f90
module mymodule
contains
function add(a, b)
real(8), intent(in):: a
real(8), intent(in):: b
real(8) :: add
add = a + b
end function
end module
Instead of using the command: python -m numpy.f2py -m routines -c routines.f90, I want to compile from within a python script, as follows:
#main.py
import numpy as np
import numpy.f2py as f2py
with open(r'routines.f90', 'r') as f:
source = f.read()
f2py.compile(source, modulename='routines')
print('OK')
But when I try to execute this script: python main.py I get the following error:
Traceback (most recent call last):
File "main.py", line 7, in <module>
f2py.compile(source, modulename='routines')
File "/home/user1/anaconda3/lib/python3.6/site-packages/numpy/f2py/__init__.py", line 59, in compile
f.write(source)
File "/home/user1/anaconda3/lib/python3.6/tempfile.py", line 485, in func_wrapper
return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'
Could you please tell me what is the issue?
open(r'routines.f90', 'r') opens your file for reading text (a.k.a. str), but, apparently, f2py.compile requires that its first argument be of type bytes. To satisfy that, open your file in binary mode:
open(r'routines.f90', 'rb')
(Also, there's no need for the first r in r'routines...', you can just do 'routines.f90', although it doesn't change much).

Why do I get "_curses.error: cbreak() returned ERR" when using TensorFlow CLI Debugger?

I am trying to use the TensorFlow CLI debugger in order to identify the operation which is causing a NaN during training of a network, but when I try to run the code I get an error:
_curses.error: cbreak() returned ERR
I'm running the code on an Ubuntu server, which I'm connecting to via SSH, and have tried to follow this tutorial.
I have tried using tf.add_check_numerics_ops(), but the layers in the network include while loops so are not compatible. This is the section of code where the error is being raised:
import tensorflow as tf
from tensorflow.python import debug as tf_debug
...
#Prepare data
train_data, val_data, test_data = dataset.prepare_datasets(model_config)
sess = tf.Session()
sess = tf_debug.LocalCLIDebugWrapperSession(sess)
# Create iterators
handle = tf.placeholder(tf.string, shape=[])
iterator = tf.data.Iterator.from_string_handle(handle, train_data.output_types, train_data.output_shapes)
mixed_spec, voice_spec, mixed_audio, voice_audio = iterator.get_next()
training_iterator = train_data.make_initializable_iterator()
validation_iterator = val_data.make_initializable_iterator()
testing_iterator = test_data.make_initializable_iterator()
training_handle = sess.run(training_iterator.string_handle())
...
and the full error is:
Traceback (most recent call last):
File "main.py", line 64, in <module>
#ex.automain
File "/home/enterprise.internal.city.ac.uk/acvn728/.local/lib/python3.5/site-packages/sacred/experiment.py", line 137, in automain
self.run_commandline()
File "/home/enterprise.internal.city.ac.uk/acvn728/.local/lib/python3.5/site-packages/sacred/experiment.py", line 260, in run_commandline
return self.run(cmd_name, config_updates, named_configs, {}, args)
File "/home/enterprise.internal.city.ac.uk/acvn728/.local/lib/python3.5/site-packages/sacred/experiment.py", line 209, in run
run()
File "/home/enterprise.internal.city.ac.uk/acvn728/.local/lib/python3.5/site-packages/sacred/run.py", line 221, in __call__
self.result = self.main_function(*args)
File "/home/enterprise.internal.city.ac.uk/acvn728/.local/lib/python3.5/site-packages/sacred/config/captured_function.py", line 46, in captured_function
result = wrapped(*args, **kwargs)
File "main.py", line 95, in do_experiment
training_handle = sess.run(training_iterator.string_handle())
File "/home/enterprise.internal.city.ac.uk/acvn728/.local/lib/python3.5/site-packages/tensorflow/python/debug/wrappers/framework.py", line 455, in run
is_callable_runner=bool(callable_runner)))
File "/home/enterprise.internal.city.ac.uk/acvn728/.local/lib/python3.5/site-packages/tensorflow/python/debug/wrappers/local_cli_wrapper.py", line 255, in on_run_start
self._run_start_response = self._launch_cli()
File "/home/enterprise.internal.city.ac.uk/acvn728/.local/lib/python3.5/site-packages/tensorflow/python/debug/wrappers/local_cli_wrapper.py", line 431, in _launch_cli
title_color=self._title_color)
File "/home/enterprise.internal.city.ac.uk/acvn728/.local/lib/python3.5/site-packages/tensorflow/python/debug/cli/curses_ui.py", line 492, in run_ui
self._screen_launch(enable_mouse_on_start=enable_mouse_on_start)
File "/home/enterprise.internal.city.ac.uk/acvn728/.local/lib/python3.5/site-packages/tensorflow/python/debug/cli/curses_ui.py", line 445, in _screen_launch
curses.cbreak()
_curses.error: cbreak() returned ERR
I'm pretty new to using Ubuntu (and TensorFlow), but as far as I can tell the server does have ncurses installed, which should allow the required curses based interface:
acvn728#america:~/MScFinalProject$ dpkg -l '*ncurses*' | grep '^ii'
ii libncurses5:amd64 6.0+20160213-1ubuntu1 amd64 shared libraries for terminal handling
ii libncursesw5:amd64 6.0+20160213-1ubuntu1 amd64 shared libraries for terminal handling (wide character support)
ii ncurses-base 6.0+20160213-1ubuntu1 all basic terminal type definitions
ii ncurses-bin 6.0+20160213-1ubuntu1 amd64 terminal-related programs and man pages
ii ncurses-term 6.0+20160213-1ubuntu1 all additional terminal type definitions
Problem solved! The solution was to change
sess = tf_debug.LocalCLIDebugWrapperSession(sess)
to
sess = tf_debug.LocalCLIDebugWrapperSession(sess, ui_type="readline")
This is similar to the solution to this question, but I I think it is important to note that they are different because a) it refers to a different function and a different API and b) I wasn't trying to run from an IDE, as mentioned in that solution.
cbreak would return ERR if you run a curses application that is not on a real terminal (i.e., something that works with POSIX termios calls).
From the description,
but the layers in the network include while loops so are not compatible
it does not seem you are running in a terminal.

What dependencies do I need for USB programing in python with pyUSB?

I am trying to get the usb.find command to work properly in a python script I'm writing on Angstrom for the Beagleboard.
Here is my code:
#!/usr/bin/env python
import usb.core
import usb.util
import usb.backend.libusb01 as libusb
PYUSB_DEBUG_LEVEL = 'debug'
# find our device
# Bus 002 Device 006: ID 1208:0815
# idVendor 0x1208
# idProduct 0x0815
# dev = usb.core.find(idVendor=0xfffe, idProduct=0x0001)
# iManufacturer 1 TOROBOT.com
dev = usb.core.find(idVendor=0x1208, idProduct=0x0815,
backend=libusb.get_backend() )
I don't know what's missing, but here is what I do know.
When I don't specify the backend, no backend is found. When I do specify the backend "usb.backend.libusb01" I get the following error:
root#beagleboard:~/servo# ./pyServo.py
Traceback (most recent call last):
File "./pyServo.py", line 17, in <module>
dev = usb.core.find(idVendor=0x1208, idProduct=0x0815, backend=libusb.get_backend() )
File "/usr/lib/python2.6/site-packages/usb/core.py", line 854, in find
return _interop._next(device_iter(k, v))
File "/usr/lib/python2.6/site-packages/usb/_interop.py", line 60, in _next
return next(iter)
File "/usr/lib/python2.6/site-packages/usb/core.py", line 821, in device_iter
for dev in backend.enumerate_devices():
File "/usr/lib/python2.6/site-packages/usb/backend/libusb01.py", line 390, in enumerate_devices
_check(_lib.usb_find_busses())
File "/usr/lib/python2.6/ctypes/__init__.py", line 366, in __getattr__
func = self.__getitem__(name)
File "/usr/lib/python2.6/ctypes/__init__.py", line 371, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: python: undefined symbol: usb_find_busses
What am I missing so that this will work properly?
Thank you.
Python, pyusb, libusb.
I think that's it.
Well libc, ld-linux.so too of course, but those are in your system by default.
Do nm -D /path-to/libusb.so and see if usb_find_busses symbol is really there. There's always a chance that your libusb is outdated or somehow specially compiled.
Check if you get any log, for example missing libusb.so should produce something like 'Error loading libusb 0.1 backend'

Categories

Resources