Skip to content
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

ENH: update numpy.linalg.multi_dot to accept an out argument #15715

Merged
merged 14 commits into from
Apr 29, 2020

Conversation

sslivkoff
Copy link
Contributor

out is a common argument in numpy functions that specifies where the result of the function should be stored. A typical use case for out is performance-critical situations where it is desirable to avoid creating unnecessary copies of the result array. numpy.linalg.multi_dot is also commonly used in such situations, so it would be nice if it could accept an out argument. This PR adds out functionality to numpy.linalg.multi_dot

@sslivkoff
Copy link
Contributor Author

I'm finding the PyPy pipeline test failures a bit cryptic

https://dev.azure.com/numpy/numpy/_build/results?buildId=8523&view=logs&j=cb7817a0-aed9-5b1b-ffdb-285e933f569f&t=713c0b5c-565d-5b6b-cd58-66e4f64bd786

Is there something special that needs to be done for PyPy?

@seberg
Copy link
Member

seberg commented Mar 6, 2020

Not sure what is going on with this failure in the PyPy pipeline, although maybe we can rewrite that script. It fails with: tools/pypy-test.sh: line 47: pypy3/bin/pip: No such file or directory
In any case, do not worry about it, the pypy tests are sometimes a bit flaky.

@mattip
Copy link
Member

mattip commented Mar 6, 2020

For some reason there is pip3 in pypy/bin but not pip. Perhaps we should be building against a known good version of pypy and not latest HEAD.

@charris charris added 01 - Enhancement 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes component: numpy.linalg labels Mar 6, 2020
@charris
Copy link
Member

charris commented Mar 6, 2020

Needs a release note.

@sslivkoff
Copy link
Contributor Author

Needs a release note.

Added one in ef7486a. Is it in the proper format?

@sslivkoff
Copy link
Contributor Author

@eric-wieser @wkschwartz do you have any more thoughts on this?

numpy/linalg/linalg.py Outdated Show resolved Hide resolved
@@ -2610,12 +2610,12 @@ def norm(x, ord=None, axis=None, keepdims=False):

# multi_dot

def _multidot_dispatcher(arrays):
def _multidot_dispatcher(arrays, out=None):
return arrays
Copy link
Member

Choose a reason for hiding this comment

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

Should this visit out too?

Copy link
Member

Choose a reason for hiding this comment

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

Cc @shoyer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eric-wieser @shoyer grepping around (rg -A 4 "_dispatcher.*out") it seems that out should be returned by the dispatcher. Other functions with an optional out return it. Added in 5427885

return arrays


@array_function_dispatch(_multidot_dispatcher)
def multi_dot(arrays):
def multi_dot(arrays, out=None):
Copy link
Member

Choose a reason for hiding this comment

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

Thoughts on making this as follows?

Suggested change
def multi_dot(arrays, out=None):
def multi_dot(arrays, *, out=None):

Copy link
Member

Choose a reason for hiding this comment

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

+1 let's make this keyword only!

Copy link
Member

Choose a reason for hiding this comment

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

+1 from me as well.

normally I'd be concerned about consistency between functions, but for out it seems like such an improvement to have keyword-only that we should just do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eric-wieser @shoyer @rgommers Good point. Added in 1f26974

@sslivkoff
Copy link
Contributor Author

Anyone have further thoughts on this? @shoyer @rgommers

Copy link
Member

@mattip mattip left a comment

Choose a reason for hiding this comment

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

A couple of formatting nits, unless the function names were requested that way by another reviewer (I don't want to be too pedantic, just a little pedantic).

`numpy.linalg.multi_dot` now accepts an ``out`` argument
--------------------------------------------------------

``out`` can be used to avoid creating unnecessary copies of the final product computed by `numpy.linalg.multidot`.
Copy link
Member

Choose a reason for hiding this comment

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

Line exceeds 80 characters

def test_basic_function_with_dynamic_programing_optimization_with_out_argument(self):
# multi_dot with four or more arguments uses the dynamic programing
# optimization and therefore deserve a separate
A = np.random.random((6, 2))
Copy link
Member

Choose a reason for hiding this comment

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

comment seems cut off: "separate test"

These test name are quite long. Rather than test_basic_function_with can we have test_multidot_, and drop the _argument, so this would become test_multidot_with_dynamic_programming_and_out.

Copy link
Member

Choose a reason for hiding this comment

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

I will commit your suggestion, but also removed the multidot_with part, this is already in the TestMultidot class, so that should be implicit.

I will then merge this later today or tomorrow.

@mattip mattip removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Mar 27, 2020
Copy link
Member

@mattip mattip left a comment

Choose a reason for hiding this comment

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

A few nits and I think this is ready

numpy/linalg/tests/test_linalg.py Outdated Show resolved Hide resolved
numpy/linalg/tests/test_linalg.py Outdated Show resolved Hide resolved
numpy/linalg/tests/test_linalg.py Outdated Show resolved Hide resolved
numpy/linalg/tests/test_linalg.py Outdated Show resolved Hide resolved
Co-Authored-By: Matti Picus <matti.picus@gmail.com>
@seberg seberg self-requested a review April 10, 2020 18:37
numpy/linalg/linalg.py Outdated Show resolved Hide resolved
@eric-wieser
Copy link
Member

What do we use for the concatenate dispatcher? That one could maybe be reused here.

numpy/linalg/linalg.py Outdated Show resolved Hide resolved
Co-Authored-By: Eric Wieser <wieser.eric@gmail.com>
@mattip mattip merged commit 4753652 into numpy:master Apr 29, 2020
@mattip
Copy link
Member

mattip commented Apr 29, 2020

Thanks @sslivkoff and reviewers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants