I’ve faced similar issues during deploying scripted model on Android.
The first question is “How to check whole grap or code of scripted model?”. ScriptedModule.code provides only upper level code, but inside stacktrace on mobile I see more information about how the code looks like.
Second is about isinstance(). My model has plenty of isinstance(tensor, torch.Tensor) or such which are converted to in tensor1 = unchecked_cast(Tensor, tensor) script code. Here comes an error.
Why there’s no error while exporting/scripting of module?
As for 1), we recently changed the behavior so that functions in .code and .graph appear as function calls (previously we were inlining the function bodies). So we’re still missing the functionality to show the called functions. For now you can re-enable inlining to see the entire graph:
def other_fn(x):
return x + 10
# Change the inlining mode before you compile
torch._C._jit_set_inline_everything_mode(True)
@torch.jit.script
def fn(x):
return other_fn(x)
print(fn.code)
print(fn.graph)
__init__ on nn.Modules runs in Python (torch.jit.script only sees the module after it has been initialized), so you can annotate that with whatever Python annotations you want (but they won’t be enforced by the compiler at all, for that you should use something like mypy).
For methods that are compiled (e.g. forward and anything it calls), the return types can be deduced from the code. If you want to explicitly write it out, you can use any of the type annotations listed here.
Unions aren’t supported so they won’t work in TorchScript. As a workaround you could do something like Tuple[Optional[int], Optional[List[str]]].