Hi - I’m writing some code that uses a tree structure, which I’ve defined as a jitclass using deferred types.
I would like to have a list of these trees stored inside another jitclass. However the following code produces an “AttributeError: ‘ClassInstanceType’ object has no attribute ‘class_type’”.
import numpy as np
from numba import int32, float32, int64, deferred_type, optional, typeof
from numba import njit
from numba.typed import List
from numba import types
from numba import typed
from numba import objmode
from numba.experimental import jitclass
# https://github.com/numba/numba-examples/blob/master/legacy/linkedlist.py for self-references in classes!
node_type = deferred_type()
@jitclass([("left",optional(node_type)),("right",optional(node_type))
])
class TreeNode:
isLeaf: bool
size: int
i: int
def __init__(self,size=0,#pred_vec=None,b_vec=None,
isLeaf=True,left=None,right=None,i=0):
self.isLeaf = isLeaf
self.size=size
self.left=left
self.right=right
self.i=i
node_type.define(TreeNode.class_type.instance_type)
tn1 = TreeNode()
tn1_type = typeof(tn1)
# no problems here - so putting tn1 into a list isn't a problem
l1 = typed.List([tn1])
l1
l1[0]
@jitclass([("t",types.ListType(tn1_type)) #https://numba.discourse.group/t/how-do-i-create-a-jitclass-that-takes-a-list-of-jitclass-objects/366/2
])
class SubProblem:
def __init__(self, t):
self.t = t
# ok here
sp1 = SubProblem(l1)
sptype = typeof(sp1)
# this throws an error - "AttributeError: 'ClassInstanceType' object has no attribute 'class_type'"
sp1.t
Doing any of the following allows the code to run without error (but all three are incompatible with my use case):
- Storing a single TreeNode rather than a list of them inside the SubProblem class
- Removing the deferred type and left/right properties of TreeNode
- Storing something other than a TreeNode (e.g. int32s) inside the SubProblem.t list.
Note also that putting a TreeNode inside a TypedList via the example of l1 above does not cause problems.
So it appears that there is something strange that happens when a jitclass that is a deferred type, a typed list, and another jitclass get together and party.
Not sure if this is a bug, or if it is expected, and if there is anything I can do about it?