espnet2.torch_utils.forward_adaptor.ForwardAdaptor
Less than 1 minute
espnet2.torch_utils.forward_adaptor.ForwardAdaptor
class espnet2.torch_utils.forward_adaptor.ForwardAdaptor(module: Module, name: str)
Bases: Module
Wrapped module to parallelize specified method
torch.nn.DataParallel parallelizes only “forward()” and, maybe, the method having the other name can’t be applied except for wrapping the module just like this class.
Examples
>>> class A(torch.nn.Module):
... def foo(self, x):
... ...
>>> model = A()
>>> model = ForwardAdaptor(model, "foo")
>>> model = torch.nn.DataParallel(model, device_ids=[0, 1])
>>> x = torch.randn(2, 10)
>>> model(x)
Initializes internal Module state, shared by both nn.Module and ScriptModule.
forward(*args, **kwargs)
Defines the computation performed at every call.
Should be overridden by all subclasses.
NOTE
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.