torchscript中的torch.jit.trace和torch.jit.script有什么区别?

mid*_*gan 7 jit pytorch torchscript

Torchscript 提供了 torch.jit.trace 和 torch.jit.script 将 pytorch 代码从 eager 模式转换为 script 模型。从文档中,我可以理解 torch.jit.trace 无法处理 python 中存在的控制流和其他数据结构。因此,开发了 torch.jit.script 来克服 torch.jit.trace 中的问题。

但看起来 torch.jit.script 适用于所有情况,那么为什么我们需要 torch.jit.trace 呢?

请帮助我理解这两种方法之间的区别

小智 4

如果torch.jit.script适用于您的代码,那么这就是您所需要的。编译器不支持使用动态行为(例如多态性)的代码torch.jit.script,因此对于类似的情况,您需要使用torch.jit.trace.