Torch::cholesky ignoring exception

username: eusouoandre
cooked: <p>I’m having for some matrices on my batch exception due singular matrices.</p>
<blockquote>
<blockquote>
<p>cholesky_cpu: For batch 51100: U(22,22) is zero, singular U</p>
</blockquote>
</blockquote>
<p>I would like to ignore the exception for those and further set as <code>Nan</code> is it possible somehow?</p>
<p>Actually if I catch the exception and use <code>continue</code> still it doesn’t finish the calculations of the other matrices.</p>

username: ptrblck
cooked: <p>Could you post the code you are using to catch the exception?</p>

username: eusouoandre
cooked: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a> here is the code</p>
<pre><code>        th::Tensor L;
        try {
            L = th::cholesky(Xt.bmm(Xbtc));
        }
        catch (const std::exception &amp; ex) {
            std::cout &lt;&lt; "c++ exception: " &lt;&lt; std::endl;
        }
        std::cout &lt;&lt; L &lt;&lt; std::endl;
        auto Gi = th::cholesky_solve(th::eye(p + 3, dtype_option.device(deviceifGPU)), L); // (X ^ T.X) ^ -1
</code></pre>
<p>It prints:</p>
<p>c++ exception:<br>
[ Tensor (undefined) ]</p>

username: ptrblck
cooked: <p>Thanks for the code.<br>
It seems you want to ignore this single sample in the calculation.<br>
I’m not sure, but I would assume that removing this sample from the batch and call <code>cholesky</code> again on the cleaned batch of matrices might work.</p>

username: eusouoandre
cooked: <p>Thank you!<br>
Unfortunantly I would loose my peformance gain if I had to analyse which samples fail and remove those specifically. There might be more than one.</p>
<p>I am running this <a href="https://stats.stackexchange.com/questions/449207/augmented-dickey-fuller-adf-test-statistics-gpu-formulation/449354?noredirect=1#comment837236_449354" rel="nofollow noopener">here</a></p>
<p><strong>I really would like to run the cholesky ignoring those and dealing with <code>nans</code> or <code>infs</code> after.</strong><br>
Is it possible somehow?</p>

username: eusouoandre
cooked: <p>I guess there is no way then. I think I will try tensorflow then… But thanks <a class="mention" href="/u/ptrblck">@ptrblck</a></p>

username: ptrblck
cooked: <p>Sure! Feel free to let us know, how you solved your use case in the end. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: eusouoandre
cooked: <p>Just to let everybody know that’s how I solved.</p>
<p>The solution, unfortunately, was to  <strong>implement my own</strong>  simple batched cholesky ( <code>th.cholesky(..., upper=False)</code> ) and then deal with Nan values using  <code>th.isnan</code> .</p>
<pre><code class="lang-auto">import torch as th

# nograd cholesky
def cholesky(A):
    L = th.zeros_like(A)

    for i in range(A.shape[-1]):
        for j in range(i+1):
            s = 0.0
            for k in range(j):
                s = s + L[...,i,k] * L[...,j,k]

            L[...,i,j] = th.sqrt(A[...,i,i] - s) if (i == j) else \
                      (1.0 / L[...,j,j] * (A[...,i,j] - s))
    return L
</code></pre>
<p>Using this code on <a href="https://www.pugetsystems.com/labs/hpc/PyTorch-for-Scientific-Computing---Quantum-Mechanics-Example-Part-3-Code-Optimizations---Batched-Matrix-Operations-Cholesky-Decomposition-and-Inverse-1225/" rel="nofollow noopener">here</a> I also implemented this on C++ Libtorch and worked flawless.</p>