What are the differences between torch.jit.trace and torch.jit.script in torchscript?

Viewed 2601

Torchscript provides torch.jit.trace and torch.jit.script to convert pytorch code from eager mode to script model. From the documentation, I can understand torch.jit.trace cannot handle control flows and other data structures present in the python. Hence torch.jit.script was developed to overcome the problems in torch.jit.trace.

But it looks like torch.jit.script works for all the cases, then why do we need torch.jit.trace?

Please help me understand the difference between these two methods

1 Answers

If torch.jit.script works for your code, then that's all you should need. Code that uses dynamic behavior such as polymorphism isn't supported by the compiler torch.jit.script uses, so for cases like that, you would need to use torch.jit.trace.

Related