[Duplicate] 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'

Ramansh_Sharma: <p>I am trying to multiply a sparse COO PyTorch tensor (which requires grad) with a dense matrix, using this method: <code>torch.sparse.mm</code>. I am getting the following error with the following lines of code:</p>
<pre><code class="lang-auto"># sparse is a PyTorch sparse_coo tensor,
# dense is a dense matrix. Both have requires_grad
# set to True. This line of code works normally.
output = torch.sparse.mm(sparse, dense).diagonal().unsqueeze(1)

# both dense and sparse matrices are functions of
# the w tensor. This line of code results in an error.
output_w = grad(output, w, grad_outputs=torch.ones_like(output), 
                create_graph=True)[0]
</code></pre>
<p>To this, I get the following error:</p>
<p><code>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'</code></p>
<p>Does anyone know how I can fix this?</p>

Ramansh_Sharma: <p>Hello <a class="mention" href="/u/ptrblck">@ptrblck</a>. I was wondering if you can maybe help me with this? Any help would be much appreciated. Thank you.</p>

KFrank: <p>Hi Ramansh!</p>
<aside class="quote no-group quote-modified" data-username="Ramansh_Sharma" data-post="1" data-topic="172345" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ramansh_sharma/48/55510_2.png" class="avatar"> Ramansh_Sharma:</div>
<blockquote>
<pre><code class="lang-auto">output = torch.sparse.mm(sparse, dense).diagonal().unsqueeze(1)

output_w = grad(output, w, grad_outputs=torch.ones_like(output), 
                create_graph=True)[0]
</code></pre>
<p><code>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'</code></p>
</blockquote>
</aside>
<p>Pytorch support for sparse-tensor operations is, forgive me, sparse.</p>
<p>Per the documentation, you can <code>backward()</code> (or <code>grad()</code>) through<br>
<code>torch.sparse.mm()</code>, but, per various tests, you cannot do so with<br>
<code>create_graph = True</code>.</p>
<aside class="quote no-group">
<blockquote>
<p>Does anyone know how I can fix this?</p>
</blockquote>
</aside>
<p>Poking around the internet, I find a github that claims to be a “PyTorch<br>
Extension Library of Optimized Autograd Sparse Matrix Operations”<br>
that contains a suggestively-named <code>torch_sparse.spmm()</code> function.</p>
<p>Who knows if it works?  (I don’t.)</p>
<p>Here is the github:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/rusty1s/pytorch_sparse">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/rusty1s/pytorch_sparse" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/344;"><img src="https://opengraph.githubassets.com/e0753595c09b6fcb199b8a72a5c6f9f31528025bef3fb6a45cf2ac15a08a043e/rusty1s/pytorch_sparse" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/rusty1s/pytorch_sparse" target="_blank" rel="noopener nofollow ugc">GitHub - rusty1s/pytorch_sparse: PyTorch Extension Library of Optimized...</a></h3>

  <p>PyTorch Extension Library of Optimized Autograd Sparse Matrix Operations - GitHub - rusty1s/pytorch_sparse: PyTorch Extension Library of Optimized Autograd Sparse Matrix Operations</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Good luck!</p>
<p>K. Frank</p>