|
16 | 16 |
|
17 | 17 | import os
|
18 | 18 | import sys
|
| 19 | +import traceback |
19 | 20 | from pathlib import Path
|
20 | 21 | from typing import Dict, Optional, Union
|
21 | 22 | from uuid import uuid4
|
|
24 | 25 | from huggingface_hub.utils import is_jinja_available
|
25 | 26 |
|
26 | 27 | from .. import __version__
|
27 |
| -from .constants import HUGGINGFACE_CO_RESOLVE_ENDPOINT |
| 28 | +from .constants import DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT |
28 | 29 | from .import_utils import (
|
29 | 30 | ENV_VARS_TRUE_VALUES,
|
30 | 31 | _flax_version,
|
@@ -129,3 +130,72 @@ def create_model_card(args, model_name):
|
129 | 130 |
|
130 | 131 | card_path = os.path.join(args.output_dir, "README.md")
|
131 | 132 | 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