Skip to content

Instantly share code, notes, and snippets.

@wanchaol
Created January 29, 2020 17:56
Show Gist options
  • Save wanchaol/2892c2539a3e0cfed41bcd93cf4dbd2f to your computer and use it in GitHub Desktop.
Save wanchaol/2892c2539a3e0cfed41bcd93cf4dbd2f to your computer and use it in GitHub Desktop.
class MyScriptModule(torch.jit.ScriptModule):
def __init__(self):
super().__init__()
self.a = torch.randn(10)
@torch.jit.script_method
def my_method(self):
return self.a
@torch.jit.ignore
def mod_init():
return MyScriptModule()
@torch.jit.script
def test_script_mod():
# type: () -> Tensor
# call a jit ignore function to initialize a scriptmodule (since we can't do it in a script function)
res = mod_init()
# but res here is being treated default as tensor type from the ignore function, so we can't call the method
return res.my_method()
=============
RuntimeError:
Tried to access nonexistent attribute or method 'my_method' of type 'Tensor'.:
File "/home/wanchaol/test_script.py", line 28
# type: () -> Tensor
res = mod_init()
return res.my_method()
~~~~~~~~~~~~~ <--- HERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment