numba.errors.TypingError: 在 nopython 模式管道中失败(步骤:nopython 前端)



我想使用numba加速我的python代码,如下所示:

import numpy as np
from numba import njit, jit
import time
@njit
def dist_gpu(a, b):
d = np.linalg.norm(a[:, None, :] - b[None, :, :], axis=2)
d = np.transpose(d)
sorted_d = np.sort(d)
sorted_ind = np.argsort(d)
return sorted_d, sorted_ind
def get_a_b(r=10**4,c=10**1):
a = np.random.uniform(-1, 1, (r, c)).astype('f')
b = np.random.uniform(-1, 1, (r, c)).astype('f')
return a,b
if __name__ == "__main__":
a, b = get_a_b()
st_t = time.time()
dist_gpu(a,b)
print('it took {} s'.format(time.time()-st_t))

我为我的函数使用装饰器@njit@jit(nopython=True)时收到以下错误:

Traceback (most recent call last):
File "stackoverflow_Q.py", line 22, in <module>
dist_gpu(a,b)
File "/usr/local/lib/python2.7/dist-packages/numba/dispatcher.py", line 401, in _compile_for_args
error_rewrite(e, 'typing')
File "/usr/local/lib/python2.7/dist-packages/numba/dispatcher.py", line 344, in error_rewrite
reraise(type(e), e, None)
File "<string>", line 2, in reraise
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<built-in function getitem>) with argument(s) of type(s): (array(float32, 2d, C), Tuple(slice<a:b>, none, slice<a:b>))
* parameterized
In definition 0:
All templates rejected with literals.
In definition 1:
All templates rejected without literals.
In definition 2:
All templates rejected with literals.
In definition 3:
All templates rejected without literals.
In definition 4:
All templates rejected with literals.
In definition 5:
All templates rejected without literals.
In definition 6:
All templates rejected with literals.
In definition 7:
All templates rejected without literals.
In definition 8:
TypeError: unsupported array index type none in Tuple(slice<a:b>, none, slice<a:b>)
In definition 9:
TypeError: unsupported array index type none in Tuple(slice<a:b>, none, slice<a:b>)
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: typing of intrinsic-call at stackoverflow_Q.py (8)
[2] During: typing of static-get-item at stackoverflow_Q.py (8)
File "stackoverflow_Q.py", line 8:
def dist_gpu(a, b):
d = np.linalg.norm(a[:, None, :] - b[None, :, :], axis=2)
^
This is not usually a problem with Numba itself but instead often caused by
the use of unsupported features or an issue in resolving types.
To see Python/NumPy features supported by the latest release of Numba visit:
http://numba.pydata.org/numba-doc/latest/reference/pysupported.html
and
http://numba.pydata.org/numba-doc/latest/reference/numpysupported.html
For more information about typing errors and how to debug them visit:
http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#my-code-doesn-t-compile
If you think your code should work with Numba, please report the error message
and traceback, along with a minimal reproducer at:
https://github.com/numba/numba/issues/new

任何人都可以使用numba帮助我做错的事情,即使没有它就不会发生此错误?

也许可以尝试一下

@njit
def dist_gpu(a, b):
d = np.linalg.norm(np.expand_dims(a, 1) - np.expand_dims(b, 0), axis=2)

AFAIK,努姆巴(还)不喜欢None,也不喜欢np.newaxis......并且一次仅扩展 1 个轴np.expand_dims工作。

相关内容

  • 没有找到相关文章

最新更新