Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: fix matmul with transposed out arg #29179

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

Merged
merged 3 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/release/upcoming_changes/29179.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix bug in ``matmul`` for non-contiguous out kwarg parameter
------------------------------------------------------------
In some cases, if ``out`` was non-contiguous, ``np.matmul`` would cause
memory corruption or a c-level assert. This was new to v2.3.0 and fixed in v2.3.1.
6 changes: 6 additions & 0 deletions doc/source/release/2.3.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ the best performance.

(`gh-28769 <https://github.com/numpy/numpy/pull/28769>`__)

Performance improvements for ``np.matmul``
------------------------------------------
Enable using BLAS for ``matmul`` even when operands are non-contiguous by copying
if needed.

(`gh-23752 <https://github.com/numpy/numpy/pull/23752>`__)

Changes
=======
Expand Down
2 changes: 1 addition & 1 deletion numpy/_core/src/umath/matmul.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ NPY_NO_EXPORT void
* Use transpose equivalence:
* matmul(a, b, o) == matmul(b.T, a.T, o.T)
*/
if (o_f_blasable) {
if (o_transpose) {
Copy link
Contributor

@xor2k xor2k Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we be sure that this is the only change necessary or do we need to extend the test cases?

Copy link
Contributor

@xor2k xor2k Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what you mean with

but still need tests that copy the output both for C and F order. (Ideally, make sure we have tests for all other cases as well.)

right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I got a solution for that, see my last comment on the lines changed in numpy/_core/tests/test_multiarray.py.

@TYPE@_matmul_matrixmatrix(
ip2_, is2_p_, is2_n_,
ip1_, is1_n_, is1_m_,
Expand Down
6 changes: 6 additions & 0 deletions numpy/_core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7317,6 +7317,12 @@ def test_dot_equivalent(self, args):
r3 = np.matmul(args[0].copy(), args[1].copy())
assert_equal(r1, r3)

# matrix matrix, issue 29164
if [len(args[0].shape), len(args[1].shape)] == [2, 2]:
out_f = np.zeros((r2.shape[0] * 2, r2.shape[1] * 2), order='F')
r4 = np.matmul(*args, out=out_f[::2, ::2])
assert_equal(r2, r4)

def test_matmul_object(self):
import fractions

Expand Down
Loading