Numba KeyError with Python3.4, `KeyError: "Does not support option: 'arg_types'"`

Numba KeyError with Python3.4, `KeyError: "Does not support option: 'arg_types'"`

我正在将 Python2.7 numba 代码转换为 Python3.4。此函数 pairwise_distance 从多维数组 XY 转换距离矩阵。

但是,我使用 numba 装饰器 @jit 来加速代码:

import numpy as np
from numba import double
from numba.decorators import jit

@jit(arg_types = [double[:,:], double[:,:]])
def pairwise_distance(X, D):
    M = X.shape[0]
    N = X.shape[1]
    for i in range(M):
        for j in range(M):
            d = 0.0
            for k in range(N):
                tmp = X[i, k] - X[j, k]
                d += tmp * tmp
            D[i, j] = np.sqrt(d)

# calculate the pairwise distance between X and Y

X = np.random.random((1000, 3))
Y = np.empty((1000, 1000))
pairwise_distance(X, Y)

这会输出以下错误:

KeyError: "Does not support option: 'arg_types'"

我不完全确定这个错误是什么意思,或者如何将此从 Python2.7 翻译成与 Python3.4

兼容

这是完整的错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numba/targets/options.py in from_dict(self, dic)
     15             try:
---> 16                 ctor = self.OPTIONS[k]
     17             except KeyError:

KeyError: 'arg_types'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-15-2c486d04f659> in <module>()
     19 X = np.random.random((1000, 3))
     20 Y = np.empty((1000, 1000))
---> 21 pairwise_numba(X, Y)

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
    286             else:
    287                 real_args.append(self.typeof_pyval(a))
--> 288         return self.compile(tuple(real_args))
    289 
    290     def inspect_llvm(self, signature=None):

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numba/dispatcher.py in compile(self, sig)
    504 
    505             self._cache_misses[sig] += 1
--> 506             cres = self._compiler.compile(args, return_type)
    507             self.add_overload(cres)
    508             self._cache.save_overload(sig, cres)

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numba/dispatcher.py in compile(self, args, return_type)
     76     def compile(self, args, return_type):
     77         flags = compiler.Flags()
---> 78         self.targetdescr.options.parse_as_flags(flags, self.targetoptions)
     79 
     80         impl = self._get_implementation(args, {})

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numba/targets/options.py in parse_as_flags(cls, flags, options)
     24     def parse_as_flags(cls, flags, options):
     25         opt = cls()
---> 26         opt.from_dict(options)
     27         opt.set_flags(flags)
     28         return flags

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numba/targets/options.py in from_dict(self, dic)
     17             except KeyError:
     18                 fmt = "Does not support option: '%s'"
---> 19                 raise KeyError(fmt % k)
     20             else:
     21                 self.values[k] = ctor(v)

KeyError: "Does not support option: 'arg_types'"

当我使用 argtypes 而不是 arg_types 时,我没有得到 KeyError,而是收到一条弃用警告,提示我改为使用 signature

以下使用 python 3.5 和 numba 0.25.0

对我有用
import numpy as np
from numba import jit

@jit('void(double[:,:], double[:,:])')
def pairwise_distance(X, D):
    M = X.shape[0]
    N = X.shape[1]
    for i in range(M):
        for j in range(M):
            d = 0.0
            for k in range(N):
                tmp = X[i, k] - X[j, k]
                d += tmp * tmp
            D[i, j] = np.sqrt(d)

# calculate the pairwise distance between X and Y

X = np.random.random((1000, 3))
Y = np.empty((1000, 1000))
pairwise_distance(X, Y)