huggingface / diffusers

🤗 Diffusers: State-of-the-art diffusion models for image and audio generation in PyTorch and FLAX.

Home Page:https://huggingface.co/docs/diffusers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

convert_diffusers_to_original_sdxl.py doesn't work anymore

zyddnys opened this issue · comments

Describe the bug

Nowadays models are saved as diffusion_pytorch_model-00001-of-00002.safetensors and diffusion_pytorch_model-00002-of-00002.safetensors instead of a single diffusion_pytorch_model.safetensors
This causes convert_diffusers_to_original_sdxl.py to generate file not found error

FileNotFoundError: [Errno 2] No such file or directory: 'output/XXX/unet/diffusion_pytorch_model.bin'

Reproduction

python convert_diffusers_to_original_sdxl.py --model_path output/XXX/ --checkpoint XXX.safetensors --use_safetensors

Logs

No response

System Info

diffusers=0.29.2

Who can help?

No response

meet this issue too

I solve this issue by adding these line :
image

Load models from safetensors if it exists, if it doesn't pytorch

if osp.exists(unet_path):
    unet_state_dict = load_file(unet_path, device="cpu")
###  modify:
elif osp.exists(osp.join(args.model_path, "unet", "diffusion_pytorch_model.safetensors.index.json")):
    dict_list = glob.glob((args.model_path + "/" +"unet" + "/*.safetensors"))
    unet_state_dict = {}
    for dict_path in dict_list:
        part_dict = {}
        with safe_open(dict_path, framework="pt", device="cpu") as f:
            for k in f.keys():
                part_dict[k] = f.get_tensor(k)
        unet_state_dict.update(part_dict)
else:
    unet_path = osp.join(args.model_path, "unet", "diffusion_pytorch_model.bin")
    unet_state_dict = torch.load(unet_path, map_location="cpu")

Thanks for reporting. Would you be able to open a PR for this now that you have a solution too? Please also provide a full reproducible example that we can verify on our end to confirm that the fix is working.

commented

meet this issue too

Related to #8974