ConformerEncoder object has no attribute output_dim
State: closed | Pull request: no
Labels: name: bug

Hi 👋 As I was trying to train a Conformer model from scratch, I got these exceptions:

> Traceback (most recent call last):
>   File "/home/ubuntu/.local/lib/python3.8/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 62, in _call_target
>     return target(*args, **kwargs)
>   File "/home/ubuntu/.local/lib/python3.8/site-packages/nemo/collections/asr/modules/conformer_encoder.py", line 202, in __init__
>     if feat_out > 0 and feat_out != self.output_dim:
>   File "/home/ubuntu/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 778, in __getattr__
>     raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
> torch.nn.modules.module.ModuleAttributeError: 'ConformerEncoder' object has no attribute 'output_dim'
> 
> During handling of the above exception, another exception occurred:
> 
> Traceback (most recent call last):
>   File "base_model.py", line 38, in <module>
>     asr_model = nemo_asr.models.EncDecCTCModel(cfg=config.model, trainer=trainer)
>   File "/home/ubuntu/.local/lib/python3.8/site-packages/nemo/collections/asr/models/ctc_models.py", line 155, in __init__
>     self.encoder = EncDecCTCModel.from_config_dict(self._cfg.encoder)
>   File "/home/ubuntu/.local/lib/python3.8/site-packages/nemo/core/classes/common.py", line 437, in from_config_dict
>     instance = hydra.utils.instantiate(config=config)
>   File "/home/ubuntu/.local/lib/python3.8/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 180, in instantiate
>     return instantiate_node(config, *args, recursive=_recursive_, convert=_convert_)
>   File "/home/ubuntu/.local/lib/python3.8/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 249, in instantiate_node
>     return _call_target(target, *args, **kwargs)
>   File "/home/ubuntu/.local/lib/python3.8/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 64, in _call_target
>     raise type(e)(
>   File "/home/ubuntu/.local/lib/python3.8/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 62, in _call_target
>     return target(*args, **kwargs)
>   File "/home/ubuntu/.local/lib/python3.8/site-packages/nemo/collections/asr/modules/conformer_encoder.py", line 202, in __init__
>     if feat_out > 0 and feat_out != self.output_dim:
>   File "/home/ubuntu/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 778, in __getattr__
>     raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
> torch.nn.modules.module.ModuleAttributeError: Error instantiating 'nemo.collections.asr.modules.conformer_encoder.ConformerEncoder' : 'ConformerEncoder' object has no attribute 'output_dim'

I investigated the code in nemo/collections/asr/modules/conformer_encoder.py and it turns out attribute 'output_dim' indeed is only present in this one line 202, and is never defined or passed previously. This is the part where it's used:

`        if feat_out > 0 and feat_out != self.output_dim:`
`            self.out_proj = nn.Linear(self.feat_out, feat_out)`
`            self._feat_out = feat_out`
`        else:`
`            self.out_proj = None`
`            self._feat_out = d_model`

Which I believe should look something like this (at least I got my code working with these corrections):

`if feat_out > 0 and feat_out != self._feat_out:`
`    self.out_proj = nn.Linear(self._feat_out, feat_out)`
`    self._feat_out = feat_out`
`else:`
`    self.out_proj = None`
`    self._feat_out = d_model`

Please, correct me if I'm wrong. 

**Environment:**

I'm using 
- NeMo ver. 1.1.0. (installed via pip). 
- torch ver. 1.7.0+cu110
- Python ver. 3.8
- 2 NVIDIA GeForce RTX 3090 GPUs
- OS: Ubuntu 20.04.2

Code to reproduce bug: base_model in the attachments. Model config is also in the attachments, I got it from here: https://github.com/NVIDIA/NeMo/blob/main/examples/asr/conf/conformer/conformer_ctc_char.yaml and modified for my case.

Thank you.

Attachments:
[conformer_ctc_char-ru.txt](https://github.com/NVIDIA/NeMo/files/7057233/conformer_ctc_char-ru.txt)
[base_model.txt](https://github.com/NVIDIA/NeMo/files/7057234/base_model.txt)