Numba jit 和延迟类型



我正在传递 numba 作为我的函数的签名

@numba.jit(numba.types.UniTuple(numba.float64[:, :], 2)(
numba.float64[:, :], numba.float64[:, :], numba.float64[:, :], 
earth_model_type))

其中earth_model_type定义为

earth_model_type = numba.deferred_type()
earth_model_type.define(em.EarthModel.class_type.instance_type)

它编译得很好,但是当我尝试调用该函数时,我得到

类型错误: 参数类型(s) array(float64, 2d, F), array(

float64, 2d, C), array(float64, 2d, F), instance.jitclass.EarthModel#7fd9c48dd668

在我看来,定义不匹配的参数类型与上面的类型几乎相同。另一方面,如果我不只使用@numba.jit(nopython=True)来指定签名,它工作正常,numba 编译的函数的签名是

ipdb> numbed_cowell_propagator_propagate.signatures                   
[(array(float64, 2d, F), array(float64, 2d,

C), array(float64, 2d, F), instance.jitclass.EarthModel#7f81bbc0e780)]

编辑

如果我使用常见问题解答中的方式强制执行 C 顺序数组,我仍然会收到错误

类型错误: 参数类型数组(float64, 2d, C), array(float64, 2d, C), array(float64, 2d, C), instance.jitclass.EarthModel#7f6edd8d57b8

我很确定问题与延迟类型有关,因为如果我不是传递 jit 类,而是从该类传递我需要的所有属性(4numba.float64s),它工作正常。

当我指定签名时,我做错了什么?

干杯。

如果不确切了解您的完整代码是如何工作的,我不确定为什么您需要使用延迟类型。通常,它用于包含相同类型的实例变量的 jitclass,例如链表或其他节点树,因此需要推迟到编译器处理类本身之后(请参阅源代码) 以下最小示例有效(如果我使用延迟类型,我可以重现您的错误):

import numpy as np
import numba as nb
spec = [('x', nb.float64)]
@nb.jitclass(spec)
class EarthModel:
def __init__(self, x):
self.x = x
earth_model_type = EarthModel.class_type.instance_type
@nb.jit(nb.float64(nb.float64[:, :], nb.float64[:, :], nb.float64[:, :], earth_model_type))
def test(x, y, z, em):
return em.x

然后运行它:

em = EarthModel(9.9)
x = np.random.normal(size=(3,3))
y = np.random.normal(size=(3,3))
z = np.random.normal(size=(3,3))
res = test(x, y, z, em)
print(res)  # 9.9

最新更新