无NVIDIA GPU下Stable Diffusion CPU运行问题及AMD适配咨询
问题描述
- 环境:无NVIDIA GPU,使用CompVis/stable-diffusion仓库,采用LAION400m权重(获取命令:
wget -O models/ldm/text2img-large/model.ckpt https://ommer-lab.com/files/latent-diffusion/nitro/txt2img-f8-large/model.ckpt) - 操作:尝试强制PyTorch使用CPU运行Stable Diffusion文本生成图像功能,已在
txt2img.py中指定CPU,但采样阶段出现报错 - 报错信息:
Falling back to LAION 400M model... Global seed set to 42 Loading model from models/ldm/text2img-large/model.ckpt LatentDiffusion: Running in eps-prediction mode DiffusionWrapper has 872.30 M params. making attention of type 'vanilla' with 512 in_channels Working with z of shape (1, 4, 32, 32) = 4096 dimensions. making attention of type 'vanilla' with 512 in_channels data: 0%| | 0/1 [00:00<?, ?it/s] Sampling: 0%| | 0/2 [00:00<?, ?it/s] Traceback (most recent call last): File "scripts/txt2img.py", line 279, in <module> main() File "scripts/txt2img.py", line 233, in main uc = model.get_learned_conditioning(batch_size * [""]) File "c:\users\louis\stable-diffusion\ldm\models\diffusion\ddpm.py", line 558, in get_learned_conditioning c = self.cond_stage_model.encode(c) File "c:\users\louis\stable-diffusion\ldm\modules\encoders\modules.py", line 111, in encode return self(text) File "C:\Users\louis\anaconda3\envs\ldm\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl return forward_call(*input, **kwargs) File "c:\users\louis\stable-diffusion\ldm\modules\encoders\modules.py", line 103, in forward tokens = self.tknz_fn(text)#.to(self.device) File "C:\Users\louis\anaconda3\envs\ldm\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl return forward_call(*input, **kwargs) File "c:\users\louis\stable-diffusion\ldm\modules\encoders\modules.py", line 74, in forward tokens = batch_encoding["input_ids"].to(self.device) File "C:\Users\louis\anaconda3\envs\ldm\lib\site-packages\torch\cuda\__init__.py", line 216, in _lazy_init torch._C._cuda_init() RuntimeError: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx
- 咨询问题:
- 无NVIDIA GPU时,用CPU运行Stable Diffusion是否可行?
- 如何修改代码实现CPU运行?
- 能否适配AMD GPU通过ROCm运行?
解答
1. CPU运行方案是否可行?
可行,但运行速度极慢——8亿多参数的模型在CPU上生成一张图可能需要几十分钟甚至数小时,仅适合功能测试,日常使用体验极差。
2. 代码修改实现CPU运行
报错根源是部分模块(如文本编码器)仍在尝试调用CUDA设备,需全局强制所有模型和张量运行在CPU上,具体步骤:
步骤1:全局锁定CPU设备
在txt2img.py开头添加代码,强制PyTorch禁用CUDA检测:
import torch torch.cuda.is_available = lambda: False device = torch.device("cpu")
步骤2:修正模型加载与设备分配
- 在
txt2img.py的main函数中,加载模型时指定CPU映射:model = load_model_from_config(config, opt.ckpt, map_location=device) - 修改
ldm/modules/encoders/modules.py中的设备设置:
找到BertEmbedder类的forward方法,把tokens = batch_encoding["input_ids"].to(self.device)直接改为:
或者在该类的tokens = batch_encoding["input_ids"].to(torch.device("cpu"))__init__方法中添加self.device = torch.device("cpu")。
步骤3:禁用GPU相关优化
检查所有涉及CUDA、混合精度的代码分支,确保CPU模式下跳过这些优化逻辑。
3. AMD GPU通过ROCm运行是否可行?
可行,但有严格前提:
- 仅支持Linux系统(ROCm对Windows的支持目前不完善)
- 需使用支持ROCm的AMD GPU(如RX 5000/6000系列、Instinct系列)
- 先安装ROCm环境,再安装适配ROCm版本的PyTorch
- CompVis/stable-diffusion仓库基本兼容ROCm,安装对应PyTorch后,运行时会自动识别AMD GPU,仅需少量调整设备分配逻辑即可正常使用
内容的提问来源于stack exchange,提问作者Louid
相关产品推荐
相关产品推荐

