[Numpy-discussion] Rules for argument parsing/forwarding in __array_function__ and __array_ufunc__

Stefan van der Walt stefanv at berkeley.edu
Thu Dec 3 00:07:32 EST 2020


Hi Sebastian,

Looking at these three rules, they all seem to stem from one simple question: do we desire for a single code snippet to be runnable on multiple array implementations?

On Wed, Dec 2, 2020, at 15:34, Sebastian Berg wrote:
> 1. If an argument is invalid in NumPy it is considered and error.
>    For example:
> 
>        np.log(arr, my_weird_argument=True)
> 
>    is always an error even if the `__array_function__` implementation
>    of `arr` would support it.
>    NEP 18 explicitly says that allowing forwarding could be done, but
>    will not be done at this time.

Relaxing this rule will mean that code working for one array implementation (which has this keyword) may not work for another.

> 2. Arguments must only be forwarded if they are passed in:
> 
>        np.mean(cupy_array)
> 
>    ends up as `cupy.mean(cupy_array)` and not:
> 
>        cupy.mean(cupy_array, axis=None, dtype=None, out=None,
>                  keepdims=False, where=True)
> 
>    meaning that CuPy does not need to implement all of those kwargs and
>    NumPy can add new ones without breaking anyones code.

This may ultimately make it harder for array implementors (they will only see errors once someone tries to pass in an argument that they forgot to implement).  Perhaps better to pass all so they know what they're dealing with?

> 3. NumPy should not check the *validity* of the arguments. For example:
>    `np.add.reduce(xarray, axis="long")` should probably work in xarray.
>    (`xarray.DataArray` does not actually implement the above.)
>    But a string cannot be used as an axis in NumPy.

Getting back to the original question: if this code is to be run on multiple implementations, we should ensure that no strange values pass through.

Personally, I like the idea of a single API that works on multiple backends.  As such, I would 1) not pass through unknown arguments, 2) always pass through all arguments, and 3) validate inputs to each call.

Best regards,
Stéfan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/numpy-discussion/attachments/20201202/e2c7ef5c/attachment.html>


More information about the NumPy-Discussion mailing list