Skip to content

Instantly share code, notes, and snippets.

@MarshalW
Last active December 5, 2023 11:25
Show Gist options
  • Save MarshalW/0870ce53225ff30dbb37f21f1fc0e1e1 to your computer and use it in GitHub Desktop.
Save MarshalW/0870ce53225ff30dbb37f21f1fc0e1e1 to your computer and use it in GitHub Desktop.
stable diffusion 使用 gtx1650 的问题

stable diffusion 使用 gtx1650 的问题

正常的代码:

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]

缺点是模型没有加载到显存中常驻,只在使用时加载,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment