Expected object of device type cuda but got device type cpu for argument #2 'mat1' in call to _th_addmm

username: Kai_Eiji
cooked: <p>Hi,<br>
I 've been trying to utilize cuda to train my neural network on colab but I’m having some trouble.<br>
Even though I set all tensor to use cuda(), it’s still saying it’s getting cpu for device.<br>
Here’s the error stack.</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-21-fc6b4297452e&gt; in &lt;module&gt;()
    221 perturbations = int(args.ptb_rate * (pure_adj.sum()//2))
    222 #test(adj, model)
--&gt; 223 test(adj, model)
    224 #print(torch.cuda.device_count())
    225 

6 frames
&lt;ipython-input-21-fc6b4297452e&gt; in test(adj, model)
    208     if source_domain == target_domain and train_mode == True:
    209         for epoch in range(args.epochs):
--&gt; 210             train(epoch, adj, model, optimizer)
    211         print("Optimization Finished!")
    212         print("Total time elapsed: {:.4f}s".format(time.time() - t_total))

&lt;ipython-input-21-fc6b4297452e&gt; in train(epoch, adj, model, optimizer)
    145     model.train()
    146     optimizer.zero_grad()
--&gt; 147     output, ctx = model(features, adj, nums)
    148 
    149     loss_train = F.nll_loss(output[idx_train], labels[idx_train])    #get loss for predicted log-likelyfood on train data

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--&gt; 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

/content/GCN_models.py in forward(self, inputx, adj, nums)
     32 #                x[it] = self.linear_p(torch.FloatTensor(k))
     33 #            else:
---&gt; 34 #                x[it] = self.linear_u(torch.FloatTensor(k))
     35         # review
     36         t1 = torch.FloatTensor(inputx[:nums[0][0]]).cuda()

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--&gt; 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/linear.py in forward(self, input)
     85 
     86     def forward(self, input):
---&gt; 87         return F.linear(input, self.weight, self.bias)
     88 
     89     def extra_repr(self):

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1608     if input.dim() == 2 and bias is not None:
   1609         # fused op is marginally faster
-&gt; 1610         ret = torch.addmm(bias, input, weight.t())
   1611     else:
   1612         output = input.matmul(weight.t())

RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'mat1' in call to _th_addmm
</code></pre>
<p>In my code I set</p>
<pre><code class="lang-auto">if has_cuda:
    model.cuda()
    adj = adj.cuda()
    labels = labels.cuda()
    idx_train = idx_train.cuda()
    idx_val = idx_val.cuda()
    idx_test = idx_test.cuda()
</code></pre>
<p>Also input is set to cuda in nn layer.</p>

username: SANTOSH_S
cooked: <p>Hi <a class="mention" href="/u/kai_eiji">@Kai_Eiji</a><br>
I am guessing that there is some operation in your code that uses variable(in cpu) and tensor(in gpu). You’ll need to check the code again.</p>

username: harsha_g
cooked: <p>Maybe <code>features</code> and/or <code>nums</code> has to be put on the gpu as well?</p>