What am I doing wrong with Numba here?

Viewed 2557

I'm trying to learn how to use the Numba module. So far I haven't been able to get anything working because of some problem interfacing with NumPy. This is the code I'm running (from the Numba docs) and the error I get:

from numba import jit
import numpy as np

x = np.arange(100).reshape(10, 10)

@jit(nopython=True) # Set "nopython" mode for best performance, equivalent to @njit
def go_fast(a): # Function is compiled to machine code when called the first time
    trace = 0.0
    for i in range(a.shape[0]):   # Numba likes loops
        trace += np.tanh(a[i, i]) # Numba likes NumPy functions
    return a + trace              # Numba likes NumPy broadcasting

print(go_fast(x))
    Traceback (most recent call last):
File "C:/Users/JoHn/Documents/Current Classes/MEEN575_Optimization/HW6/Optimal_controller/angle_wrapping.py", line 84, in <module>
print(go_fast(x))
TypeError: expected dtype object, got 'numpy.dtype[float64]'

I know from some searching that this was or is a known error somewhat recently and had something to do with new builds of Numba requiring newer builds of NumPy or something like that, but as far as I can tell I have the most recent NumPy build, version 1.20. Any tips on what I'm doing wrong? To be clear I've never had a great understanding of how to cleanly setup environments in python so it is very possible I'm just missing something obvious here.

1 Answers

Update to 0.53.1 works. It failed on 0.47.x as well for me. Seems more of numpy issue. One way to resolve install numpy >=1.20.0 and numba v>0.52.

More information on this issue: https://github.com/numba/numba/issues/6041

P.S: not sure if you still have this error, just wanted to update, was facing similar issue.

Related