Linalg lstsq grad doesn't work for complex tensors

souryadey: <p>Autograd doesn’t work for <code>torch.linalg.lstsq</code> when dealing with complex tensors. Example:</p>
<pre data-code-wrap="python"><code class="lang-python">A = torch.complex(torch.tensor([[1.,0.],[0.,1.]]), torch.tensor([[1.,0.],[0.,1.]]))
b = torch.complex(torch.tensor([2.,3.]), torch.tensor([1.,0.]))

A.requires_grad = True
b.requires_grad = True

x = torch.linalg.lstsq(A,b).solution
print(x) # gives tensor([1.5000-0.5000j, 1.5000-1.5000j], grad_fn=&lt;LinalgLstsqBackward0&gt;)

x[0].backward()
</code></pre>
<p>Results in <code>IndexError: Dimension out of range (expected to be in range of [-1, 0], but got -2)</code>.</p>
<p>Note: I can achieve the same result for <code>x</code> via <code>torch.linalg.pinv(A) @ b</code>, and autograd does work for <code>pinv</code>. However, I am curious why the <code>lstsq</code> autograd doesn’t work, and also because according to the <a href="https://pytorch.org/docs/stable/generated/torch.linalg.pinv.html" rel="noopener nofollow ugc">documentation</a>:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/9/49447adcfcad6cbcf8840e547eac83d4f9de9dca.png" data-download-href="https://discuss.pytorch.org/uploads/default/49447adcfcad6cbcf8840e547eac83d4f9de9dca" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/9/49447adcfcad6cbcf8840e547eac83d4f9de9dca_2_690x105.png" alt="image" data-base62-sha1="as9zwkfQ39Hz1BpIgzyo7o5yWIW" width="690" height="105" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/9/49447adcfcad6cbcf8840e547eac83d4f9de9dca_2_690x105.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/9/49447adcfcad6cbcf8840e547eac83d4f9de9dca_2_1035x157.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/9/49447adcfcad6cbcf8840e547eac83d4f9de9dca_2_1380x210.png 2x" data-dominant-color="EAEBED"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1520×232 26.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

KFrank: <p>Hi Sourya!</p>
<aside class="quote no-group" data-username="souryadey" data-post="1" data-topic="157649" 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/souryadey/48/44490_2.png" class="avatar"> souryadey:</div>
<blockquote>
<p>Autograd doesn’t work for <code>torch.linalg.lstsq</code></p>
</blockquote>
</aside>
<p>This is a known bug that hasn’t been fixed yet.  See this <a href="https://github.com/pytorch/pytorch/issues/77225" rel="noopener nofollow ugc">github issue.</a></p>
<aside class="quote no-group">
<blockquote>
<p>when dealing with complex tensors.</p>
</blockquote>
</aside>
<p>As an aside, this bug is not limited to the complex case.</p>
<aside class="quote no-group">
<blockquote>
<p>Note: I can achieve the same result for <code>x</code> via <code>torch.linalg.pinv(A) @ b</code>, and autograd does work for <code>pinv</code>.</p>
</blockquote>
</aside>
<p>I think that your pseudo-inverse workaround would be the most practical<br>
path forward (until the <code>lstsq()</code> bug is fixed).</p>
<p>Best.</p>
<p>K. Frank</p>