各位大神們好:
以下是小弟目前遇到的問題:
當兩筆圖片生成請求同時使用同一個已載入的 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 **
1.Pipeline 是否設計上就不支援 thread-safe?
2.有沒有參數或設定可以讓同一個 pipeline 安全地處理並行請求?
3.或者有推薦的 best practice 來處理這種場景?
感謝各位