user15964
user15964

Reputation: 2639

numba @vectorize target='parallel' TypeError

If I define

import numba as nb
import numpy as np
@nb.vectorize
def nb_vec(x):
    if x>0:
        x=x+100
    return x

then

x=np.random.random(1000000)
nb_vec(x)

run without problem

But If I add target option like

@nb.vectorize(target='parallel')
def nb_vec(x):
    if x>0:
        x=x+100
    return x

then

x=np.random.random(1000000)
nb_vec(x)

outputs error message

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in 1 x=np.random.random(1000000) ----> 2 nb_vec(x)

TypeError: ufunc 'nb_vec' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

What is wrong?

Upvotes: 3

Views: 267

Answers (1)

MSeifert
MSeifert

Reputation: 152677

In numba 0.46 the numba.vectorize decorator without signature will create a dynamic universal function, that means it compiles the code based on the types when called. So you don't need to supply a signature.

import numpy as np
import numba as nb

@nb.vectorize()
def nb_vec(x):
    if x > 0:
        x = x + 100
    return x

>>> nb_vec
<numba._DUFunc 'nb_vec'>
>>> nb_vec.types
[]
>>> nb_vec(np.ones(5))
array([101., 101., 101., 101., 101.])
>>> nb_vec.types
['d->d']

However if you specify target='parallel' then it will create a normal universal function. So it only supports the provided signatures. In your case you omitted the signatures so it actually doesn't support any inputs.

import numpy as np
import numba as nb

@nb.vectorize(target='parallel')
def nb_vec(x):
    if x > 0:
        x = x + 100
    return x

>>> nb_vec
<ufunc 'nb_vec'>
>>> nb_vec.types
[]

The solution here is to specify a signature with the appropriate types when you use parallel vectorize:

import numpy as np
import numba as nb

@nb.vectorize(
    [nb.int32(nb.int32), 
     nb.int64(nb.int64), 
     nb.float32(nb.float32), 
     nb.float64(nb.float64)], 
    target='parallel')
def nb_vec(x):
    if x > 0:
        x = x + 100
    return x

Upvotes: 2

Related Questions