问题描述
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1!
解决办法
这个办法比较常见,就是说在训练的时候需要张量在同一个设备上,但是呢不小心放到了其他的设备,所以有这个错误,那怎么办呢?调呗
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
改为
device = torch.device('cuda:1' if torch.cuda.is_available() else 'cpu')
再ctr+F在各个py文件查找关键词“cuda” ,找到了
改!!!
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
改为
device = torch.device('cuda:1' if torch.cuda.is_available() else 'cpu')
齐活!
总结
其实写到这里会发现上面两部分代码是一样的,无所谓,主要是证明两个得是一样的,仅此而已。
延展阅读
首先我们要知道原因有两种:
参与运算的两个或多个变量,有的在CPU上,有的在GPU上
- 首先找到报错的行,看看计算时都用到哪些变量或者数据,然后在调试模式下使用.is_cuda这个属性去查看到底哪些是在GPU上,哪些是在CPU上,然后把它们统一都放在CPU,或者统一放在GPU上就可以。
- 如果增加了变量,需要将新增加的变量也搬到GPU上去
一个数据在GPU0上,一个数据在GPU1上
造成这个错误的可能性有挺多,总起来是模型、输入、模型内参数不在一个GPU上。本人是在调试RandLA-Net pytorch源码,希望使用双GPU训练,经过尝试解决这个问题,此处做一个记录,希望给后来人一个提醒。
经过调试,发现报错的地方主要是在数据拼接的时候,即一个数据在GPU0上,一个数据在GPU1上,这就会出现错误,就按照我上面的解决办法就可以了,也可以参考这个博主
完结撒花
我不是不知道痛,只是习惯了而以