Skip to content

Sparse tensor creation ignores indices placement #28500

@mbahri

Description

@mbahri

🐛 Bug

Attempting to create a sparse tensor from a tensor of indices and values placed on another GPU than cuda:0 (for instance cuda:1) fails with error device of indices (0) must match device of values (1).

Placing the values on cuda:0 while leaving the indices on cuda:1 works and returns a sparse tensor on cuda:0.

The following demo should show the bug:

In [1]: import torch                                                            

In [2]: ind = torch.arange(10).unsqueeze(0).to('cuda:1')                        

In [3]: sp_ind = torch.cat((ind, ind), 0)                                       

In [4]: sp_val = torch.rand(10).to('cuda:1')                                    

In [5]: sp_shape = (10, 10)                                                     

In [6]: x = torch.sparse_coo_tensor(sp_ind, sp_val, sp_shape)                   
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-6-776f23c64083> in <module>
----> 1 x = torch.sparse_coo_tensor(sp_ind, sp_val, sp_shape)

RuntimeError: device of indices (0) must match device of values (1)

In [7]: # Now this                                                              

In [8]: x = torch.sparse_coo_tensor(sp_ind, sp_val.cuda(0), sp_shape)           

In [9]: x                                                                       
Out[9]: 
tensor(indices=tensor([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]),
       values=tensor([0.5324, 0.1738, 0.4186, 0.2776, 0.4665, 0.9525, 0.6555,
                      0.2805, 0.9244, 0.4847]),
       device='cuda:0', size=(10, 10), nnz=10, layout=torch.sparse_coo)

In [10]: sp_ind                                                                 
Out[10]: 
tensor([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]], device='cuda:1')

In [11]: sp_val                                                                 
Out[11]: 
tensor([0.5324, 0.1738, 0.4186, 0.2776, 0.4665, 0.9525, 0.6555, 0.2805, 0.9244,
        0.4847], device='cuda:1')

To Reproduce

Steps to reproduce the behavior:

See code snipped above.

Expected behavior

Provided both indices and values are placed on the same device, torch.sparse_coo_tensor should return a sparse tensor on that device.

Environment

Please copy and paste the output from our
environment collection script
(or fill out the checklist below manually).

You can get the script and run it with:

wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
  • PyTorch Version (e.g., 1.0): 1.3.0
  • OS (e.g., Linux): Ubuntu 18.04 LTS
  • How you installed PyTorch (conda, pip, source): conda
  • Build command you used (if compiling from source): N/A
  • Python version: 3.7.4
  • CUDA/cuDNN version: 10.2/7.5
  • GPU models and configuration: 2x2080 Ti with Nvlink
  • Any other relevant information:

Additional context

Metadata

Metadata

Assignees

Labels

module: cudaRelated to torch.cuda, and CUDA support in generalmodule: sparseRelated to torch.sparsetriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions