torch.jit.ignore¶
- torch.jit.ignore(drop=False, **kwargs)[原始碼][原始碼]¶
此裝飾器 (decorator) 向編譯器表明,應忽略某個函式或方法,並將其保留為 Python 函式。 這允許您在模型中保留尚未與 TorchScript 相容的程式碼。 如果從 TorchScript 呼叫,被忽略的函式會將呼叫分派給 Python 直譯器。 具有被忽略函式的模型無法匯出;請改用
@torch.jit.unused
。範例(在方法上使用
@torch.jit.ignore
)import torch import torch.nn as nn class MyModule(nn.Module): @torch.jit.ignore def debugger(self, x): import pdb pdb.set_trace() def forward(self, x): x += 10 # The compiler would normally try to compile `debugger`, # but since it is `@ignore`d, it will be left as a call # to Python self.debugger(x) return x m = torch.jit.script(MyModule()) # Error! The call `debugger` cannot be saved since it calls into Python m.save("m.pt")
範例(在方法上使用
@torch.jit.ignore(drop=True)
)import torch import torch.nn as nn class MyModule(nn.Module): @torch.jit.ignore(drop=True) def training_method(self, x): import pdb pdb.set_trace() def forward(self, x): if self.training: self.training_method(x) return x m = torch.jit.script(MyModule()) # This is OK since `training_method` is not saved, the call is replaced # with a `raise`. m.save("m.pt")