How can I handle this error "svd_cuda: the updating process of SBDSDC did not converge"

username: S_M
cooked: <p>Hi all<br>
While I am using the following code on Cuda, where A and B are 2D tensors with shape [128,128].<br>
<code>C = A.pinverse().matmul(B)</code><br>
I get the error, <code>svd_cuda: the updating process of SBDSDC did not converge</code>.<br>
how can I handle this error?<br>
Any help would be appreciated.</p>

username: JuanFMontesinos
cooked: <p>Well I would say it probably means your matrix is not invertible…?</p>
<p>You can check<br>
<a href="https://www.planetmath.org/Pseudoinverse" class="onebox" target="_blank" rel="noopener nofollow ugc">https://www.planetmath.org/Pseudoinverse</a></p>

username: S_M
cooked: <p>Do you mean by its dimensions nbyn is not investable? Or is it related to entities ‘I mean the vales’ of the matrix?<br>
What is the solution? How can l compute the inverse of the matrix A?<br>
Actually, I need the matrix C to calculate eigenvalues and eigenvectors</p>

username: JuanFMontesinos
cooked: <p>I mean, you should check that the matrix you want to invert accomplish the theoretical properties of an invertible matrix.</p>
<p>It looks like in your case, your matrix doesn’t. Moreover if A and B are square you can compute its inverse directly.<br>
<a href="https://pytorch.org/docs/stable/generated/torch.linalg.inv.html#torch.linalg.inv" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.linalg.inv.html#torch.linalg.inv</a></p>

username: KFrank
cooked: <p>Hi S!</p>
<aside class="quote no-group" data-username="S_M" data-post="1" data-topic="145436" 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/s_m/48/19733_2.png" class="avatar"> S_M:</div>
<blockquote>
<p><code>C = A.pinverse().matmul(B)</code><br>
I get the error, <code>svd_cuda: the updating process of SBDSDC did not converge</code>.</p>
</blockquote>
</aside>
<p>First, to be clear, this is <em>not</em> about whether <code>A</code> is invertible.  The whole<br>
purpose of the <em>pseudo-inverse</em> (<code>torch.pinverse()</code>) is to perform a<br>
mathematically useful operation even when the matrix is not invertible.<br>
(And the pseudo-inverse coincides with the regular matrix inverse<br>
(<code>torch.inverse()</code>) when the matrix is invertible.)</p>
<p>As to your specific problem, let me first note that I cannot reproduce<br>
your error exact message (but I can cause other errors).</p>
<p>But things to try:</p>
<p>First check that <code>A</code> does not contain 'inf’s nor 'nan’s (<code>A.isinf().any()</code>,<br>
<code>A.isnan().any()</code>).  That could lead to a <code>pinverse()</code> error.</p>
<p>Try upgrading to the latest stable pytorch version or the nightly build.<br>
(By the way, which pytorch version are you using?)</p>
<p>Try using <code>pinverse()</code>'s <code>rcond</code> argument.  You will have to experiment<br>
with the value, e.g., <code>A.pinverse (rcond = 1.e-2)</code>, and see whether<br>
larger values eliminate your error.</p>
<p>Replace your specific usage of <code>pinverse()</code> (namely multiplying <code>B</code>) with<br>
<code>C = torch.linalg.lstsq (A, B).solution</code>.</p>
<p>Try perturbing <code>A</code> before calculating its pseudo-inverse:<br>
<code>A += 1.e-3 * torch.randn (A.shape)</code>.  Again, play around with<br>
the scale factor.</p>
<p>Perform the call to <code>pinverse()</code> on the cpu.  It will be a pain if you need<br>
to backpropagate through this step, because you will have to copy your<br>
intermediate gradients over to the cpu, backpropagate on the cpu, and<br>
then copy them back to the gpu.</p>
<p>Here is an illustration of some of these points:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.10.0'
&gt;&gt;&gt;
&gt;&gt;&gt; _ = torch.manual_seed (2022)
&gt;&gt;&gt;
&gt;&gt;&gt; A = torch.randn (5, 5, device = 'cuda')
&gt;&gt;&gt; Az = m.clone()
&gt;&gt;&gt; Az[:, 4] = 0
&gt;&gt;&gt; Ai = m.clone()
&gt;&gt;&gt; Ai[2, 3] = float ('inf')
&gt;&gt;&gt;
&gt;&gt;&gt; A
tensor([[-0.4984,  1.0116, -0.2725, -2.4033, -0.0222],
        [ 1.0673,  0.9031, -0.7990,  2.6576, -1.7713],
        [ 0.2904,  0.1456, -0.0956, -2.0614, -0.5158],
        [-0.0729, -0.0587,  0.0698,  0.0745,  0.8687],
        [-2.3846,  2.1474, -0.1243,  0.6857, -0.0301]], device='cuda:0')
&gt;&gt;&gt; A.inverse()
tensor([[-2.7031e+00,  6.0249e-01,  4.3399e+00,  3.7649e+00,  8.2871e-01],
        [-3.4336e+00,  6.6966e-01,  5.5499e+00,  4.6267e+00,  1.5518e+00],
        [-9.0598e+00,  5.3106e-01,  1.2691e+01,  8.5048e+00,  3.4168e+00],
        [-2.7618e-01,  9.6305e-02, -4.5012e-04,  1.9232e-01,  9.4890e-02],
        [ 2.9279e-01,  4.4850e-02, -2.8051e-01,  1.0797e+00, -1.0827e-01]],
       device='cuda:0')
&gt;&gt;&gt; torch.allclose (A.pinverse(), A.inverse(), atol = 1.e-6)
True
&gt;&gt;&gt;
&gt;&gt;&gt; Az
tensor([[-0.4984,  1.0116, -0.2725, -2.4033,  0.0000],
        [ 1.0673,  0.9031, -0.7990,  2.6576,  0.0000],
        [ 0.2904,  0.1456, -0.0956, -2.0614,  0.0000],
        [-0.0729, -0.0587,  0.0698,  0.0745,  0.0000],
        [-2.3846,  2.1474, -0.1243,  0.6857,  0.0000]], device='cuda:0')
&gt;&gt;&gt; Az.inverse()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
RuntimeError: inverse_cuda: (Batch element 0): The diagonal element 5 is zero, the inversion could not be completed because the input matrix is singular.
&gt;&gt;&gt; Az.pinverse()
tensor([[-3.1374,  0.5360,  4.7560,  2.1632,  0.9893],
        [-3.9337,  0.5931,  6.0290,  2.7825,  1.7368],
        [-9.6315,  0.4435, 13.2387,  6.3964,  3.6282],
        [-0.3025,  0.0923,  0.0248,  0.0951,  0.1046],
        [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000]], device='cuda:0')
&gt;&gt;&gt;
&gt;&gt;&gt; Ai
tensor([[-0.4984,  1.0116, -0.2725, -2.4033, -0.0222],
        [ 1.0673,  0.9031, -0.7990,  2.6576, -1.7713],
        [ 0.2904,  0.1456, -0.0956,     inf, -0.5158],
        [-0.0729, -0.0587,  0.0698,  0.0745,  0.8687],
        [-2.3846,  2.1474, -0.1243,  0.6857, -0.0301]], device='cuda:0')
&gt;&gt;&gt; Ai.pinverse()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
RuntimeError: svd_cuda: (Batch element 0): The algorithm failed to converge because the input matrix is ill-conditioned or has too many repeated singular values (error code: 6).
&gt;&gt;&gt;
&gt;&gt;&gt; B = torch.randn (5, 5, device = 'cuda')
&gt;&gt;&gt; torch.allclose (A.pinverse().matmul (B), torch.linalg.lstsq (A, B).solution, atol = 1.e-6)
True
</code></pre>
<p>Good luck.</p>
<p>K. Frank</p>