Skip to content

Commit a26e5e2

Browse files
Matthew Hoffmanpytorchmergebot
authored andcommitted
Improve type hints for Module forward hooks (#92061)
Fixes #91654. Currently, the `hook` parameters of `nn.Module.register_forward_pre_hook` and `nn.Module.register_forward_hook` are typed as `Callable[..., None]`, which 1) does not enable the validation of the signature of `hook` and 2) incorrectly restricts the return type of `hook`, which the docstrings of these methods themselves state can be non-`None`. The typing of the first parameter of `hook` as `TypeVar("T", bound="Module")` allows the binding of `Callable` whose first parameter is a subclass of `Module`. --- Here are some examples of: 1. forward hooks and pre-hook hooks being accepted by mypy according to the new type hints 2. mypy throwing errors d.t. incorrect `hook` signatures 3. false negatives of pre-hooks being accepted as forward hooks 4. false negatives of hooks with kwargs being accepted irrespective of the value provided for `with_kwargs` ```python from typing import Any, Dict, Tuple import torch from torch import nn def forward_pre_hook( module: nn.Linear, args: Tuple[torch.Tensor, ...], ) -> None: ... def forward_pre_hook_return_input( module: nn.Linear, args: Tuple[torch.Tensor, ...], ) -> Tuple[torch.Tensor, ...]: ... def forward_pre_hook_with_kwargs( module: nn.Linear, args: Tuple[torch.Tensor, ...], kwargs: Dict[str, Any], ) -> None: ... def forward_pre_hook_with_kwargs_return_input( module: nn.Linear, args: Tuple[torch.Tensor, ...], kwargs: Dict[str, Any], ) -> Tuple[Tuple[torch.Tensor, ...], Dict[str, Any]]: ... def forward_hook( module: nn.Linear, args: Tuple[torch.Tensor, ...], output: torch.Tensor, ) -> None: ... def forward_hook_return_output( module: nn.Linear, args: Tuple[torch.Tensor, ...], output: torch.Tensor, ) -> torch.Tensor: ... def forward_hook_with_kwargs( module: nn.Linear, args: Tuple[torch.Tensor, ...], kwargs: Dict[str, Any], output: torch.Tensor, ) -> None: ... def forward_hook_with_kwargs_return_output( module: nn.Linear, args: Tuple[torch.Tensor, ...], kwargs: Dict[str, Any], output: torch.Tensor, ) -> torch.Tensor: ... model = nn.Module() # OK model.register_forward_pre_hook(forward_pre_hook) model.register_forward_pre_hook(forward_pre_hook_return_input) model.register_forward_pre_hook(forward_pre_hook_with_kwargs, with_kwargs=True) model.register_forward_pre_hook(forward_pre_hook_with_kwargs_return_input, with_kwargs=True) model.register_forward_hook(forward_hook) model.register_forward_hook(forward_hook_return_output) model.register_forward_hook(forward_hook_with_kwargs, with_kwargs=True) model.register_forward_hook(forward_hook_with_kwargs_return_output, with_kwargs=True) # mypy(error): [arg-type] model.register_forward_pre_hook(forward_hook) model.register_forward_pre_hook(forward_hook_return_output) model.register_forward_pre_hook(forward_hook_with_kwargs) model.register_forward_pre_hook(forward_hook_with_kwargs_return_output) model.register_forward_hook(forward_pre_hook) model.register_forward_hook(forward_pre_hook_return_input) # false negatives model.register_forward_hook(forward_pre_hook_with_kwargs) model.register_forward_hook(forward_pre_hook_with_kwargs_return_input) model.register_forward_pre_hook(forward_pre_hook_with_kwargs, with_kwargs=False) model.register_forward_pre_hook(forward_pre_hook_with_kwargs_return_input, with_kwargs=False) ... ``` --- Though it is not functional as of mypy 0.991, the ideal typing of these methods would use [`typing.Literal`](https://mypy.readthedocs.io/en/stable/literal_types.html#literal-types): ```python T = TypeVar("T", bound="Module") class Module: @overload def register_forward_hook( self, hook: Callable[[T, Tuple[Any, ...], Any], Optional[Any]], *, prepend: bool = ..., with_kwargs: Literal[False] = ..., ) -> RemovableHandle: ... @overload def register_forward_hook( self, hook: Callable[[T, Tuple[Any, ...], Dict[str, Any], Any], Optional[Any]], *, prepend: bool = ..., with_kwargs: Literal[True] = ..., ) -> RemovableHandle: ... def register_forward_hook(...): ... ``` which would: 1. validate the signature of `hook` according to the corresponding literal value provided for `with_kwargs` (and fix the false negative examples above) 2. implicitly define the [fallback `bool` signature](python/mypy#6113 (comment)) e.g. to handle if a non-literal is provided for `with_kwargs` Pull Request resolved: #92061 Approved by: https://github.com/albanD
1 parent 890b682 commit a26e5e2

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎torch/distributed/_composable/checkpoint_activation.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def checkpoint(module: nn.Module, *, use_reentrant: bool = True) -> nn.Module:
213213
214214
"""
215215

216-
def forward_pre_hook(module: nn.Module, inputs: Tuple[Any]) -> None:
216+
def forward_pre_hook(module: nn.Module, inputs: Tuple[Any, ...]) -> None:
217217
if checkpoint.state(module).enable_hook:
218218
checkpoint.state(module).orig_grad_enabled = torch.is_grad_enabled()
219219
if checkpoint.state(module).use_reentrant:
@@ -253,7 +253,7 @@ def forward_pre_hook(module: nn.Module, inputs: Tuple[Any]) -> None:
253253
saved_tensor_hooks.__enter__()
254254
checkpoint.state(module).saved_tensor_hooks = saved_tensor_hooks
255255

256-
def forward_hook(module: nn.Module, inputs: Tuple[Any], output: Any) -> Any:
256+
def forward_hook(module: nn.Module, inputs: Tuple[Any, ...], output: Any) -> Any:
257257
if checkpoint.state(module).enable_hook:
258258
torch.set_grad_enabled(checkpoint.state(module).orig_grad_enabled)
259259
if checkpoint.state(module).use_reentrant:

‎torch/distributed/_composable/replicate.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def init_helper(self) -> None:
7979

8080
self._ddp = _ddp.DistributedDataParallel(self._param_list, **self.kwargs)
8181

82-
def forward_pre_hook(self, module: nn.Module, input: Tuple[torch.Tensor]) -> None:
82+
def forward_pre_hook(self, module: nn.Module, input: Tuple[torch.Tensor, ...]) -> None:
8383
self.init_helper()
8484
self._ddp.pre_forward()
8585

‎torch/distributed/nn/api/remote_module.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,21 @@ def register_backward_hook( # type: ignore[return]
367367

368368
def register_forward_pre_hook( # type: ignore[return]
369369
self,
370-
hook: Callable[..., None],
370+
hook: Union[
371+
Callable[[T, Tuple[Any, ...]], Optional[Any]],
372+
Callable[[T, Tuple[Any, ...], Dict[str, Any]], Optional[Tuple[Any, Dict[str, Any]]]],
373+
],
371374
prepend: bool = False,
372375
with_kwargs: bool = False,
373376
) -> RemovableHandle:
374377
_raise_not_supported(self.register_forward_pre_hook.__name__)
375378

376379
def register_forward_hook( # type: ignore[return]
377380
self,
378-
hook: Callable[..., None],
381+
hook: Union[
382+
Callable[[T, Tuple[Any, ...], Any], Optional[Any]],
383+
Callable[[T, Tuple[Any, ...], Dict[str, Any], Any], Optional[Any]],
384+
],
379385
prepend: bool = False,
380386
with_kwargs: bool = False,
381387
) -> RemovableHandle:

‎torch/nn/modules/module.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,10 @@ def _maybe_warn_non_full_backward_hook(self, inputs, result, grad_fn):
13351335

13361336
def register_forward_pre_hook(
13371337
self,
1338-
hook: Callable[..., None],
1338+
hook: Union[
1339+
Callable[[T, Tuple[Any, ...]], Optional[Any]],
1340+
Callable[[T, Tuple[Any, ...], Dict[str, Any]], Optional[Tuple[Any, Dict[str, Any]]]],
1341+
],
13391342
*,
13401343
prepend: bool = False,
13411344
with_kwargs: bool = False,
@@ -1396,7 +1399,10 @@ def register_forward_pre_hook(
13961399

13971400
def register_forward_hook(
13981401
self,
1399-
hook: Callable[..., None],
1402+
hook: Union[
1403+
Callable[[T, Tuple[Any, ...], Any], Optional[Any]],
1404+
Callable[[T, Tuple[Any, ...], Dict[str, Any], Any], Optional[Any]],
1405+
],
14001406
*,
14011407
prepend: bool = False,
14021408
with_kwargs: bool = False,

0 commit comments

Comments
 (0)