Skip to content

ENH: Add support for inplace matrix multiplication #21120

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 11 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
MAINT: Explicitly raise when a @= b would otherwise broadcast the o…
…utput

Add special casing for `1d @ 1d` and `2d @ 1d` ops.
  • Loading branch information
BvB93 authored and seberg committed Dec 1, 2022
commit 402545b801aedd7034d4a861d3e4b3d1fc61e2ce
25 changes: 23 additions & 2 deletions numpy/core/src/multiarray/number.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,30 @@ array_matrix_multiply(PyObject *m1, PyObject *m2)
static PyObject *
array_inplace_matrix_multiply(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(m1, m2,
PyArrayObject *m2_array;
int m1_ndim;
int m2_ndim;

/* Explicitly raise a ValueError when the output would
* otherwise be broadcasted to `m1`. Three conditions must be met:
* * `m1.ndim in [1, 2]`
* * `m2.ndim == 1`
* * `m1.shape[-1] == m2.shape[0]`
*/
m2_array = (PyArrayObject *)PyArray_FromAny(m2, NULL, 0, 0, 0, NULL);
m1_ndim = PyArray_NDIM(m1);
m2_ndim = PyArray_NDIM(m2_array);
if (((m1_ndim == 1) || (m1_ndim == 2)) && (m2_ndim == 1)
&& (PyArray_DIMS(m1)[m1_ndim - 1] == PyArray_DIMS(m2_array)[0])) {
PyErr_Format(PyExc_ValueError,
"output parameter has the wrong number of dimensions: "
"Found %d but expected %d", m1_ndim - 1, m1_ndim);
return NULL;
}

INPLACE_GIVE_UP_IF_NEEDED(m1, m2_array,
nb_inplace_matrix_multiply, array_inplace_matrix_multiply);
Copy link
Member

Choose a reason for hiding this comment

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

I am a bit worried whether this must not happen before any conversion (and what is correct in general). But I can try to dig into that later, also.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it is always done first so lets do it first here also.

return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.matmul);
return PyArray_GenericInplaceBinaryFunction(m1, (PyObject *)m2_array, n_ops.matmul);
}

/*
Expand Down
3 changes: 3 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7185,6 +7185,9 @@ def test_basic(self, dtype1: np.dtype, dtype2: np.dtype) -> None:
SHAPES = {
"2d_large": ((10**5, 10), (10, 10)),
"3d_large": ((10**4, 10, 10), (1, 10, 10)),
"1d": ((3,), (3,)),
"2d_1d": ((3, 3), (3,)),
"1d_2d": ((3,), (3, 3)),
"2d_broadcast": ((3, 3), (3, 1)),
"2d_broadcast_reverse": ((1, 3), (3, 3)),
"3d_broadcast1": ((3, 3, 3), (1, 3, 1)),
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy