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

[pytorch] randperm lacks CUDA implementation #6874

Closed
jinserk opened this issue Apr 23, 2018 · 7 comments
Closed

[pytorch] randperm lacks CUDA implementation #6874

jinserk opened this issue Apr 23, 2018 · 7 comments
Assignees
Labels
todo Not as important as medium or high priority tasks, but we will work on these.

Comments

@jinserk
Copy link

jinserk commented Apr 23, 2018

If you have a question or would like help and support, please ask at our
forums.

If you are submitting a feature request, please preface the title with [feature request].
If you are submitting a bug report, please fill in the following details.

Issue description

when torch.cuda.FloatTensor is default tensor type, then torch.randperm shows an error as:

RuntimeError: randperm is only implemented for CPU

Code example

Python 3.6.5 (default, Apr 23 2018, 11:54:52) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.randperm(4)
tensor([ 1,  2,  3,  0])
>>> torch.randperm(4).tolist()
[1, 0, 2, 3]
>>> torch.set_default_tensor_type("torch.cuda.FloatTensor")
>>> torch.randperm(4).tolist()                                                                                                                                                                                                                
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: randperm is only implemented for CPU

System Info

PyTorch version: 0.5.0a0+b8ada73
Is debug build: No
CUDA used to build PyTorch: 9.1.85

OS: CentOS Linux 7 (Core)
GCC version: (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
CMake version: version 3.9.20170615-g7c529

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: 9.1.85
GPU models and configuration: GPU 0: GeForce GTX 1070
Nvidia driver version: 390.30
cuDNN version: Probably one of the following:
/usr/local/cudnn_6.0-cuda_8.0/lib64/libcudnn.so.6.0.21
/usr/local/cudnn_6.0-cuda_8.0/lib64/libcudnn_static.a
/usr/local/cudnn_7.0.3-cuda_9.0/lib64/libcudnn.so.7.0.3
/usr/local/cudnn_7.0.3-cuda_9.0/lib64/libcudnn_static.a
/usr/local/cudnn_7.0.4-cuda_8.0/lib64/libcudnn.so.7.0.4
/usr/local/cudnn_7.0.4-cuda_8.0/lib64/libcudnn_static.a
/usr/local/cudnn_7.0.4-cuda_9.0/lib64/libcudnn.so.7.0.4
/usr/local/cudnn_7.0.4-cuda_9.0/lib64/libcudnn_static.a
/usr/local/cudnn_7.0.5+cuda_9.1/lib64/libcudnn.so.7.0.5
/usr/local/cudnn_7.0.5+cuda_9.1/lib64/libcudnn_static.a

Versions of relevant libraries:
[pip3] numpy (1.14.2)
[pip3] torch (0.5.0a0+b8ada73)
[pip3] torchaudio (0.1)
[pip3] torchfile (0.1.0)
[pip3] torchnet (0.0.1)
[pip3] warpctc-pytorch (0.1)
[conda] Could not collect
  • PyTorch or Caffe2: Pytorch
  • How you installed PyTorch (conda, pip, source): source
  • Build command you used (if compiling from source): python setup.py install
  • OS: CentOS 7
  • PyTorch version: 0.5.0a0+b8ada73
  • Python version: 3.6.5
  • CUDA/cuDNN version: 9.1/7.0.5
  • GPU models and configuration: GTX1080
  • GCC version (if compiling from source): 4.8.5
  • CMake version: 3.9.20170615-g7c529
  • Versions of any other relevant libraries:
@zou3519
Copy link
Contributor

zou3519 commented Apr 23, 2018

It sounds like it's not implemented for CUDA. For now you can do the following:

cpu = torch.device('cpu')
tensor = torch.randperm(..., device=cpu)
new_tensor = tensor.cuda()  # and also call new_tensor.requires_grad_() if necessary

@jinserk
Copy link
Author

jinserk commented Apr 23, 2018

How about the case when the randperm is used in RandomSampler in torch/utils/data/sampler.py?

@fmassa
Copy link
Member

fmassa commented Apr 23, 2018

It is generally a bad idea to set the default tensor type to be a cuda tensor.
We are bringing a better way of having device-agnostic code, with the support of a device argument to every tensor factory.

But still, randperm is not implemented on the GPU, and that should be fixed

@fmassa fmassa added enhancement proposal accepted The core team has reviewed the feature request and agreed it would be a useful addition to PyTorch labels Apr 23, 2018
@fmassa fmassa changed the title RuntimeError: randperm is only implemented for CPU [pytorch] randperm lacks CUDA implementation Apr 23, 2018
@yf225
Copy link
Contributor

yf225 commented Apr 28, 2018

I’ll take a stab at this.

@yf225 yf225 self-assigned this Apr 28, 2018
@soumith soumith removed the proposal accepted The core team has reviewed the feature request and agreed it would be a useful addition to PyTorch label Apr 29, 2018
@zou3519 zou3519 added the todo Not as important as medium or high priority tasks, but we will work on these. label May 14, 2018
@unnir
Copy link

unnir commented May 30, 2018

Ok, I solved the issue by changing two lines in the utils/data/sampler.py file.

class RandomSampler(Sampler):
    r"""Samples elements randomly, without replacement.

    Arguments:
        data_source (Dataset): dataset to sample from
    """

    def __init__(self, data_source):
        self.data_source = data_source

    def __iter__(self):
        cpu = torch.device('cpu')
        return iter( torch.randperm( len(self.data_source), device = cpu).tolist())

    def __len__(self):
        return len(self.data_source)

if it is okay, I can PR.

@yf225
Copy link
Contributor

yf225 commented May 30, 2018

@unnir We have a PR open at #7606 which will add randperm to CUDA.

@apaszke
Copy link
Contributor

apaszke commented May 30, 2018

@yf225 In this particular case we never want to use a CUDA tensor over there, so @unnir please send a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
todo Not as important as medium or high priority tasks, but we will work on these.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants