Relative path causes error when calling push_to_hub to upload a custom model
Author: lazyhope
Pull request: no

### System Info

- `transformers` version: 4.24.0
- Platform: macOS-10.16-x86_64-i386-64bit
- Python version: 3.9.15
- Huggingface_hub version: 0.11.1
- PyTorch version (GPU?): 1.12.1 (False)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: No
- Using distributed or parallel set-up in script?: No

### Who can help?

@sgugger

### Information

- [ ] The official example scripts
- [X] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

By running the following code and fill `<YOUR-REPO>` and `<YOUR-TOKEN>`:
```
from transformers import AutoConfig, AutoModel

config = AutoConfig.from_pretrained("Lazyhope/python-clone-detection")
model = AutoModel.from_pretrained("Lazyhope/python-clone-detection", config=config)
config.register_for_auto_class()
model.register_for_auto_class("AutoModel")

model.push_to_hub("<YOUR-REPO>", use_auth_token = "<YOUR-TOKEN>")
```

### Expected behavior

Hi, when I was trying to upload a custom model which inherent from transformers.RobertaPreTrainedModel, the following error occurs:
```
FileNotFoundError       (note: full exception trace is shown but execution is paused at: _run_module_as_main)
[Errno 2] No such file or directory: '/Users/rino/miniconda3/envs/CloneDetection/lib/python3.9/site-packages/transformers/models/roberta/.bert.configuration_bert.py'
  File "/Users/rino/miniconda3/envs/CloneDetection/lib/python3.9/site-packages/transformers/dynamic_module_utils.py", line 70, in get_relative_imports
    with open(module_file, "r", encoding="utf-8") as f:
  File "/Users/rino/miniconda3/envs/CloneDetection/lib/python3.9/site-packages/transformers/dynamic_module_utils.py", line 97, in get_relative_import_files
    new_imports.extend(get_relative_imports(f))
  File "/Users/rino/miniconda3/envs/CloneDetection/lib/python3.9/site-packages/transformers/dynamic_module_utils.py", line 439, in custom_object_save
    for needed_file in get_relative_import_files(object_file):
  File "/Users/rino/miniconda3/envs/CloneDetection/lib/python3.9/site-packages/transformers/configuration_utils.py", line 441, in save_pretrained
    custom_object_save(self, save_directory, config=self)
  File "/Users/rino/miniconda3/envs/CloneDetection/lib/python3.9/site-packages/transformers/modeling_utils.py", line 1579, in save_pretrained
    model_to_save.config.save_pretrained(save_directory)
  File "/Users/rino/miniconda3/envs/CloneDetection/lib/python3.9/site-packages/transformers/utils/hub.py", line 790, in push_to_hub
    self.save_pretrained(work_dir, max_shard_size=max_shard_size)
  File "/Users/rino/Desktop/RepoAnalysis/huggingface/register.py", line 9, in <module>
    model.push_to_hub("Lazyhope/python-clone-detection", user_auth_token=<Hidden>)
```
It seems that relative imports like https://github.com/huggingface/transformers/blob/7032e0203262ebb2ebf55da8d2e01f873973e835/src/transformers/models/roberta/modeling_roberta.py#L27 were directly turned into paths like `/miniconda3/envs/CloneDetection/lib/python3.9/site-packages/transformers/models/roberta/..modeling_outputs.py` and opening this kind of path in https://github.com/huggingface/transformers/blob/7032e0203262ebb2ebf55da8d2e01f873973e835/src/transformers/dynamic_module_utils.py#L70 would caused a FileNotFound error.

Comments:
I'm not too sure I understand your use case. The code sample you provide is indeed not supported as you are just re-using the code of the library, so you should just remove the line registering your new model. When defining a custom model, the modeling file will be exported in the repo and shouldn't indeed contain any relative imports (you'll need to convert them to regular imports)

> I'm not too sure I understand your use case. The code sample you provide is indeed not supported as you are just re-using the code of the library, so you should just remove the line registering your new model. When defining a custom model, the modeling file will be exported in the repo and shouldn't indeed contain any relative imports (you'll need to convert them to regular imports)

I have a custom RobertaRBERT model stored in my local directory `custom/robertarbert.py`, previously I use the following code to load  the checkpoint:
```
config = AutoConfig.from_pretrained(PLM, use_auth_token=access_token)
config.dropout_rate = 0.1
model = RobertaRBERT.from_pretrained(PLM, config=config, use_auth_token=access_token)
```
Now I want to upload this local `RobertaRBERT` class definition to the hub, and hopefully use `AutoModel.from_pretained` to load it directly without loading config again, so I followed this document: https://huggingface.co/docs/transformers/custom_models, and running the following code:
```
from custom.robertarbert import RobertaRBERT
from transformers import AutoConfig

config = AutoConfig.from_pretrained("Lazyhope/python-clone-detection")
model = RobertaRBERT.from_pretrained("Lazyhope/python-clone-detection", config=config)
config.register_for_auto_class()
model.register_for_auto_class("AutoModel")

model.push_to_hub("Lazyhope/new_model", use_auth_token = access_token)
``` 
caused the error I mentioned above, is it because my code was incorrect?

Like I said, the custom modeling file shouldn't contain any relative imports.

> Like I said, the custom modeling file shouldn't contain any relative imports.

Here is my custom model file:
```
import torch.nn as nn
from transformers import (
    RobertaPreTrainedModel,
    RobertaModel,
)
from transformers.modeling_outputs import SequenceClassifierOutput


class RobertaRBERT(RobertaPreTrainedModel):
```
I think it doesn't contain any relative imports

@sgugger You could also reproduce the error by calling 
`get_relative_import_files('src/transformers/models/roberta/configuration_roberta.py')`
which is defined in https://github.com/huggingface/transformers/blob/f76518e56a5ef0836a780630de6f5b4456e9aa4a/src/transformers/dynamic_module_utils.py#L81

I suppose there is a bug in the relative import extracting function?

It's a known limitation: the relative imports are only permitted at one level and no more for custom models.

> It's a known limitation: the relative imports are only permitted at one level and no more for custom models.

Is there a workaround? As you can see above my custom model file doesn't contain any relative import but it still doesn't work.

Was a little bit confused by the doc, turns out I just need to use the following code:
```
CloneDetectionModel.register_for_auto_class("AutoModel")
custom_model = CloneDetectionModel.from_pretrained("<PLM>", config=config)
custom_model.push_to_hub("<DIR>")
```