Skip to content

Commit 477aaa9

Browse files
pcuencaWauplin
andauthored
Use "hub" directory for cache instead of "diffusers" (#2005)
* Use "hub" directory for cache instead of "diffusers" * Import cache locations from huggingface_hub I verified that the constants are available in huggingface_hub version 0.10.0, which is the minimum we require. Co-authored-by: Lucain Pouget <[email protected]> * make style * Move cached directories to new location. * make style * Apply suggestions by @Wauplin Co-authored-by: Lucain <[email protected]> * Fix is_file * Ignore symlinks. Especially important if we want to ensure that the user may want to invoke the process again later, if they are keeping multiple envs with different versions. * Style --------- Co-authored-by: Lucain Pouget <[email protected]>
1 parent e3a2c7f commit 477aaa9

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

src/diffusers/utils/constants.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
# limitations under the License.
1414
import os
1515

16+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home
1617

17-
hf_cache_home = os.path.expanduser(
18-
os.getenv("HF_HOME", os.path.join(os.getenv("XDG_CACHE_HOME", "~/.cache"), "huggingface"))
19-
)
20-
default_cache_path = os.path.join(hf_cache_home, "diffusers")
18+
19+
default_cache_path = HUGGINGFACE_HUB_CACHE
2120

2221

2322
CONFIG_NAME = "config.json"

src/diffusers/utils/hub_utils.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import os
1818
import sys
19+
import traceback
1920
from pathlib import Path
2021
from typing import Dict, Optional, Union
2122
from uuid import uuid4
@@ -24,7 +25,7 @@
2425
from huggingface_hub.utils import is_jinja_available
2526

2627
from .. import __version__
27-
from .constants import HUGGINGFACE_CO_RESOLVE_ENDPOINT
28+
from .constants import DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT
2829
from .import_utils import (
2930
ENV_VARS_TRUE_VALUES,
3031
_flax_version,
@@ -129,3 +130,72 @@ def create_model_card(args, model_name):
129130

130131
card_path = os.path.join(args.output_dir, "README.md")
131132
model_card.save(card_path)
133+
134+
135+
# Old default cache path, potentially to be migrated.
136+
# This logic was more or less taken from `transformers`, with the following differences:
137+
# - Diffusers doesn't use custom environment variables to specify the cache path.
138+
# - There is no need to migrate the cache format, just move the files to the new location.
139+
hf_cache_home = os.path.expanduser(
140+
os.getenv("HF_HOME", os.path.join(os.getenv("XDG_CACHE_HOME", "~/.cache"), "huggingface"))
141+
)
142+
old_diffusers_cache = os.path.join(hf_cache_home, "diffusers")
143+
144+
145+
def move_cache(old_cache_dir: Optional[str] = None, new_cache_dir: Optional[str] = None) -> None:
146+
if new_cache_dir is None:
147+
new_cache_dir = DIFFUSERS_CACHE
148+
if old_cache_dir is None:
149+
old_cache_dir = old_diffusers_cache
150+
151+
old_cache_dir = Path(old_cache_dir).expanduser()
152+
new_cache_dir = Path(new_cache_dir).expanduser()
153+
for old_blob_path in old_cache_dir.glob("**/blobs/*"): # move file blob by blob
154+
if old_blob_path.is_file() and not old_blob_path.is_symlink():
155+
new_blob_path = new_cache_dir / old_blob_path.relative_to(old_cache_dir)
156+
new_blob_path.parent.mkdir(parents=True, exist_ok=True)
157+
os.replace(old_blob_path, new_blob_path)
158+
try:
159+
os.symlink(new_blob_path, old_blob_path)
160+
except OSError:
161+
logger.warning(
162+
"Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded."
163+
)
164+
# At this point, old_cache_dir contains symlinks to the new cache (it can still be used).
165+
166+
167+
cache_version_file = os.path.join(DIFFUSERS_CACHE, "version_diffusers_cache.txt")
168+
if not os.path.isfile(cache_version_file):
169+
cache_version = 0
170+
else:
171+
with open(cache_version_file) as f:
172+
cache_version = int(f.read())
173+
174+
if cache_version < 1:
175+
old_cache_is_not_empty = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0
176+
if old_cache_is_not_empty:
177+
logger.warning(
178+
"The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your "
179+
"existing cached models. This is a one-time operation, you can interrupt it or run it "
180+
"later by calling `diffusers.utils.hub_utils.move_cache()`."
181+
)
182+
try:
183+
move_cache()
184+
except Exception as e:
185+
trace = "\n".join(traceback.format_tb(e.__traceback__))
186+
logger.error(
187+
f"There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease "
188+
"file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole "
189+
"message and we will do our best to help."
190+
)
191+
192+
if cache_version < 1:
193+
try:
194+
os.makedirs(DIFFUSERS_CACHE, exist_ok=True)
195+
with open(cache_version_file, "w") as f:
196+
f.write("1")
197+
except Exception:
198+
logger.warning(
199+
f"There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure "
200+
"the directory exists and can be written to."
201+
)

0 commit comments

Comments
 (0)