Numba nopython模式下调用np.hstack出现类型错误的问题求助
我最近在Numba的nopython模式下编译一个简单函数时遇到了问题,代码如下:
from numba import njit import numpy as np @njit def fun(x,y): points = np.hstack((x,y)) return points a = 5 b = 2 res = fun(a,b)
这段代码去掉@njit装饰器后能正常运行,但加上装饰器就会抛出TypingError,报错信息如下:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function hstack at 0x7f491475fe50>) found for signature:hstack(UniTuple(int64 x 2))
There are 4 candidate implementations:
- Of which 4 did not match due to:
Overload in function '_OverloadWrapper._build..ol_generated': File: numba/core/overload_glue.py: Line 129.
With argument(s): '(UniTuple(int64 x 2))':
Rejected as the implementation raised a specific error:
TypeError: np.hstack(): expecting a non-empty tuple of arrays, got UniTuple(int64 x 2)
raised from /usr/local/lib/python3.8/dist-packages/numba/core/typing/npydecl.py:748During: resolving callee type: Function(<function hstack at 0x7f491475fe50>)
During: typing of call at(28) File "
", line 28:
def fun(x, y):
points = np.hstack((x, y))
^
我还尝试了传入一个标量和一个数组的情况(这也是我实际业务场景中会遇到的情况),结果还是报错:
修改后的调用代码:
b = np.ones(3) res = fun(a,b)
对应的报错信息:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function hstack at 0x7f491475fe50>) found for signature:hstack(Tuple(int64, array(float64, 1d, C)))
There are 4 candidate implementations:
- Of which 4 did not match due to:
Overload in function '_OverloadWrapper._build..ol_generated': File: numba/core/overload_glue.py: Line 129.
With argument(s): '(Tuple(int64, array(float64, 1d, C)))':
Rejected as the implementation raised a specific error:
TypeError: np.hstack(): expecting a non-empty tuple of arrays, got Tuple(int64, array(float64, 1d, C))
raised from /usr/local/lib/python3.8/dist-packages/numba/core/typing/npydecl.py:748During: resolving callee type: Function(<function hstack at 0x7f491475fe50>)
During: typing of call at(28) File "
", line 28:
def fun(x, y):
points = np.hstack((x, y))
而且我发现np.concatenate也有类似的问题,这真的让我很困惑。我现在使用的是Numba 0.56.4版本(应该是当时的稳定版),有没有大佬能给我一些解决建议?非常感谢!
备注:内容来源于stack exchange,提问作者FDP

