| 1 |
Dask Delayed |
| 2 |
============ |
| 3 |
|
| 4 |
.. meta:: |
| 5 |
:description: The Dask delayed function decorates your functions so that they operate lazily. Rather than executing your function immediately, it will defer execution, placing the function and its arguments into a task graph. |
| 6 |
|
| 7 |
.. toctree:: |
| 8 |
:maxdepth: 1 |
| 9 |
:hidden: |
| 10 |
|
| 11 |
delayed-collections.rst |
| 12 |
delayed-best-practices.rst |
| 13 |
|
| 14 |
Sometimes problems don't fit into one of the collections like ``dask.array`` or |
| 15 |
``dask.dataframe``. In these cases, users can parallelize custom algorithms |
| 16 |
using the simpler ``dask.delayed`` interface. This allows you to create graphs |
| 17 |
directly with a light annotation of normal python code: |
| 18 |
|
| 19 |
.. code-block:: python |
| 20 |
|
| 21 |
>>> x = dask.delayed(inc)(1) |
| 22 |
>>> y = dask.delayed(inc)(2) |
| 23 |
>>> z = dask.delayed(add)(x, y) |
| 24 |
>>> z.compute() |
| 25 |
5 |
| 26 |
>>> z.visualize() |
| 27 |
|
| 28 |
.. image:: images/inc-add.svg |
| 29 |
:alt: A Dask Delayed task graph with two "inc" functions combined using an "add" function resulting in an output node. |
| 30 |
|
| 31 |
.. raw:: html |
| 32 |
|
| 33 |
<iframe width="560" |
| 34 |
height="315" |
| 35 |
src="https://www.youtube.com/embed/SHqFmynRxVU" |
| 36 |
style="margin: 0 auto 20px auto; display: block;" |
| 37 |
frameborder="0" |
| 38 |
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" |
| 39 |
allowfullscreen></iframe> |
| 40 |
|
| 41 |
Example |
| 42 |
------- |
| 43 |
|
| 44 |
Visit https://examples.dask.org/delayed.html to see and run examples using Dask |
| 45 |
Delayed. |
| 46 |
|
| 47 |
Sometimes we face problems that are parallelizable, but don't fit into high-level |
| 48 |
abstractions like Dask Array or Dask DataFrame. Consider the following example: |
| 49 |
|
| 50 |
.. code-block:: python |
| 51 |
|
| 52 |
def inc(x): |
| 53 |
return x + 1 |
| 54 |
|
| 55 |
def double(x): |
| 56 |
return x * 2 |
| 57 |
|
| 58 |
def add(x, y): |
| 59 |
return x + y |
| 60 |
|
| 61 |
data = [1, 2, 3, 4, 5] |
| 62 |
|
| 63 |
output = [] |
| 64 |
for x in data: |
| 65 |
a = inc(x) |
| 66 |
b = double(x) |
| 67 |
c = add(a, b) |
| 68 |
output.append(c) |
| 69 |
|
| 70 |
total = sum(output) |
| 71 |
|
| 72 |
There is clearly parallelism in this problem (many of the ``inc``, |
| 73 |
``double``, and ``add`` functions can be evaluated independently), but it's not |
| 74 |
clear how to convert this to an array or DataFrame computation. As written, |
| 75 |
this code runs sequentially in a single thread. However, we see that a |
| 76 |
lot of this could be executed in parallel. |
| 77 |
|
| 78 |
The Dask ``delayed`` function decorates your functions so that they operate |
| 79 |
*lazily*. Rather than executing your function immediately, it will defer |
| 80 |
execution, placing the function and its arguments into a task graph. |
| 81 |
|
| 82 |
.. currentmodule:: dask.delayed |
| 83 |
|
| 84 |
.. autosummary:: |
| 85 |
delayed |
| 86 |
|
| 87 |
We slightly modify our code by wrapping functions in ``delayed``. |
| 88 |
This delays the execution of the function and generates a Dask graph instead: |
| 89 |
|
| 90 |
.. code-block:: python |
| 91 |
|
| 92 |
import dask |
| 93 |
|
| 94 |
output = [] |
| 95 |
for x in data: |
| 96 |
a = dask.delayed(inc)(x) |
| 97 |
b = dask.delayed(double)(x) |
| 98 |
c = dask.delayed(add)(a, b) |
| 99 |
output.append(c) |
| 100 |
|
| 101 |
total = dask.delayed(sum)(output) |
| 102 |
|
| 103 |
We used the ``dask.delayed`` function to wrap the function calls that we want |
| 104 |
to turn into tasks. None of the ``inc``, ``double``, ``add``, or ``sum`` calls |
| 105 |
have happened yet. Instead, the object ``total`` is a ``Delayed`` result that |
| 106 |
contains a task graph of the entire computation. Looking at the graph we see |
| 107 |
clear opportunities for parallel execution. The :doc:`Dask schedulers <scheduling>` |
| 108 |
will exploit this parallelism, generally improving performance (although not in this |
| 109 |
example, because these functions are already very small and fast.) |
| 110 |
|
| 111 |
.. code-block:: python |
| 112 |
|
| 113 |
total.visualize() # see image to the right |
| 114 |
|
| 115 |
.. image:: images/delayed-inc-double-add.svg |
| 116 |
:align: right |
| 117 |
:alt: A task graph with many nodes for "inc" and "double" that combine with "add" nodes. The output of the "add" nodes finally aggregate with a "sum" node. |
| 118 |
|
| 119 |
We can now compute this lazy result to execute the graph in parallel: |
| 120 |
|
| 121 |
.. code-block:: python |
| 122 |
|
| 123 |
>>> total.compute() |
| 124 |
45 |
| 125 |
|
| 126 |
Decorator |
| 127 |
--------- |
| 128 |
|
| 129 |
It is also common to see the delayed function used as a decorator. Here is a |
| 130 |
reproduction of our original problem as a parallel code: |
| 131 |
|
| 132 |
.. code-block:: python |
| 133 |
|
| 134 |
import dask |
| 135 |
|
| 136 |
@dask.delayed |
| 137 |
def inc(x): |
| 138 |
return x + 1 |
| 139 |
|
| 140 |
@dask.delayed |
| 141 |
def double(x): |
| 142 |
return x * 2 |
| 143 |
|
| 144 |
@dask.delayed |
| 145 |
def add(x, y): |
| 146 |
return x + y |
| 147 |
|
| 148 |
data = [1, 2, 3, 4, 5] |
| 149 |
|
| 150 |
output = [] |
| 151 |
for x in data: |
| 152 |
a = inc(x) |
| 153 |
b = double(x) |
| 154 |
c = add(a, b) |
| 155 |
output.append(c) |
| 156 |
|
| 157 |
total = dask.delayed(sum)(output) |
| 158 |
|
| 159 |
|
| 160 |
Real time |
| 161 |
--------- |
| 162 |
|
| 163 |
Sometimes you want to create and destroy work during execution, launch tasks |
| 164 |
from other tasks, etc. For this, see the :doc:`Futures <futures>` interface. |
| 165 |
|
| 166 |
|
| 167 |
Best Practices |
| 168 |
-------------- |
| 169 |
|
| 170 |
For a list of common problems and recommendations see |
| 171 |
:doc:`Delayed Best Practices <delayed-best-practices>`. |
| 172 |
|
| 173 |
|
| 174 |
Indirect Dependencies |
| 175 |
--------------------- |
| 176 |
|
| 177 |
Sometimes you might find yourself wanting to add a dependency to a task that does |
| 178 |
not take the result of that dependency as an input. For example when a task depends |
| 179 |
on the side-effect of another task. In these cases you can use |
| 180 |
``dask.graph_manipulation.bind``. |
| 181 |
|
| 182 |
.. code-block:: python |
| 183 |
|
| 184 |
import dask |
| 185 |
from dask.graph_manipulation import bind |
| 186 |
|
| 187 |
DATA = [] |
| 188 |
|
| 189 |
@dask.delayed |
| 190 |
def inc(x): |
| 191 |
return x + 1 |
| 192 |
|
| 193 |
@dask.delayed |
| 194 |
def add_data(x): |
| 195 |
DATA.append(x) |
| 196 |
|
| 197 |
@dask.delayed |
| 198 |
def sum_data(x): |
| 199 |
return sum(DATA) + x |
| 200 |
|
| 201 |
a = inc(1) |
| 202 |
b = add_data(a) |
| 203 |
c = inc(3) |
| 204 |
d = add_data(c) |
| 205 |
e = inc(5) |
| 206 |
f = bind(sum_data, [b, d])(e) |
| 207 |
f.compute() |
| 208 |
|
| 209 |
``sum_data`` will operate on DATA only after both the expected items have |
| 210 |
been appended to it. ``bind`` can also be used along with direct dependencies |
| 211 |
passed through the function arguments. |
| 212 |
|
| 213 |
|
| 214 |
Execution |
| 215 |
--------- |
| 216 |
|
| 217 |
By default, Dask Delayed uses the threaded scheduler in order to avoid data |
| 218 |
transfer costs. |
| 219 |
You should consider using `multi-processing`_ scheduler or `dask.distributed`_ scheduler |
| 220 |
on a local machine or on a cluster if your code does not `release the GIL well`_ |
| 221 |
(computations that are dominated by pure Python code, or computations wrapping external |
| 222 |
code and holding onto it). |
| 223 |
|
| 224 |
.. _`dask.distributed`: https://distributed.dask.org/en/latest/ |
| 225 |
.. _`multi-processing`: https://docs.dask.org/en/stable/scheduling.html#local-processes |
| 226 |
.. _`release the GIL well`: https://docs.dask.org/en/stable/graphs.html#avoid-holding-the-gil |