# tensorplay.nn.modules.module API Source: https://www.tensorplay.cn/docs/api/tensorplay.nn.modules.module.html ## Functions 7 [#](#api-tensorplay.nn.modules.module.register_module_buffer_registration_hook) ### register_module_buffer_registration_hook function[Full reference ↗](/docs/generated/tensorplay.nn.modules.module.register_module_buffer_registration_hook.html) ```python tensorplay.nn.modules.module.register_module_buffer_registration_hook(hook: Callable[[...], None]) → RemovableHandle ``` Register a buffer registration hook common to all modules. > **Warning** > > This adds global state to the nn.Module module The hook will be called every time register_buffer() is invoked. It should have the following signature: ``` hook(module, name, buffer) -> None or new buffer ``` The hook can modify the input or return a single modified value in the hook. Returns: a handle that can be used to remove the added hook by calling handle.remove() Return type: tensorplay.utils.hooks.RemovableHandle [#](#api-tensorplay.nn.modules.module.register_module_forward_hook) ### register_module_forward_hook function[Full reference ↗](/docs/generated/tensorplay.nn.modules.module.register_module_forward_hook.html) ```python tensorplay.nn.modules.module.register_module_forward_hook(hook: Callable[[...], None], *, with_kwargs: bool = False, always_call: bool = False) → RemovableHandle ``` Register 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. You can optionally modify the output of the module by returning a new value that will replace the output from the forward() function. Parameters: - hook (Callable) – The user defined hook to be registered. - always_call ([bool](https://docs.python.org/3/builtins/functions.html#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: tensorplay.utils.hooks.RemovableHandle This hook will be executed before specific module hooks registered with register_forward_hook. [#](#api-tensorplay.nn.modules.module.register_module_forward_pre_hook) ### register_module_forward_pre_hook function[Full reference ↗](/docs/generated/tensorplay.nn.modules.module.register_module_forward_pre_hook.html) ```python tensorplay.nn.modules.module.register_module_forward_pre_hook(hook: Callable[[...], None]) → RemovableHandle ``` Register a forward pre-hook common to all 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 before forward() is invoked. It should have the following signature: ``` hook(module, input) -> None or modified input ``` 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 input. User can either return a tuple or a single modified value in the hook. We will wrap the value into a tuple if a single value is returned(unless that value is already a tuple). This hook has precedence over the specific module hooks registered with register_forward_pre_hook. Returns: a handle that can be used to remove the added hook by calling handle.remove() Return type: tensorplay.utils.hooks.RemovableHandle [#](#api-tensorplay.nn.modules.module.register_module_full_backward_hook) ### register_module_full_backward_hook function[Full reference ↗](/docs/generated/tensorplay.nn.modules.module.register_module_full_backward_hook.html) ```python tensorplay.nn.modules.module.register_module_full_backward_hook(hook: Callable[[Module, tuple[Tensor, ...] | Tensor, tuple[Tensor, ...] | Tensor], None | tuple[Tensor, ...] | Tensor]) → RemovableHandle ``` Register a backward hook common to all the modules. > **Warning** > > This adds global state to the nn.module module and it is only intended for debugging/profiling purposes. Hooks registered using this function behave in the same way as those registered by tensorplay.nn.Module.register_full_backward_hook(). Refer to its documentation for more details. Hooks registered using this function will be called before hooks registered using tensorplay.nn.Module.register_full_backward_hook(). Returns: a handle that can be used to remove the added hook by calling handle.remove() Return type: tensorplay.utils.hooks.RemovableHandle [#](#api-tensorplay.nn.modules.module.register_module_full_backward_pre_hook) ### register_module_full_backward_pre_hook function[Full reference ↗](/docs/generated/tensorplay.nn.modules.module.register_module_full_backward_pre_hook.html) ```python tensorplay.nn.modules.module.register_module_full_backward_pre_hook(hook: Callable[[Module, tuple[Tensor, ...] | Tensor], None | tuple[Tensor, ...] | Tensor]) → RemovableHandle ``` Register a backward pre-hook common to all the modules. > **Warning** > > This adds global state to the nn.module module and it is only intended for debugging/profiling purposes. Hooks registered using this function behave in the same way as those registered by tensorplay.nn.Module.register_full_backward_pre_hook(). Refer to its documentation for more details. Hooks registered using this function will be called before hooks registered using tensorplay.nn.Module.register_full_backward_pre_hook(). Returns: a handle that can be used to remove the added hook by calling handle.remove() Return type: tensorplay.utils.hooks.RemovableHandle [#](#api-tensorplay.nn.modules.module.register_module_module_registration_hook) ### register_module_module_registration_hook function[Full reference ↗](/docs/generated/tensorplay.nn.modules.module.register_module_module_registration_hook.html) ```python tensorplay.nn.modules.module.register_module_module_registration_hook(hook: Callable[[...], None]) → RemovableHandle ``` Register a module registration hook common to all modules. > **Warning** > > This adds global state to the nn.Module module The hook will be called every time register_module() is invoked. It should have the following signature: ``` hook(module, name, submodule) -> None or new submodule ``` The hook can modify the input or return a single modified value in the hook. Returns: a handle that can be used to remove the added hook by calling handle.remove() Return type: tensorplay.utils.hooks.RemovableHandle [#](#api-tensorplay.nn.modules.module.register_module_parameter_registration_hook) ### register_module_parameter_registration_hook function[Full reference ↗](/docs/generated/tensorplay.nn.modules.module.register_module_parameter_registration_hook.html) ```python tensorplay.nn.modules.module.register_module_parameter_registration_hook(hook: Callable[[...], None]) → RemovableHandle ``` Register a parameter registration hook common to all modules. > **Warning** > > This adds global state to the nn.Module module The hook will be called every time register_parameter() is invoked. It should have the following signature: ``` hook(module, name, param) -> None or new parameter ``` The hook can modify the input or return a single modified value in the hook. Returns: a handle that can be used to remove the added hook by calling handle.remove() Return type: tensorplay.utils.hooks.RemovableHandle