BUG: sparse.linalg: Cast index arrays to intc before calling SuperLU functions#18644
BUG: sparse.linalg: Cast index arrays to intc before calling SuperLU functions#18644tylerjereddy merged 7 commits intoscipy:mainfrom
Conversation
|
Test failure seems to be unrelated, only appearing on "Linux Meson tests / Python-debug & ATLAS": |
|
For some reason that test is flaky on that run. I've restarted it. Typically it passes on the second run but I'll check if the lapack version is the issue |
|
From the little that is visible in the error log, it looks like a column of one of the results might have a flipped sign. Is this another case of the solution not being unique? |
I though the same but only this job is consistantly flaking. Hence I'm not sure what is happening there. |
|
Looks like I'd suggest we remove the |
The problem this is trying to address: Indeed, in my initial tests, I tested on scalars and not arrays, which behave differently. Scalar behavior, desired: Array behavior, not desired: I'm not sure what the idiomatic way is of spelling "cast when the values fit" as it does in the scalar case. Do we need an explicit value check? Something like: |
|
I think this PR isn't trying to fix the problem of downcasting. That problem already existed for this library. At least that is my understanding for this comment from #18603) I think this PR is trying to get back to the state before #18509 ... where the index is aggressively downcast so it can work with SPLU. That is, it is a quick fix. The slower fix would be to make SPLU work for int64. I guess the above suggestion about checking values before downcasting is a "middle-speed" fix. What's the performance hit for Is it negligible compared to the SPLU computations? Am I understanding the conversation correctly? |
|
My concern is that downcasting aggressively will return some result, but it won't be a correct result for the case where int64 indices were needed (i.e., large sparse matrix). SuperLU cannot handle those arrays, so we should warn the users. But it is probably true that this incorrect behavior was in SciPy for many versions. |
This reverts commit 59e7578. Safe casting does not behave the way I thought, i.e. it does not take values into consideration. Reverting this patch will cast large indices to a wrapped around int32 version, which is incorrect, but which has also been the status quo for a long time. A future patch should put in place safeguards against incorrect index casting.
|
I've reverted the patch anyway, since that was not the correct solution to the problem. |
|
Using Re: the scalar vs array behavior difference, that looks related to numpy/numpy#8987 If we want to check for overflow explicitly, we could do some fast checks up front before the slow check:
|
|
There is unfortunately no clear idiomatic way to spell "cast if fits". For advanced indexing (with arrays), NumPy even just uses "same kind" and gets away with it, although there we are talking about indices that are by definition impossibly large to be a valid index. There was talk about adding an Whether (or how) to apply cast safety especially to Python scalars is something I haven't quite figured out to be honest. (I am leaning to just considering them "safe", but raising anyway when it doesn't fit; of course the NumPy integers overflow on the same thing though :() Sorry for probably too much details. Introducing a cast mode (or changing "same kind") that checks values may be a way forward. That does require explicit loops for that purprose, although unless we wish to support |
manually check for safe downcasting
|
AFAIU this would fix some new bugs in 1.11? I put the backport-candidate label so this can get in as soon as possible. |
|
I just pushed a fix commit. Pending CI results, I think this should now be in good shape. Note: The CircleCI build_scipy action failed with a suspicious message: |
|
For the issue in the CI, you need to merge main into your PR. CircleCI is not good with that 😅 |
|
Then it looks like the test failures are not indicating issues with this PR. |
tylerjereddy
left a comment
There was a problem hiding this comment.
It sounds like folks agree this should go in and get backported. The CI failures don't look related and when I rebase this branch locally on main it passes the full testsuite on x86_64 Linux.
Due to these bugs scipy/scipy#18644, scipy/scipy#18603
* Add current_through_paths * Require scipy<1.11 Due to these bugs scipy/scipy#18644, scipy/scipy#18603 * Update test_solution.py * Run notebooks
Reference issue
Fixes gh-18603.
What does this implement/fix?
The SuperLU code expects sparse matrix index arrays to have type
intc, so this PR adds a cast prior to those calls. See the linked issue for context.