New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: issue overflow warning when using abs
on np.int8(-128)
#21648
Conversation
Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
Couple things I found that I have addressed in this PR (trying to avoid too large a scope):
|
Just mentioning #21507 as it handles a few cases. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, seems like it should be pretty much ready, the main thing is that I would prefer omitting the unnecessary floating point flag handling.
numpy/core/tests/test_scalarmath.py
Outdated
@@ -898,7 +898,8 @@ def test_scalar_integer_operation_overflow(dtype, operation): | |||
@pytest.mark.parametrize("operation", [ | |||
lambda min, neg_1: abs(min), | |||
lambda min, neg_1: min * neg_1, | |||
lambda min, neg_1: min // neg_1], ids=["abs", "*", "//"]) | |||
pytest.param(lambda min, neg_1: min // neg_1, | |||
marks=pytest.mark.xfail)], ids=["abs", "*", "//"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably need to comment it out or skip this, because it is just too broken...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the risk of slightly expanding the scope of this PR, I went ahead and just avoided the actual error-causing computation in the integer division function, setting it to return NPY_MIN_@NAME@
as I think matches gcc
behavior (in lieu of true defined behavior, this is the next-best thing imo). The test runs now, and the behavior matches expectations I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I would like to backport these division fixes at some point. But I am starting to suspect we should make a PR with those from scratch… (i.e. a backport only PR)
@ganesh-k13 maybe you want to review/finalize this? @Micky774, unfortunately, I don't quite believe you have double-checked that the tests check everything as advertised :). Can you please make sure we have tests for this new abs path (and the negative one for that matter). Tests that fail without the changes applied. EDIT: To be clear, modulo making sure the tests actually exist/do what they should, I think this can go in. |
@@ -167,7 +167,7 @@ static NPY_INLINE int | |||
} | |||
#if @neg@ | |||
else if (b == -1 && a == NPY_MIN_@NAME@) { | |||
*out = a / b; | |||
*out = NPY_MIN_@NAME@; | |||
return NPY_FPE_OVERFLOW; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is suspicious: is this line really not hit during tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep #21507 has tests that covers this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think both PRs have tests for this, so suspect that it is just in-lining/optimization leading to coverage errors (that might be the first time I have seen coverage failing, but...).
@ganesh-k13 if you prefer it to not be included here, maybe just push the change yourself? (We have to mark that one additional test as skip
then probably.) Otherwise, I am happy to merge as-is.
Yes, it would be nice to follow-up to expand the tests so that they raise the error, and there is still lots of little stuff to clear out in multiplication
... But at this point it feels like iterating here is getting tricky.
return NULL; | ||
} | ||
Py_XDECREF(errobj); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is suspicious that these new lines are not marked as being tested
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, right. The first one is OK (I don't think it can be reasonably covered, it indicates an internal/critical error).
To hit the second one, we should also check for:
with np.errstate(over="raise"):
with pytest.raises(FloatingPointError):
operation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I missed the ping Sebastian, yeah the changes look ok but seems like Azure pipelines shows tests failed for windows builds that need to be checked.
@@ -167,7 +167,7 @@ static NPY_INLINE int | |||
} | |||
#if @neg@ | |||
else if (b == -1 && a == NPY_MIN_@NAME@) { | |||
*out = a / b; | |||
*out = NPY_MIN_@NAME@; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think #21507 will cover it so we can remove this here perhaps? All though I'm happy to rebase if it goes in first.
Gah, this is a mess... The linux code (mostly) uses Sorry that this is a a bit of never-ending story, because these integer overflow checks are so spotty, right now. |
I just spent the last couple hours eventually finding that check and scratching my head at it, so I appreciate the confirmation that I'm not going insane XD Yeah it seems there's some systemic revisions necessary here. I suppose the least-invasive, most-likely-to-not-idle result would be to:
Does that sound about right? And thanks to all of you for your patience with this 😄 |
This is because similar changes are already included in a different PR that fixes up more places where division may have problems.
OK, just added that commit myself, so that this is again focused only on I am going to merge it once the tests pass, thanks @Micky774 and Ganesh. @Micky774 it would be cool to look into the open issues here:
|
Only the |
Related Issues/PRs Follow-up to #21648 Changes Adds check to scalar negative for determining whether an overflow warning should be raised for signed integers. Improves testing for negative operator on unsigned integers.
xref gh-21506 (for an eventual release note summarizing these). |
…ication (#21793) Remane `npy_mul_with_overflow_intp` to `npy_mul_sizes_with_overflow` as it only allows positive numbers. Then introduce new versions for all integers (not intp) to use it for the integer scalar math with overflow detection. (It is OK to use everywhere, just for sizes we know they will be positive normally.) Related to #21506 Follow-up to #21648
Related Issues/PRs
Resolves #21289
Changes
Checks condition
a == NPY_MIN_@NAME@
to determine whether an overflow error has occurred fornp.int8
type. See #21289 and #21188 (comment) for reference.Comments
This is my first PR and it's not-quite-complete yet but I wanted to open it so that I could gather some feedback and assistance. In particular, I'm not sure if this change is sufficient, nor how to write tests for this that adhere to NumPy standards. Any help would be appreciated!