Error when loading custom model with AutoModelForCausalLM in V4.39.1
Author: Hajime-Y
Pull request: no
Labels: name: Version mismatch

name: bug

name: Code on the hub

### System Info

Environment(`transformers-cli env` result):
- `transformers` version: 4.39.1
- Platform: Linux-6.1.58+-x86_64-with-glibc2.35
- Python version: 3.10.12
- Huggingface_hub version: 0.20.3
- Safetensors version: 0.4.2
- Accelerate version: 0.28.0
- Accelerate config: 	not found
- PyTorch version (GPU?): 2.2.1+cu121 (True)
- Tensorflow version (GPU?): 2.15.0 (True)
- Flax version (CPU?/GPU?/TPU?): 0.8.2 (cpu)
- Jax version: 0.4.23
- JaxLib version: 0.4.23
- Using GPU in script?: <fill in>
- Using distributed or parallel set-up in script?: <fill in>

Execution Environment: Google Colab

### Who can help?

_No response_

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

Description:
This is a bug report regarding the "Sharing custom models" feature.

Steps to reproduce:

1. Following the [documentation](https://huggingface.co/docs/transformers/v4.39.1/en/custom_models), I registered my custom architecture model to AutoClass using the following code and pushed it to the Hugging Face Hub. I confirmed that modeling_bit_llama.py exists in the Hub repository.
```
from mybitnet import BitLlamaConfig, BitLlamaForCausalLM

BitLlamaConfig.register_for_auto_class()
BitLlamaForCausalLM.register_for_auto_class("AutoModelForCausalLM")
```
```
trainer.push_to_hub()
```
2. Then, I tried to load the model using AutoClass with the following code:
```
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

model_name = "HachiML/myBit-Llama2-jp-127M-test-17"

tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True)
print(model)
```
3. In version 4.38.x, no error occurred. However, in version 4.39.1, I encountered the following error:
```
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_modules.HachiML.myBit-Llama2-jp-127M-test-17.91a53eeaa608293edf70e1734a05e8ebaccd3233.modeling_bit_llama.BitLlamaConfig'> and you passed <class 'transformers_modules.HachiML.myBit-Llama2-jp-127M-test-17.91a53eeaa608293edf70e1734a05e8ebaccd3233.modeling_bit_llama.BitLlamaConfig'>. Fix one of those so they match!
```

### Expected behavior

Expected behavior:
The model should be successfully loaded using AutoClass in version 4.39.1, as it did in version 4.38.x.

Actual behavior:
In version 4.39.1, an error is raised when attempting to load the model using AutoClass, even though the same code worked in version 4.38.x.

Please let me know if you need any additional information or clarification. Thank you for your attention to this issue.

Comments:
Hi @Hajime-Y, thanks for reporting this issue! 

cc @Rocketknight1 as it looks like it might be related to the loading of remote repos with `.` in their names

@Rocketknight1 
Hi, I was mentioned that this issue might be related to the loading of remote repos with `.` in their names. Could you please take a look at this issue and provide any insights or suggestions on how to resolve it? Thank you for your help!

Investigating!

Filed a PR to fix this at #29854. @Hajime-Y can you test to confirm it fixes your problem? You can install the PR branch with `pip install --upgrade git+https://github.com/huggingface/transformers.git@update_config_class_check`

@Hajime-Y this has now been merged, you can get it by installing `transformers` from `main`!