Skip to content

Commit faacdf0

Browse files
authored
Move cache folder to huggingface/hub for consistency with hf_hub (#18492)
* Move cache folder to just huggingface * Thank you VsCode for this needless import * Move to hub * Forgot one
1 parent 280db2e commit faacdf0

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

docs/source/en/installation.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ conda install -c huggingface transformers
139139

140140
## Cache setup
141141

142-
Pretrained models are downloaded and locally cached at: `~/.cache/huggingface/transformers/`. This is the default directory given by the shell environment variable `TRANSFORMERS_CACHE`. On Windows, the default directory is given by `C:\Users\username\.cache\huggingface\transformers`. You can change the shell environment variables shown below - in order of priority - to specify a different cache directory:
142+
Pretrained models are downloaded and locally cached at: `~/.cache/huggingface/hub`. This is the default directory given by the shell environment variable `TRANSFORMERS_CACHE`. On Windows, the default directory is given by `C:\Users\username\.cache\huggingface\hub`. You can change the shell environment variables shown below - in order of priority - to specify a different cache directory:
143143

144-
1. Shell environment variable (default): `TRANSFORMERS_CACHE`.
145-
2. Shell environment variable: `HF_HOME` + `transformers/`.
146-
3. Shell environment variable: `XDG_CACHE_HOME` + `/huggingface/transformers`.
144+
1. Shell environment variable (default): `HUGGINGFACE_HUB_CACHE` or `TRANSFORMERS_CACHE`.
145+
2. Shell environment variable: `HF_HOME`.
146+
3. Shell environment variable: `XDG_CACHE_HOME` + `/huggingface`.
147147

148148
<Tip>
149149

src/transformers/utils/hub.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def is_offline_mode():
8181
hf_cache_home = os.path.expanduser(
8282
os.getenv("HF_HOME", os.path.join(os.getenv("XDG_CACHE_HOME", "~/.cache"), "huggingface"))
8383
)
84-
default_cache_path = os.path.join(hf_cache_home, "transformers")
84+
default_cache_path = os.path.join(hf_cache_home, "hub")
8585

8686
# Onetime move from the old location to the new one if no ENV variable has been set.
8787
if (
@@ -102,7 +102,8 @@ def is_offline_mode():
102102

103103
PYTORCH_PRETRAINED_BERT_CACHE = os.getenv("PYTORCH_PRETRAINED_BERT_CACHE", default_cache_path)
104104
PYTORCH_TRANSFORMERS_CACHE = os.getenv("PYTORCH_TRANSFORMERS_CACHE", PYTORCH_PRETRAINED_BERT_CACHE)
105-
TRANSFORMERS_CACHE = os.getenv("TRANSFORMERS_CACHE", PYTORCH_TRANSFORMERS_CACHE)
105+
HUGGINGFACE_HUB_CACHE = os.getenv("HUGGINGFACE_HUB_CACHE", PYTORCH_TRANSFORMERS_CACHE)
106+
TRANSFORMERS_CACHE = os.getenv("TRANSFORMERS_CACHE", HUGGINGFACE_HUB_CACHE)
106107
HF_MODULES_CACHE = os.getenv("HF_MODULES_CACHE", os.path.join(hf_cache_home, "modules"))
107108
TRANSFORMERS_DYNAMIC_MODULE_NAME = "transformers_modules"
108109
SESSION_ID = uuid4().hex
@@ -1475,9 +1476,16 @@ def move_to_new_cache(file, repo, filename, revision, etag, commit_hash):
14751476
clean_files_for(file)
14761477

14771478

1478-
def move_cache(cache_dir=None, token=None):
1479+
def move_cache(cache_dir=None, new_cache_dir=None, token=None):
1480+
if new_cache_dir is None:
1481+
new_cache_dir = TRANSFORMERS_CACHE
14791482
if cache_dir is None:
1480-
cache_dir = TRANSFORMERS_CACHE
1483+
# Migrate from old cache in .cache/huggingface/hub
1484+
old_cache = Path(TRANSFORMERS_CACHE).parent / "transformers"
1485+
if os.path.isdir(str(old_cache)):
1486+
cache_dir = str(old_cache)
1487+
else:
1488+
cache_dir = new_cache_dir
14811489
if token is None:
14821490
token = HfFolder.get_token()
14831491
cached_files = get_all_cached_files(cache_dir=cache_dir)

0 commit comments

Comments
 (0)