pytorch / 2 / generated / torch.nn.modules.module.register_module_forward_hook.html

torch.nn.modules.module.register_module_forward_hook

torch.nn.modules.module.register_module_forward_hook(hook, *, always_call=False) [source]

Registers a global forward hook for all the modules

Warning

This adds global state to the nn.module module and it is only intended for debugging/profiling purposes.

The hook will be called every time after forward() has computed an output. It should have the following signature:

hook(module, input, output) -> None or modified output

The input contains only the positional arguments given to the module. Keyword arguments won’t be passed to the hooks and only to the forward. The hook can modify the output. It can modify the input inplace but it will not have effect on forward since this is called after forward() is called.

Parameters
  • hook (Callable) – The user defined hook to be registered.
  • always_call (bool) – If True the hook will be run regardless of whether an exception is raised while calling the Module. Default: False
Returns

a handle that can be used to remove the added hook by calling handle.remove()

Return type

torch.utils.hooks.RemovableHandle

This hook will be executed before specific module hooks registered with register_forward_hook.

© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/2.1/generated/torch.nn.modules.module.register_module_forward_hook.html