【发布时间】: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 的潜在错误,请在此处发布。我想知道它为什么会这样。