RuntimeError: Expected object of type torch.LongTensor but found type torch.cuda.LongTensor for argument #3 'index'

username: aerinykim
cooked: <p>I’m getting an error saying</p>
<blockquote>
<pre><code>RuntimeError: Expected object of type torch.LongTensor but found type torch.cuda.LongTensor for argument #3 'index'
</code></pre>
</blockquote>
<p>Can someone tell me what it means by <code>argument #3 "index"</code>? I can’t find this error message either in torch.nn.modules.sparse nor <a href="https://pytorch.org/docs/0.4.0/_modules/torch/nn/functional" class="inline-onebox" rel="noopener nofollow ugc">torch.nn.functional — PyTorch master documentation</a>.</p>
<p>Here is my code:</p>
<pre><code class="lang-auto">        self.network.eval()        
        batch['doc_tok']=batch['doc_tok'].long().cpu()
        batch['query_tok']=batch['query_tok'].long().cpu()
        self.network.cpu()
        
        print(batch['doc_tok'].dtype,  batch['query_tok'].dtype)

        start, end, _ = self.network(batch)
</code></pre>
<p>As you see, I even explicitly sent all variables to <code>cpu</code> to debug this error.<br>
And batch[‘doc_tok’], batch[‘query_tok’] prints out as both <code>torch.int64</code>.</p>
<p>Here is the error I’m getting.</p>
<pre><code class="lang-auto">  File "/home/aerin/Desktop/squad_vteam/src/model.py", line 110, in adversarial_loss
    start, end, _ = self.network(batch)
  File "/home/aerin/anaconda3/envs/pytorch0.41/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/aerin/Desktop/squad_vteam/src/dreader.py", line 78, in forward
    doc_mask, query_mask = self.lexicon_encoder(batch)
  File "/home/aerin/anaconda3/envs/pytorch0.41/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/aerin/Desktop/squad_vteam/src/encoder.py", line 116, in forward
    doc_emb, query_emb = emb(doc_tok), emb(query_tok)
  File "/home/aerin/anaconda3/envs/pytorch0.41/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/aerin/anaconda3/envs/pytorch0.41/lib/python3.6/site-packages/torch/nn/modules/sparse.py", line 110, in forward
    self.norm_type, self.scale_grad_by_freq, self.sparse)
  File "/home/aerin/anaconda3/envs/pytorch0.41/lib/python3.6/site-packages/torch/nn/functional.py", line 1110, in embedding
    return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: Expected object of type torch.LongTensor but found type torch.cuda.LongTensor for argument #3 'index'
</code></pre>
<p>Where should I look at to fix this error?</p>
<p>Any comment (even though it’s short!) or just listing keywords to look at will be highly appreciated!</p>

username: albanD
cooked: <p>Hi,</p>
<p>The error states that you provide a cpu tensor while a gpu tensor is required.<br>
I guess you called <code>self.network.cuda()</code> somewhere? That would mean your net expects cuda tensors and not cpu ones.</p>

username: aerinykim
cooked: <p>Thanks <a class="mention" href="/u/alband">@albanD</a> for replying! Yes, I called self.model.cuda() when the model is initialized. I thought that I can switch it back to CPU by calling self.network.cpu(). Can’t I?</p>
<p>The reason why I’m doing this (switching GPU &lt;&gt; CPU) is because I was getting GPU out of memory error and <code>start, end, _ = self.network(batch)</code> which is a simple forward propagation (all autograd disabled by calling <code>self.network.eval()</code>.)</p>

username: albanD
cooked: <p>You can switch it back to cpu by doing <code>self.nerwork.cpu()</code>.</p>

username: aerinykim
cooked: <p>Thanks! It was a typo.</p>

username: Alekhya_Vellanki
cooked: <p>Hey <a class="mention" href="/u/aerinykim">@aerinykim</a> and <a class="mention" href="/u/alband">@albanD</a>,</p>
<p>I’m running into a similar issue. Getting exactly the same RuntimeError.<br>
This is the code I’m trying to run:<a href="https://github.com/jamie-murdoch/ContextualDecomposition/blob/master/train_model/train.py" rel="nofollow noopener">https://github.com/jamie-murdoch/ContextualDecomposition/blob/master/train_model/train.py</a></p>
<p>I’m running into the error at line 78, where we train the model:</p>
<pre><code class="lang-auto"># forward pass
answer = model(batch)
</code></pre>
<p>Not understanding why <code>batch</code> is not on GPU though we set the device in the code.<br>
Also how do we move <code>batch</code> to GPU? <code>cuda()</code> and <code>to(device)</code> methods aren’t defined for <code>batch</code> (<code>&lt;class torchtext.data.batch.Batch&gt;</code>).</p>
<p>Please help! I’m stuck with this from the past few days <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"></p>

username: Alekhya_Vellanki
cooked: <p>Not sure if this helps, here’s the full trace:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
File "./train_model/train.py", line 81, in &lt;module&gt;
answer = model(batch)
File "/usr/local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "/floyd/home/train_model/model.py", line 29, in forward
vecs = self.embed(batch.text)
File "/usr/local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "/usr/local/lib/python3.6/site-packages/torch/nn/modules/sparse.py", line 110, in forward
self.norm_type, self.scale_grad_by_freq, self.sparse)
File "/usr/local/lib/python3.6/site-packages/torch/nn/functional.py", line 1110, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: Expected object of type torch.cuda.LongTensor but found type torch.LongTensor for argument #3 'index'
</code></pre>

username: Alekhya_Vellanki
cooked: <p><a class="mention" href="/u/alband">@albanD</a> <a class="mention" href="/u/aerinykim">@aerinykim</a> Could you take a look at this please?</p>

username: albanD
cooked: <p>Hi,</p>
<p>I did take a look but unfortunately I am not familiar with torchtext and the custom dataloader used there.<br>
Similarly the Batch object is custom from that repo.<br>
I guess you should check the repo’s source code to see how to work properly with this <code>Batch</code> object (how to extract data from it / move it to a different device).</p>