解决RuntimeError: Expected all tensors to be on the same device, but found at least two devices,

本文探讨了在深度学习中常见的CUDA和CPU设备兼容问题,包括RuntimeError关于设备不一致的错误,以及如何通过检查设备并统一变量放置来修复。重点讲解了addmm操作时Tensor位置错误的解决方法。
Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen
文本生成
Qwen3

本镜像基于 RTX 4090D 24GB 显存 + CUDA 12.4 + 驱动 550.90.07 深度优化,内置完整运行环境与 Qwen3-32B 模型依赖,开箱即用。

目录

1.错误:RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

2.错误:Tensor for argument #2 ‘mat1’ is on CPU, but expected it to be on GPU (while checking arguments for addmm)


1.错误:RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

原因:参与运算的两个或多个变量,有的在CPU上,有的在GPU上

解决:

  1. 首先找到报错的行,看看计算时都用到哪些变量或者数据,然后在调试模式下使用.is_cuda这个属性去查看到底哪些是在GPU上,哪些是在CPU上,然后把它们统一都放在CPU,或者统一放在GPU上就可以。
  2. 如果增加了变量,需要将新增加的变量也搬到GPU上去
g.ndata['norm'] = norm.unsqueeze(1)
g = g.to(device)

2.错误:Tensor for argument #2 ‘mat1’ is on CPU, but expected it to be on GPU (while checking arguments for addmm)

为了使模型在GPU上进行计算,需要将变量和模型都增加.to(device),都搬到GPU上去即可

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

g = g.to(device)

model=model.to(device)

您可能感兴趣的与本文相关的镜像

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen
文本生成
Qwen3

本镜像基于 RTX 4090D 24GB 显存 + CUDA 12.4 + 驱动 550.90.07 深度优化,内置完整运行环境与 Qwen3-32B 模型依赖,开箱即用。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值