[Numpy-discussion] Adding to the non-dispatched implementation of NumPy methods

Stephan Hoyer shoyer at gmail.com
Sun Apr 28 21:20:12 EDT 2019


On Sun, Apr 28, 2019 at 8:42 AM Marten van Kerkwijk <
m.h.vankerkwijk at gmail.com> wrote:

> In summary, I think the guarantees should be as follows:
> 1.If you call np.function and
>   - do not define __array_function__, changes happen only via the usual
> cycle.
>   - define __array_function__, you take responsibility for returning the
> result.
> 2. If you call np.function.__wrapped__ and
>   - input only ndarray, changes happen only via the usual cycle;
>   - input anything but ndarray, changes can happen in any release.
>

The uses that I've seen so far (in CuPy and JAX), involve a handful of
functions that are directly re-exported from NumPy, e.g.,
jax.numpy.array_repr is the exact same object as numpy.array_repr:
https://github.com/cupy/cupy/blob/c3f1be602bf6951b007beaae644a5662f910048b/cupy/__init__.py#L341-L366
https://github.com/google/jax/blob/5edb23679f2605654949156da84e330205840695/jax/numpy/lax_numpy.py#L89-L132


I suspect this will be less common in the future if __array_function__
takes off, but for now it's convenient because users don't need to know
exactly which functions have been reimplemented. They can just use "import
jax.numpy as np" and everything works.

These libraries are indeed passing CuPy or JAX arrays into NumPy functions,
which currently happen to have the desired behavior, thanks to accidental
details about how NumPy currently supports duck-typing and/or coercions.

To this end, it would be really nice to have an alias that *is* guaranteed
to work exactly as if __array_function__ didn't exist, and not only for
numpy.ndarray arrays.

Ralf raises a good point about the name. We don't need to add this
attribute for ufuncs and __array_ufunc__ yet, but
(1) we might want this in the future, just for consistency in the design of
__array_function__ and __array_ufunc__, and
(2) we definitely don't want to rule out converting functions into ufunc.

So we might as well pick a name that works for both, e.g.,
__skip_array_overrides__ rather than __skip_array_function__. This would
let us save our users a bit of pain by not requiring them to make changes
like  np.where.__skip_array_function__ -> np.where.__skip_array_ufunc__.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20190428/6e2136d1/attachment-0001.html>


More information about the NumPy-Discussion mailing list