dbl001
dbl001

Reputation: 2459

RuntimeError: Expected object of backend CUDA but got backend CPU for argument #3 'index'

I'm working with the project 'lda2vec-pytorch' on Google CoLab, runnin pytorch 1.1.0

https://github.com/TropComplique/lda2vec-pytorch

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
cuda:0

I'm getting an exception in the forward method adding 'noise' in my class negative_sampling_loss(nn.Module):

        noise = self.multinomial.draw(batch_size*window_size*self.num_sampled)
        noise = Variable(noise).view(batch_size, window_size*self.num_sampled)

        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.embedding = self.embedding.to(device)
        #print("negative_sampling_loss::forward() self.embedding", self.embedding.is_cuda)  This line get's an error.

        # shape: [batch_size, window_size*num_sampled, embedding_dim]
        noise = self.embedding(noise)  # Exception HERE

Here's the stack trace:

Traceback (most recent call last):
  File "train.py", line 36, in <module>
    main()
  File "train.py", line 32, in main
    save_every=20, grad_clip=5.0
  File "../utils/training.py", line 138, in train
    neg_loss, dirichlet_loss = model(doc_indices, pivot_words, target_words)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "../utils/lda2vec_loss.py", line 82, in forward
    neg_loss = self.neg(pivot_words, target_words, doc_vectors, w)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "../utils/lda2vec_loss.py", line 167, in forward
    noise = self.embedding(noise)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/sparse.py", line 117, in forward
    self.norm_type, self.scale_grad_by_freq, self.sparse)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py", line 1506, in embedding
    return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: Expected object of backend CUDA but got backend CPU for argument #3 'index'

Any ideas?

Upvotes: 0

Views: 1626

Answers (1)

asymptote
asymptote

Reputation: 1402

Variable noise is available on CPU while self.embedding is on GPU. We can send noise to GPU as well:

noise = noise.to(device)

Upvotes: 2

Related Questions