函数计算FC文生图时,生成出错,怎么解决?RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument index in method wrapper_CUDA__index_select)
c-65a4f47a-0709fb26-6a7f96bfe7fc2024-01-15 17:08:56[GIN] 2024/01/15 - 09:08:55 | 422 | 891.286µs | 103.232.212.91 | POST "/internal/progress"
[GIN] 2024/01/15 - 09:08:55 | 422 | 715.813µs | 103.232.212.91 | POST "/run/predict"
[GIN] 2024/01/15 - 09:08:55 | 422 | 724.581µs | 103.232.212.91 | POST "/run/predict"
[GIN] 2024/01/15 - 09:08:55 | 422 | 707.779µs | 103.232.212.91 | POST "/run/predict"
[GIN] 2024/01/15 - 09:08:55 | 422 | 684.884µs | 103.232.212.91 | POST "/run/predict"
[GIN] 2024/01/15 - 09:08:56 | 422 | 735.541µs | 103.232.212.91 | POST "/internal/progress"
[GIN] 2024/01/15 - 09:08:56 | 404 | 728.18222ms | 103.232.212.91 | GET "/queue/join"
c-65a4f47a-0709fb26-6a7f96bfe7fc2024-01-15 17:08:57
FC Invoke End RequestId: 1-65a4f628-07f0db59-5029d756da97
c-65a4f47a-0709fb26-6a7f96bfe7fc2024-01-15 17:08:57* Error completing request
Arguments: ('task(hp2lvjjnuel7jck)', 'masterpiece, best quality, very detailed, extremely detailed beautiful, super detailed, tousled hair, illustration, dynamic angles, girly, fashion clothing, standing, mannequin, looking at viewer, interview, beach, beautiful detailed eyes, exquisitely beautiful face, floating, high saturation, beautiful and detailed light and shadow', 'loli,nsfw,logo,text,badhandv4,EasyNegative,ng_deepnegative_v1_75t,rev2-badprompt,verybadimagenegative_v1.3,negative_hand-neg,mutated hands and fingers,poorly drawn face,extra limb,missing limb,disconnected limbs,malformed hands,ugly', [], 20, 'Euler a', 1, 1, 7, 512, 512, False, 0.7, 2, 'Latent', 0, 0, 0, 'Use same checkpoint', 'Use same sampler', '', '', [], , 0, False, '', 0.8, -1, False, -1, 0, 0, 0, False, {}, {}, , , , 1, False, False, 'positive', 'comma', 0, False, False, '', 1, '', [], 0, '', [], 0, '', [], True, False, False, False, 0, False, None, None, False, None, None, False, None, None, False, 50) {}
Traceback (most recent call last):
File "/stable-diffusion-webui/modules/call_queue.py", line 57, in f
res = list(func(*args, **kwargs))
File "/stable-diffusion-webui/modules/call_queue.py", line 36, in f
res = func(*args, **kwargs)
File "/stable-diffusion-webui/modules/txt2img.py", line 55, in txt2img
processed = processing.process_images(p)
File "/stable-diffusion-webui/modules/processing.py", line 732, in process_images
res = process_images_inner(p)
File "/stable-diffusion-webui/extensions/sd-webui-controlnet/scripts/batch_hijack.py", line 42, in processing_process_images_hijack
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
根据您提供的错误信息,函数计算(FC)在执行文生图任务时出现了以下问题:
核心错误:
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0!
这表明代码中存在张量设备不一致的问题。部分张量位于 cpu
,而另一部分位于 cuda:0
(GPU)。这种设备不匹配通常发生在深度学习框架(如 PyTorch)中,未正确指定张量的设备。
HTTP 请求日志:
422
和 404
状态码,表明请求参数可能存在问题或某些资源路径未找到。/run/predict
和 /internal/progress
的多次调用失败,可能与模型加载或推理逻辑相关。其他异常:
Error completing request
表明请求未能成功完成。txt2img.py
和 processing.py
中,涉及图像生成和处理逻辑。根据错误提示,所有张量必须位于同一设备上(CPU 或 GPU)。以下是具体解决步骤:
检查模型和输入数据的设备一致性: 在代码中显式指定模型和输入数据的设备。例如,在 PyTorch 中可以使用以下代码将模型和张量移动到 GPU:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device) # 将模型移动到指定设备
input_tensor = input_tensor.to(device) # 将输入张量移动到指定设备
修改代码逻辑: 如果您的代码中存在动态加载模型或数据的部分,请确保每次加载后都进行设备一致性检查。例如:
if next(model.parameters()).device != input_tensor.device:
raise RuntimeError("Model and input tensor are on different devices!")
调试建议: 在关键位置打印设备信息,确认模型和张量的设备是否一致。例如:
print(f"Model device: {next(model.parameters()).device}")
print(f"Input tensor device: {input_tensor.device}")
注意:如果您的函数计算实例未启用 GPU 支持,请确保所有操作都在 CPU 上完成。
根据日志中的 422
和 404
错误,可能存在以下问题:
422 错误: 表示服务器理解请求实体的内容类型,并且请求实体的语法是正确的,但无法处理包含的指令。请检查以下内容:
404 错误: 表示请求的资源路径未找到。请确认以下内容:
/queue/join
路径是否存在。解决方法: - 检查函数计算的入口函数是否正确解析了请求参数。 - 确保模型文件和工作流配置文件已正确上传至 NAS 或其他存储服务,并在函数中正确挂载。
如果您的函数计算实例内存不足,可能导致程序 OOM(Out of Memory)。以下是优化建议:
增加内存规格: 登录函数计算控制台,进入目标函数的配置页面,增加内存规格并重新部署。
监控内存使用: 开启请求级别指标日志,分析内存用量。如果发现内存峰值接近上限,请进一步优化代码或增加内存。
根据知识库资料,启动命令问题可能导致函数无法正常运行。请检查以下内容:
启动命令权限: 确保启动脚本具有可执行权限。例如:
chmod +x start.sh
文件路径和格式: 确认启动命令中指定的文件路径正确,且文件格式无误。
为了更好地定位问题,建议在代码中增加异常捕获和日志记录。例如:
Python 示例:
try:
# 图像生成逻辑
processed = processing.process_images(p)
except RuntimeError as e:
print(f"RuntimeError: {e}")
raise
except Exception as e:
print(f"Unexpected error: {e}")
raise
日志记录: 使用函数计算的日志功能记录详细信息,便于后续排查。
通过以上步骤,您可以逐步解决函数计算 FC 文生图任务中的问题。重点包括:
如果问题仍未解决,请提供更多上下文信息(如完整代码片段或配置文件),以便进一步分析。您可以复制页面截图提供更多信息,我可以进一步帮您分析问题原因。