using @guvectorize inside @jit in numba produces error

Viewed 61

Adopting the example (link) from the documentation, the code:

from numba import guvectorize, int64

@guvectorize([(int64[:], int64, int64[:])], '(n),()->(n)')
def g(x, y, res):
    for i in range(x.shape[0]):
        res[i] = x[i] + y
        
@njit
def fun():
    a = np.arange(5)
    return g(a, 2)

fun()

Produces error:

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Untyped global name 'g': Cannot determine Numba type of <class 'numpy.ufunc'>

File "<ipython-input-37-02f67c08c51f>", line 4:
def fun():
    <source elided>
    a = np.arange(5)
    return g(a, 2)
    ^

However, if you comment out the decoration with @njit, fun() returns correctly.

So, the standalone @guvectorize-d function works fine, but doesn't work when it is used inside a function decorated with @njit.

Is this expected behaviour? If not, do you have any ideas about how it can be fixed?

Thanks

1 Answers

See this question. If you are not constrained to use a guvectorized function, you have several possible workarounds: one can be converting your guvectorized function to a njit one, which will work without problems inside another njit function.

Related