BUG: Fix matvec/vecmat in-place aliasing (out=input produces zeros) (#31150)#31208
Merged
Conversation
…umpy#31150) `np.matvec(A, b, out=b)` returns all zeros because matvec and vecmat are missing the ~NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE output flag that matmul already has. Without it the iterator ends up skipping the overlap check when out is the same array as an input so no temporary copy is made and BLAS gemv clobbers the input buffer. This PR adds the same fix to matvec and vecmat and adds regression testing for both.
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 #31150.
PR summary
Fixes #31144.
np.matvec(A, b, out=b)returns all zeros becausematvecandvecmatare missing the~NPY_ITER_OVERLAP_ASSUME_ELEMENTWISEoutput flag thatmatmulalready has. Without it the iterator ends up skipping the overlap check whenoutis the same array as an input so no temporary copy is made and BLASgemvclobbers the input buffer.This PR adds the same fix to
matvecandvecmatand adds regression testing for both.First time committer introduction
Hi, I'm Ijtihed Kilani (ijtihed.com); I'm an early-career software engineer based in Helsinki, Finland. I work mainly on physics-informed simulations for robotics and autonomy, currently at Supercell working on new games. I've used NumPy heavily for linear algebra in simulation code/studies.
AI Disclosure
This fix was made with help from Claude in Cursor. I used it to look over any assumptions I might have missed and draft the basic code changes which I then looked over, then rewrote and verified. All changes were reviewed and verified by me before submission.
Made with Cursor