# Multiply sparse tensor with dense tensor on GPU

**URL:** <https://discuss.pytorch.org/t/multiply-sparse-tensor-with-dense-tensor-on-gpu/81224>\
**Category:** Uncategorized\
**Created:** [May 14, 2020, 3:23pm UTC](https://discuss.pytorch.org/t/multiply-sparse-tensor-with-dense-tensor-on-gpu/81224 "2020-05-14T15:23:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkoutsou](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/f1d935/32.png) [@dkoutsou](https://discuss.pytorch.org/u/dkoutsou)\
**Post date:** [May 14, 2020, 3:23pm UTC](https://discuss.pytorch.org/t/multiply-sparse-tensor-with-dense-tensor-on-gpu/81224/1 "2020-05-14T15:23:41Z")

</div>

I have the following implementation of page rank using sparse tensors:

```auto
i = torch.LongTensor(idx)
values = torch.FloatTensor([1] * len(idx))
M = torch.sparse.FloatTensor(i.t(), values, torch.Size([4847571, 4847571]))
N = M.shape[1]
v = torch.rand(N, 1).float()
values = torch.FloatTensor([(1 - self.d)/N] * len(indices))
temp = torch.sparse.FloatTensor(i.t(), values, torch.Size([4847571,
                                                                   4847571]))
if torch.cuda.is_available():
     v = v.cuda()
     M = M.cuda()
     temp = temp.cuda()

v = v / torch.norm(v, 1)
M_hat = self.d * M + temp
for i in range(self.num_iter):
     v = torch.mm(M_hat, v)

```

On CPU everything runs fine. On GPU I am getting the following error:

```auto
    v = torch.mm(M_hat, v)
RuntimeError: sub_iter.strides(0)[0] == 0 INTERNAL ASSERT FAILED at /pytorch/aten/src/ATen/native/cuda/Reduce.cuh:706, please report a bug to PyTorch.

```

Is this a known issue? Should I do something differently?

This is my configuration:

```auto
Collecting environment information...
PyTorch version: 1.5.0
Is debug build: No
CUDA used to build PyTorch: 10.2
OS: Ubuntu 18.04.4 LTS
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
CMake version: version 3.10.2
Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: Tesla P100-PCIE-16GB
Nvidia driver version: 440.82
cuDNN version: Could not collect
Versions of relevant libraries:
[pip3] numpy==1.18.3
[pip3] torch==1.5.0

```

Thanks very much for the help!

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [May 15, 2020, 5:28am UTC](https://discuss.pytorch.org/t/multiply-sparse-tensor-with-dense-tensor-on-gpu/81224/2 "2020-05-15T05:28:08Z")

</div>

Could you try to use [`torch.sparse.mm`](https://pytorch.org/docs/stable/sparse.html#torch.sparse.mm), which is the equivalent of `torch.mm` for a sparse and dense matrix?

Also, could you post all shapes and values of the tensors so that we can reproduce this error?  
The error message isn’t really helpful in this case.

---

<div class="post-metadata">

**Author:** ![dkoutsou](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/f1d935/32.png) [@dkoutsou](https://discuss.pytorch.org/u/dkoutsou)\
**Post date:** [May 15, 2020, 2:18pm UTC](https://discuss.pytorch.org/t/multiply-sparse-tensor-with-dense-tensor-on-gpu/81224/3 "2020-05-15T14:18:36Z")

</div>

> Also, could you post all shapes and values of the tensors so that we can reproduce this error?  
> The error message isn’t really helpful in this case.

Yes, sorry about that but it’s really hard to give you the weights since they were parsed using [this](https://snap.stanford.edu/data/soc-LiveJournal1.html) graph. The shapes can be deduced from the code though. M and M\_hat are (4847571, 4847571) and v is (4847571, 1).

Actually the error is the same as [this](https://github.com/pytorch/pytorch/issues/37583) one and it has been solved in the current master.

Thanks for taking the time though!
