Skip to content

Add __array_ufunc__ protocol #1230

@mrocklin

Description

@mrocklin

The Numpy __array_ufunc__ protocol allows projects to use many of the Numpy functions to drive their own data structures. Here is a small example with dask.array.

In [1]: import dask.array as da

In [2]: x = da.arange(100, chunks=(10,))

In [3]: x
Out[3]: dask.array<arange, shape=(100,), dtype=int64, chunksize=(10,)>

In [4]: import numpy as np

In [5]: np.exp(x)  # note that this returns a dask array
Out[5]: dask.array<exp, shape=(100,), dtype=float64, chunksize=(10,)>

In [6]: np.sum(np.exp(x))
Out[6]: dask.array<sum-aggregate, shape=(), dtype=float64, chunksize=()>

In [7]: _.compute()
Out[7]: 1.5644215618731406e+43

This provides a protocol for downstream projects to write code that works independently of the choice of ndarray data structures, at least for a subset of the NumPy API. The Numpy core team seems dedicated to increasing this subset in the near future.

It would be useful for CuPy to also implement this protocol to improve interoperation with the rest of the ecosystem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions