RuntimeError: Input, output and indices must be on the current device

username: Yunchao_Liu
cooked: <p>Hello, I think I already put all inputs and model in the same device. Yet I’m still getting the errror. Can someone help me?</p>
<pre><code class="lang-auto">def train(args, model, device, loader, optimizer):
    model.train()

    for step, batch in enumerate(tqdm(loader, desc="Iteration")):
        # batch = batch.to(device)
        print(f'device:{device}')
        batch.x = batch.x.to(device)
        batch.p = batch.p.to(device)
        batch.edge_index = batch.edge_index.to(device)
        batch.edge_attr = batch.edge_attr.to(device)
        batch.batch = batch.batch.to(device)
        model.to(device)
        print(f'batch.x:{batch.x}')
        print(f'batch.p:{batch.p}')
        print(f'batch.edge_index:{batch.edge_index}')
        print(f'batch.edge_attr:{batch.edge_attr}')
        print(f'batch:{batch.batch}')
        batch = batch.to(device)
        print(f'model device cuda: {next(model.parameters()).is_cuda}')
        pred, h = model(batch.x, batch.p, batch.edge_index,
                        batch.edge_attr, batch.batch)
        y = batch.y.view(pred.shape).to(torch.float64)

        # Whether y is non-null or not.
        is_valid = y**2 &gt; 0
        # Loss matrix
        loss_mat = criterion(pred.double(), (y + 1) / 2)
        # loss matrix after removing null target
        loss_mat = torch.where(is_valid, loss_mat, torch.zeros(
            loss_mat.shape).to(loss_mat.device).to(loss_mat.dtype))

        optimizer.zero_grad()
        loss = torch.sum(loss_mat) / torch.sum(is_valid)
        loss.backward()

        optimizer.step()
</code></pre>
<p>The output of the above codes:</p>
<blockquote>
<p>device:cuda:0<br>
batch.x:tensor([[ 8.0000, 15.9990,  1.5500,  2.0000,  6.0000],<br>
[ 6.0000, 12.0110,  1.7000,  4.0000,  4.0000],<br>
[ 6.0000, 12.0110,  1.7000,  4.0000,  4.0000],<br>
…,<br>
[ 1.0000,  1.0080,  1.2000,  1.0000,  1.0000],<br>
[ 1.0000,  1.0080,  1.2000,  1.0000,  1.0000],<br>
[ 1.0000,  1.0080,  1.2000,  1.0000,  1.0000]], device=‘cuda:0’)<br>
batch.p:tensor([[-3.4916, -0.1727],<br>
[-2.6944, -0.1722],<br>
[-2.2801, -1.6138],<br>
…,<br>
[ 4.4742, -5.0393],<br>
[ 3.3981, -2.2389],<br>
[-2.5284, -1.3024]], device=‘cuda:0’)<br>
batch.edge_index:tensor([[   0,    1,    1,  …, 1416, 1395, 1417],<br>
[   1,    0,    2,  …, 1394, 1417, 1395]], device=‘cuda:0’)<br>
batch.edge_attr:tensor([[2],<br>
[2],<br>
[1],<br>
…,<br>
[1],<br>
[1],<br>
[1]], device=‘cuda:0’)<br>
batch:tensor([ 0,  0,  0,  …, 31, 31, 31], device=‘cuda:0’)<br>
model device cuda: True</p>
</blockquote>

username: InnovArul
cooked: <aside class="quote no-group" data-username="Yunchao_Liu" data-post="1" data-topic="126534">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yunchao_liu/48/38216_2.png" class="avatar"> Yunchao_Liu:</div>
<blockquote>
<pre><code class="lang-auto">        loss_mat = torch.where(is_valid, loss_mat, torch.zeros(
            loss_mat.shape).to(loss_mat.device).to(loss_mat.dtype))
</code></pre>
</blockquote>
</aside>
<p>Do you think <code>is_valid</code> is on the same device?<br>
Also, do you know the line number of the error?</p>

username: Yunchao_Liu
cooked: <p>Hi, <a class="mention" href="/u/innovarul">@InnovArul</a></p>
<p>The error occurs before loss_mat and is on this line “pred, h = model(batch.x, batch.p, batch.edge_index, batch.edge_attr, batch.batch)”</p>
<p>Traceback (most recent call last):<br>
File “finetune.py”, line 286, in <br>
main()<br>
File “finetune.py”, line 256, in main<br>
train(args, model, device, train_loader, optimizer)<br>
File “finetune.py”, line 48, in train<br>
batch.edge_attr, batch.batch)<br>
File “/home/liuy69/bin/anaconda2/envs/GCN_property/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 727, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “/home/liuy69/projects/GCN_Syn/examples/pretrain-gnns/chem/model.py”, line 140, in forward<br>
node_representation = self.gnn(x= x, edge_index = edge_index, edge_attr = edge_attr, p = p)<br>
File “/home/liuy69/bin/anaconda2/envs/GCN_property/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 727, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “/home/liuy69/projects/GCN_Syn/examples/pretrain-gnns/chem/model.py”, line 52, in forward<br>
sim_sc = kernel_layer(data = data)<br>
File “/home/liuy69/bin/anaconda2/envs/GCN_property/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 727, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “/home/liuy69/projects/GCN_Syn/examples/pretrain-gnns/chem/kernels.py”, line 569, in forward<br>
return self.conv(data=data)<br>
File “/home/liuy69/bin/anaconda2/envs/GCN_property/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 727, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “/home/liuy69/projects/GCN_Syn/examples/pretrain-gnns/chem/kernels.py”, line 506, in forward<br>
deg, x, p, edge_index, edge_attr)<br>
File “/home/liuy69/projects/GCN_Syn/examples/pretrain-gnns/chem/kernels.py”, line 451, in convert_graph_to_receptive_field<br>
deg=deg, x=x, edge_index=edge_index, p=p, edge_attr=edge_attr)<br>
File “/home/liuy69/projects/GCN_Syn/examples/pretrain-gnns/chem/kernels.py”, line 425, in get_neighbor_nodes_and_edges_of_degree<br>
edge_attr, edge_index, center_index[i])<br>
File “/home/liuy69/projects/GCN_Syn/examples/pretrain-gnns/chem/kernels.py”, line 393, in get_edge_attr_support_from_center_node<br>
input=edge_attr, dim=0, index=bond_id)<br>
RuntimeError: Input, output and indices must be on the current device</p>

username: Yunchao_Liu
cooked: <p>Solved. It turned out that I have some tensors created inside the model, which were not assigned device.</p>