| 1 |
Specification |
| 2 |
============= |
| 3 |
|
| 4 |
Dask is a specification to encode a graph -- specifically, a directed |
| 5 |
acyclic graph of tasks with data dependencies -- using ordinary Python data |
| 6 |
structures, namely dicts, tuples, functions, and arbitrary Python |
| 7 |
values. |
| 8 |
|
| 9 |
|
| 10 |
Definitions |
| 11 |
----------- |
| 12 |
|
| 13 |
A **Dask graph** is a dictionary mapping **keys** to **computations**: |
| 14 |
|
| 15 |
.. code-block:: python |
| 16 |
|
| 17 |
{'x': 1, |
| 18 |
'y': 2, |
| 19 |
'z': (add, 'x', 'y'), |
| 20 |
'w': (sum, ['x', 'y', 'z']), |
| 21 |
'v': [(sum, ['w', 'z']), 2]} |
| 22 |
|
| 23 |
A **key** is a str, bytes, int, float, or tuple thereof: |
| 24 |
|
| 25 |
.. code-block:: python |
| 26 |
|
| 27 |
'x' |
| 28 |
('x', 2, 3) |
| 29 |
|
| 30 |
A **task** is a tuple with a callable first element. Tasks represent atomic |
| 31 |
units of work meant to be run by a single worker. Example: |
| 32 |
|
| 33 |
.. code-block:: python |
| 34 |
|
| 35 |
(add, 'x', 'y') |
| 36 |
|
| 37 |
We represent a task as a tuple such that the *first element is a callable |
| 38 |
function* (like ``add``), and the succeeding elements are *arguments* for that |
| 39 |
function. An *argument* may be any valid **computation**. |
| 40 |
|
| 41 |
A **computation** may be one of the following: |
| 42 |
|
| 43 |
1. Any **key** present in the Dask graph like ``'x'`` |
| 44 |
2. Any other value like ``1``, to be interpreted literally |
| 45 |
3. A **task** like ``(inc, 'x')`` (see below) |
| 46 |
4. A list of **computations**, like ``[1, 'x', (inc, 'x')]`` |
| 47 |
|
| 48 |
So all of the following are valid **computations**: |
| 49 |
|
| 50 |
.. code-block:: python |
| 51 |
|
| 52 |
np.array([...]) |
| 53 |
(add, 1, 2) |
| 54 |
(add, 'x', 2) |
| 55 |
(add, (inc, 'x'), 2) |
| 56 |
(sum, [1, 2]) |
| 57 |
(sum, ['x', (inc, 'x')]) |
| 58 |
(np.dot, np.array([...]), np.array([...])) |
| 59 |
[(sum, ['x', 'y']), 'z'] |
| 60 |
|
| 61 |
To encode keyword arguments, we recommend the use of ``functools.partial`` or |
| 62 |
``toolz.curry``. |
| 63 |
|
| 64 |
|
| 65 |
What functions should expect |
| 66 |
---------------------------- |
| 67 |
|
| 68 |
In cases like ``(add, 'x', 'y')``, functions like ``add`` receive concrete |
| 69 |
values instead of keys. A Dask scheduler replaces keys (like ``'x'`` and ``'y'``) with |
| 70 |
their computed values (like ``1``, and ``2``) *before* calling the ``add`` function. |
| 71 |
|
| 72 |
|
| 73 |
Entry Point - The ``get`` function |
| 74 |
---------------------------------- |
| 75 |
|
| 76 |
The ``get`` function serves as entry point to computation for all |
| 77 |
:doc:`schedulers <scheduler-overview>`. This function gets the value |
| 78 |
associated to the given key. That key may refer to stored data, as is the case |
| 79 |
with ``'x'``, or to a task, as is the case with ``'z'``. In the latter case, |
| 80 |
``get`` should perform all necessary computation to retrieve the computed |
| 81 |
value. |
| 82 |
|
| 83 |
.. _scheduler: scheduler-overview.rst |
| 84 |
|
| 85 |
.. code-block:: python |
| 86 |
|
| 87 |
>>> from dask.threaded import get |
| 88 |
|
| 89 |
>>> from operator import add |
| 90 |
|
| 91 |
>>> dsk = {'x': 1, |
| 92 |
... 'y': 2, |
| 93 |
... 'z': (add, 'x', 'y'), |
| 94 |
... 'w': (sum, ['x', 'y', 'z'])} |
| 95 |
|
| 96 |
.. code-block:: python |
| 97 |
|
| 98 |
>>> get(dsk, 'x') |
| 99 |
1 |
| 100 |
|
| 101 |
>>> get(dsk, 'z') |
| 102 |
3 |
| 103 |
|
| 104 |
>>> get(dsk, 'w') |
| 105 |
6 |
| 106 |
|
| 107 |
Additionally, if given a ``list``, get should simultaneously acquire values for |
| 108 |
multiple keys: |
| 109 |
|
| 110 |
.. code-block:: python |
| 111 |
|
| 112 |
>>> get(dsk, ['x', 'y', 'z']) |
| 113 |
[1, 2, 3] |
| 114 |
|
| 115 |
Because we accept lists of keys as keys, we support nested lists: |
| 116 |
|
| 117 |
.. code-block:: python |
| 118 |
|
| 119 |
>>> get(dsk, [['x', 'y'], ['z', 'w']]) |
| 120 |
[[1, 2], [3, 6]] |
| 121 |
|
| 122 |
Internally ``get`` can be arbitrarily complex, calling out to distributed |
| 123 |
computing, using caches, and so on. |
| 124 |
|
| 125 |
|
| 126 |
Why use tuples |
| 127 |
-------------- |
| 128 |
|
| 129 |
With ``(add, 'x', 'y')``, we wish to encode the result of calling ``add`` on |
| 130 |
the values corresponding to the keys ``'x'`` and ``'y'``. |
| 131 |
|
| 132 |
We intend the following meaning: |
| 133 |
|
| 134 |
.. code-block:: python |
| 135 |
|
| 136 |
add('x', 'y') # after x and y have been replaced |
| 137 |
|
| 138 |
But this will err because Python executes the function immediately |
| 139 |
before we know values for ``'x'`` and ``'y'``. |
| 140 |
|
| 141 |
We delay the execution by moving the opening parenthesis one term to the left, |
| 142 |
creating a tuple: |
| 143 |
|
| 144 |
.. code:: |
| 145 |
|
| 146 |
Before: add( 'x', 'y') |
| 147 |
After: (add, 'x', 'y') |
| 148 |
|
| 149 |
This lets us store the desired computation as data that we can analyze using |
| 150 |
other Python code, rather than cause immediate execution. |
| 151 |
|
| 152 |
LISP users will identify this as an s-expression, or as a rudimentary form of |
| 153 |
quoting. |