Repository navigation
BUG: check for non-finite input before calling gesdd/gelsd and handle it in np.cond - #32593
Conversation
Signed-off-by: Iason Krommydas <[email protected]>
Signed-off-by: Iason Krommydas <[email protected]>
|
Another way would be to do this in python like |
Signed-off-by: Iason Krommydas <[email protected]>
There was a problem hiding this comment.
Thanks, looks all fine to me, just a link out might be nice. Two small comments I don't actually care about:
- I suspect this bug was just around for-ever. Don't mind a release note, but also don't particularly need it. (I think the the one computer I tried had a strange build with lapack-lite.)
- I trust that this is really unmeasurably faster than anything (because the algorithms below always do multiple iterations).
One curiosity (I suspect it's fine): Can we also get in trouble for almost infinite results here, due to overflows in later calculation?
Signed-off-by: Iason Krommydas <[email protected]>
Things like this work on my machines: rng = np.random.default_rng(1234)
a = rng.random((6, 6))
scale = 2.0 ** 1000
s = np.linalg.svd(a * scale, compute_uv=False)
np.testing.assert_allclose(s, np.linalg.svd(a, compute_uv=False) * scale, rtol=1e-12) |
|
@seberg should be good to merge right? |
ngoldbaum
left a comment
There was a problem hiding this comment.
There are some regressions on cond introduced by this PR:
import numpy as np
print(np.__version__)
A = np.ones((3, 3))
A[0, 1] = np.inf
for p in [None, 2, -2, 1, -1, 'fro', np.inf, -np.inf]:
try:
print(f"cond(A, {p!r}) = {np.linalg.cond(A, p)}")
except np.linalg.LinAlgError as e:
print(f"cond(A, {p!r}) raised LinAlgError: {e}")The first inputs raise LinAlgError on this PR and return inf on main. Was that intended? If not, I think you need to work around it in cond.
Also note that returning inf there is intentional, see #10571.
|
@ikrommyd I can push a commit that addresses my review if you'd like |
Any idea how cheap? It is another pass across the array, which should be much faster than the actual calculation, but it might be nice to measure. |
Whoops I missed the notification that this PR had more comments. Will fix them. |
Less than 1% of the total SVD runtime on both my Ryzen 7 7800X3D Linux machine and my M4 Max macOS machine. |
I need to think about this a bit. I believe we're getting into the territory of #18914 here and that those are closely related. |
np.cond
|
@ngoldbaum - please check the last commit and let me know what you think. I've updated the title and PR description too. I am relying on the new check in c++ in SVD to not run the LAPACK routine here regarding I'm also sneaking in a 1 line docstring fix here which is that |
There was a problem hiding this comment.
IMO the release notes are too long and overly detailed. Claude likes to do that.
What about this instead (as a single combined improvement note):
* Fixed hangs in `numpy.linalg.svd` and `numpy.linalg.lstsq` for inputs containing infinite inputs for recent versions of some LAPACK implementations.
* `numpy.linalg.cond` handles non-finite inputs more consistently.| x : (..., M, N) array_like | ||
| The matrix whose condition number is sought. | ||
| p : {None, 1, -1, 2, -2, inf, -inf, 'fro'}, optional | ||
| p : {None, 1, -1, 2, -2, inf, -inf, 'fro', 'nuc'}, optional |
There was a problem hiding this comment.
I double-checked and this docstring change is just documenting behavior that already exists. This is not adding a new feature.
Did a slightly modified version of that because I also wanted to state that even in the case where there was no hang, SVD will now not return garbage results and raise instead. |
|
I don't know if this is good to backport or not...technically it's a bugfix and is making behavior more consistent but at the same time we're changing behavior for the non-physical cases like raising. |
|
I think it would be fine to backport. OTOH, the next release isn't that far off. |
IMO it's worth it to help users hitting hangs... |
|
Thanks @ikrommyd |
PR summary
Closes gh-32591
Closes gh-18914
np.linalg.svdnever returns on some LAPACK implementations when the input containsinf:dgesddrescales byBIGNUM/inf = 0, which turns theinfinto NaN, anddbdsqrthen loops forever (Reference-LAPACK lapack#1409). Thegesddandgelsdwrappers now check for non-finite input and raiseLinAlgError, as they already did for NaN. The check costs ≤ 1% of the SVD.np.linalg.condpromisesinf/nanfor such input (gh-10571), so it now calls the svd gufunc directly with errors ignored, like itsinvbranch. This also makes NaN input givenanfor everypregardless of the LAPACK version, which fixes gh-18914, and un-xfailsTestCond.test_nan.AI Disclosure
An AI model has reviewed the changes and also figured out that
np.condactually handles the undocumented nuclear norm.