`modular_model_converter` can not handle objects import via try - except
Author: HIT-cwh
Pull request: no
Labels: name: bug

name: Modular

### System Info

transformers 4.48.0.dev0 d8c1db2f568d4bcc254bc046036acf0d6bba8373

### Who can help?

@ArthurZucker 

### Information

- [ ] The official example scripts
- [X] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [X] My own task or dataset (give details below)

### Reproduction

# How to reproduce?

1. Clone the Transformers repository and check out the specified commit:

`git clone git@github.com:huggingface/transformers.git && cd transformers && git checkout d8c1db2f568d4bcc254bc046036acf0d6bba8373`

2. Create a new folder named `xxx_model` in `src/transformers/models/`

3. Inside this folder, create a new Python file called `modular_xxx.py` with the following content:
```
import torch
import torch.nn as nn
try:
    import torch.nn.functional as F
except:
    pass

from ..llama.modeling_llama import (
    LlamaMLP,
)

class Model(nn.Module):
    def forward(self, x, w):
        return F.linear(x, w)
```

4. Run the following command to execute the model converter:
`python utils/modular_model_converter.py --files_to_parse src/transformers/models/xxx_model/modular_xxx.py` 

This will generate the modeling file at:  `src/transformers/models/xxx_model/modeling_xxx.py`.

### Expected behavior

# Expected vs Actual Contents in src/transformers/models/xxx_model/modeling_xxx.py

The **expected** contents in `src/transformers/models/xxx_model/modeling_xxx.py` is : 

```
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/xxx_model/modular_xxx.py.
#               Do NOT edit this file manually as any edits will be overwritten by the generation of
#             the file from the modular. If any change should be done, please apply the change to the
#                          modular_xxx.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
import torch.nn as nn

try:
    import torch.nn.functional as F
except:
    pass

class Model(nn.Module):
    def forward(self, x, w):
        return F.linear(x, w)
```

However, the **actual** content generated in `src/transformers/models/xxx_model/modeling_xxx.py` is : 

```
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/xxx_model/modular_xxx.py.
#               Do NOT edit this file manually as any edits will be overwritten by the generation of
#             the file from the modular. If any change should be done, please apply the change to the
#                          modular_xxx.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
import torch.nn as nn


class Model(nn.Module):
    def forward(self, x, w):
        return F.linear(x, w)
```

# Issue

The lines `try: import torch.nn.functional as F except: pass` are missing in the actual content, even though it exists in the original modular file.


Comments:
cc @Cyrilvallez if you have time! 

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

gentle ping @arthurzucker @Cyrilvallez 

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.