Backprop on sparse tensor error

Hi, guys,

I got error when running backward(), which has torch.mm sparse operation (sparse x dense -> dense) in forward process. Here is the toy code:

m_indices=torch.LongTensor([[0, 0, 1], [0, 1, 1]])
m_values = torch.FloatTensor([1, 1, 1])
sizes = [2,2]
m = torch.sparse_coo_tensor(m_indices, m_values, sizes, requires_grad=True)
print(m)
'''
tensor(indices=tensor([[0, 0, 1],
                       [0, 1, 1]]),
       values=tensor([1., 1., 1.]),
       size=(2, 2), nnz=3, layout=torch.sparse_coo, requires_grad=True)
'''
x = torch.autograd.Variable(torch.FloatTensor([[0.5],[0.6]]), requires_grad=True)
z = torch.mm(m, x)
z.backward(x)
'''
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ''.../lib/python3.5/site-packages/torch/tensor.py", line 93, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File ".../lib/python3.5/site-packages/torch/autograd/__init__.py", line 90, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: sparse tensors do not have strides
'''
# when I change requires_grad to False in sparse tensor:
m = torch.sparse_coo_tensor(m_indices, m_values, sizes, requires_grad=False)
x = torch.autograd.Variable(torch.FloatTensor([[0.5],[0.6]]), requires_grad=True)
z = torch.mm(m, x)
z.backward(x)
# it works!

So I think at least for troch.mm, backprop on sparse tensor is not avalilable yet?

What should I do if I want backprop on sparse tensor?

Best,
Huan

Hi, what version of PyTorch are you using?

Thanks. I am using version ‘0.4.1’.