huggingface / diffusers

🤗 Diffusers: State-of-the-art diffusion models for image and audio generation in PyTorch and FLAX.
https://huggingface.co/docs/diffusers
Apache License 2.0
25.03k stars 5.17k forks source link

ImportError: cannot import name 'AutoPipelineForText2Image' from 'diffusers' #8915

Closed blueclowd closed 1 month ago

blueclowd commented 1 month ago

Describe the bug

It seems like an old issue that was mentioned a few times before. I encountered it at version 0.29.2. I am able to finished the training and get the weights, so probably environment setting is fine. The import error is shown on inference stage from the below script.

Reproduction

from diffusers import AutoPipelineForText2Image
import torch

pipeline = AutoPipelineForText2Image.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to("cuda")

Logs

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[53], line 1
----> 1 from diffusers import AutoPipelineForText2Image
      2 import torch
      4 pipeline = AutoPipelineForText2Image.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to("cuda")

ImportError: cannot import name 'AutoPipelineForText2Image' from 'diffusers' (unknown location)

System Info

Diffusers : v0.29.2

Who can help?

No response

tolgacangoz commented 1 month ago

The first thing that comes to mind is that this seems related to the environment. Could you restart the notebook kernel and try again? Could you be sure that the env notebook used at that time has diffusers? Also, could you provide some more information before running this cell? How did you train exactly?

blueclowd commented 1 month ago

Hi @tolgacangoz Thanks for replying.

  1. I tried to restart the session but no luck.
  2. Same error message shown in the same session right after the training and in a new session, so probably the diffusion folder is not the problem.
  3. I am working in the kaggle notebook with the below steps

Setup

!git clone https://github.com/huggingface/diffusers
!cd /kaggle/working/diffusers
!pip install /kaggle/working/diffusers/.
!cd /kaggle/working/diffusers/examples/text_to_image
!pip install -r /kaggle/working/diffusers/examples/text_to_image/requirements.txt
!pip install --upgrade peft transformers torch xformers bitsandbytes

Training

!accelerate config default
!mkdir /kaggle/working/figurine

!accelerate launch --mixed_precision="fp16"  /kaggle/working/diffusers/examples/dreambooth/train_dreambooth_lora.py \
  --mixed_precision="fp16" \
  --pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5" \
  --instance_data_dir="/kaggle/input/aabc-train" \
  --instance_prompt="a photo of aabc figurine" \
  --class_data_dir="/kaggle/working/figurine" \
  --class_prompt="a photo of figurine" \
  --with_prior_preservation \
  --prior_loss_weight=1.0 \
  --use_8bit_adam \
  --lr_scheduler="constant" \
  --gradient_checkpointing \
  --enable_xformers_memory_efficient_attention \
  --num_train_epochs=50 \
  --output_dir="/kaggle/working/dreambooth-lora"

Thanks

tolgacangoz commented 1 month ago

I have just run this code in a Kaggle notebook, and it worked seamlessly:

!git clone https://github.com/huggingface/diffusers
!cd /kaggle/working/diffusers
!pip install /kaggle/working/diffusers/. -q
!cd /kaggle/working/diffusers/examples/text_to_image
!pip install -r /kaggle/working/diffusers/examples/text_to_image/requirements.txt -q
!pip install --upgrade peft transformers torch xformers bitsandbytes -q

from diffusers import AutoPipelineForText2Image
import torch

pipeline = AutoPipelineForText2Image.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)

Additional info: To change the directory use %cd diffusers instead of !cd diffusers.

blueclowd commented 1 month ago

Hi @tolgacangoz Thanks for helping. I tried to copy your script and run. It doesn't work on my notework for the first time, but after restarting the session for a few times, all of sudden it works. Probably restarting the session is a common practice for now?

blueclowd commented 1 month ago

Just wanna share my findings. On Kaggle Notebook, restarting the session works for me by choosing No Persistence for the session.

Screenshot 2024-07-26 at 12 35 12 AM

It seems like there is some system file that may break the library link, start a 100% clean session resolve the issue.

yiyixuxu commented 1 month ago

can we close the issue now? :)