Search code examples
pythonpytorchgetattrstable-diffusion

"TypeError: getattr(): attribute name must be string" in PyTorch diffusers, how to fix it?


I am trying the diffusers of Pytorch to generate pictures in my Mac M1. I have a simple syntax like this:

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) 

when I run my script, it throws an error,

(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

In torch_dtype=torch.float16, I have tried all different types available here: https://pytorch.org/docs/stable/tensor_attributes.html, but none of it works.

Would anyone please help?

Updates on 6 Dec: I copy and paste the code from the official page which is dedicated to M1, https://huggingface.co/docs/diffusers/optimization/mps The code is as follow,

# 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]

But I still get the same error:

---------------------------------------------------------------------------
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

Solution

  • The device should be mps (device='mps'). Mac M1 has no inbuilt Nvidia GPU.

    Also, I would suggest you check How to use Stable Diffusion in Apple Silicon (M1/M2) HG blog and make sure all the requirements are satisfied.

    Also, check for your installed diffusers version.

    import diffusers
    print(diffusers.__version__)
    

    If it is <=0.4.0, please update it using,

    pip install --upgrade diffusers transformers scipy