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

使用taiyi_xl_minimal_inference.py推理huggingface上的模型报错 #7

Open
shyshuai90 opened this issue May 8, 2024 · 17 comments

Comments

@shyshuai90
Copy link

shyshuai90 commented May 8, 2024

您好,如题,原始模型是从huggingface上直接下载的,我在taiyi_xl_minimal_inference.py中修改了加载的位置

`

# todo
args.ckpt_path = "/Volumes/Taiyi-Stable-Diffusion-XL-3.5B/taiyi_diffusion_xl.safetensors"

# todo
# HuggingFaceのmodel id
PRETRAINED_MODEL_DIR = "/Volumes/Taiyi-Stable-Diffusion-XL-3.5B"
text_encoder_1_name = f"{PRETRAINED_MODEL_DIR}/tokenizer"
text_encoder_2_name = f"{PRETRAINED_MODEL_DIR}/tokenizer_2"

`

推理的log如下

`
(sd) ➜ Taiyi-Diffusion-XL git:(debug) ✗ python taiyi_xl_minimal_inference.py
building U-Net
loading U-Net from checkpoint
U-Net:
building text encoders
使用taiyi模型微调!
loading text encoders from checkpoint
Traceback (most recent call last):
File "/Users/codes/Taiyi-Diffusion-XL/taiyi_xl_minimal_inference.py", line 267, in
text_model1, text_model2, vae, unet, _, _ = sdxl_model_util.load_models_from_sdxl_checkpoint(
File "/Users/codes/Taiyi-Diffusion-XL/library/sdxl_model_util.py", line 275, in load_models_from_sdxl_checkpoint
info1 = _load_state_dict_on_device(text_model1, te1_sd, device=map_location) # remain fp32
File "/Users/codes/Taiyi-Diffusion-XL/library/sdxl_model_util.py", line 158, in _load_state_dict_on_device
raise RuntimeError("Error(s) in loading state_dict for {}:\n\t{}".format(model.class.name, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for CLIPTextModel:
Unexpected key(s) in state_dict: "text_model.embeddings.position_ids".

`

@wxj630
Copy link
Collaborator

wxj630 commented May 8, 2024

你的transformers版本是多少?

@shyshuai90
Copy link
Author

你的transformers版本是多少?

好的,transformers改成 requirements.txt中的版本就可以了! 感谢回复!

@wxj630
Copy link
Collaborator

wxj630 commented May 8, 2024

不客气~

@wxj630 wxj630 closed this as completed May 8, 2024
@yangy996
Copy link

yangy996 commented Jun 4, 2024

不客气~

大佬,如果我想升级transformers版本,这个应该怎么办呢?由于别的几个模型用的版本比较高,导致也会出现这个错误

@wxj630
Copy link
Collaborator

wxj630 commented Jun 4, 2024

不客气~

大佬,如果我想升级transformers版本,这个应该怎么办呢?由于别的几个模型用的版本比较高,导致也会出现这个错误

找到text_model.load_state_dict(converted_text_encoder_checkpoint),改成text_model.load_state_dict(converted_text_encoder_checkpoint, strict = False)

@yangy996
Copy link

yangy996 commented Jun 4, 2024

不客气~

大佬,如果我想升级transformers版本,这个应该怎么办呢?由于别的几个模型用的版本比较高,导致也会出现这个错误

找到text_model.load_state_dict(converted_text_encoder_checkpoint),改成text_model.load_state_dict(converted_text_encoder_checkpoint, strict = False)

大佬,不好使呀,还是会报这个错误
image
8f49173391c1492d1f16b49268bbb574

@wxj630
Copy link
Collaborator

wxj630 commented Jun 5, 2024

https://github.com/IDEA-CCNL/Taiyi-Diffusion-XL/blob/main/library/sdxl_model_util.py
第158行的raise RuntimeError注释掉应该就可以了

@yangy996
Copy link

yangy996 commented Jun 5, 2024

https://github.com/IDEA-CCNL/Taiyi-Diffusion-XL/blob/main/library/sdxl_model_util.py 第158行的raise RuntimeError注释掉应该就可以了

大佬,实在不好意思,再打扰下,还是有点问题,transformers==4.33.2后,又出现新的问题
image

image

@wxj630
Copy link
Collaborator

wxj630 commented Jun 5, 2024

先删除position_ids试下:
del text_model1["embeddings.position_ids"]
del text_model2["embeddings.position_ids"]

再去正常to(self.device)

@yangy996
Copy link

yangy996 commented Jun 5, 2024

先删除position_ids试下: del text_model1["embeddings.position_ids"] del text_model2["embeddings.position_ids"]

再去正常to(self.device)

不行,没有这个
image

@yangy996
Copy link

yangy996 commented Jun 5, 2024

text_model1

text_model1打印数据如下:
image

@wxj630
Copy link
Collaborator

wxj630 commented Jun 5, 2024

https://github.com/IDEA-CCNL/Taiyi-Diffusion-XL/blob/main/library/sdxl_model_util.py 第158行的raise RuntimeError注释掉应该就可以了

那在这里同样的函数,在函数最开始,删掉state_dict里的embeddings.position_ids,就不会有unexcepted key(s)了

查看huggingface的源码https://github.com/huggingface/transformers/blob/main/src/transformers/models/clip/modeling_clip.py第202行,position_ids会在缓存里自己生成,所以我们在删掉是没事的

@yangy996
Copy link

yangy996 commented Jun 5, 2024

embeddings.position_ids

不行,预测的时候,还是报错

@wxj630
Copy link
Collaborator

wxj630 commented Jun 5, 2024

报啥错

@yangy996
Copy link

yangy996 commented Jun 5, 2024

报啥错

#7 (comment)

@wxj630
Copy link
Collaborator

wxj630 commented Jun 5, 2024

image 你好,我测试这样改是可以的,transformers==4.33.2

@wxj630 wxj630 reopened this Jun 5, 2024
@yangy996
Copy link

yangy996 commented Jun 5, 2024

image 你好,我测试这样改是可以的,transformers==4.33.2

🐮,可以了,按大佬这个方式改正常了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants