pytorch/torch/jit
Rodrigo Berriel b80bdcc73b Add register_module alias to nn.Module (#65174)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/60397. I'm not sure how aliases are supposed to be implemented, but this is the most basic/direct way, IMO. As a side-effect, this implementation results in a "duplicate" doc entry, inheriting the one from `add_module`:

![monkey-patch](https://user-images.githubusercontent.com/7027770/133693137-8408d8e7-1f4f-436b-b176-57dda9bc3a32.png)

An alternative implementation could be:

```python
def register_module(self, name: str, module: Optional['Module']) -> None:
    r"""Alias for :func:`add_module`."""
    self.add_module(name, module)
```

which results in this documentation:

![image](https://user-images.githubusercontent.com/7027770/133693249-d969a71a-be44-489d-9633-4f38b44ab887.png)

Questions:
1. Should I replicate the tests? There are two for `add_module`: [test_add_module_raises_error_if_attr_exists](873255c6d9/test/test_nn.py (L1420-L1434)) and [test_add_module](873255c6d9/test/test_nn.py (L1837-L1855)).
2. This PR only adds `register_module` to `nn.Module`. There is an `add_module` in [`_RemoteModule`](https://github.com/pytorch/pytorch/blob/master/torch/distributed/nn/api/remote_module.py#L311-L312), which raises `NotSupported`, and there is another one in [`ConcreteModuleTypeBuilder`](873255c6d9/torch/_C/__init__.pyi.in (L468)), which means something else, I think. Should I do anything about them?

cc ngimel SsnL

Pull Request resolved: https://github.com/pytorch/pytorch/pull/65174

Reviewed By: soulitzer

Differential Revision: D31089717

Pulled By: jbschlosser

fbshipit-source-id: abd8d14a434fd8c7efa0bd8c242df56da33491e9
2021-09-22 16:37:28 -07:00
..
mobile remove unused type: ignore directives (#60006) 2021-06-18 07:23:31 -07:00
__init__.py Back out "D30740897 Add fusion enabled apis" (#64500) 2021-09-04 20:55:58 -07:00
_async.py [docs] Fix backticks in docs (#60474) 2021-06-24 06:27:41 -07:00
_builtins.py [TS] Add complex support for more ops (#54541) 2021-05-12 23:33:29 -07:00
_check.py Add lint for unqualified type: ignore (#56290) 2021-04-21 08:07:23 -07:00
_freeze.py update optimize_for_inference docs (#64428) 2021-09-02 22:17:58 -07:00
_fuser.py Ge v1 (#52136) 2021-02-28 00:53:13 -08:00
_logging.py
_monkeytype_config.py More robust check of whether a class is defined in torch (#64083) 2021-08-27 08:55:35 -07:00
_pickle.py
_recursive.py [jit] Better checking for overload function declarations. (#59956) 2021-08-05 14:21:48 -07:00
_script.py Add register_module alias to nn.Module (#65174) 2021-09-22 16:37:28 -07:00
_serialization.py Add lint for unqualified type: ignore (#56290) 2021-04-21 08:07:23 -07:00
_state.py [package] fix mangling issues with TorchScript (#54915) 2021-03-31 00:58:05 -07:00
_trace.py Updates internal assert_allclose callsites in favor of assert_close (#61841) 2021-08-19 12:50:41 -07:00
annotations.py Support Union in TorchScript (#64234) 2021-09-03 06:12:24 -07:00
frontend.py Support Union in TorchScript (#64234) 2021-09-03 06:12:24 -07:00
quantized.py Add lint for unqualified type: ignore (#56290) 2021-04-21 08:07:23 -07:00
supported_ops.py Add lint for unqualified noqa (#56272) 2021-04-19 13:16:18 -07:00
unsupported_tensor_ops.py