| 1 |
.. _high-level-graphs: |
| 2 |
|
| 3 |
High Level Graphs |
| 4 |
================= |
| 5 |
|
| 6 |
Dask graphs produced by collections like Arrays, Bags, and DataFrames have |
| 7 |
high-level structure that can be useful for visualization and high-level |
| 8 |
optimization. The task graphs produced by these collections encode this |
| 9 |
structure explicitly as ``HighLevelGraph`` objects. This document describes |
| 10 |
how to work with these in more detail. |
| 11 |
|
| 12 |
|
| 13 |
Motivation and Example |
| 14 |
---------------------- |
| 15 |
|
| 16 |
In full generality, Dask schedulers expect arbitrary task graphs where each |
| 17 |
node is a single Python function call and each edge is a dependency between |
| 18 |
two function calls. These are usually stored in flat dictionaries. Here is |
| 19 |
some simple Dask DataFrame code and the task graph that it might generate: |
| 20 |
|
| 21 |
.. code-block:: python |
| 22 |
|
| 23 |
import dask.dataframe as dd |
| 24 |
|
| 25 |
df = dd.read_csv('myfile.*.csv') |
| 26 |
df = df + 100 |
| 27 |
df = df[df.name == 'Alice'] |
| 28 |
|
| 29 |
.. code-block:: python |
| 30 |
|
| 31 |
{ |
| 32 |
('read-csv', 0): (pandas.read_csv, 'myfile.0.csv'), |
| 33 |
('read-csv', 1): (pandas.read_csv, 'myfile.1.csv'), |
| 34 |
('read-csv', 2): (pandas.read_csv, 'myfile.2.csv'), |
| 35 |
('read-csv', 3): (pandas.read_csv, 'myfile.3.csv'), |
| 36 |
('add', 0): (operator.add, ('read-csv', 0), 100), |
| 37 |
('add', 1): (operator.add, ('read-csv', 1), 100), |
| 38 |
('add', 2): (operator.add, ('read-csv', 2), 100), |
| 39 |
('add', 3): (operator.add, ('read-csv', 3), 100), |
| 40 |
('filter', 0): (lambda part: part[part.name == 'Alice'], ('add', 0)), |
| 41 |
('filter', 1): (lambda part: part[part.name == 'Alice'], ('add', 1)), |
| 42 |
('filter', 2): (lambda part: part[part.name == 'Alice'], ('add', 2)), |
| 43 |
('filter', 3): (lambda part: part[part.name == 'Alice'], ('add', 3)), |
| 44 |
} |
| 45 |
|
| 46 |
The task graph is a dictionary that stores every Pandas-level function call |
| 47 |
necessary to compute the final result. We can see that there is some structure |
| 48 |
to this dictionary if we separate out the tasks that were associated to each |
| 49 |
high-level Dask DataFrame operation: |
| 50 |
|
| 51 |
.. code-block:: python |
| 52 |
|
| 53 |
{ |
| 54 |
# From the dask.dataframe.read_csv call |
| 55 |
('read-csv', 0): (pandas.read_csv, 'myfile.0.csv'), |
| 56 |
('read-csv', 1): (pandas.read_csv, 'myfile.1.csv'), |
| 57 |
('read-csv', 2): (pandas.read_csv, 'myfile.2.csv'), |
| 58 |
('read-csv', 3): (pandas.read_csv, 'myfile.3.csv'), |
| 59 |
|
| 60 |
# From the df + 100 call |
| 61 |
('add', 0): (operator.add, ('read-csv', 0), 100), |
| 62 |
('add', 1): (operator.add, ('read-csv', 1), 100), |
| 63 |
('add', 2): (operator.add, ('read-csv', 2), 100), |
| 64 |
('add', 3): (operator.add, ('read-csv', 3), 100), |
| 65 |
|
| 66 |
# From the df[df.name == 'Alice'] call |
| 67 |
('filter', 0): (lambda part: part[part.name == 'Alice'], ('add', 0)), |
| 68 |
('filter', 1): (lambda part: part[part.name == 'Alice'], ('add', 1)), |
| 69 |
('filter', 2): (lambda part: part[part.name == 'Alice'], ('add', 2)), |
| 70 |
('filter', 3): (lambda part: part[part.name == 'Alice'], ('add', 3)), |
| 71 |
} |
| 72 |
|
| 73 |
By understanding this high-level structure we are able to understand our task |
| 74 |
graphs more easily (this is more important for larger datasets when there are |
| 75 |
thousands of tasks per layer) and how to perform high-level optimizations. For |
| 76 |
example, in the case above we may want to automatically rewrite our code to |
| 77 |
filter our datasets before adding 100: |
| 78 |
|
| 79 |
.. code-block:: python |
| 80 |
|
| 81 |
# Before |
| 82 |
df = dd.read_csv('myfile.*.csv') |
| 83 |
df = df + 100 |
| 84 |
df = df[df.name == 'Alice'] |
| 85 |
|
| 86 |
# After |
| 87 |
df = dd.read_csv('myfile.*.csv') |
| 88 |
df = df[df.name == 'Alice'] |
| 89 |
df = df + 100 |
| 90 |
|
| 91 |
Dask's high level graphs help us to explicitly encode this structure by storing |
| 92 |
our task graphs in layers with dependencies between layers: |
| 93 |
|
| 94 |
.. code-block:: python |
| 95 |
|
| 96 |
>>> import dask.dataframe as dd |
| 97 |
|
| 98 |
>>> df = dd.read_csv('myfile.*.csv') |
| 99 |
>>> df = df + 100 |
| 100 |
>>> df = df[df.name == 'Alice'] |
| 101 |
|
| 102 |
>>> graph = df.__dask_graph__() |
| 103 |
>>> graph.layers |
| 104 |
{ |
| 105 |
'read-csv': {('read-csv', 0): (pandas.read_csv, 'myfile.0.csv'), |
| 106 |
('read-csv', 1): (pandas.read_csv, 'myfile.1.csv'), |
| 107 |
('read-csv', 2): (pandas.read_csv, 'myfile.2.csv'), |
| 108 |
('read-csv', 3): (pandas.read_csv, 'myfile.3.csv')}, |
| 109 |
|
| 110 |
'add': {('add', 0): (operator.add, ('read-csv', 0), 100), |
| 111 |
('add', 1): (operator.add, ('read-csv', 1), 100), |
| 112 |
('add', 2): (operator.add, ('read-csv', 2), 100), |
| 113 |
('add', 3): (operator.add, ('read-csv', 3), 100)} |
| 114 |
|
| 115 |
'filter': {('filter', 0): (lambda part: part[part.name == 'Alice'], ('add', 0)), |
| 116 |
('filter', 1): (lambda part: part[part.name == 'Alice'], ('add', 1)), |
| 117 |
('filter', 2): (lambda part: part[part.name == 'Alice'], ('add', 2)), |
| 118 |
('filter', 3): (lambda part: part[part.name == 'Alice'], ('add', 3))} |
| 119 |
} |
| 120 |
|
| 121 |
>>> graph.dependencies |
| 122 |
{ |
| 123 |
'read-csv': set(), |
| 124 |
'add': {'read-csv'}, |
| 125 |
'filter': {'add'} |
| 126 |
} |
| 127 |
|
| 128 |
While the DataFrame points to the output layers on which it depends directly: |
| 129 |
|
| 130 |
.. code-block:: python |
| 131 |
|
| 132 |
>>> df.__dask_layers__() |
| 133 |
{'filter'} |
| 134 |
|
| 135 |
|
| 136 |
HighLevelGraphs |
| 137 |
--------------- |
| 138 |
|
| 139 |
The :obj:`HighLevelGraph` object is a ``Mapping`` object composed of other |
| 140 |
sub-``Mappings``, along with a high-level dependency mapping between them: |
| 141 |
|
| 142 |
.. code-block:: python |
| 143 |
|
| 144 |
class HighLevelGraph(Mapping): |
| 145 |
layers: Dict[str, Mapping] |
| 146 |
dependencies: Dict[str, Set[str]] |
| 147 |
|
| 148 |
You can construct a HighLevelGraph explicitly by providing both to the |
| 149 |
constructor: |
| 150 |
|
| 151 |
.. code-block:: python |
| 152 |
|
| 153 |
layers = { |
| 154 |
'read-csv': {('read-csv', 0): (pandas.read_csv, 'myfile.0.csv'), |
| 155 |
('read-csv', 1): (pandas.read_csv, 'myfile.1.csv'), |
| 156 |
('read-csv', 2): (pandas.read_csv, 'myfile.2.csv'), |
| 157 |
('read-csv', 3): (pandas.read_csv, 'myfile.3.csv')}, |
| 158 |
|
| 159 |
'add': {('add', 0): (operator.add, ('read-csv', 0), 100), |
| 160 |
('add', 1): (operator.add, ('read-csv', 1), 100), |
| 161 |
('add', 2): (operator.add, ('read-csv', 2), 100), |
| 162 |
('add', 3): (operator.add, ('read-csv', 3), 100)}, |
| 163 |
|
| 164 |
'filter': {('filter', 0): (lambda part: part[part.name == 'Alice'], ('add', 0)), |
| 165 |
('filter', 1): (lambda part: part[part.name == 'Alice'], ('add', 1)), |
| 166 |
('filter', 2): (lambda part: part[part.name == 'Alice'], ('add', 2)), |
| 167 |
('filter', 3): (lambda part: part[part.name == 'Alice'], ('add', 3))} |
| 168 |
} |
| 169 |
|
| 170 |
dependencies = {'read-csv': set(), |
| 171 |
'add': {'read-csv'}, |
| 172 |
'filter': {'add'}} |
| 173 |
|
| 174 |
graph = HighLevelGraph(layers, dependencies) |
| 175 |
|
| 176 |
This object satisfies the ``Mapping`` interface, and so operates as a normal |
| 177 |
Python dictionary that is the semantic merger of the underlying layers: |
| 178 |
|
| 179 |
.. code-block:: python |
| 180 |
|
| 181 |
>>> len(graph) |
| 182 |
12 |
| 183 |
>>> graph[('read-csv', 0)] |
| 184 |
('read-csv', 0): (pandas.read_csv, 'myfile.0.csv'), |
| 185 |
|
| 186 |
|
| 187 |
API |
| 188 |
--- |
| 189 |
|
| 190 |
.. currentmodule:: dask.highlevelgraph |
| 191 |
|
| 192 |
.. autoclass:: HighLevelGraph |
| 193 |
:members: |
| 194 |
:inherited-members: |
| 195 |
:exclude-members: visualize |
| 196 |
|
| 197 |
.. TODO: Fix graphviz dependency in docs build and remove ``visualize`` from |
| 198 |
exclude-members in the above directive |