pytorch/torch/nn/modules
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
..
__init__.py Adds _LazyInstanceNorm and LazyInstanceNormXd (#60982) 2021-07-21 06:45:45 -07:00
_functions.py replace all_gather with more efficient collective api _all_gather_base (#57769) 2021-05-24 11:34:45 -07:00
activation.py [docs] Add images to some activation functions (#65415) 2021-09-22 11:05:29 -07:00
adaptive.py Support factory kwargs in torch.nn modules (#54508) 2021-04-22 16:16:53 -07:00
batchnorm.py Adds _LazyInstanceNorm and LazyInstanceNormXd (#60982) 2021-07-21 06:45:45 -07:00
channelshuffle.py Fix ChanelShuffle named tensor warnings (#55911) 2021-04-15 15:36:35 -07:00
container.py Type annotate torch.nn.Module ctor (#61334) 2021-07-16 13:59:06 -07:00
conv.py Clarify ConvTransposeNd + reference links (#60291) 2021-06-18 17:18:11 -07:00
distance.py [nn] TripletMarginLoss and PairwiseDistance : no batch dim (#64882) 2021-09-21 07:29:48 -07:00
dropout.py ENH Adds test and docs for dropout for no batch dims (#61911) 2021-07-21 09:07:10 -07:00
flatten.py DOC Improves shape documentation for *Flatten (#60980) 2021-07-06 10:47:11 -07:00
fold.py Enforce PEP263 for PyTorch python codebase (#55346) 2021-04-06 18:31:38 -07:00
instancenorm.py Adds _LazyInstanceNorm and LazyInstanceNormXd (#60982) 2021-07-21 06:45:45 -07:00
lazy.py Add lint for unqualified type: ignore (#56290) 2021-04-21 08:07:23 -07:00
linear.py ENH Adds no_batch_dim tests/docs for LPPool1d and Identity (#62190) 2021-08-24 06:59:41 -07:00
loss.py [nn] TripletMarginLoss and PairwiseDistance : no batch dim (#64882) 2021-09-21 07:29:48 -07:00
module.py Add register_module alias to nn.Module (#65174) 2021-09-22 16:37:28 -07:00
normalization.py DOC Improve documentation for LayerNorm (#63144) 2021-08-13 07:04:40 -07:00
padding.py ENH Adds no_batch_dim support for pad 2d and 3d (#62183) 2021-07-28 11:10:44 -07:00
pixelshuffle.py
pooling.py ENH Adds no_batch_dim tests/docs for LPPool1d and Identity (#62190) 2021-08-24 06:59:41 -07:00
rnn.py Improve LSTM documentation for proj_size > 0 (#65102) 2021-09-16 06:35:27 -07:00
sparse.py Support factory kwargs in torch.nn modules (#54508) 2021-04-22 16:16:53 -07:00
transformer.py Callable activation function support for Transformer modules (Python) (#61355) 2021-07-28 21:42:56 -07:00
upsampling.py
utils.py Add return type hint and improve the docstring of consume_prefix_in_state_dict_if_present method (#63388) 2021-08-17 11:30:27 -07:00