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

win*_*win 5 python types numpy numba

与 一起工作时numba,我偶然发现了非常意想不到的行为。我创建了一个nb.njit函数,在其中尝试创建数组nb.typed.Listint8 numpy因此我尝试创建相应的numba类型。

nb.int8[:]  # type of the list elements
Run Code Online (Sandbox Code Playgroud)

因此,我将此类型设置为nb.typed.Listvialsttype关键字。

l = nb.typed.List(lsttype=nb.int8[:])  # list of int8 numpy ndarrays
Run Code Online (Sandbox Code Playgroud)

我得到的是:

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.
Run Code Online (Sandbox Code Playgroud)

我想,这意味着numba正在尝试对nb.int8类型对象进行切片,就好像它不理解符号一样。

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

nb.typeof(np.array([], dtype=np.int8))
Run Code Online (Sandbox Code Playgroud)

它返回了:

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'>)
Run Code Online (Sandbox Code Playgroud)

这个我不明白!怎么numba看不到自己

最小的例子非常简单:

import numba as nb
import numpy as np

@nb.njit
def v():
    print(nb.typeof(np.array([], dtype=np.int8)))

v()
Run Code Online (Sandbox Code Playgroud)

所以我尝试使用相同的功能,但没有@nb.njit.

并打印出来!

array(int8, 1d, C)
Run Code Online (Sandbox Code Playgroud)

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

numba.core.errors.UnsupportedError: Failed in nopython mode pipeline (step: analyzing                    bytecode)
Use of unsupported opcode (IMPORT_NAME) found
Run Code Online (Sandbox Code Playgroud)

我也尝试重新安装和更新numbanumpy

这是什么诡计?

win*_*win 4

我找到了一个令人讨厌的解决方法。

@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...
Run Code Online (Sandbox Code Playgroud)

[::1]numpy.ndarray表示C型的一维。