Rate this Page

torch.jit.ignore#

torch.jit.ignore(drop=False,**kwargs)[source]#

This decorator indicates to the compiler that a function or method shouldbe ignored and left as a Python function. This allows you to leave code inyour model that is not yet TorchScript compatible. If called from TorchScript,ignored functions will dispatch the call to the Python interpreter. Models with ignoredfunctions cannot be exported; use@torch.jit.unused instead.

Example (using@torch.jit.ignore on a method):

importtorchimporttorch.nnasnnclassMyModule(nn.Module):@torch.jit.ignoredefdebugger(self,x):importpdbpdb.set_trace()defforward(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 Pythonself.debugger(x)returnxm=torch.jit.script(MyModule())# Error! The call `debugger` cannot be saved since it calls into Pythonm.save("m.pt")

Example (using@torch.jit.ignore(drop=True) on a method):

importtorchimporttorch.nnasnnclassMyModule(nn.Module):@torch.jit.ignore(drop=True)deftraining_method(self,x):importpdbpdb.set_trace()defforward(self,x):ifself.training:self.training_method(x)returnxm=torch.jit.script(MyModule())# This is OK since `training_method` is not saved, the call is replaced# with a `raise`.m.save("m.pt")