Skip to content

Concurrent Thread Failure in Image Inference: StableDiffusionPipeline #3672

@Fantast616

Description

@Fantast616

Describe the bug

When I attempt to concurrently initiate two threads for image inference in one pipe, the process fails and the following information is displayed as logs during the inference. I intend to submit a pull request for thread-safety in one inference pipe if anyone else need.

Reproduction

from diffusers import StableDiffusionPipeline
import torch
import threading

model_id = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")

def infer_one():
    pipe("a photo of an astronaut riding a horse on mars").images[0] 

x1 = threading.Thread(target=infer_one) 
x1.start()
    
x2 = threading.Thread(target=infer_one) 
x2.start()

Logs

Exception in thread Thread-1 (infer_one):                                                                                        | 0/50 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "/opt/miniconda3/envs/diffusers/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/opt/miniconda3/envs/diffusers/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/mnt/workdata/A40/main.py", line 10, in infer_one
    pipe("a photo of an astronaut riding a horse on mars").images[0] 
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/diffusers/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/workdata/yehang/projects/diffusers/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py", line 710, in __call__
    latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/workdata/yehang/projects/diffusers/src/diffusers/schedulers/scheduling_pndm.py", line 221, in step
    return self.step_plms(model_output=model_output, timestep=timestep, sample=sample, return_dict=return_dict)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/workdata/yehang/projects/diffusers/src/diffusers/schedulers/scheduling_pndm.py", line 337, in step_plms
    prev_sample = self._get_prev_sample(sample, timestep, prev_timestep, model_output)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/workdata/yehang/projects/diffusers/src/diffusers/schedulers/scheduling_pndm.py", line 371, in _get_prev_sample
    alpha_prod_t = self.alphas_cumprod[timestep]
                   ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
IndexError: index 1001 is out of bounds for dimension 0 with size 1000
  0%|                                                                                                                            | 0/50 [00:04<?, ?it/s]
Exception in thread Thread-2 (infer_one):
Traceback (most recent call last):
  File "/opt/miniconda3/envs/diffusers/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/opt/miniconda3/envs/diffusers/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/mnt/workdata/A40/main.py", line 10, in infer_one
    pipe("a photo of an astronaut riding a horse on mars").images[0] 
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/diffusers/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/workdata/yehang/projects/diffusers/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py", line 710, in __call__
    latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/workdata/yehang/projects/diffusers/src/diffusers/schedulers/scheduling_pndm.py", line 221, in step
    return self.step_plms(model_output=model_output, timestep=timestep, sample=sample, return_dict=return_dict)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/workdata/yehang/projects/diffusers/src/diffusers/schedulers/scheduling_pndm.py", line 337, in step_plms
    prev_sample = self._get_prev_sample(sample, timestep, prev_timestep, model_output)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/workdata/yehang/projects/diffusers/src/diffusers/schedulers/scheduling_pndm.py", line 396, in _get_prev_sample
    sample_coeff * sample - (alpha_prod_t_prev - alpha_prod_t) * model_output / model_output_denom_coeff
    ~~~~~~~~~~~~~^~~~~~~~
TypeError: unsupported operand type(s) for *: 'Tensor' and 'NoneType'

System Info

  • diffusers version: 0.17.0.dev0
  • Platform: Linux-5.15.0-72-generic-x86_64-with-glibc2.31
  • Python version: 3.11.3
  • PyTorch version (GPU?): 2.0.1+cu117 (True)
  • Huggingface_hub version: 0.14.1
  • Transformers version: 4.29.1
  • Accelerate version: 0.19.0
  • xFormers version: not installed
  • Using GPU in script?:
  • Using distributed or parallel set-up in script?:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions