Skip to content

ENH: __array_function__ for multiarray functions #12175

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 4 commits into from
Oct 19, 2018

Conversation

shoyer
Copy link
Member

@shoyer shoyer commented Oct 14, 2018

Testing out the Python wrapper approach.

xref #12028

@shoyer
Copy link
Member Author

shoyer commented Oct 15, 2018

Notes/questions:

  • This technically changes the signature of the functions like concatenate/where that previously only accepted positional arguments, since they're now defined in Python. Are we OK with this, and with these specific argument names?
  • This probably also adds some overhead/slowdown for these functions (which I haven't measured yet). Are we OK with this?
  • Do we still need to define all of these functions in numpy.core.multiarray or can we move them to more logical places?

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Only a comment about use of None in where - not sure how best to solve that... Revenge of the object dtype!

More generally, except perhaps for where, I think the extra overhead for these functions would generally be acceptable, as the work they do is fairly complex (for empty_like, in filling the newly created array).

In any case, it seems worth extending this especially to concatenate, even if right now it makes things a little slower.

The only disadvantage that I can see is related to the arguments: if we now allow keyword arguments, we're stuck with them, and in C that is so slow that we are effectively stuck with the python wrapper. But, again, in practice I think the benefits well outweigh these disadvantages.

@eric-wieser eric-wieser changed the title WIP: __array_ufunc__ for multiarray functions WIP: __array_function__ for multiarray functions Oct 15, 2018
@shoyer
Copy link
Member Author

shoyer commented Oct 15, 2018

I also tend to think the tradeoffs are worth it here. Even if np.concatenate() becomes twice as slow for small arrays, users don't seem to notice performance differences of a few microseconds.

@shoyer
Copy link
Member Author

shoyer commented Oct 16, 2018

I finished implementing multiarray overloads with NumPy wrappers.

The most awkward part was for a few datetime functions, because their docstrings lie about their function signatures -- they claim to accept None for default values, but the C implementation actually expects default arguments not to be supplied.

@shoyer shoyer changed the title WIP: __array_function__ for multiarray functions ENH: __array_function__ for multiarray functions Oct 16, 2018
@shoyer
Copy link
Member Author

shoyer commented Oct 16, 2018

The Azure build failure looks spurious -- is there an easy way for me to reset that?

@tylerjereddy
Copy link
Contributor

@shoyer I restarted the build manually. I added your google email address to Azure devops admin group--not sure if you have a different Microsoft account though.

@shoyer
Copy link
Member Author

shoyer commented Oct 16, 2018

Thanks Tyler! If you could add my personal Gmail address (my GitHub username at Gmail.com) that would be great.

@tylerjereddy
Copy link
Contributor

image

@charris
Copy link
Member

charris commented Oct 16, 2018

@tylerjereddy You can rerun the failing test by itself; click on the failing test so that it shows, pull down the three dot menu up top, and hit rebuild.

@tylerjereddy
Copy link
Contributor

@charris I thought I did that, but maybe I failed to properly select so the whole matrix was re-triggered by accident

Copy link
Contributor

@hameerabbasi hameerabbasi left a comment

Choose a reason for hiding this comment

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

I don't have anything to add, this looks all good to me.

@mhvk
Copy link
Contributor

mhvk commented Oct 19, 2018

Since my comment about where was incorrect, this all looks good to me to. Hence, merging... Thanks, @shoyer!

@mhvk mhvk merged commit bd49e5e into numpy:master Oct 19, 2018
@charris charris changed the title ENH: __array_function__ for multiarray functions ENH: __array_function__ for multiarray functions Nov 10, 2018
shoyer added a commit to shoyer/numpy that referenced this pull request Dec 1, 2018
The original motivation for the style of these wrapper functions, introduced
in numpygh-12175, was to preserve introspection. But it turns out NumPy's functions
defined in C don't support introspection anyways, so the extra wrapper
functions are entirely pointless.

This version reverts the additional wrapper functions, which put default
arguments in two places and introduced slow-down due to the overhead of
another function call.

I've retained docstrings in multiarray.py, since it's definitely more readable
to keep docstrings and dispatchers together rather than leaving docstrings in
_add_newdocs.py.

One bonus of this approach is that dispatcher functions have the same name
as their implementations, so `np.concatenate(unknown=True)` gives an
error message mentioning "concatenate" rather than "_concatenate_dispatcher":
`TypeError: concatenate() got an unexpected keyword argument 'unknown'`
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.

5 participants