| 1 |
.. _graph_manipulation: |
| 2 |
|
| 3 |
Advanced graph manipulation |
| 4 |
=========================== |
| 5 |
There are some situations where computations with Dask collections will result in |
| 6 |
suboptimal memory usage (e.g. an entire Dask DataFrame is loaded into memory). |
| 7 |
This may happen when Dask’s scheduler doesn’t automatically delay the computation of |
| 8 |
nodes in a task graph to avoid occupying memory with their output for prolonged periods |
| 9 |
of time, or in scenarios where recalculating nodes is much cheaper than holding their |
| 10 |
output in memory. |
| 11 |
|
| 12 |
This page highlights a set of graph manipulation utilities which can be used to help |
| 13 |
avoid these scenarios. In particular, the utilities described below rewrite the |
| 14 |
underlying Dask graph for Dask collections, producing equivalent collections with |
| 15 |
different sets of keys. |
| 16 |
|
| 17 |
Consider the following example: |
| 18 |
|
| 19 |
.. code-block:: python |
| 20 |
|
| 21 |
>>> import dask.array as da |
| 22 |
>>> x = da.random.default_rng().normal(size=500_000_000, chunks=100_000) |
| 23 |
>>> x_mean = x.mean() |
| 24 |
>>> y = (x - x_mean).max().compute() |
| 25 |
|
| 26 |
The above example computes the largest value of a distribution after removing its bias. |
| 27 |
This involves loading the chunks of ``x`` into memory in order to compute ``x_mean``. |
| 28 |
However, since the ``x`` array is needed later in the computation to compute ``y``, the |
| 29 |
entire ``x`` array is kept in memory. For large Dask Arrays this can be very |
| 30 |
problematic. |
| 31 |
|
| 32 |
To alleviate the need for the entire ``x`` array to be kept in memory, one could rewrite |
| 33 |
the last line as follows: |
| 34 |
|
| 35 |
.. code-block:: python |
| 36 |
|
| 37 |
>>> from dask.graph_manipulation import bind |
| 38 |
>>> xb = bind(x, x_mean) |
| 39 |
>>> y = (xb - x_mean).max().compute() |
| 40 |
|
| 41 |
Here we use :func:`~dask.graph_manipulation.bind` to create a new Dask Array, ``xb``, |
| 42 |
which produces exactly the same output as ``x``, but whose underlying Dask graph has |
| 43 |
different keys than ``x``, and will only be computed after ``x_mean`` has been |
| 44 |
calculated. |
| 45 |
|
| 46 |
This results in the chunks of ``x`` being computed and immediately individually reduced |
| 47 |
by ``mean``; then recomputed and again immediately pipelined into the subtraction |
| 48 |
followed by reduction with ``max``. This results in a much smaller peak memory usage as |
| 49 |
the full ``x`` array is no longer loaded into memory. However, the tradeoff is that the |
| 50 |
compute time increases as ``x`` is computed twice. |
| 51 |
|
| 52 |
|
| 53 |
API |
| 54 |
--- |
| 55 |
|
| 56 |
.. currentmodule:: dask.graph_manipulation |
| 57 |
|
| 58 |
.. autosummary:: |
| 59 |
|
| 60 |
checkpoint |
| 61 |
wait_on |
| 62 |
bind |
| 63 |
clone |
| 64 |
|
| 65 |
|
| 66 |
Definitions |
| 67 |
~~~~~~~~~~~ |
| 68 |
|
| 69 |
.. autofunction:: checkpoint |
| 70 |
.. autofunction:: wait_on |
| 71 |
.. autofunction:: bind |
| 72 |
.. autofunction:: clone |