RuntimeError: Device type PRIVATEUSEONE is not supported for torch.Generator() api.”

这个问题通常出现在把 Stable Diffusion WebUI 的采样设备从 CUDA 切到 PyTorch-DirectML(dml)后,采样阶段 torch.Generator 收到 k-diffusion 里的 DirectML 张量,从而抛出 RuntimeError: Device t

快速结论:这个问题通常出现在把 Stable Diffusion WebUI 的采样设备从 CUDA 切到 PyTorch-DirectML(dml)后,采样阶段 torch.Generator 收到 k-diffusion 里的 DirectML 张量,从而抛出 RuntimeError: Device type PRIVATEUSEONE is not supported for torch.Generator() api.。优先排查 DirectML 分支对采样器与 k-diffusion 的兼容性,不要先怀疑模型或显卡驱动。

适用环境:Issue 已确认的是 Windows 10 + DirectX 12、AMD 或 Intel UHD 620 这类通过 PyTorch-DirectML 跑图的设备;涉及 Stable Diffusion WebUI 的 txt2img 与 k-diffusion 采样器(如 Euler a)。Python、CUDA、PyTorch 具体版本未在讨论中给出明确结论,不建议直接照搬版本号。

最快修复方案:暂无确认的一步修复方案。Issue 中明确验证过的处理方式是修改 k-diffusion 的 external.py 里 sigma_to_t 函数,绕过对 DirectML 张量调用 torch.Generator 相关的设备检查;这只是社区 workaround,并非官方修复。

注意事项:该 workaround 修改的是第三方仓库 k-diffusion 源码,WebUI 更新或重新拉取子模块时可能被覆盖;只验证了 Intel UHD 620 等有限环境,其他采样器、图生图、批量生成是否同样稳定未在 Issue 中确认。DirectML 路径本身依赖的算子支持仍不完整,不能等同于 CUDA 方案。

问题场景

用户在 Windows 10 + DirectX 12 环境里给 Stable Diffusion WebUI 接入 PyTorch-DirectML,希望在没有 CUDA 的 AMD 或 Intel 显卡上获得比 CPU 更快的速度。把设备指向 dml、在 WebUI 中执行 512×512、20 步的 txt2img 生成时,采样器进入 k-diffusion 的 sample_euler_ancestral,在 sigma_to_t 里对 sigma 距离做 ge/cumsum/argmax/clamp 时触发异常。

报错原文

RuntimeError: Device type PRIVATEUSEONE is not supported for torch.Generator() api.

Traceback (most recent call last):
  File "D:\dev\stable-diffusion-webui\modules\processing.py", line 627, in process_images_inner
    samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, ...)
  File "D:\dev\stable-diffusion-webui\modules\sd_samplers.py", line 544, in sample
    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, x, ...))
  File "D:\dev\stable-diffusion-webui\repositories\k-diffusion\k_diffusion\sampling.py", line 145, in sample_euler_ancestral
    denoised = model(x, sigmas[i] * s_in, **extra_args)
  File "D:\dev\stable-diffusion-webui\repositories\k-diffusion\k_diffusion\external.py", line 72, in sigma_to_t
    low_idx = dists.ge(0).cumsum(dim=0).argmax(dim=0).clamp(max=self.log_sigmas.shape[0] - 2)
RuntimeError

原因分析

可能原因是 DirectML 后端把设备注册成 PyTorch 内部的 PRIVATEUSEONE 设备类型,而 k-diffusion 的 sigma_to_t 路径(以及相关采样逻辑)会用到 torch.Generator 或依赖默认生成器的算子,这些算子只支持 cuda/cpu 等常规设备,于是直接报设备类型不受支持。Issue 讨论也提到 DirectML 当前的算子覆盖仍不完整,需要不少 workaround 才能在 WebUI 里跑通,因此这不是单个模型或权重的问题。

环境排查

  • 确认操作系统为 Windows 10 且支持 DirectX 12,GPU 为 AMD 或 Intel 等非 CUDA 设备。
  • 确认 WebUI 使用的是 PyTorch-DirectML 的独立虚拟环境(Issue 中路径为 venv-dml),而不是原来的 CUDA 环境。
  • 确认 modules/devices.py 中 get_optimal_device 已按 Issue 讨论的方式优先返回 torch_directml.device(),否则采样仍可能落在 CPU 上。
  • 确认 k-diffusion 子模块是否被更新或重置,因为 workaround 修改的是 repositories/k-diffusion/k_diffusion/external.py。
  • 确认触发报错时使用的采样器(Issue 中为 Euler a,对应 sample_euler_ancestral),以及生成分辨率、步数、批量大小。
  • PyTorch、PyTorch-DirectML、kornia、xformers、PyTorch Lightning 的版本在 Issue 中没有形成一致性结论;xformers 在旧 DirectML 方案下被认为不可用,建议不要把它当作必装项。

解决步骤

  1. 先按 Issue 讨论确认 WebUI 已能识别 DirectML 设备:在 modules/devices.py 的 get_optimal_device 中,在 CUDA 判断之后尝试 import torch_directml 并返回 torch_directml.device(),失败再回退 mps/cpu(可优先尝试,属于社区方案)。
  2. 打开 repositories/k-diffusion/k_diffusion/external.py,定位 sigma_to_t 函数。
  3. 用 Issue 中用户提供的 workaround 替换 sigma_to_t 的默认实现:先计算 dists = torch.abs(sigma - self.sigmas[:, None]),需要量化时用 torch.argmin(dists, dim=0).view(sigma.shape) 取最近索引,避免走到对 DirectML 张量不支持的生成器/设备路径。
  4. 保存后重启 WebUI,优先用报错时相同的采样器(如 Euler a)、512×512、20 步做最小复现测试。
  5. 如果仍失败,换一个不依赖该 k-diffusion 分支的采样器或先退回 CPU 验证模型与 WebUI 本身正常,再把问题收敛到 DirectML 算子兼容性。

验证方法

重新执行同一组 txt2img 参数,不再出现 RuntimeError: Device type PRIVATEUSEONE is not supported for torch.Generator() api.,且任务队列能正常完成并输出图片,即可认为该 workaround 在当前环境生效。若只是换采样器后不报错,也应记录采样器名称与参数,便于判断是局部绕过还是整体可用。

参考来源

AUTOMATIC1111/stable-diffusion-webui #3756

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

这个方案解决了吗?

celebrityanime
celebrityanime
文章: 29911

发表回复

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