Creating `NumPy` arrays inside a function decorated with `numba`'s `@jit(nopython=True)`?

浪子不回头ぞ 提交于 2019-12-13 02:16:39

问题


I would like to create a numpy array inside a function decorated with numba's @jit(nopython=True). For example:

import numpy as np
import numba

@numba.jit(nopython=True)
def funny_func():
    zero_array = np.zeros(10)

    sum_result = 0

    for elem in zero_array:
        sum_result += elem

    return sum_result

print funny_func()

Compiling this script creates the following error:

UntypedAttributeError: Unknown attribute "zeros" of type Module(<module
'numpy' from 'A:\Anaconda\lib\site-packages\numpy\__init__.pyc'>)

So, numba does't support NumPy array creation functions. How then do I create NumPy arrays inside such a decorated "numba function"?


回答1:


As the docs explicitly say:

NumPy array creation is not supported in nopython mode. Numba mitigates this by automatically trying to jit loops in nopython mode. This allows for array creation at the top of a function while still getting almost all the performance of nopython mode.

(I linked to an older version, because I believe this limitation doesn't exist in 0.18, which implies that you're using an older one. Even older versions, I think before 0.12 or so, don't have this documentation because the auto-lifting didn't exist yet, and you had to do it manually, but the same approach below will work.)

If you're using too old a version of Numba to have that feature, or you've done something that's complicated enough to confuse that feature and make it not work, you have to do the same thing manually. For example:

@numba.jit(nopython=True)
def _funny_func(zero_array):    
    sum_result = 0

    for elem in zero_array:
        sum_result += elem

    return sum_result

@numba.jit(nopython=False)
def funny_func():
    zero_array = np.zeros(10)
    return _funny_func(zero_array)



回答2:


This isn't an issue in newer versions of numba anymore. On version 0.33 for example the code you posted works without problems.

Two years ago (corresponding pull request on github) they added support for:

  • np.zeros
  • np.zeros_like
  • np.ones
  • np.ones_like
  • np.empty
  • np.empty_like
  • np.full
  • np.full_like
  • np.arange
  • np.linspace (only the three argument form)
  • np.eye
  • np.identity

Which are listed now as "Supported NumPy features" - 'Other functions'.



来源:https://stackoverflow.com/questions/30427081/creating-numpy-arrays-inside-a-function-decorated-with-numbas-jitnopytho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!