iT邦幫忙

0

Diffusion Pipeline 同時多筆請求會 index out of bounds,有無可同時處理多筆的作法?

  • 分享至 

  • xImage

各位大神們好:

以下是小弟目前遇到的問題:
當兩筆圖片生成請求同時使用同一個已載入的 pipeline 實例進行推論時(num_inference_steps=50),兩個請求都會在約第 26 步時報錯:
index 51 is out of bounds for dimension 0 with size 51

Log:

0%|          | 0/50 [00:00<?, ?it/s]
2%|▏         | 1/50 [00:00<00:18, 2.59it/s]
0%|          | 0/50 [00:00<?, ?it/s]
4%|▍         | 2/50 [00:00<00:22, 2.18it/s]
2%|▏         | 1/50 [00:00<00:38, 1.28it/s]
... (omitted) ...
52%|█████▏    | 26/50 [00:20<00:19, 1.26it/s]
52%|█████▏    | 26/50 [00:20<00:19, 1.25it/s]
CRITICAL | Unexpected error: index 51 is out of bounds for dimension 0 with size 51

推測原因: 兩筆請求共用了 scheduler 內部的 timestep index,當兩邊各跑 26 步時(26 × 2 = 52),超過了 timesteps 的 size 50,導致越界錯誤。

程式碼如下

python
from diffusers import DiffusionPipeline
import torch

  self.device = "cuda"
   self.torch_dtype = torch.bfloat16

   self.pipe = DiffusionPipeline.from_pretrained(
                "Qwen/Qwen-Image",
                torch_dtype=self.torch_dtype,
                local_files_only=True,
                cache_dir=self.cache_dir
            ).to(self.device) 


   images = self.pipe(
            prompt=prompt,
            negative_prompt=negative_prompt,
            width=width,
            height=height,
            num_inference_steps=50,
            true_cfg_scale=true_cfg_scale,
            num_images_per_prompt=num_images_per_prompt,
            generator=generator
        ).images

**已知的 workaround **

  • 加 Lock 序列化 — 無法並行處理,是目前的做法,但一次只能一筆有點沒效率。
  • Batching — 需要收集請求(prompt),再一次發送給 pipe 共用同個權重,但就是會增加等待收集的時間,也想要看看有沒有更好的做法或原生的作法。
  • 多個 Pipeline 實例 — 但 VRAM 消耗翻倍(GPU 資源有限)。

想請問

1.Pipeline 是否設計上就不支援 thread-safe?
2.有沒有參數或設定可以讓同一個 pipeline 安全地處理並行請求?
3.或者有推薦的 best practice 來處理這種場景?

感謝各位

圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答