GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||
GeezerVison is showing github.com — BUG: Fix matvec/vecmat in-place aliasing (out=input produces zeros) by Ijtihed · Pull Request #31150 · numpy/numpy Conversation
Contributor
PR summaryFixes #31144.
This PR adds the same fix to First time committer introductionHi, 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 DisclosureThis 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
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Clear NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE on the output operand of matvec and vecmat, matching the existing matmul fix. Without this, the iterator skips the overlap copy when out aliases an input, and BLAS gemv clobbers the input buffer. Closes numpygh-31144 Made-with: Cursor
jorenham
reviewed
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your contribution. Could you revert the file permission changes?
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsMade-with: Cursor
|
There was an error while loading. Please reload this page.
Done. I hope everything was done properly 😄 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| expected = np.vecmat(b, a) | ||
| c = np.vecmat(b, a, out=b) | ||
| assert_(c is b) | ||
| assert_equal(c, expected) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Both tests do indeed fail before and pass after the patch.
Note for other NumPy devs--the original problem was only reproducible for me on x86_64 Linux with OpenBLAS, not with ARM Mac/Accelerate, in case that's relevant.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| expected = np.matvec(a, b) | ||
| c = np.matvec(a, b, out=b) | ||
| assert_(c is b) | ||
| assert_equal(c, expected) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I suppose we could try to parametrize the two new tests, but the complexity of the argument reversal probably negates any potential simplification.
I guess these are both very similar to the original test_matmul_out() above.
Minor/debatable points might be preference for assert over the old assert_ that should no longer be needed in new tests, and maybe assert_allclose given we're comparing floats here (bit of a stretch that it matters, but if they do or could someday follow different codepaths...).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
happy to switch to assert and assert_allclose if you think that might be better. I was (like you mentioned) just matching the style of test_matmul_out
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
should I make the edits or leave as is? im okay with either
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You could for example parametrize it with a lambda A, x: np.vecmat(A, x) and lambda A, x: np.matvec(x, A).
I would also prefer if you move these to test_ufunc.py, there seem to be more tests for these there, and that file is at least slightly smaller.
But otherwise this looks good and really we could basically put it in as is.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
should be done now, thanks for the quick feedback
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Addresses reviewer feedback on numpygh-31150: - Remove test_matvec_out/test_vecmat_out from test_multiarray.py - Add parametrized test_matvec_vecmat_out in test_ufunc.py using lambdas - Switch to bare assert and assert_allclose Made-with: Cursor
Made-with: Cursor
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM now and an important fix if someone uses in-place operators here.
(Just waiting for CI.)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…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.
BUG: Fix matvec/vecmat in-place aliasing (out=input produces zeros) (#31150)
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
BUG: matvec(A, b, out=b) zeros out b
There was an error while loading. Please reload this page.