Can't load a saved tokenizer with AutoTokenizer.from_pretrained without saving Config as well
Author: eladsegal
Pull request: no
Labels: name: wontfix

### Environment info
- `transformers` version: master (https://github.com/huggingface/transformers/commit/6e8a38568eb874f31eb49c42285c3a634fca12e7)

### Who can help
 tokenizers: @mfuntowicz

### Information
When saving a tokenizer with .save_pretrained, it can be loaded with the class it was saved with but not with AutoTokenizer:
```
from transformers import BertTokenizer, AutoTokenizer
BertTokenizer.from_pretrained("bert-base-cased").save_pretrained(".")

BertTokenizer.from_pretrained(".") # works

AutoTokenizer.from_pretrained(".") # throws exception
```
The error is:
```
Traceback (most recent call last):
  File "/home/transformers/src/transformers/configuration_utils.py", line 333, in get_config_dict
    local_files_only=local_files_only,
  File "/home/transformers/src/transformers/file_utils.py", line 684, in cached_path
    raise EnvironmentError("file {} not found".format(url_or_filename))
OSError: file ./config.json not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/transformers/src/transformers/tokenization_auto.py", line 205, in from_pretrained
    config = AutoConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
  File "/home/transformers/src/transformers/configuration_auto.py", line 203, in from_pretrained
    config_dict, _ = PretrainedConfig.get_config_dict(pretrained_model_name_or_path, **kwargs)
  File "/home/transformers/src/transformers/configuration_utils.py", line 346, in get_config_dict
    raise EnvironmentError(msg)
OSError: Can't load config for '.'. Make sure that:

- '.' is a correct model identifier listed on 'https://huggingface.co/models'

- or '.' is the correct path to a directory containing a config.json file
```

If a configuration is saved as well, then loading with AutoTokenizer does work:
```
from transformers import BertTokenizer, BertConfig, AutoTokenizer
BertConfig.from_pretrained("bert-base-cased").save_pretrained(".")
BertTokenizer.from_pretrained("bert-base-cased").save_pretrained(".")

AutoTokenizer.from_pretrained(".") # works
```

### Expected behavior
I'd expect that loading a tokenizer with AutoTokenizer would require the same files as a dedicated tokenizer class (e.g. BertTokenizer) requires.


Comments:
@eladsegal I think that the `AutoTokenizer` requires the config file to determine what model to use. In https://huggingface.co/transformers/model_doc/auto.html it states that: 

> The from_pretrained() method takes care of returning the correct tokenizer class instance based on the model_type property of the config object, or when it’s missing, falling back to using pattern matching on the pretrained_model_name_or_path string.

So I think that if your model path variable includes the name of the model that it was using, it should be able to load the right tokenizer. If it doesn't it expects to have a config file.

@TarasPriadka When providing a path, a config file is required even if the model name is in the path (https://github.com/huggingface/transformers/blob/6e8a38568eb874f31eb49c42285c3a634fca12e7/src/transformers/tokenization_auto.py#L205). 
The model name in the path is used only when an existing config file is missing the model_type property (https://github.com/huggingface/transformers/blob/6e8a38568eb874f31eb49c42285c3a634fca12e7/src/transformers/configuration_auto.py#L203-L212).


I bumped on that as well.

~~I believe the issue is purely due to mismatch in filename convention AutoTokenizer throws an exception of './config.json' missing, while the file saved is called 'tokenizer_config.json'~~

Maybe it is a different case - looks like when you want to instantiate BertTokenizer it just needs tokenizer_config.json but when you want to instantiate AutoTokenizer it requires config.json - the config of whole model.

So the simplest steps to reproduce are just:

```
from transformers import AutoTokenizer
AutoTokenizer.from_pretrained("bert-base-cased").save_pretrained(".")
AutoTokenizer.from_pretrained(".") # throws exception
```

looking at the source code - a workaround is to call
```
AutoTokenizer.from_pretrained(tokenizer_path, config=AutoConfig.from_pretrained(model_path))
```

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.


Here is another workaround by using directly the corresponding tokenizer class such as BertTokenizer.from_pretrained instead of AutoTokenizer.
https://stackoverflow.com/questions/62472238/autotokenizer-from-pretrained-fails-to-load-locally-saved-pretrained-tokenizer/62664374#62664374

I had the same issue and I realized some wired things going on. I'm running IDLEX and Jupyter Note book, both on Windows 10. I installed my python system on "D:\WPy64-3740". IDLEX can successfully loads pretrained model but Jupyter Notebook can not. But for some reason, it does load pretrained model when I load .py file with import directive.

# Issue

Usually, I directly launch IDEX.exe from the path above. In that case, it doesn't cause any problem. For example, some code like:

```python
# On IDLEX.exe
>>> tokenizer = AutoTokenizer.from_pretrained('prajjwal1/bert-tiny')
```
works fine. But when I use Jupyter Notebook, usually launch from the same directory, causes an Error. This is the part of the error message

```python
# On Jupyter Notebook.exe
tokenizer = AutoTokenizer.from_pretrained('prajjwal1/bert-tiny')

# Output of the cell
Could not locate the tokenizer configuration file, will try to use the model config instead.
loading configuration file https://huggingface.co/prajjwal1/bert-tiny/resolve/main/config.json from cache at D:\WPy64-3740\settings/.cache\huggingface\transformers\3cf34679007e9fe5d0acd644dcc1f4b26bec5cbc9612364f6da7262aed4ef7a4.a5a11219cf90aae61ff30e1658ccf2cb4aa84d6b6e947336556f887c9828dc6d
Model config BertConfig {
  "_name_or_path": "prajjwal1/bert-tiny",
  ...
  "transformers_version": "4.20.1",
  "type_vocab_size": 2,
  "use_cache": true,
  "vocab_size": 30522
}

loading file https://huggingface.co/prajjwal1/bert-tiny/resolve/main/vocab.txt from cache at D:\WPy64-3740\settings/.cache\huggingface\transformers\585ac1c3dedc6b808dd35e8770afafe10905d3e723a02617af749d39db780e09.d789d64ebfe299b0e416afc4a169632f903f693095b4629a7ea271d5a0cf2c99
loading file https://huggingface.co/prajjwal1/bert-tiny/resolve/main/tokenizer.json from cache at None
loading file https://huggingface.co/prajjwal1/bert-tiny/resolve/main/added_tokens.json from cache at None
loading file https://huggingface.co/prajjwal1/bert-tiny/resolve/main/special_tokens_map.json from cache at None
loading file https://huggingface.co/prajjwal1/bert-tiny/resolve/main/tokenizer_config.json from cache at None
loading configuration file https://huggingface.co/prajjwal1/bert-tiny/resolve/main/config.json from cache at D:\WPy64-3740\settings/.cache\huggingface\transformers\3cf34679007e9fe5d0acd644dcc1f4b26bec5cbc9612364f6da7262aed4ef7a4.a5a11219cf90aae61ff30e1658ccf2cb4aa84d6b6e947336556f887c9828dc6d
Model config BertConfig {
  "_name_or_path": "prajjwal1/bert-tiny"
  ...
  "type_vocab_size": 2,
  "use_cache": true,
  "vocab_size": 30522
}

loading configuration file https://huggingface.co/prajjwal1/bert-tiny/resolve/main/config.json from cache at D:\WPy64-3740\settings/.cache\huggingface\transformers\3cf34679007e9fe5d0acd644dcc1f4b26bec5cbc9612364f6da7262aed4ef7a4.a5a11219cf90aae61ff30e1658ccf2cb4aa84d6b6e947336556f887c9828dc6d
Model config BertConfig {
  "_name_or_path": "prajjwal1/bert-tiny",
  "attention_probs_dropout_prob": 0.1,
  ...
```

I thought maybe Notebook cause an error because it working on some different directory. So I checked the current working directory on both environment. Here is the code, I used for it.

```python
import os
os.getcwd()
```

As the result, I confirmed both program working on the same directory (or folder, whatever). I also confirmed Python version on shell/Notebook and it was the same, too. By the way, the location of python.exe is "D:\WPy64-3740\python-3.7.4.amd64". Both IDEX and Notebook uses same python.exe....I suppose.

# Wired behaviour

The funny thing about the issue is when I load .py file from Jupyter Notebook, it can load pretrained model. For example,

```python
# On Jupyter Notebook

# load some module loads pretrained model.
# This code will import symbol, "tokenizer", an instance of AutoTokenizer initialized with from_pretrained method.
from transformer_fine_tune_2 import *


tokenizer

# Output of the console
reTrainedTokenizerFast(name_or_path='prajjwal1/bert-tiny', vocab_size=30522, model_max_len=1000000000000000019884624838656, is_fast=True, padding_side='right', truncation_side='right', special_tokens={'unk_token': '[UNK]', 'sep_token': '[SEP]', 'pad_token': '[PAD]', 'cls_token': '[CLS]', 'mask_token': '[MASK]'})
```

works file. I even trained my model in this way. So I suspect this could be a bug, somehow path related, or maybe  one of those "Windows things" or something else.

Hope this information helps.