I am trying to get into numba gpu processing. I have this MWE:
import numpy as np
import numba
#numba.njit
def function():
ar = np.zeros((3, 3))
for i in range(3):
ar[i] = (1, 2, 3)
return ar
ar = function()
print(ar)
Output:
[[1. 2. 3.]
[1. 2. 3.]
[1. 2. 3.]]
Now I want to run it on my gpu. I tried using following decorators:
#numba.njit(target='cuda')
#numba.njit(target='gpu')
#numba.cuda.jit
none of which work. Here are the error messages of above decorators:
Traceback (most recent call last):
File "/home/amu/Desktop/RL_framework/help_functions/test.py", line 4, in <module>
#numba.jit(target='cuda')
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/core/decorators.py", line 171, in jit
targetoptions=options, **dispatcher_args)
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/core/decorators.py", line 179, in _jit
dispatcher = registry.dispatcher_registry[target]
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/core/registry.py", line 96, in __getitem__
return super(TargetRegistry, self).__getitem__(item)
KeyError: 'cuda'
Traceback (most recent call last):
File "/home/amu/Desktop/RL_framework/help_functions/test.py", line 4, in <module>
#numba.njit(target='gpu')
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/core/decorators.py", line 236, in njit
return jit(*args, **kws)
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/core/decorators.py", line 171, in jit
targetoptions=options, **dispatcher_args)
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/core/decorators.py", line 179, in _jit
dispatcher = registry.dispatcher_registry[target]
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/core/registry.py", line 96, in __getitem__
return super(TargetRegistry, self).__getitem__(item)
KeyError: 'gpu'
Traceback (most recent call last):
File "/home/amu/Desktop/RL_framework/help_functions/test.py", line 4, in <module>
#numba.cuda.jit()
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/__init__.py", line 140, in __getattr__
) from None
AttributeError: module 'numba' has no attribute 'cuda'
numba.cuda is not even recognized as module function. I have numba 49.1 and cudatoolkit 9.0 installed.
Do I have to change the function for this to work? I have a huge numba.njit function that I need to run on a gpu.
I was trying to learn more about numba's cuda implementation from:
http://numba.pydata.org/numba-doc/0.16.0/modules/numba.cuda.html
Thank you in advance.
Edit:
As #talonmies proposed I imported cuda explicitly from the numba module and outsourced the array creation:
import numpy as np
import numba
from numba import cuda
#numba.njit(target='cuda')
def function(ar=None):
for i in range(3):
ar[i] = (1, 2, 3)
return ar
ar = np.zeros((3, 3))
ar_result = function(ar=ar)
print(ar_result)
Output:
Traceback (most recent call last):
File "/home/amu/Desktop/RL_framework/help_functions/test.py", line 12, in <module>
ar_result = function(ar=ar)
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/cuda/dispatcher.py", line 40, in __call__
return self.compiled(*args, **kws)
TypeError: __call__() got an unexpected keyword argument 'ar'
This error occurs with every of the aforementioned decorators except #numba.njit.
Edit_2:
When I try to run:
import numpy as np
import numba
from numba import cuda
#numba.jit(target='cuda')
def function(ar):
for i in range(3):
ar[i] = (1,2,3)
ar = np.zeros((3, 3))
function(ar)
print(ar)
the output is:
Traceback (most recent call last):
File "/home/amu/Desktop/RL_framework/help_functions/test.py", line 11, in <module>
function(ar)
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/cuda/dispatcher.py", line 40, in __call__
return self.compiled(*args, **kws)
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/cuda/compiler.py", line 758, in __call__
kernel = self.specialize(*args)
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/cuda/compiler.py", line 769, in specialize
kernel = self.compile(argtypes)
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/cuda/compiler.py", line 785, in compile
**self.targetoptions)
File "/home/amu/anaconda3/lib/python3.7/site-packages/numba/core/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
TypeError: compile_kernel() got an unexpected keyword argument 'boundscheck'
Three points:
You have to explicitly import the cuda module from numba to use it (this isn't specific to numba, all python libraries work like this)
The nopython mode (njit) doesn't support the CUDA target
Array creation, return values, keyword arguments are not supported in Numba for CUDA code
I can fix all that like this:
...: import numpy as np
...: import numba
...: from numba import cuda
...:
...: #numba.jit(target='cuda')
...: def function(ar):
...: for i in range(3):
...: ar[i] = (1,2,3)
...:
...: ar = np.zeros((3, 3))
...: function(ar)
...: print(ar)
[[1. 2. 3.]
[1. 2. 3.]
[1. 2. 3.]]
Related
The code :::: Does anyone can help me with this ?
from numba import vectorize
#vectorize(['int64(int64, int64)'], target='cuda')
def add_ufunc(x, y):
return x + y
print('a+b:\n', add_ufunc(a, b))
print()
print('b_col + c:\n', add_ufunc(b_col, c))
The error ::::
Traceback (most recent call last): File "array.py", line 1, in
from numba import vectorize File "/usr/lib/python3.7/site-packages/numba/init.py", line 11, in
from . import config, errors, _runtests as runtests, types File "/usr/lib/python3.7/site-packages/numba/config.py", line 9, in
import multiprocessing File "/usr/lib/python3.7/multiprocessing/init.py", line 16, in
from . import context File "/usr/lib/python3.7/multiprocessing/context.py", line 6, in
from . import reduction File "/usr/lib/python3.7/multiprocessing/reduction.py", line 136, in
import array File "/home/felipe/cuda/array.py", line 1, in
from numba import vectorize ImportError: cannot import name 'vectorize' from 'numba'
(/usr/lib/python3.7/site-packages/numba/init.py)
I want to compute something using numpy and numba. This is my code:
import numpy as np
from numba import jit,double,int64
#jit(locals=dict(i=int64,j=int64,k=int64,l=int64,suma=double))
def omega_comp_arrays(omega,p_kl,eta,theta,K,L,links_by_ratings_array):
#new_omega = np.zeros(omega.shape)
for rating,links in enumerate(links_by_ratings_array):
for i,j in links:
suma = 0
for k in range(K):
for l in range(L):
omega[i,j,k,l] = p_kl[k,l,rating]*theta[i,k]*eta[j,l]
suma += omega[i,j,k,l]
omega[i,j,:,:] /= suma
return omega
N_nodes=2
N_veins=[1,1]
N_items=2
N_ratings=1
K=2
L=2
##Definim matrius
theta = np.random.rand(N_nodes,K)
eta = np.random.rand(N_items,L)
p_kl = np.random.rand(K,L,N_ratings)
suma = np.sum(theta,axis =1)
theta /=suma[:,np.newaxis]
suma = np.sum(eta,axis=1)
eta /= suma[:,np.newaxis]
suma = np.sum(p_kl,axis =2)
p_kl /=suma[:,:,np.newaxis]
links_by_ratings_array = [np.array([0,0])]
omega = np.ones((N_nodes,N_items,K,L))
omega = omega_comp_arrays(omega,p_kl,eta,theta,K,L,links_by_ratings_array)
The problem occurs when I run the code:
Traceback (most recent call last):
File "test_omega.py", line 39, in <module>
omega = omega_comp_arrays(omega,p_kl,eta,theta,K,L,links_by_ratings_array)
TypeError: 'numpy.int64' object is not iterable
Exception TypeError: "'NoneType' object is not callable" in <bound method ModuleRef.__del__ of <llvmlite.binding.module.ModuleRef object at 0x7f801123d3d0>> ignored
But if I activate the nopython mode, another error appears:
Traceback (most recent call last):
File "test_omega.py", line 39, in <module>
omega = omega_comp_arrays(omega,p_kl,eta,theta,K,L,links_by_ratings_array)
File "/usr/local/lib/python2.7/dist-packages/numba/dispatcher.py", line 330, in _compile_for_args
raise e
numba.errors.TypingError: Caused By:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/numba/compiler.py", line 240, in run
stage()
File "/usr/local/lib/python2.7/dist-packages/numba/compiler.py", line 454, in stage_nopython_frontend
self.locals)
File "/usr/local/lib/python2.7/dist-packages/numba/compiler.py", line 881, in type_inference_stage
infer.propagate()
File "/usr/local/lib/python2.7/dist-packages/numba/typeinfer.py", line 846, in propagate
raise errors[0]
TypingError: failed to unpack int64
File "test_omega.py", line 10
[1] During: typing of exhaust iter at test_omega.py (10)
Failed at nopython (nopython frontend)
failed to unpack int64
File "test_omega.py", line 10
[1] During: typing of exhaust iter at test_omega.py (10)
In other words, I can't do a loop of a list of arrays because the error is in the loop of links (for i,j in links). Any suggestion?
I am trying to solve a very simple ODE using the dsolve sympy function:
from sympy import*
init_printing()
t = symbols('t', real = True)
A = symbols('A', complex = True)
f = Function('rho')(t)
dsolve(Derivative(f, t)+A*f, f)
But, then I get:
Traceback (most recent call last):
File "<ipython-input-9-3d1f21a5ef22>", line 1, in <module>
dsolve(Derivative(f, t)+A*f, f)
File "/home/ted/anaconda3/lib/python3.6/site-packages/sympy/solvers/ode.py", line 566, in dsolve
hints = _desolve(eq, func=func, hint=hint, simplify=True, xi=xi, eta=eta, type='ode', ics=ics, x0=x0, n=n, **kwargs)
File "/home/ted/anaconda3/lib/python3.6/site-packages/sympy/solvers/deutils.py", line 205, in _desolve
n=terms, x0=x0, prep=prep)
File "/home/ted/anaconda3/lib/python3.6/site-packages/sympy/solvers/ode.py", line 1251, in classify_ode
r = _nth_linear_match(reduced_eq, func, order)
File "/home/ted/anaconda3/lib/python3.6/site-packages/sympy/solvers/ode.py", line 3723, in _nth_linear_match
terms[f.derivative_count] += c
AttributeError: 'Derivative' object has no attribute 'derivative_count'
The sympy version is 1.1.1, the python version is 3.6.4 and the ipython version is 6.2.1.
My bad! I tried to use one of the files (ode.py) from the current development version without updating the rest. Now I have it working. Thanks!
When trying to set up the DataIterator as explained on neon tutorial.
from neon.data import DataIterator
import numpy as np
X = np.random.rand(10000, 3072)
y = np.random.randint(1, 11, 10000)
train = DataIterator(X=X, y=y, nclass=10, lshape=(3, 32, 32))
I encountered a weird error:
ERROR:neon.data.dataiterator:DataIterator class has been deprecated and renamed"ArrayIterator" please use that name.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "neon/data/dataiterator.py", line 168, in __init__
super(DataIterator, self).__init__(*args, **kwargs)
File "neon/data/dataiterator.py", line 82, in __init__
self.Xdev = [self.be.array(x) for x in X]
AttributeError: 'NoneType' object has no attribute 'array'
I then tried with ArrayIterator, keeping X, y the same.
ArrayIterator(X=X, y=y, nclass=10, lshape=(3,32,32))
With the same NoneType error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "neon/data/dataiterator.py", line 82, in __init__
self.Xdev = [self.be.array(x) for x in X]
AttributeError: 'NoneType' object has no attribute 'array'
Why would this be the case? Is there an easy fix?
Fixed the problem by generating backend.
from neon.backends import gen_backend
be = gen_backend()
(...)
Keeping it simple:
----------------------------------
from mpmath import *
from scipy.integrate import *
mp.dps=30
def F(x):
return Q**(x)
Q=735
print fixed_quad(F,0,1,n=1)[0]
print fixed_quad(F,0,1,n=2)[0]
print fixed_quad(F,0,1,n=3)[0]
--------------------
returns
27.1108834235
93.1213589089
109.673420158
However, if I change F(x) from “Q**(x)” to even just a simple function—e.g., “cos(x)”—I get
--------------------
Traceback (most recent call last):
File "Test.py", line 7, in <module>
print fixed_quad(F,0,1,n=1)[0]
File "/usr/lib/python2.7/dist-packages/scipy/integrate/quadrature.py", line 67, in fixed_quad
return (b-a)/2.0*sum(w*func(y,*args),0), None
File "Test.py", line 5, in F
return cos(x)
File "/usr/lib/pymodules/python2.7/mpmath/ctx_mp_python.py", line 984, in f
x = ctx.convert(x)
File "/usr/lib/pymodules/python2.7/mpmath/ctx_mp_python.py", line 662, in convert
return ctx._convert_fallback(x, strings)
File "/usr/lib/pymodules/python2.7/mpmath/ctx_mp.py", line 614, in _convert_fallback
raise TypeError("cannot create mpf from " + repr(x))
TypeError: cannot create mpf from array([ 0.5])
Is this some known bug? Or is “fixed_quad” only meant for certain uses, not general integration (like “trapz”)?
All of the other regulars (“quad”, “dblquad”, “tplquad”, “nquad”) donʼt seem to have that problem/error.