1 报错内容

  • RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument mat2 in method wrapper_mm)

2 解决方法

  • 报错处的最后一行如下,需要在linear.py文件中将其修改。
File "/.../torch/nn/modules/linear.py", line xx, in forward 
return F.linear(input, self.weight, self.bias)
  • 由于在服务器中调用的是内置文件,并不能直接修改,因此需要转到该路径,然后使用文本编辑器(vim)修改其中的代码,具体操作如下。
vim /.../torch/nn/modules/linear.py
  • 源代码为:
def forward(self, input: Tensor) -> Tensor:
        return F.linear(input, self.weight, self.bias)
  • 修改为:
    def forward(self, input: Tensor) -> Tensor:
        device = input.device
        return F.linear(input, self.weight.to(device), self.bias)
  • 输入英文状态的 i 进入编辑模式,修改结束后按ESC退出编辑模式,然后输入:wq并按回车保存退出即可。
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐