AutoModel not loading model correctly due to config_class inconsistency
Author: liamclarkza
Pull request: no

### System Info

- `transformers` version: 4.41.1
- Platform: macOS-14.4.1-arm64-arm-64bit
- Python version: 3.11.9
- Huggingface_hub version: 0.23.1
- Safetensors version: 0.4.3
- Accelerate version: not installed
- Accelerate config: not found
- PyTorch version (GPU?): 2.3.0 (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?

@ArthurZucker @younesbelkada

### 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

Steps to reproduce:

1. Install transformers version 4.41.1: `pip install transformers==4.41.1`
2. Run the following code: 
```python
from transformers import AutoTokenizer, AutoModel

model_name = "zhihan1996/DNABERT-2-117M"
revision = "25abaf0bd247444fcfa837109f12088114898d98"
model = AutoModel.from_pretrained( model_name, revision=revision, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained( model_name, revision=revision)
```
This produces the following error:
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../python3.11/site-packages/transformers/models/auto/auto_factory.py", line 557, in from_pretrained
    cls.register(config.__class__, model_class, exist_ok=True)
  File ".../lib/python3.11/site-packages/transformers/models/auto/auto_factory.py", line 583, in register
    raise ValueError(
ValueError: The model class you are passing has a `config_class` attribute that is not consistent with the config class you passed (model has <class 'transformers.models.bert.configuration_bert.BertConfig'> and you passed <class 'transformers_modules.zhihan1996.DNABERT-2-117M.25abaf0bd247444fcfa837109f12088114898d98.configuration_bert.BertConfig'>. Fix one of those so they match!
```

### Expected behavior

The expected behaviour is that the model will load successfully. 

This code is very similar to the `# Load model directly` starter code provided by Hugging Face shown below, which will also cause the same error:
<img width="904" alt="image" src="https://github.com/huggingface/transformers/assets/15366437/cd51f22f-2508-4984-be85-047ad78833dc">


Comments:
cc @Rocketknight1 

Hi @liamclarkza , I got the same error with the same version of transformers you mentioned and got rid of it by 
`pip install transformers==4.29`

Thanks @RUFFY-369, unfortunately I am needing to use the latest version of transformers to get access to new models within the code base I'm working on, and so pinning to an old version isn't an option.

@liamclarkza Oh!okay. For a little quick solution what I think could work if you may try then could you please add these lines in `bert_layers.py` on the hub where you have the files:

`from .configuration_bert import BertConfig` (simple Config class import)

`config_class = BertConfig` :- Put this in `class BertModel(BertPreTrainedModel):` before `def __init__()` like such:
```
class BertModel(BertPreTrainedModel):
    config_class = BertConfig
    .
    .
    .

    def __init__(self, config, add_pooling_layer=True):
        .
        .
        .
```

Thanks @RUFFY-369, that makes a lot of sense. I have just tested it after modifying the bert_layers.py file, and it works as expected. I'll open a PR on the hugging face hub for the models concerned. Thanks again :)

Cheers! Oh!  I just opened one thinking you had the access. Anyways feel free to open with complete reference. :+1: :100: 


Ah, sorry I missed that! There are a few other models with the same issue, so I'll open a PR for those too 👍