无法从njit函数内部调用numba函数

时间:2020-06-21 19:31:15

标签: python numpy types numba

totals = sorted( [ (amount * price, name) for name, amount, price in zip(products, amounts, prices) ], reverse=True ) 一起工作时,我偶然发现了非常意外的行为。我创建了一个numba函数,试图在其中创建一个nb.njit个数组,nb.typed.List,所以我试图创建一个对应的int8 numpy类型。

numba

因此,我通过nb.int8[:] # type of the list elements 关键字将此类型设置为nb.typed.List

lsttype

我得到的是:

l = nb.typed.List(lsttype=nb.int8[:])  # list of int8 numpy ndarrays

我想这意味着numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend) No implementation of function Function(<built-in function getitem>) found for signature: >>> getitem(class(int8), slice<a:b>) There are 16 candidate implementations: - Of which 16 did not match due to: Overload in function 'getitem': File: <built-in>: Line <N/A>. With argument(s): '(class(int8), slice<a:b>)': No match. 试图对numba类型的对象进行切片,好像它不理解该表示法一样。

因此,我尝试了另一种方法,创建了一个nb.int8类型的空数组,并使用了np.int8函数。

nb.typeof

它返回:

nb.typeof(np.array([], dtype=np.int8))

我不明白! numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend) Unknown attribute 'typeof' of type Module(<module 'numba' from '/Users/.../venv37/lib/python3.7/site-packages/numba/__init__.py'>) 怎么看不到自己

最小的例子很简单:

numba

因此,我尝试使用相同的功能,但没有使用import numba as nb import numpy as np @nb.njit def v(): print(nb.typeof(np.array([], dtype=np.int8))) v()

和它的印记!

@nb.njit

此外,我尝试将numba导入该函数中,因为它看不到模块,但产生了:

array(int8, 1d, C)

我也尝试重新安装和更新numba.core.errors.UnsupportedError: Failed in nopython mode pipeline (step: analyzing bytecode) Use of unsupported opcode (IMPORT_NAME) found numba

那是什么骗术?

2 个答案:

答案 0 :(得分:2)

我发现了一个讨厌的解决方法。

$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');

@nb.njit def f(types=(nb.int8[::1], nb.float64[::1])): a = nb.typed.List.empty_list(types[0]) b = nb.typed.List.empty_list(types[1]) # and so on... 表示C型的一维[::1]

答案 1 :(得分:0)

让numba自己找出类型呢?

@nb.njit
def v():
    l=nb.typed.List()
    l.append(np.array([1,2,3], dtype=np.int8))
    return l
v()

为我返回ListType[array(int8, 1d, C)]([[1 2 3]])