diffusers icon indicating copy to clipboard operation
diffusers copied to clipboard

Question about thread safety

Open miroljub1995 opened this issue 2 years ago • 4 comments

We are thinking if we can reuse the single pipe, created on startup, between multiple threads from ThreadPoolExecutor. Is it thread safe?

miroljub1995 avatar Dec 08 '22 12:12 miroljub1995

I think this really depends on how you use the pipeline. Note that schedulers which are part of the pipeline change the inner state quite a bit, therefore I don't think the pipeline is thread safe. You could however try to copy only the scheduler (it's very light weight and just a Python class) across threads.

Very curious to hear if "thread safety" is a common concern, issue -> we could think about a better API to ensure thread safety.

patrickvonplaten avatar Dec 12 '22 15:12 patrickvonplaten

For some more details this implementation should be thread safe:

from diffusers import DiffusionPipeline, StableDiffusionPipeline
from copy import deepcopy

pipe = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")

components = pipe.components
components.pop("scheduler")
scheduler = pipe.scheduler

threaded_pipelines = []

num_threads = 10
for i in range(num_threads):
    scheduler = deepcopy(scheduler)
    threaded_pipelines.append(StableDiffusionPipeline(**components, scheduler=scheduler))

Here all the schedulers are copied and only the schedulers are stateful in inference, so this should work just fine at 0 memory overhead :-)

patrickvonplaten avatar Dec 12 '22 15:12 patrickvonplaten

I have the same question, how to make sure pipeline generating thing in threadsafe ?

nguyenmeteorops avatar Dec 12 '22 15:12 nguyenmeteorops

Here we go: https://github.com/huggingface/diffusers/issues/1606#issuecomment-1346667389 ;-)

patrickvonplaten avatar Dec 16 '22 14:12 patrickvonplaten

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

github-actions[bot] avatar Jan 09 '23 15:01 github-actions[bot]

Here we go: #1606 (comment) ;-)

I don't think this guarantees the thread safety of the pipeline

haoqiangyu avatar Jul 03 '23 12:07 haoqiangyu

Here we go: #1606 (comment) ;-)

I don't think this guarantees the thread safety of the pipeline

If you use pytorch2.0.0 and above, it will cause the thread to freeze.

haoqiangyu avatar Jul 05 '23 03:07 haoqiangyu