“类型错误:getattr():在PyTorch扩散器中,属性名称必须是字符串”,如何修复?

8xiog9wr  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(358)

我正在尝试Pytorch的扩散器在我的Mac M1中生成图片。我有一个简单的语法如下:

modelid = "CompVis/stable-diffusion-v1-4"
device = "cuda"
pipe = StableDiffusionPipeline.from_pretrained(modelid, revision="fp16", torch_dtype=torch.float16, use_auth_token=auth_token) 
pipe.to(device)

当我运行脚本时,它会抛出一个错误,

(meta_ai) ➜  Difussion_Model /Users/urs/miniforge3/envs/meta_ai/bin/python "/Users/urs/Downloads/Difussion_Model/03_StableD
iffusionApp/app trial1.py"
Fetching 19 files: 100%|██████████████████████████████████████████████████████████████████████████████████| 

19/19 [00:00<00:00, 10253.70it/s]
Traceback (most recent call last):
  File "/Users/urs/Downloads/Difussion_Model/03_StableDiffusionApp/app trial1.py", line 27, in <module>
    pipe = StableDiffusionPipeline.from_pretrained(modelid, revision="fp16", torch_dtype=torch.float16, use_auth_token=auth_token) 
  File "/Users/urs/miniforge3/envs/meta_ai/lib/python3.9/site-packages/diffusers/pipeline_utils.py", line 239, in from_pretrained
    load_method = getattr(class_obj, load_method_name)
TypeError: getattr(): attribute name must be string

在torch_dtype=torch.float16中,我已经尝试了这里提供的所有不同类型:https://pytorch.org/docs/stable/tensor_attributes.html,但都不起作用。
有人能帮忙吗?

12月6日更新:我复制并粘贴的代码从官方页面这是专门为M1,https://huggingface.co/docs/diffusers/optimization/mps的代码如下,

# make sure you're logged in with `huggingface-cli login`
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
pipe = pipe.to("mps")

# Recommended if your computer has < 64 GB of RAM
pipe.enable_attention_slicing()

prompt = "a photo of an astronaut riding a horse on mars"

# First-time "warmup" pass (see explanation above)
_ = pipe(prompt, num_inference_steps=1)

# Results match those from the CPU device after the warmup pass.
image = pipe(prompt).images[0]

但我还是得到同样的错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[14], line 4
      1 # make sure you're logged in with `huggingface-cli login`
      2 from diffusers import StableDiffusionPipeline
----> 4 pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
      5 pipe = pipe.to("mps")
      7 # Recommended if your computer has < 64 GB of RAM

File ~/miniforge3/envs/meta_ai/lib/python3.9/site-packages/diffusers/pipeline_utils.py:239, in DiffusionPipeline.from_pretrained(cls, pretrained_model_name_or_path, **kwargs)
    236     if issubclass(class_obj, class_candidate):
    237         load_method_name = importable_classes[class_name][1]
--> 239 load_method = getattr(class_obj, load_method_name)
    241 loading_kwargs = {}
    242 if issubclass(class_obj, torch.nn.Module):

TypeError: getattr(): attribute name must be string
4c8rllxm

4c8rllxm1#

设备应该是mps(device= 'mps')。Mac M1没有内置Nvidia GPU。
另外,我建议你检查How to use Stable Diffusion in Apple Silicon (M1/M2) HG博客,确保所有的要求都得到满足。
此外,请检查安装的扩散器版本。

import diffusers
print(diffusers.__version__)

如果是<=0.4.0,请使用以下命令更新它,

pip install --upgrade diffusers transformers scipy

相关问题