AutoTokenizer.from_pretrained is confused by custom model configs 
Author: Craigacp
Pull request: no

### System Info

transformers: 4.23.1, OS: macOS x86, Python: 3.9

### 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

When loading a tokenizer with `AutoTokenizer.from_pretrained("bert-base-uncased", config=my_model_config_instance)` it fails in tokenization_auto.py line 640 with:
```
File "venv/lib/python3.9/site-packages/transformers/models/auto/tokenization_auto.py", line 640, in <genexpr>
    f"Model type should be one of {', '.join(c.__name__ for c in TOKENIZER_MAPPING.keys())}."
AttributeError: 'NoneType' object has no attribute '__name__'
```

I registered the custom model & config using:

```python
    CONFIG_MAPPING.register("my_model", MyModelConfig)
    TOKENIZER_MAPPING.register(MyModelConfig, (BertTokenizer, BertTokenizerFast))
    MODEL_MAPPING.register(MyModelConfig, MyModelModel)
    SPECIAL_MODEL_TYPE_TO_MODULE_NAME["my_model"] = "my_model.modeling_my_model"
```

### Expected behavior

I'd expect it to load `bert-base-uncased`. We always supply the config as we use this code for multiple models in `transformers` as well as our custom models and the loading code can't differentiate the two at the point it happens.

I think it's occurring because `configuration_auto.config_class_to_model_type()` doesn't respect `SPECIAL_MODEL_TYPE_TO_MODULE_NAME`, so it looks for a class called `MyModelConfig` in `CONFIG_MAPPING_NAMES` when it should be looking for `my_model.modeling_my_model.MyModelConfig`, and also `CONFIG_MAPPING_NAMES` and/or `config_class_to_model_type` doesn't seem to respect additional configs loaded in via `CONFIG_MAPPING.register()`.

The error still occurs if I download the files for the `bert-base-uncased` tokenizer and use the path to it on disk rather than the name `bert-base-uncased`.

Comments:
I’ll have a look thanks for posting 

Bump for the stale bot.

Any updates on this?

I did not have time to dive on this, cc @younesbelkada do you think you can have a look,?

If there's some guidance or documentation on how unified the configuration stuff should be for custom models then I can look at fixing places where it's inconsistent, but I'm not sure how consistent your team want it to be, because I don't understand the full set of constraints.

I would mostly focus on 
```python 
Note that the first argument used when registering your custom config to [AutoConfig](https://huggingface.co/docs/transformers/v4.26.0/en/model_doc/auto#transformers.AutoConfig) needs to match the model_type of your custom config, and the first argument used when registering your custom models to any auto model class needs to match the config_class of those models.
```
from [this](https://huggingface.co/docs/transformers/custom_models), but you probably already looked there

Yeah I think we're hitting the right points in the register calls, but then the loading side isn't fully respecting the custom dicts (or the register call is missing adding to a custom dict, not sure which). I can try to take a look next week.

Thanks for the issue @Craigacp 
A workaround to this is to hack the function `config_class_to_model_type` [here](https://github.com/huggingface/transformers/blob/main/src/transformers/models/auto/configuration_auto.py#L566-L571):
```python
def config_class_to_model_type(config):
    """Converts a config class name to the corresponding model type"""
    for key, cls in CONFIG_MAPPING_NAMES.items():
        if cls == config:
            return key
    return None
```
To use `CONFIG_MAPPING` instead of `CONFIG_MAPPING_NAMES` as `CONFIG_MAPPING` gets updated when calling `.register`. 
You can apply this quick hack with the snippet below without having to change the source code:
```python
from transformers import AutoTokenizer, PretrainedConfig, PreTrainedModel, CONFIG_MAPPING, TOKENIZER_MAPPING, BertTokenizer, BertTokenizerFast, MODEL_MAPPING
from transformers.models.auto.configuration_auto import CONFIG_MAPPING_NAMES

class MyModel(PreTrainedModel):
    def __init__(self, config):
        super().__init__(config)

        self.config = config
        self.my_custom_variable = config.my_custom_variable


class MyConfig(PretrainedConfig):
    model_type = "my_model"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.vocab_size = 1000
        self.my_custom_variable = 42

config = MyConfig()
model = MyModel(config)

CONFIG_MAPPING.register("my_model", MyConfig.__name__)
CONFIG_MAPPING_NAMES["my_model"] = MyConfig.__name__
MyConfig.register_for_auto_class()
TOKENIZER_MAPPING.register(MyConfig, (BertTokenizer, BertTokenizerFast))
MODEL_MAPPING.register(MyConfig, MyModel)

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased", config=config)
```
But I am clearly unsure about this approach here, also I don't really understand what do you want to achieve exactly, could you maybe share with us more details on what you are trying yo achieve?
Thanks 


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.