FileNotFoundError: Unable to Create Symlink in `cache_dir` - No Such File or Directory
Author: Xiaoven
Pull request: no
Labels: name: Usage

name: bug

### System Info

- transformers==4.44.2
- python: 3.12.3
- platform: MacOS 14.4.1 (with Apple M3 chip)

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

1. Run the below code
    ```py
    import transformers
    mask_filling_model_name = 'Salesforce/codet5p-770m'
    cache_dir = "~/.cache/huggingface/hub"
    mask_model = transformers.AutoModelForSeq2SeqLM.from_pretrained(mask_filling_model_name, cache_dir=cache_dir)
    ```
2. Then it throws FileNotFoundError when trying to create symbol link. I have no idea where the relative path `'../../blobs/05564ee645140947482e12fa785eab3aa5f789a8'` is coming from.
```
    mask_model = transformers.AutoModelForSeq2SeqLM.from_pretrained(mask_filling_model_name, cache_dir=cache_dir)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/sklearn/lib/python3.12/site-packages/transformers/models/auto/auto_factory.py", line 485, in from_pretrained
    resolved_config_file = cached_file(
                           ^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/sklearn/lib/python3.12/site-packages/transformers/utils/hub.py", line 402, in cached_file
    resolved_file = hf_hub_download(
                    ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/sklearn/lib/python3.12/site-packages/huggingface_hub/utils/_deprecation.py", line 101, in inner_f
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/sklearn/lib/python3.12/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/sklearn/lib/python3.12/site-packages/huggingface_hub/file_download.py", line 1240, in hf_hub_download
    return _hf_hub_download_to_cache_dir(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/sklearn/lib/python3.12/site-packages/huggingface_hub/file_download.py", line 1372, in _hf_hub_download_to_cache_dir
    _create_symlink(blob_path, pointer_path, new_blob=False)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/sklearn/lib/python3.12/site-packages/huggingface_hub/file_download.py", line 924, in _create_symlink
    os.symlink(src_rel_or_abs, abs_dst)
FileNotFoundError: [Errno 2] No such file or directory: '../../blobs/05564ee645140947482e12fa785eab3aa5f789a8' -> '/Users/xiaowen/.cache/huggingface/hub/models--Salesforce--codet5p-770m/snapshots/627d346e49cb68e600895ab8744e0e1efd5c4536/config.json'
```

### Expected behavior

No error occurs. The model should be download successfully and save to the cache dir.

Comments:
1. I tried to download the model without using `cache_dir`, and it succeeds to download the model.
```py
mask_model = transformers.AutoModelForSeq2SeqLM.from_pretrained(mask_filling_model_name)
```
2. The tree of `~/.cache/huggingface` is listed as below:
```sh
$ tree .
.
└── hub
    ├── models--Salesforce--codet5p-770m
    │   ├── blobs
    │   │   ├── 05564ee645140947482e12fa785eab3aa5f789a8
    │   │   └── 61b6f30de1356fbc8e91f663454f01f2b7a635d93ccb255b88b07896dde679f1
    │   ├── refs
    │   │   └── main
    │   └── snapshots
    │       └── 627d346e49cb68e600895ab8744e0e1efd5c4536
    │           ├── config.json -> ../../blobs/05564ee645140947482e12fa785eab3aa5f789a8
    │           └── pytorch_model.bin -> ../../blobs/61b6f30de1356fbc8e91f663454f01f2b7a635d93ccb255b88b07896dde679f1
    └── version.txt

7 directories, 6 files
```

3. Then, I tried `transformers.AutoModelForSeq2SeqLM.from_pretrained(mask_filling_model_name, cache_dir=cache_dir)` again, but it threw another FileNotFoundError. However, the file  '~/.cache/huggingface/hub/models--Salesforce--codet5p-770m/snapshots/627d346e49cb68e600895ab8744e0e1efd5c4536/config.json' does exist on my computer.
```
  File "/Users/xiaowen/Documents/GitHub/DetectCodeGPT/code-detection/baselines/utils/loadmodel.py", line 58, in load_mask_filling_model
    mask_model = transformers.AutoModelForSeq2SeqLM.from_pretrained(mask_filling_model_name, cache_dir=model_config['cache_dir'])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/sklearn/lib/python3.12/site-packages/transformers/models/auto/auto_factory.py", line 524, in from_pretrained
    config, kwargs = AutoConfig.from_pretrained(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/sklearn/lib/python3.12/site-packages/transformers/models/auto/configuration_auto.py", line 976, in from_pretrained
    config_dict, unused_kwargs = PretrainedConfig.get_config_dict(pretrained_model_name_or_path, **kwargs)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/sklearn/lib/python3.12/site-packages/transformers/configuration_utils.py", line 632, in get_config_dict
    config_dict, kwargs = cls._get_config_dict(pretrained_model_name_or_path, **kwargs)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/sklearn/lib/python3.12/site-packages/transformers/configuration_utils.py", line 722, in _get_config_dict
    config_dict = cls._dict_from_json_file(resolved_config_file)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/sklearn/lib/python3.12/site-packages/transformers/configuration_utils.py", line 824, in _dict_from_json_file
    with open(json_file, "r", encoding="utf-8") as reader:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '~/.cache/huggingface/hub/models--Salesforce--codet5p-770m/snapshots/627d346e49cb68e600895ab8744e0e1efd5c4536/config.json'
```

I found the cause! The issue is that `cache_dir = "~/.cache/huggingface/hub"`, where `~` should be replaced with `/Users/username`.