New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
storage variables inside functions do not work in nopython mode #4246
Comments
The problem in # this does not work
@njit(int64[:,:](int64))
def bar(number):
results = np.array([], dtype=np.int64).reshape(-1, 2) is due to the empty list The other reported error: @njit(int64[:,:](int64, int64[:,:]))
def buzz(number, results=np.array([], dtype=np.int64).reshape(-1, 2)):
for i in range(number):
# slightly modified to add missing parenthesis so that the `np.array([[numba...` is not treated
# as the `axis` arg
results = np.concatenate((results, np.array([[number, number + 1]])))
return results
buzz(10) #<--- throws error is caused by the signature. The provided type signature does not account for the second argument to be of optional argument with default value. It works when the user-provided signature is omitted. Numba will infer the signature to be |
re: empty list re: function signature
or shouldn't |
It's best to just leave out the signature. Just a bare
|
@sklam going along this discussion, what is the most performant way to use this convention with @njit
def foo(iterable):
# empty list with shape of thing we want to store
results = np.array([0][:0]).reshape(-1, 3)
for i in prange(len(iterable)):
# some things happen here
# maybe a call to a helper njit function
partial = _helper(iterable[i])
# this probably has a cleaner solution
results = np.concatenate((results, np.array([partial]))) # <--- this feels wrong
# because np.append is not supported - as well as axis - we have to remake
# an array rather than just to stack the results
return results |
A common coding convention is to use a "storage" variable to collect results inside of a function, e.g.
Numba supports numpy arrays, but it seems this is not the case for multidimensional arrays.
this forces a weird wrapper requirement like:
because although numpy arrays are allowed as default arguments,
numba
does not like not recieving the optinal argumentplease fix
The text was updated successfully, but these errors were encountered: