5

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

2 Answers2

3

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.

dogman288
  • 613
  • 3
  • 9
0

This is a deep dive into differences between the two which outlines when the "default recommended" jit.script is worse than jit.trace https://ppwwyyxx.com/blog/2022/TorchScript-Tracing-vs-Scripting/

rtviii
  • 807
  • 8
  • 19