我使用的是来自Python3.10.2中的拥抱脸StableDiffusionPipeline库的M2 Mac (我标记它是因为这可能是问题所在)。当我尝试从1个提示符生成1个图像时,输出看起来很好,但是当我试图使用同一个提示符生成多个图像时,图像要么是黑色方块,要么是随机图像(参见下面的示例)。有什么问题吗?
我的代码如下(为了打破它,我将n_imgs从1更改为多个):
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
pipe = pipe.to("mps") # for M1/M2 chips
pipe.enable_attention_slicing()
prompt = "a photo of an astronaut driving a car on mars"
# First-time "warmup" pass (because of weird M1 behaviour)
_ = pipe(prompt, num_inference_steps=1)
# generate images
n_imgs = 1
imgs = pipe([prompt] * n_imgs).images我还尝试设置num_images_per_prompt,而不是在管道调用中创建一个重复的提示列表,但这也产生了同样的坏结果。
示例输出(用于多个图像):

编辑/更新:当我在围绕管道调用的循环中生成映像而不是传递一个可迭代的管道调用时,它确实有效:
# generate images
n_imgs = 3
for i in range(n_imgs):
img = pipe(prompt).images[0]
# do something with img但对我来说,原因仍然是个谜。
发布于 2022-12-01 13:41:33
显然,这确实是一个苹果硅(M1/M2)的问题,拥抱脸还不确定为什么会发生这种情况,更多细节请看这个GitHub问题。
发布于 2022-12-02 11:22:19
我认为这可能是一个PyTorch问题,因为在我上次测试时,纯MPS版本的代码(在Swift中)工作得很好:
import MetalPerformanceShadersGraph
let graph = MPSGraph()
let x = graph.constant(1, shape: [32, 4096, 4096], dataType: .float32)
let y = graph.constant(1, shape: [32, 4096, 1], dataType: .float32)
let z = graph.matrixMultiplication(primary: x, secondary: y, name: nil)
let device = MTLCreateSystemDefaultDevice()!
let buf = device.makeBuffer(length: 16384)!
let td = MPSGraphTensorData(buf, shape: [64, 64], dataType: .int32)
let cmdBuf = MPSCommandBuffer(from: device.makeCommandQueue()!)
graph.encode(to: cmdBuf, feeds: [:], targetOperations: nil, resultsDictionary: [z:td], executionDescriptor: nil)
cmdBuf.commit()有关详细信息,请参阅此线程:https://github.com/pytorch/pytorch/issues/84039
https://stackoverflow.com/questions/74642594
复制相似问题