【问题标题】:Python Numba issue (TypingError: Failed in nopython mode pipeline)Python Numba 问题(TypingError: Failed in nopython mode pipeline)
【发布时间】:2021-06-15 04:30:59
【问题描述】:

我刚开始熟悉如何使用 Numba 加速我的代码,所以我正在测试非常简单的函数:

import numpy as np
from numba import jit
from time import time

@jit(nopython = True)
def dotter(X):
    Z = np.dot(X,X.T)
    return(Z)

tic = time()
dotter(X)
toc = time()
print(1000*(toc-tic), ' ms')
X = np.random.randint(1, 1000, (1000, 1))

我收到此错误消息:

> --------------------------------------------------------------------------- TypingError                               Traceback (most recent call
> last) <ipython-input-8-ae7154a2d7d0> in <module>
>       1 tic = time()
> ----> 2 dotter(X)
>       3 toc = time()
>       4 print(1000*(toc-tic), ' ms')
> 
> ~/anaconda3/lib/python3.8/site-packages/numba/core/dispatcher.py in
> _compile_for_args(self, *args, **kws)
>     418                 e.patch_message(msg)
>     419 
> --> 420             error_rewrite(e, 'typing')
>     421         except errors.UnsupportedError as e:
>     422             # Something unsupported is present in the user code, add help info
> 
> ~/anaconda3/lib/python3.8/site-packages/numba/core/dispatcher.py in
> error_rewrite(e, issue_type)
>     359                 raise e
>     360             else:
> --> 361                 raise e.with_traceback(None)
>     362 
>     363         argtypes = []
> 
> TypingError: Failed in nopython mode pipeline (step: nopython
> frontend) No implementation of function Function(<function dot at
> 0x7fdbf7c19670>) found for signature:
>  
>  >>> dot(array(int64, 2d, C), array(int64, 2d, F))   There are 2 candidate implementations:
>       - Of which 2 did not match due to:
>       Overload in function 'dot': File: numba/core/typing/npydecl.py: Line 957.
>         With argument(s): '(array(int64, 2d, C), array(int64, 2d, F))':
>        Rejected as the implementation raised a specific error:
>          TypingError: np.dot() only supported on float and complex arrays   raised from
> /Users/Zsolti/anaconda3/lib/python3.8/site-packages/numba/core/typing/npydecl.py:942
> 
> During: resolving callee type: Function(<function dot at
> 0x7fdbf7c19670>) During: typing of call at
> <ipython-input-7-093e59547e26> (3)
> 
> 
> File "<ipython-input-7-093e59547e26>", line 3: def dotter(X):
>     Z = np.dot(X,X.T)
>     ^

我把X的定义改成了np.random.rand()解决了这个问题,还是不明白为什么numba不能理解random.randint。 有人可以向我解释吗? 提前致谢!

【问题讨论】:

    标签: python numpy numba


    【解决方案1】:

    答案似乎在这里:TypingError: np.dot() only supported on float and complex arrays

    Numba 通过重新实现 Numpy 的一个子集来工作。见the docs。在这种情况下,似乎np.dot 没有针对整数类型实现,这是您从np.random.randint 得到的。 np.matmul 也是如此。

    因此,要使用 Numba 进行 JIT,您首先必须转换为浮点数组。请注意,这比纯 python 实现要慢。另请注意提前致电dotter。这会触发 JIT 编译器,因此编译时间不包含在您的计时器中。

    import numpy as np
    from numba import jit
    from time import time
    
    @jit(nopython = True)
    def dotter(X):
        Z = np.dot(X,X.T)
        return(Z)
    
    X = np.random.randint(1, 1000, (1000, 1)).astype(float)
    z = dotter(X)
    
    tic = time()
    z = dotter(X).astype(int)
    toc = time()
    print(1000*(toc-tic), ' ms')
    

    当您拥有可以编译的 python for 循环时,Numba 真的很出色。在这个用例中,它可能会减慢您的速度,也可能会加快速度。

    【讨论】:

      猜你喜欢
      • 2019-08-21
      • 2020-08-08
      • 2016-12-10
      • 2019-07-30
      • 2018-03-19
      • 2018-11-17
      • 2021-07-24
      • 2017-06-21
      • 2017-08-08
      相关资源
      最近更新 更多