BUG: incorrect temp elision for new-style (NEP 43) user-defined dtypes (#31193)#31329
Merged
Merged
Conversation
numpy#31193) Co-authored-by: Maarten Baert <[email protected]> Co-authored-by: Sebastian Berg <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #31193.
PR summary
can_elide_temp()intemp_elide.cincorrectly identifies new-style user-defined dtypes as numeric types eligible for in-place buffer reuse. For large arrays this silently rewritesa*a + b*binto(a*a) += (b*b), which raises aTypeErrorwhen the result dtype of the in-place add does not match the pre-allocated buffer.I encountered this bug while implementing a custom fixed-point dtype, which automatically increases its bit width on every operation as required to avoid overflow. Since addition increases the bit width by one, attempting to reuse the temporary buffer results in an incompatible destination dtype being provided to the
addufunc and raises aTypeError.The root cause is that
PyArray_ISNUMBERevaluates totruefor new-style user dtypes since it checks(type) <= NPY_CLONGDOUBLEandtype_num = -1for these types.This could be fixed either in
PyArray_ISNUMBER(probably more correct, but may have unexpected side effects) or with a minimal extra check incan_elide_temp(the approach used by the PR). Let me know if you'd like me to fixPyArray_ISNUMBERinstead.AI Disclosure
This bug was identified and fixed by Claude Sonnet 4.6, and then reviewed by me.
Full AI-generated explanation: https://gist.github.com/MaartenBaert/7540176c4005d26a0b292baefbec8519