Error: Expected all tensors to be on the same device, While all are on same device

Viewed 45

I don't know why I am getting this error, while my all tensors are at the same device "cuda". Here is code

def train(self):
    self.model.train()
    self.optimizer.zero_grad()  # Clear gradients.
    print("X is",self.XT.get_device())
    print("edge_index_ is", self.edge_index_.get_device())
    print("R_train_mask is", self.R_train_mask.get_device())

    print("datas.y is", self.datas.y.get_device())
    print("edge_index_ is", self.edge_index_.get_device())
    out = self.model(self.XT, self.edge_index_)  # Perform a single forward pass.
    loss = self.criterion(out[self.R_train_mask],
                          self.datas.y[self.R_train_mask])

Here is error

X is 0
edge_index_ is 0
R_train_mask is 0
datas.y is 0
edge_index_ is 0
Traceback (most recent call last):
  File "/home/adnan/GNNPaperCodes/PyGCL/examples/GCAPaperAd.py", line 126, in <module>
    main()
  File "/home/adnan/GNNPaperCodes/PyGCL/examples/GCAPaperAd.py", line 91, in main
    topfeatures=tf.TopFeaturesFind()
  File "/home/adnan/GNNPaperCodes/PyGCL/examples/PatchFinder.py", line 64, in TopFeaturesFind
    loss = self.train()
  File "/home/adnan/GNNPaperCodes/PyGCL/examples/PatchFinder.py", line 154, in train
    out = self.model(self.XT, self.edge_index_)  # Perform a single forward pass.
  File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/adnan/GNNPaperCodes/PyGCL/examples/PatchFinder.py", line 211, in forward
    x = self.conv1(x, edge_index_)
  File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch_geometric/nn/conv/gcn_conv.py", line 181, in forward
    x = self.lin(x)
  File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch_geometric/nn/dense/linear.py", line 103, in forward
    return F.linear(x, self.weight, self.bias)
  File "/home/adnan/.conda/envs/forGit/lib/python3.9/site-packages/torch/nn/functional.py", line 1848, in linear
    return torch._C._nn.linear(input, weight, bias)
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)

Obvioulsy, Everything is ok if i change device to 'cpu'

1 Answers

You forgot to transfer self.model to the device. Indeed, the model has weights that must be transferred to the correct device, since they interact with your input.

You can do it with self.model.to("cuda").

Related