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

I’m getting an error saying

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

Can someone tell me what it means by argument #3 "index"? I can’t find this error message either in torch.nn.modules.sparse nor https://pytorch.org/docs/0.4.0/_modules/torch/nn/functional.

Here is my code:

        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)

As you see, I even explicitly sent all variables to cpu to debug this error.
And batch[‘doc_tok’], batch[‘query_tok’] prints out as both torch.int64.

Here is the error I’m getting.

  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'

Where should I look at to fix this error?

Any comment (even though it’s short!) or just listing keywords to look at will be highly appreciated!

1 Like

Hi,

The error states that you provide a cpu tensor while a gpu tensor is required.
I guess you called self.network.cuda() somewhere? That would mean your net expects cuda tensors and not cpu ones.

2 Likes

Thanks @albanD 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?

The reason why I’m doing this (switching GPU <> CPU) is because I was getting GPU out of memory error and start, end, _ = self.network(batch) which is a simple forward propagation (all autograd disabled by calling self.network.eval().)

You can switch it back to cpu by doing self.nerwork.cpu().

2 Likes

Thanks! It was a typo.

Hey @aerinykim and @albanD,

I’m running into a similar issue. Getting exactly the same RuntimeError.
This is the code I’m trying to run:https://github.com/jamie-murdoch/ContextualDecomposition/blob/master/train_model/train.py

I’m running into the error at line 78, where we train the model:

# forward pass
answer = model(batch)

Not understanding why batch is not on GPU though we set the device in the code.
Also how do we move batch to GPU? cuda() and to(device) methods aren’t defined for batch (<class torchtext.data.batch.Batch>).

Please help! I’m stuck with this from the past few days :frowning:

Not sure if this helps, here’s the full trace:

Traceback (most recent call last):
File "./train_model/train.py", line 81, in <module>
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'

@albanD @aerinykim Could you take a look at this please?

Hi,

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