I have a module with some
torch.jit.exported methods and some regular methods. This module has some attributes that I want to use in non-scripted methods. These attributes disappear when I script the module.
class Foo: def __init__(self): # Commenting out this line gets rid of the AttributeError pathlib.Path("/tmp/tmp").mkdir(parents=True, exist_ok=True) self.y = 5 class M(nn.Module): def __init__(self, f): super(M, self).__init__() self.f = f @torch.jit.ignore def get_f(self): return self.f # <-- Getting an attribute error here m = torch.jit.script(M(Foo())) m.get_f()
I am curious if this is a bug or there is some rationale behind this. It seems like assigning
self.f = f in
M.__init__ makes torch script analyze
Foo.__init__ and discard the attribute if scripting it fails.
Because I never use this attribute in compiled methods, there should not be a reason to try compiling it and discarding. Maybe there is some annotation I can use to ask torch script to just keep this attribute attached to
M? Is there some “good” workaround?