Numpy Numba是否支持内置python函数,例如`setitem` TypingError:在nopython模式管道中失败(步骤:nopython前端) 未找到签名的函数()的实现:

Numpy Numba是否支持内置python函数,例如`setitem` TypingError:在nopython模式管道中失败(步骤:nopython前端) 未找到签名的函数()的实现:,numpy,numba,numpy-ufunc,numba-pro,Numpy,Numba,Numpy Ufunc,Numba Pro,当试图使用以下命令将SG_Z[SG_Z

当试图使用以下命令将
SG_Z[SG_ZSG_Z
矩阵中小于给定阈值的所有元素设置为零时,会遇到此错误


此命令用于使用Numba
@jit
并行化的函数中。由于该行的存在,函数没有运行。

您对
SG_Z
没有太多介绍,但我怀疑它是2d(或更高)
numba
的多维索引能力有限(与
numpy
相比)

一般来说,缺少的不是
setitem
numba
总是这样做。对于这个特殊的参数组合,它是
setitem

如果我先解开数组,它确实可以工作

In [137]: import numba
In [138]: @numba.njit
     ...: def foo(arr, thresh):
     ...:     arr[arr<.5]=0
     ...:     return arr
     ...: 
In [139]: foo(arr,.5)
Traceback (most recent call last):
  File "<ipython-input-139-33ea2fda1ea2>", line 1, in <module>
    foo(arr,.5)
  File "/usr/local/lib/python3.8/dist-packages/numba/core/dispatcher.py", line 420, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/usr/local/lib/python3.8/dist-packages/numba/core/dispatcher.py", line 361, in error_rewrite
    raise e.with_traceback(None)
TypingError: No implementation of function Function(<built-in function setitem>) found for signature:
 
 >>> setitem(array(float64, 2d, C), array(bool, 2d, C), Literal[int](0))
 
There are 16 candidate implementations:
  - Of which 14 did not match due to:
  Overload of function 'setitem': File: <numerous>: Line N/A.
    With argument(s): '(array(float64, 2d, C), array(bool, 2d, C), int64)':
   No match.
  - Of which 2 did not match due to:
  Overload in function 'SetItemBuffer.generic': File: numba/core/typing/arraydecl.py: Line 171.
    With argument(s): '(array(float64, 2d, C), array(bool, 2d, C), int64)':
   Rejected as the implementation raised a specific error:
     TypeError: unsupported array index type array(bool, 2d, C) in [array(bool, 2d, C)]
  raised from /usr/local/lib/python3.8/dist-packages/numba/core/typing/arraydecl.py:68

During: typing of setitem at <ipython-input-138-6861f217f595> (3)
但是在
numba
中,我们不需要害怕迭代,因此对于2d输入,我们可以对行进行迭代:

In [140]: foo(arr.ravel(),.5)
Out[140]: 
array([0.8466427 , 0.        , 0.        , 0.        , 0.86591184,
       0.        , 0.        , 0.9811717 , 0.        , 0.        ,
       0.54819722, 0.78863841])
[148]中的
:@numba.njit
…:def foo(arr,thresh):
…:对于arr中的i:
…:我
In [137]: import numba
In [138]: @numba.njit
     ...: def foo(arr, thresh):
     ...:     arr[arr<.5]=0
     ...:     return arr
     ...: 
In [139]: foo(arr,.5)
Traceback (most recent call last):
  File "<ipython-input-139-33ea2fda1ea2>", line 1, in <module>
    foo(arr,.5)
  File "/usr/local/lib/python3.8/dist-packages/numba/core/dispatcher.py", line 420, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/usr/local/lib/python3.8/dist-packages/numba/core/dispatcher.py", line 361, in error_rewrite
    raise e.with_traceback(None)
TypingError: No implementation of function Function(<built-in function setitem>) found for signature:
 
 >>> setitem(array(float64, 2d, C), array(bool, 2d, C), Literal[int](0))
 
There are 16 candidate implementations:
  - Of which 14 did not match due to:
  Overload of function 'setitem': File: <numerous>: Line N/A.
    With argument(s): '(array(float64, 2d, C), array(bool, 2d, C), int64)':
   No match.
  - Of which 2 did not match due to:
  Overload in function 'SetItemBuffer.generic': File: numba/core/typing/arraydecl.py: Line 171.
    With argument(s): '(array(float64, 2d, C), array(bool, 2d, C), int64)':
   Rejected as the implementation raised a specific error:
     TypeError: unsupported array index type array(bool, 2d, C) in [array(bool, 2d, C)]
  raised from /usr/local/lib/python3.8/dist-packages/numba/core/typing/arraydecl.py:68

During: typing of setitem at <ipython-input-138-6861f217f595> (3)
In [140]: foo(arr.ravel(),.5)
Out[140]: 
array([0.8466427 , 0.        , 0.        , 0.        , 0.86591184,
       0.        , 0.        , 0.9811717 , 0.        , 0.        ,
       0.54819722, 0.78863841])
In [148]: @numba.njit
     ...: def foo(arr, thresh):
     ...:     for i in arr:
     ...:         i[i<thresh] = 0
     ...:     return arr
     ...: 
In [149]: foo(arr,.5)
Out[149]: 
array([[0.8466427 , 0.        , 0.        , 0.        ],
       [0.86591184, 0.        , 0.        , 0.9811717 ],
       [0.        , 0.        , 0.54819722, 0.78863841]])