Stable Diffusion生成图错误,如何解决

各位博主们好,小小问题,希望博主们可以帮忙解答。
本地部署完Stable Diffusion后,在文生图的时候,提示如下报错。

NansException: A tensor with all NaNs was produced in Unet. This could be either because there's not enough precision to represent the picture, or because your video card does not support half type. Try setting the "Upcast cross attention layer to float32" option in Settings > Stable Diffusion or using the --no-half commandline argument to fix this. Use --disable-nan-check commandline argument to disable this check.

这个报错信息表明,在使用 Stable Diffusion 进行文生图时,Unet 模型产生了全为 NaN 的张量,可能是因为计算精度不足或显卡不支持 half 类型所致。可以尝试在 Stable Diffusion 的设置中将“Upcast cross attention layer to float32”选项设置为打开,或者使用 --no-half 命令行参数来解决此问题。如果需要禁用此检查,可以使用 --disable-nan-check 命令行参数。

具体来说,可以按照以下步骤来设置 Stable Diffusion:

打开 Stable Diffusion 的设置页面。

在设置页面中,找到“Upcast cross attention layer to float32”选项,并将其打开。

重新运行文生图程序,看看是否还会出现报错。

如果仍然存在问题,可以尝试使用 --no-half 命令行参数来禁用 half 类型。在运行文生图程序时,可以在命令行中添加 --no-half 参数,例如:

python run_diffusion.py --no-half
这样可以强制使用 float32 类型,避免由于显卡不支持 half 类型而导致的问题。

如果以上方法都无法解决问题,可以尝试使用 --disable-nan-check 命令行参数来禁用 NaN 检查。在运行文生图程序时,可以在命令行中添加 --disable-nan-check 参数,例如:

python run_diffusion.py --disable-nan-check
需要注意的是,禁用 NaN 检查可能会导致程序出现其他问题,因此建议仅在无法通过其他方式解决问题时才使用此选项。