Numba:在jitted函数中访问Class属性

时间:2017-06-20 16:59:49

标签: python numba

我有以下简单的问题。

import numba
import numpy as np

class MyClass(object):
   def __init__(self):
       self.Value=0.0
       self.Array=np.array([],dtype=np.float64)

@numba.jit(nopython=True)
def Fun(ClassInstance):
    print ClassInstance.Array*1.0

当我执行这些简单的行

A=MyClass()
Fun(A)

我收到以下错误:

numba/dataflow.py:346: RuntimeWarning: Python2 style print partially supported.  Please use Python3 style print.
  "Python3 style print.", RuntimeWarning)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "numba/dispatcher.py", line 310, in _compile_for_args
    raise e
numba.errors.UntypedAttributeError: Caused By:
Traceback (most recent call last):
  File "numba/compiler.py", line 230, in run
    stage()
  File "numba/compiler.py", line 444, in stage_nopython_frontend
    self.locals)
  File "numba/compiler.py", line 800, in type_inference_stage
    infer.propagate()
  File "numba/typeinfer.py", line 767, in propagate
    raise errors[0]
UntypedAttributeError: Unknown attribute 'Array' of type pyobject
File "bumba.py", line 11
[1] During: typing of get attribute at bumba.py (11)

Failed at nopython (nopython frontend)
Unknown attribute 'Array' of type pyobject
File "bumba.py", line 11
[1] During: typing of get attribute at bumba.py (11)

This error may have been caused by the following argument(s):
- argument 0: cannot determine Numba type of <class '__main__.MyClass'>

为什么numba不认识阵列让我感到惊讶。由于它具有这样的基本功能,我必须假设我遗漏了一些至关重要的东西,但到目前为止我还没有找到任何解决方案。

1 个答案:

答案 0 :(得分:1)

正如@ user2537112在评论中指出的那样,Numba nopython jit选项导致通过PyObject_GetAttr调用Python C API,因为Numba目前不支持属性访问。变通方法包括将对象属性直接传递给函数而不是通过对象实例。