Fix incorrect sparse add behavior when the sparse tensor has non-contiguous values...
authorWill Feng <willfeng@fb.com>
Sat, 23 Mar 2019 02:25:58 +0000 (19:25 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sat, 23 Mar 2019 02:35:14 +0000 (19:35 -0700)
commit7be05b822c01e8db5e23c8a213ba2e83d17b0bd2
treec995e5d28f65dcee65b45d7ee48a02b64a74058c
parent6052d04100cff9bddcdd5a1444405f5daf7b3b74
Fix incorrect sparse add behavior when the sparse tensor has non-contiguous values (#18179)

Summary:
Currently, this code gives incorrect result:
```python
import torch
indices=torch.tensor([[7, 1, 3]])
values=torch.tensor([[1., 1., 1.],
               [1., 1., 1.],
               [1., 1., 1.]])
x = torch.sparse_coo_tensor(indices, values, size=(10, 3))
values=torch.tensor(1.).expand(3, 3)
y = torch.sparse_coo_tensor(indices, values, size=(10, 3))
z = x + y

tensor(indices=tensor([[7, 1, 3]]),
       values=tensor([[2., 1., 1.],
                      [1., 1., 1.],
                      [1., 1., 1.]]),
       size=(10, 3), nnz=3, layout=torch.sparse_coo)
```

This PR fixes the bug by adding special handling for sparse tensors with non-contiguous values in the addition function (specifically, by cat'ing the indices and values together).

This PR closes https://github.com/pytorch/pytorch/issues/17950 and https://github.com/pytorch/pytorch/issues/17919.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18179

Reviewed By: ezyang

Differential Revision: D14569591

Pulled By: yf225

fbshipit-source-id: f5a14c4a31337fc95eab64596212066b4fb18b1a
aten/src/ATen/native/sparse/SparseTensorMath.cpp
test/test_nn.py
test/test_sparse.py