正常的代码:
from diffusers import DiffusionPipeline
import torch
# runwayml/stable-diffusion-v1-5
model_path="/models/stable-diffusion-v1-5"
pipeline = DiffusionPipeline.from_pretrained(model_path,
torch_dtype=torch.float16
)
pipeline.to("cuda")
pipeline("1 cat").images[0]
会报错并生成一张全黑的图片:
RuntimeWarning: invalid value encountered in cast
images = (images * 255).round().astype("uint8")
pytorch/pytorch#58123 (comment) - 虽然这里说的是 1660
,主要问题是:
- cuda, fp16 核心很少,大部分是fp32核心
- 因此使用 fp16 会造成问题
- 但是如果使用 fp32,显存不够
解决办法是 huggingface/diffusers#2153 (comment) :
from diffusers import DiffusionPipeline
import torch
# runwayml/stable-diffusion-v1-5
model_path="/models/stable-diffusion-v1-5"
pipeline = DiffusionPipeline.from_pretrained(model_path,
use_safetensors=True,
safety_checker = None,
requires_safety_checker = False
)
pipeline.enable_sequential_cpu_offload()
pipeline("1cat").images[0]
缺点是模型没有加载到显存中常驻,只在使用时加载,