【问题标题】:Understanding Numba TypingError with jit nopython使用 jit nopython 理解 Numba TypingError
【发布时间】:2020-08-08 02:27:23
【问题描述】:

我无法使用@jit(nopython=True) 解决(可能是基本的)Numba 错误。它归结为下面的最小示例,它产生TypingError(下面的完整日志)。如果相关,我正在使用 Python 3.6.10 和 Numba v0.49.0。

错误发生在创建 numpy 数组的d 行(如果我删除d 并返回c,它工作正常)。我该如何解决这个问题?

from numba import jit
import numpy as np

n = 5
foo = np.random.rand(n,n)

@jit(nopython=True)
def bar(x):
    a = np.array([0,3,2])
    b = np.array([1,2,3])
    c = [x[i,j] for i,j in zip(a,b)]
    # print(c) # Un-commenting this line solves the issue‽ (per @Ethan's comment)
    d = np.array(c)
    return d

baz = bar(foo)

完整的错误如下:

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-13-950d2be33d72> in <module>
     14     return d
     15 
---> 16 baz = bar(foo)
     17 print(baz)

~/miniconda3/envs/py3k/lib/python3.6/site-packages/numba/core/dispatcher.py in _compile_for_args(self, *args, **kws)
    399                 e.patch_message(msg)
    400 
--> 401             error_rewrite(e, 'typing')
    402         except errors.UnsupportedError as e:
    403             # Something unsupported is present in the user code, add help info

~/miniconda3/envs/py3k/lib/python3.6/site-packages/numba/core/dispatcher.py in error_rewrite(e, issue_type)
    342                 raise e
    343             else:
--> 344                 reraise(type(e), e, None)
    345 
    346         argtypes = []

~/miniconda3/envs/py3k/lib/python3.6/site-packages/numba/core/utils.py in reraise(tp, value, tb)
     77         value = tp()
     78     if value.__traceback__ is not tb:
---> 79         raise value.with_traceback(tb)
     80     raise value
     81 

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<intrinsic range_iter_len>) with argument(s) of type(s): (zip(iter(array(int64, 1d, C)), iter(array(int64, 1d, C))))
 * parameterized
In definition 0:
    All templates rejected with literals.
In definition 1:
    All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<intrinsic range_iter_len>)
[2] During: typing of call at <ipython-input-13-950d2be33d72> (9)


File "<ipython-input-13-950d2be33d72>", line 9:
def bar(x):
    a = np.array([0,3,2])
    ^

更新:使用以下函数会以类似的方式失败(尽管 print(c) 技巧在这种情况下没有帮助):

@jit(nopython=True)
def bar(x):
    a = [0,3,2]
    b = [1,2,3]
    c = x[a, b]
    d = np.array(c)
    return d

【问题讨论】:

  • 我知道这并不能真正回答你的问题,但我偶然发现如果我在 d = np.array(c) 之前插入 print(c) 你的代码在 nopython=True 中运行良好.我还必须添加“np”。在您的 a 和 b 数组实例化之前。
  • @Ethan 丢失的np. 是一个愚蠢的复制/过去错误,我更新了问题。这个print(c) 似乎解决了这个问题,这很有趣(也有点令人费解),谢谢!
  • 不客气。如果您有答案或将其报告为 Numba 的潜在错误,请在此处发布。我想知道它为什么会这样。

标签: python jit numba


【解决方案1】:

我有一个类似的问题,只是通过更新 numba 解决了它:

pip install --upgrade numba

【讨论】:

    【解决方案2】:

    函数的第一个版本的问题,以及添加print(c) 解决它的事实,对我来说是个谜。 Numba 应该实现zip(显然,在这种情况下,当print(c) 行以某种方式触发时它可以实现),所以这似乎是一个错误。

    该函数的第二个版本的问题并不神秘。根据current Numba documentation

    数组支持正常迭代。支持完整的基本索引和切片。还支持高级索引的子集:只允许一个高级索引,并且它必须是一维数组(它也可以与任意数量的基本索引组合)。

    由于您尝试在 c = x[a, b] 行中使用两个高级索引 ab,因此 Numba 不支持该代码。事实上,这就是冗长的错误消息Invalid use of Function(&lt;built-in function getitem&gt;) with argument(s) of type(s): (array(float64, 2d, C), tuple(array(int64, 1d, C) x 2)) 所说的。

    如果我们改为写c=x[a,2],那么代码就可以工作,这与 Numba 允许使用一个高级索引的承诺一致。

    总的来说,我发现使用 Numba 最安全的方法是在没有 NumPy 更高级功能的情况下以循环样式编写。这有点不幸——因为这几乎就像我们需要用 C 而不是 Python 的方言来编写一样——但从好的方面来说,它仍然比实际编写 C 方便得多。

    在这种情况下,以下代码运行良好:

    @jit(nopython=True)
    def bar(x):
        a = np.array([0,3,2])
        b = np.array([1,2,3])
        c = np.empty(len(a))
        for i in range(len(a)):
            c[i] = x[a[i], b[i]]
        return c
    

    【讨论】:

      猜你喜欢
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      • 2019-07-30
      • 2017-06-21
      相关资源
      最近更新 更多