Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedding on cuda fails #7236

Closed
attardi opened this issue May 3, 2018 · 4 comments
Closed

Embedding on cuda fails #7236

attardi opened this issue May 3, 2018 · 4 comments

Comments

@attardi
Copy link

attardi commented May 3, 2018

Issue description

Calling an Embedding moved to cuda fails.
The same code on CPU works.

Code example

import torch
import torch.nn as nn
e = nn.Embedding(17,50)
dev = torch.device('cuda')
e.to(dev, torch.float)
idx=torch.tensor([1,2,3])
e(idx)
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/sparse.py", line 108, in forward
self.norm_type, self.scale_grad_by_freq, self.sparse)
File "/usr/local/lib/python3.5/dist-packages/torch/nn/functional.py", line 107 6, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, spars e)
RuntimeError: Expected object of type torch.cuda.LongTensor but found type torch .LongTensor for argument #3 'index'

System Info

PyTorch version: 0.4.0
Is debug build: No
CUDA used to build PyTorch: 9.1.85

OS: Ubuntu 16.04.4 LTS
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
CMake version: version 3.5.1

Python version: 3.5
Is CUDA available: Yes
CUDA runtime version: 8.0.61
GPU models and configuration:
GPU 0: Tesla P100-PCIE-16GB
GPU 1: Tesla P100-PCIE-16GB
GPU 2: Tesla P100-PCIE-16GB
GPU 3: Tesla P100-PCIE-16GB

Nvidia driver version: 390.30
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.6.0.21
/usr/lib/x86_64-linux-gnu/libcudnn.so.7.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_static_v7.a
/usr/local/MATLAB/R2017a/bin/glnxa64/libcudnn.so.5.1.5

Versions of relevant libraries:
[pip3] numpy (1.14.0)
[pip3] torch (0.4.0)
[pip3] torchvision (0.2.1)

@apaszke
Copy link
Contributor

apaszke commented May 3, 2018

The error states that it expects a torch.cuda.LongTensor, while you gave it a torch.LongTensor (a CPU array). Move the inputs to the GPU using the .cuda() method and the error should go away.

@apaszke apaszke closed this as completed May 3, 2018
@attardi
Copy link
Author

attardi commented May 3, 2018

I must be missing something:

import torch
import torch.nn as nn
e = nn.Embedding(7,5)
e.cuda()
Embedding(7, 5)
idx=torch.tensor([1,2,3])
idx.cuda()
tensor([ 1, 2, 3], device='cuda:0')
e(idx)
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/sparse.py", line 108, in forward
self.norm_type, self.scale_grad_by_freq, self.sparse)
File "/usr/local/lib/python3.5/dist-packages/torch/nn/functional.py", line 1076, 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'

@apaszke
Copy link
Contributor

apaszke commented May 3, 2018

e.cuda() modifies the actual embedding module (it's expensive to clone modules). idx.cuda() returns a new tensor, but idx stays on the CPU. Change it to idx = idx.cuda() and it will work fine.

@attardi
Copy link
Author

attardi commented May 3, 2018

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants