Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot change pre-trained-model download directory #2614

Closed
xingzhehe opened this issue Mar 9, 2023 · 12 comments
Closed

Cannot change pre-trained-model download directory #2614

xingzhehe opened this issue Mar 9, 2023 · 12 comments
Labels
bug Something isn't working

Comments

@xingzhehe
Copy link

Describe the bug

I tried the very first example Text-to-Image generation with Stable Diffusion.

I have set every single variable I can find on Internet to the current path, but it keeps downloading model to ~/.cache. I have very limited home space, so it keeps telling me no space on the disk.

Reproduction

import torch
from diffusers import StableDiffusionPipeline
import os

os.environ['TORCH_HOME'] = os.path.abspath(os.getcwd())
os.environ['HF_HOME'] = os.path.abspath(os.getcwd())
os.environ['XDG_CACHE_HOME'] = os.path.abspath(os.getcwd())
os.environ['HUGGINGFACE_HUB_CACHE'] = os.path.abspath(os.getcwd())
os.environ['TRANSFORMERS_CACHE'] = os.path.abspath(os.getcwd())

pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]

Logs

No response

System Info

I am using ubuntu 20.04, and the latest diffuser.

@xingzhehe xingzhehe added the bug Something isn't working label Mar 9, 2023
@patrickvonplaten
Copy link
Contributor

Hey @xingzhehe,

Can you just try to use the cache_dir=... parameter to define your cache? See:

cache_dir (`Union[str, os.PathLike]`, *optional*):

(sorry for some reason our docs don't display it atm - will check)

@stevhliu
Copy link
Member

stevhliu commented Mar 9, 2023

You can see the cache_dir parameter from the main version of the docs :)

@xingzhehe
Copy link
Author

xingzhehe commented Mar 9, 2023

@patrickvonplaten @stevhliu

Thanks! but after I set cache_dir=".", it seems not to be able to find some config file:

Traceback (most recent call last):
File "XXXXXX/test_diffuser.py", line 13, in
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, cache_dir='.')
File "XXXXXX/anaconda3/envs/xxxx/lib/python3.9/site-packages/diffusers/pipelines/pipeline_utils.py", line 589, in from_pretrained
config_dict = cls.load_config(
File "XXXXXX/anaconda3/envs/xxxx/lib/python3.9/site-packages/diffusers/configuration_utils.py", line 380, in load_config
config_dict = cls._dict_from_json_file(config_file)
File "XXXXXX/anaconda3/envs/xxxx/lib/python3.9/site-packages/diffusers/configuration_utils.py", line 479, in _dict_from_json_file
with open(json_file, "r", encoding="utf-8") as reader:
FileNotFoundError: [Errno 2] No such file or directory: './models--runwayml--stable-diffusion-v1-5/snapshots/39593d5650112b4cc580433f6b0435385882d819/model_index.json'

@patrickvonplaten
Copy link
Contributor

Hmm that's weird what happens if you set cache_dir="./temp" as a test?

@xingzhehe
Copy link
Author

Hmm that's weird what happens if you set cache_dir="./temp" as a test?

It gives the same error.

@wrk226
Copy link

wrk226 commented Mar 11, 2023

I encountered a similar situation and solved my problem by using symbolic link.
For example, assume you want to move the cache files to /home/temp folder,you can do:

mv ~/.cache/huggingface /home/temp/
ln -s /home/temp/huggingface ~/.cache/huggingface

Hope it helps.

@xingzhehe
Copy link
Author

I encountered a similar situation and solved my problem by using symbolic link. For example, assume you want to move the cache files to /home/temp folder,you can do:

mv ~/.cache/huggingface /home/temp/
ln -s /home/temp/huggingface ~/.cache/huggingface

Hope it helps.

Yes it helps! Thank you so much! But I really think this is a bug and should be fixed.

@patrickvonplaten
Copy link
Contributor

Hmm I really cannot reproduce this - @Wauplin do you have an idea maybe what could be going on here?

Also @xingzhehe, could you please post what error message you get when doing:

import torch
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, cache_dir="./cache_dir")

@Wauplin
Copy link
Collaborator

Wauplin commented Mar 13, 2023

@xingzhehe sorry about that. It is related to a bug introduced by latest huggingface_hub release. I'm working on fixing it and will keep you updated once published. In the meantime you can either downgrade huggingface_hub to 0.12.1 or set an absolute path for cache_dir (e.g. /path/to/your/folder)

Related issue: huggingface/huggingface_hub#1388

@Wauplin
Copy link
Collaborator

Wauplin commented Mar 13, 2023

Hi @xingzhehe, I made a patch release to fix this (see release notes).
So if you upgrade huggingface_hub to version 0.13.2, it should be your issue. You must delete the corrupted cache folder before. Please let me know if you have any related issue.

@xingzhehe
Copy link
Author

Hi @xingzhehe, I made a patch release to fix this (see release notes). So if you upgrade huggingface_hub to version 0.13.2, it should be your issue. You must delete the corrupted cache folder before. Please let me know if you have any related issue.

Thanks! The issue has been fixed!

@SuperBruceJia
Copy link

Please simply use

# Export the TRANSFORMERS_CACHE, HF_HOME, and HF_DATASETS_CACHE paths
export TRANSFORMERS_CACHE=/projectnb/pnn/.cache
export HF_HOME=/projectnb/pnn/.cache
export HF_DATASETS_CACHE=/projectnb/pnn/.cache

or

import os

os.environ['TRANSFORMERS_CACHE'] = "/projectnb/pnn/.cache"
os.environ['HF_HOME'] = "/projectnb/pnn/.cache"
os.environ['HF_DATASETS_CACHE'] = "/projectnb/pnn/.cache"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants