Hi,
currently I have to convert a model, and struggle to script a class that contains a parent member of the same class type.
As
@torch.jit.script
class Tree(object):
def __init__(self):
self.parent:List[Tree]=[]
results in
RuntimeError:
Assignment to attribute 'parent' cannot be of a type that contains class '__torch__.Tree '.
Classes that recursively contain instances of themselves are not yet supported:
I don’t think, this issue has already been tackled in TorchScript, so
can someone please give me a hint, how to work around this problem?
So far, I have tried:
class Model(Module):
def __init__(self, parent:Any)->None:
super().__init__()
self.parent = [parent]
def do_something(self, x:Tensor)->Tensor: return x
def forward(self, x:Tensor)->Tensor:
src = self
if torch.jit.isinstance(self.parent[0], Model):
src=self.parent[0]
print('using parent')
return src.do_something(x)
p = Model(None)
m = Model(p)
m(torch.rand(1,1))
xxx = torch.jit.script(m)
but not really successful… 'Model' has no attribute 'parent'