为什么Numba无法编译此功能?

时间:2020-07-11 06:56:25

标签: python python-3.x numpy numba

我正在尝试使用Numba编译以下函数:

@njit(fastmath=True, nogil=True)
def generate_items(array, start):
    array_positions = np.empty(SIZE, dtype=np.int64)
    count = 0
    while count < SIZE - start:
        new_array = mutate(np.empty(0, dtype=np.uint8))

        if len(new_array) > 0:
            array_positions[count] = len(array)  # <<=== FAILS HERE
            array = np.append(array, np.append(new_array, 255))
            count += 1

    return array, array_positions

但是它在上面指示的行上失败,并显示以下错误消息:

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Cannot unify array(uint8, 1d, C) and array(int64, 1d, C) for 'array.3', defined at ...

这似乎没有什么意义,因为我只是将intlen的结果)分配给一个dtypenp.int64的数组?

请注意,array的类型为np.uint8-但我不是在分配数组本身,因此该消息对我来说毫无意义。

我试图对此进行重构:

tmp = len(array)  # <<=== FAILS HERE
array_positions[count] = tmp  

但是随后失败了...相同的消息。

我也尝试用len(array)替换array.size,因为这是一维数组,但有相同的错误。

谁能看到为什么失败?

我使用的是Python 3.7和Numba 0.50

谢谢!

1 个答案:

答案 0 :(得分:0)

第一个问题是您使用了不受支持的功能mutateas stated here)。 接下来,如错误所示,您尝试在其中添加具有不同数据类型的数组

np.append(array, np.append(new_array, 255))

其中array的类型为int64,而new_array的类型值为uint8。如果您使用过@jit,它会向您显示警告,提示您将跌落到object模式,但是由于您已经通过使用nonpython装饰器来强制执行@njit模式,它抛出一个错误。 干杯。