| 1 |
Generalized Ufuncs |
| 2 |
================== |
| 3 |
|
| 4 |
`NumPy <https://www.numpy.org>`_ provides the concept of `generalized ufuncs <https://docs.scipy.org/doc/numpy/reference/c-api/generalized-ufuncs.html>`_. Generalized ufuncs are functions |
| 5 |
that distinguish the various dimensions of passed arrays in the two classes loop dimensions |
| 6 |
and core dimensions. To accomplish this, a `signature <https://docs.scipy.org/doc/numpy/reference/c-api/generalized-ufuncs.html#details-of-signature>`_ is specified for NumPy generalized ufuncs. |
| 7 |
|
| 8 |
`Dask <https://dask.org/>`_ integrates interoperability with NumPy's generalized ufuncs |
| 9 |
by adhering to respective `ufunc protocol <https://docs.scipy.org/doc/numpy/reference/arrays.classes.html#numpy.class.__array_ufunc__>`_, and provides a wrapper to make a Python function a generalized ufunc. |
| 10 |
|
| 11 |
|
| 12 |
Usage |
| 13 |
----- |
| 14 |
|
| 15 |
NumPy Generalized UFuncs |
| 16 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 17 |
.. note:: |
| 18 |
|
| 19 |
`NumPy <https://www.numpy.org>`_ generalized ufuncs are currently (v1.14.3 and below) stored in |
| 20 |
inside ``np.linalg._umath_linalg`` and might change in the future. |
| 21 |
|
| 22 |
|
| 23 |
.. code-block:: python |
| 24 |
|
| 25 |
import dask.array as da |
| 26 |
import numpy as np |
| 27 |
|
| 28 |
x = da.random.default_rng().normal(size=(3, 10, 10), chunks=(2, 10, 10)) |
| 29 |
|
| 30 |
w, v = np.linalg._umath_linalg.eig(x, output_dtypes=(float, float)) |
| 31 |
|
| 32 |
|
| 33 |
Create Generalized UFuncs |
| 34 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 35 |
|
| 36 |
It can be difficult to create your own GUFuncs without going into the CPython API. |
| 37 |
However, the `Numba <https://numba.pydata.org>`_ project does provide a |
| 38 |
nice implementation with their ``numba.guvectorize`` decorator. See `Numba's |
| 39 |
documentation |
| 40 |
<https://numba.pydata.org/numba-doc/dev/user/vectorize.html#the-guvectorize-decorator>`_ |
| 41 |
for more information. |
| 42 |
|
| 43 |
Wrap your own Python function |
| 44 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 45 |
``gufunc`` can be used to make a Python function behave like a generalized ufunc: |
| 46 |
|
| 47 |
|
| 48 |
.. code-block:: python |
| 49 |
|
| 50 |
x = da.random.default_rng().normal(size=(10, 5), chunks=(2, 5)) |
| 51 |
|
| 52 |
def foo(x): |
| 53 |
return np.mean(x, axis=-1) |
| 54 |
|
| 55 |
gufoo = da.gufunc(foo, signature="(i)->()", output_dtypes=float, vectorize=True) |
| 56 |
|
| 57 |
y = gufoo(x) |
| 58 |
|
| 59 |
|
| 60 |
Instead of ``gufunc``, also the ``as_gufunc`` decorator can be used for convenience: |
| 61 |
|
| 62 |
|
| 63 |
.. code-block:: python |
| 64 |
|
| 65 |
x = da.random.normal(size=(10, 5), chunks=(2, 5)) |
| 66 |
|
| 67 |
@da.as_gufunc(signature="(i)->()", output_dtypes=float, vectorize=True) |
| 68 |
def gufoo(x): |
| 69 |
return np.mean(x, axis=-1) |
| 70 |
|
| 71 |
y = gufoo(x) |
| 72 |
|
| 73 |
|
| 74 |
Disclaimer |
| 75 |
---------- |
| 76 |
This experimental generalized ufunc integration is not complete: |
| 77 |
|
| 78 |
* ``gufunc`` does not create a true generalized ufunc to be used with other input arrays besides Dask. |
| 79 |
I.e., at the moment, ``gufunc`` casts all input arguments to ``dask.array.Array`` |
| 80 |
|
| 81 |
* Inferring ``output_dtypes`` automatically is not implemented yet |
| 82 |
|
| 83 |
|
| 84 |
API |
| 85 |
--- |
| 86 |
|
| 87 |
.. currentmodule:: dask.array.gufunc |
| 88 |
|
| 89 |
.. autosummary:: |
| 90 |
apply_gufunc |
| 91 |
as_gufunc |
| 92 |
gufunc |