Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: check that all elements are finite before calling gesdd to prevent hangs or garbage results - #32593

Open
ikrommyd wants to merge 4 commits into
numpy:mainfrom
ikrommyd:check-all-finite-before-gesdd
Open

BUG: check that all elements are finite before calling gesdd to prevent hangs or garbage results#32593
ikrommyd wants to merge 4 commits into
numpy:mainfrom
ikrommyd:check-all-finite-before-gesdd

Conversation

@ikrommyd

@ikrommyd ikrommyd commented Sep 11, 2026

Copy link
Copy Markdown
Member

PR summary

Closes #32591

Adds a check that the input is all finite before the LAPACK gesdd routines are called inside numpy.linalg.svd, numpy.linalg.lstsq and the functions built on them. If the input contains infinities, LAPACK often hangs (see the linked issue for more info) or returns garbage results otherwise. That check is very very cheap to make compared to the actual computation.

AI Disclosure

An AI model has reviewed the changes.

@ikrommyd

ikrommyd commented Sep 11, 2026

Copy link
Copy Markdown
Member Author

Another way would be to do this in python like eig and eigvals do with the _assert_finite helper. Let me know if you wanna go that route instead.

Signed-off-by: Iason Krommydas <[email protected]>
@ikrommyd

Copy link
Copy Markdown
Member Author

cc @seberg and @mattip :)

@ikrommyd
ikrommyd marked this pull request as ready for review September 11, 2026 15:34

@seberg seberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks all fine to me, just a link out might be nice. Two small comments I don't actually care about:

  1. 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.)
  2. 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?

Comment thread numpy/linalg/umath_linalg.cpp
Comment thread numpy/linalg/umath_linalg.cpp Outdated
@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Sep 13, 2026
Signed-off-by: Iason Krommydas <[email protected]>
@ikrommyd

Copy link
Copy Markdown
Member Author

One curiosity (I suspect it's fine): Can we also get in trouble for almost infinite results here, due to overflows in later calculation?

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 09 - Backport-Candidate PRs tagged should be backported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: np.linalg.svd hangs forever on a square >=3x3 matrix containing 'inf'

2 participants