Stable Diffusion 生成出现错误:NansException: A tensor with all NaNs was produced in Une,解决方法

因正常运行 Stable Diffusion 需要 nvidia 独立显卡8G以上,但是以前没有独立显卡,通过设置文件可以利用CPU运行 stable diffusion ,不过生成图片确实太慢了,几分钟才出一张图。

实在不能忍受于是购了一张 nvidia RTX 3060 12G,装上以后启动 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.

下面是解决方法:

翻译一下错误的意思大概是:

NansException:在 Unet 中生成了所有 NaN 的张量。 这可能是因为没有足够的精度来表示图片,或者是因为您的视频卡不支持半字。 尝试在 Settings > Stable Diffusion 中将“Upcast cross attention layer to float32”选项设置为或使用 –no-half 命令行参数来解决这个问题。 使用 –disable-nan-check 命令行参数禁用此检查。

第一次解决方法:

点开:设置 – Stable Diffusion – 将交叉关注层向上投射到float32,看见没有勾选,于是勾选,保存设置

重新点生成图片,又出现了新的错误提示:

ValueError: Query/Key/Value should all have the same dtype query.dtype: torch.float32 key.dtype : torch.float32 value.dtype: torch.float16

翻译大概意思是:

ValueError:查询/键/值都应具有相同的数据类型 query.dtype:torch.float32 key.dtype:torch.float32 value.dtype:torch.float16

最终解决方法:

于是我又重复上面的步骤:设置 – Stable Diffusion – 将交叉关注层向上投射到float32,但这次是取消勾选,保存设置重置UI

等待 Stable Diffusion 重置后,这次重新点击:生成,生成进度百分比居然正常了可以正常生成图片了。

遇到同样问题的小伙伴们可以尝试我上面的解决方法,顺便说说 Stable Diffusion 换成 nvidia RTX 3060 12G 后生成一张图片几秒钟就完成了,和以前运行CPU生成图片对比,生成时间简直悬殊太大了 。

最新更新:上面介绍的解决方法有可能不能解决根本问题,会引起另外一个报错,可以看看后面这篇文章:

Stable Diffusion 生成时报错解决方法:ValueError: Query/Key/Value should all have the same dtype query.dtype: torch.float32 key.dtype : torch.float32 value.dtype: torch.float16

点击直达: AI 图片免费生成工具

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注