RuntimeError: The backward pass for this operation requires the 'self' tensor to be strided, but a sparse tensor was given instead. Please either use a strided tensor or set requires_grad=False for 'self'

Hi,
when i run forward() as follows:

 def forward(self, input, adj):
        support = torch.mm(input, self.weight)
        output = torch.spmm(adj, support)

this
output = torch.spmm(adj, support)
got an error
RuntimeError: The backward pass for this operation requires the 'self' tensor to be strided, but a sparse tensor was given instead. Please either use a strided tensor or set requires_grad=False for 'self'
here adj is an output, a sparse tensor, of another layer which has requires_grad=True.
how can i solve the problem?

Hi,
Can you please brief in words what you are actually trying to do?

I tried reproducing your error and was able to do that with this piece of code :

input = torch.randn(3,3)
weight = torch.randn(3,3)
weight.requires_grad_()
i = [[0, 1, 1],
     [2, 0, 2]]
v =  [3.0, 4, 5]

adj = torch.sparse_coo_tensor(i, v, (2, 3), requires_grad=True) 
def forward(input, adj):
    support = torch.mm(input, weight)
    output = torch.smm(adj, support)
    
forward(input, adj)

and got the same error as you:

RuntimeError: The backward pass for this operation requires the 'self' tensor to be strided, but a sparse tensor was given instead. Please either use a strided tensor or set requires_grad=False for 'self'

Does this piece of code align with your use-case?

Also, from what I could understand from the error message, I set requires_grad=False for the sparse adj tensor, and the following code then ran fine:

input = torch.randn(3,3)
weight = torch.randn(3,3)
weight.requires_grad_()
i = [[0, 1, 1],
     [2, 0, 2]]
v =  [3.0, 4, 5]

adj = torch.sparse_coo_tensor(i, v, (2, 3))
def forward(input, adj):
    support = torch.mm(input, weight)
    output = torch.smm(adj, support)
    
forward(input, adj)

More importantly, I couldn’t find any official docs for torch.spmm that you are using.

I know torch.smm is used to perform a matrix multiplication of a sparse matrix with a dense matrix. So, I replaced torch.spmm in the above code to get this:

input = torch.randn(3,3)
weight = torch.randn(3,3)
weight.requires_grad_()
i = [[0, 1, 1],
     [2, 0, 2]]
v =  [3.0, 4, 5]
adj = torch.sparse_coo_tensor(i, v, (2, 3), requires_grad=True)
def forward(input, adj):
    support = torch.mm(input, weight)
    output = torch.smm(adj, support)
    
forward(input, adj)

And this code runs perfectly.

So, if you let me know what exactly you are trying to achieve with torch.spmm, I might be able to help you further.

Additionally, find the list of available functions for sparse tensors here:
https://pytorch.org/docs/stable/sparse.html#supported-linear-algebra-operations

I want to perform a matrix multiplication of a sparse tensor with a dense tensor, both having requires_grad=True, by torch.spmm().

After reading your reply, i found a function tensor.sparse.mm() that performs well on your given link.

Finally, i figure it out like this:

  def forward(self, input, adj):
        support = torch.mm(input, self.weight)
        output = torch.sparse.mm(adj, support)

Thank you for helping me again. You are really nice!