Python 如何进行嵌套类数组实例化

Python 如何进行嵌套类数组实例化,python,jit,numba,Python,Jit,Numba,我正试图使用numba来加速代码,需要在另一个jitclass中使用一个jitclass数组。然而,我似乎找不到将数组传递给JIT类的方法 from numba import jitclass, int32 my_inner_spec = [ ('a', int32) ] @jitclass(my_inner_spec) class MyInner: def __init__(self, a): self.a = a my_outer_spec = [('inner'

我正试图使用numba来加速代码,需要在另一个jitclass中使用一个jitclass数组。然而,我似乎找不到将数组传递给JIT类的方法

from numba import jitclass, int32

my_inner_spec = [
    ('a', int32)
]

@jitclass(my_inner_spec)
class MyInner:
  def __init__(self, a):
    self.a = a


my_outer_spec = [('inner', MyInner.class_type.instance_type[:])]

@jitclass(my_outer_spec)
class MyOuter:
  def __init__(self, inner):
    self.inner= inner

MyOuter([MyInner(0)])
这会产生以下错误:

TypingError                               Traceback (most recent call last)
<ipython-input-106-3f644c4985b0> in <module>()
     18     self.inner= inner
     19 
---> 20 MyOuter([MyInner(0)])

...
...

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.typeinfer.CallConstraint object at 0x7fbdde552550>.
Failed in nopython mode pipeline (step: nopython mode backend)
Cannot cast reflected list(instance.jitclass.MyInner#7fbdde6c8650<a:int32>) to array(instance.jitclass.MyInner#7fbdde6c8650<a:int32>, 1d, A): %".19" = load {i8*, i8*}, {i8*, i8*}* %"inner"

File "<ipython-input-106-3f644c4985b0>", line 18:
  def __init__(self, inner):
    self.inner= inner
    ^

[1] During: lowering "(self).inner = inner" at <ipython-input-106-3f644c4985b0> (18)
[2] During: resolving callee type: jitclass.MyOuter#7fbdde6c8810<inner:array(instance.jitclass.MyInner#7fbdde6c8650<a:int32>, 1d, A)>
[3] During: typing of call at <string> (3)

Enable logging at debug level for details.

File "<string>", line 3:
<source missing, REPL/exec in use?>
TypingError回溯(最近一次调用)
在()
18.自我内部=内部
19
--->20个MyOuter([MyInner(0)])
...
...
TypingError:在nopython模式管道中失败(步骤:nopython前端)
在的内部错误。
在nopython模式管道中失败(步骤:nopython模式后端)
无法将反射列表(instance.jitclass.MyInner#7fbdde6c8650)强制转换为数组(instance.jitclass.MyInner#7fbdde6c8650,1d,A):%.19“=加载{i8*,i8*},{i8*,i8*}*%“inner”
文件“”,第18行:
定义初始化(自,内部):
内在的
^
[1] 期间:在(18)处降低“(自身)。内部=内部”
[2] 期间:解析被调用方类型:jitclass.MyOuter#7fbdde6c8810
[3] 期间:在(3)处键入呼叫
在调试级别启用日志记录以了解详细信息。
文件“”,第3行:
比如说,typed.List是一个实验性功能,从普通Python列表推断类型似乎不起作用

下面的内容有点冗长,但它可以工作:

my_outer_spec = [('inner', nb.types.ListType(MyInner.class_type.instance_type))]

@nb.experimental.jitclass(my_outer_spec)
class MyOuter:
    def __init__(self, inner):
        self.inner = inner

lst = nb.typed.List()
lst.append(MyInner(0))
MyOuter(lst)