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

from_pretrained downloads *.bin and *.safetensors weights if both are present in the repo #1811

Closed
apolinario opened this issue Dec 22, 2022 · 6 comments
Labels
bug Something isn't working

Comments

@apolinario
Copy link
Collaborator

Describe the bug

If both *.bin and *.safetensors weights exist in a repository both are downloaded even thought only the *.safetensors version are used, doubling the disk size & time to download models.

As we load *.safetensors by default, we should only download them.

Reproduction

import torch
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)

Logs

No response

System Info

diffusers==0.11.1

@apolinario apolinario added the bug Something isn't working label Dec 22, 2022
@camenduru
Copy link
Contributor

camenduru commented Dec 24, 2022

maybe this is a workaround low_cpu_mem_usage=False

def _get_model_file(

def _get_model_file(

if is_safetensors_available():
try:
model_file = cls._get_model_file(
pretrained_model_name_or_path,
weights_name=SAFETENSORS_WEIGHTS_NAME,
cache_dir=cache_dir,
force_download=force_download,
resume_download=resume_download,
proxies=proxies,
local_files_only=local_files_only,
use_auth_token=use_auth_token,
revision=revision,
subfolder=subfolder,
user_agent=user_agent,
)
except:
pass
if model_file is None:
model_file = cls._get_model_file(
pretrained_model_name_or_path,
weights_name=WEIGHTS_NAME,
cache_dir=cache_dir,
force_download=force_download,
resume_download=resume_download,
proxies=proxies,
local_files_only=local_files_only,
use_auth_token=use_auth_token,
revision=revision,
subfolder=subfolder,
user_agent=user_agent,
)
if low_cpu_mem_usage:
# Instantiate model with empty weights
with accelerate.init_empty_weights():
config, unused_kwargs = cls.load_config(
config_path,
cache_dir=cache_dir,
return_unused_kwargs=True,
force_download=force_download,
resume_download=resume_download,
proxies=proxies,
local_files_only=local_files_only,
use_auth_token=use_auth_token,
revision=revision,
subfolder=subfolder,
device_map=device_map,
**kwargs,
)
model = cls.from_config(config, **unused_kwargs)
# if device_map is Non,e load the state dict on move the params from meta device to the cpu
if device_map is None:
param_device = "cpu"
state_dict = load_state_dict(model_file)
# move the parms from meta device to cpu
for param_name, param in state_dict.items():
set_module_tensor_to_device(model, param_name, param_device, value=param)
else: # else let accelerate handle loading and dispatching.
# Load weights and dispatch according to the device_map
# by deafult the device_map is None and the weights are loaded on the CPU
accelerate.load_checkpoint_and_dispatch(model, model_file, device_map)
loading_info = {
"missing_keys": [],
"unexpected_keys": [],
"mismatched_keys": [],
"error_msgs": [],
}

model_file = hf_hub_download(

model_file = hf_hub_download(

@camenduru
Copy link
Contributor

camenduru commented Dec 26, 2022

also diffusers generating new snapshot again and again snapshots pointing to same thing but idk is this a bug? or is it downloading when new version in the main repo?

Screenshot 2022-12-26 042323

@julien-c
Copy link
Member

@camenduru do you use Developer mode on Windows? If yes, those snapshots would be symlink'ed to the actual files (i.E. not duplicated on disk space)

@camenduru
Copy link
Contributor

Hi @julien-c 👋 yes snapshots pointing to same thing it is not a bug but I think this is a bug 🐜

Screenshot 2022-12-26 085357

@pcuenca
Copy link
Member

pcuenca commented Dec 26, 2022

@apolinario I'm not able to reproduce this using diffusers @ main. If I delete my cache directory and then load the stable diffusion pipeline for model stabilityai/stable-diffusion-2-1, this is what ends up being downloaded:

pedro@deep-hack:diffusers$ tree models--stabilityai--stable-diffusion-2-1/
models--stabilityai--stable-diffusion-2-1/
├── blobs
│   ├── 1238522277c48923ff2751e238f2742c562e45643f3d50cc93d163cb30638b0c
│   ├── 469be27c5c010538f845f518c4f5e8574c78f7c8
│   ├── 5294955ff7801083f720b34b55d0f1f51313c5c5
│   ├── 536b82b4e3c62c4898b4ac8725bc514f2a98f5de
│   ├── 76e821f1b6f0a9709293c3b6b51ed90980b3166b
│   ├── 9b1458658e8651398962171a8c5c56c5c0bd5aea
│   ├── 9c60528fdcb99a7caf834426a94ea13c56cf422b
│   ├── a1d993488569e928462932c8c38a0760b874d166399b14414135bd9c42df5815
│   ├── ae0c5be6f35217e51c4c000fd325d8de0294e99c
│   ├── cce6febb0b6d876ee5eb24af35e27e764eb4f9b1d0b7c026c8c3333d4cfc916c
│   ├── cefd6c7732bbda467ed1ec3e33cfa332a5fe32cf
│   ├── f4fe219b936c0e171504b4bba0c33c7bef6ea211
│   └── f97af6a6a8235236b1346312f328569ce2d70f81
├── refs
│   └── main
└── snapshots
    └── 36a01dc742066de2e8c91e7cf0b8f6b53ef53da1
        ├── feature_extractor
        │   └── preprocessor_config.json -> ../../../blobs/5294955ff7801083f720b34b55d0f1f51313c5c5
        ├── model_index.json -> ../../blobs/cefd6c7732bbda467ed1ec3e33cfa332a5fe32cf
        ├── scheduler
        │   └── scheduler_config.json -> ../../../blobs/536b82b4e3c62c4898b4ac8725bc514f2a98f5de
        ├── text_encoder
        │   ├── config.json -> ../../../blobs/9c60528fdcb99a7caf834426a94ea13c56cf422b
        │   └── model.safetensors -> ../../../blobs/cce6febb0b6d876ee5eb24af35e27e764eb4f9b1d0b7c026c8c3333d4cfc916c
        ├── tokenizer
        │   ├── merges.txt -> ../../../blobs/76e821f1b6f0a9709293c3b6b51ed90980b3166b
        │   ├── special_tokens_map.json -> ../../../blobs/ae0c5be6f35217e51c4c000fd325d8de0294e99c
        │   ├── tokenizer_config.json -> ../../../blobs/f4fe219b936c0e171504b4bba0c33c7bef6ea211
        │   └── vocab.json -> ../../../blobs/469be27c5c010538f845f518c4f5e8574c78f7c8
        ├── unet
        │   ├── config.json -> ../../../blobs/9b1458658e8651398962171a8c5c56c5c0bd5aea
        │   └── diffusion_pytorch_model.safetensors -> ../../../blobs/1238522277c48923ff2751e238f2742c562e45643f3d50cc93d163cb30638b0c
        └── vae
            ├── config.json -> ../../../blobs/f97af6a6a8235236b1346312f328569ce2d70f81
            └── diffusion_pytorch_model.safetensors -> ../../../blobs/a1d993488569e928462932c8c38a0760b874d166399b14414135bd9c42df5815

10 directories, 27 files

Edit: tested with diffusers 0.11.1 too.

@pcuenca
Copy link
Member

pcuenca commented Dec 26, 2022

Ok, I've been able to reproduce it when a repo contains safetensors versions of just some files, but not all. This is the case for @camenduru's example.

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

4 participants