Skip to content

Ambiguous docstring on register_module_forward_hook #74024

@BrandoZhang

Description

@BrandoZhang

📚 The doc issue

In current docstring of forward_hook, it says:

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
:func:`forward` is called.

What's the meaning of It can modify the input inplace but it will not have effect on forward while The hook can modify the output. also holds?

When I check the implementation of Module, the result of forward is indeed overridden by hook_result as long as the hook function returns a modified result.

if _global_forward_hooks or self._forward_hooks:
for hook in (*_global_forward_hooks.values(), *self._forward_hooks.values()):
hook_result = hook(self, input, result)
if hook_result is not None:
result = hook_result

Suggest a potential alternative/fix

I suppose the above docstring to be modified as follows:

     The hook can modify the module output as long as it returns 
     the modified output. It cannot modify the given argument `output` 
     inplace since this is called after :func:`forward` is called. 

cc @brianjo @mruberry @albanD @jbschlosser @walterddr @kshitij12345

Metadata

Metadata

Assignees

No one assigned

    Labels

    actionablemodule: docsRelated to our documentation, both in docs/ and docblocksmodule: nnRelated to torch.nntriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions