| 1 |
.. _optimization: |
| 2 |
|
| 3 |
Optimization |
| 4 |
============ |
| 5 |
|
| 6 |
Performance can be significantly improved in different contexts by making |
| 7 |
small optimizations on the Dask graph before calling the scheduler. |
| 8 |
|
| 9 |
The ``dask.optimization`` module contains several functions to transform graphs |
| 10 |
in a variety of useful ways. In most cases, users won't need to interact with |
| 11 |
these functions directly, as specialized subsets of these transforms are done |
| 12 |
automatically in the Dask collections (``dask.array``, ``dask.bag``, and |
| 13 |
``dask.dataframe``). However, users working with custom graphs or computations |
| 14 |
may find that applying these methods results in substantial speedups. |
| 15 |
|
| 16 |
In general, there are two goals when doing graph optimizations: |
| 17 |
|
| 18 |
1. Simplify computation |
| 19 |
2. Improve parallelism |
| 20 |
|
| 21 |
Simplifying computation can be done on a graph level by removing unnecessary |
| 22 |
tasks (``cull``), or on a task level by replacing expensive operations with |
| 23 |
cheaper ones (``RewriteRule``). |
| 24 |
|
| 25 |
Parallelism can be improved by reducing |
| 26 |
inter-task communication, whether by fusing many tasks into one (``fuse``), or |
| 27 |
by inlining cheap operations (``inline``, ``inline_functions``). |
| 28 |
|
| 29 |
Below, we show an example walking through the use of some of these to optimize |
| 30 |
a task graph. |
| 31 |
|
| 32 |
Example |
| 33 |
------- |
| 34 |
|
| 35 |
Suppose you had a custom Dask graph for doing a word counting task: |
| 36 |
|
| 37 |
.. code-block:: python |
| 38 |
|
| 39 |
>>> def print_and_return(string): |
| 40 |
... print(string) |
| 41 |
... return string |
| 42 |
|
| 43 |
>>> def format_str(count, val, nwords): |
| 44 |
... return (f'word list has {count} occurrences of ' |
| 45 |
... f'{val}, out of {nwords} words') |
| 46 |
|
| 47 |
>>> dsk = {'words': 'apple orange apple pear orange pear pear', |
| 48 |
... 'nwords': (len, (str.split, 'words')), |
| 49 |
... 'val1': 'orange', |
| 50 |
... 'val2': 'apple', |
| 51 |
... 'val3': 'pear', |
| 52 |
... 'count1': (str.count, 'words', 'val1'), |
| 53 |
... 'count2': (str.count, 'words', 'val2'), |
| 54 |
... 'count3': (str.count, 'words', 'val3'), |
| 55 |
... 'format1': (format_str, 'count1', 'val1', 'nwords'), |
| 56 |
... 'format2': (format_str, 'count2', 'val2', 'nwords'), |
| 57 |
... 'format3': (format_str, 'count3', 'val3', 'nwords'), |
| 58 |
... 'print1': (print_and_return, 'format1'), |
| 59 |
... 'print2': (print_and_return, 'format2'), |
| 60 |
... 'print3': (print_and_return, 'format3')} |
| 61 |
|
| 62 |
.. image:: images/optimize_dask1.svg |
| 63 |
:width: 65 % |
| 64 |
:alt: The original non-optimized Dask task graph. |
| 65 |
|
| 66 |
Here we are counting the occurrence of the words ``'orange``, ``'apple'``, and |
| 67 |
``'pear'`` in the list of words, formatting an output string reporting the |
| 68 |
results, printing the output, and then returning the output string. |
| 69 |
|
| 70 |
To perform the computation, we first remove unnecessary components from the |
| 71 |
graph using the ``cull`` function and then pass the Dask graph and the desired |
| 72 |
output keys to a scheduler ``get`` function: |
| 73 |
|
| 74 |
.. code-block:: python |
| 75 |
|
| 76 |
>>> from dask.threaded import get |
| 77 |
>>> from dask.optimization import cull |
| 78 |
|
| 79 |
>>> outputs = ['print1', 'print2'] |
| 80 |
>>> dsk1, dependencies = cull(dsk, outputs) # remove unnecessary tasks from the graph |
| 81 |
|
| 82 |
>>> results = get(dsk1, outputs) |
| 83 |
word list has 2 occurrences of apple, out of 7 words |
| 84 |
word list has 2 occurrences of orange, out of 7 words |
| 85 |
|
| 86 |
As can be seen above, the scheduler computed only the requested outputs |
| 87 |
(``'print3'`` was never computed). This is because we called the |
| 88 |
``dask.optimization.cull`` function, which removes the unnecessary tasks from |
| 89 |
the graph. |
| 90 |
|
| 91 |
Culling is part of the default optimization pass of almost all collections. |
| 92 |
Often you want to call it somewhat early to reduce the amount of work done in |
| 93 |
later steps: |
| 94 |
|
| 95 |
.. code-block:: python |
| 96 |
|
| 97 |
>>> from dask.optimization import cull |
| 98 |
>>> outputs = ['print1', 'print2'] |
| 99 |
>>> dsk1, dependencies = cull(dsk, outputs) |
| 100 |
|
| 101 |
.. image:: images/optimize_dask2.svg |
| 102 |
:width: 50 % |
| 103 |
:alt: The Dask task graph after culling tasks for optimization. |
| 104 |
|
| 105 |
Looking at the task graph above, there are multiple accesses to constants such |
| 106 |
as ``'val1'`` or ``'val2'`` in the Dask graph. These can be inlined into the |
| 107 |
tasks to improve efficiency using the ``inline`` function. For example: |
| 108 |
|
| 109 |
.. code-block:: python |
| 110 |
|
| 111 |
>>> from dask.optimization import inline |
| 112 |
>>> dsk2 = inline(dsk1, dependencies=dependencies) |
| 113 |
>>> results = get(dsk2, outputs) |
| 114 |
word list has 2 occurrences of apple, out of 7 words |
| 115 |
word list has 2 occurrences of orange, out of 7 words |
| 116 |
|
| 117 |
.. image:: images/optimize_dask3.svg |
| 118 |
:width: 40 % |
| 119 |
:alt: The Dask task graph after inlining for optimization. |
| 120 |
|
| 121 |
Now we have two sets of *almost* linear task chains. The only link between them |
| 122 |
is the word counting function. For cheap operations like this, the |
| 123 |
serialization cost may be larger than the actual computation, so it may be |
| 124 |
faster to do the computation more than once, rather than passing the results to |
| 125 |
all nodes. To perform this function inlining, the ``inline_functions`` function |
| 126 |
can be used: |
| 127 |
|
| 128 |
.. code-block:: python |
| 129 |
|
| 130 |
>>> from dask.optimization import inline_functions |
| 131 |
>>> dsk3 = inline_functions(dsk2, outputs, [len, str.split], |
| 132 |
... dependencies=dependencies) |
| 133 |
>>> results = get(dsk3, outputs) |
| 134 |
word list has 2 occurrences of apple, out of 7 words |
| 135 |
word list has 2 occurrences of orange, out of 7 words |
| 136 |
|
| 137 |
.. image:: images/optimize_dask4.svg |
| 138 |
:width: 30 % |
| 139 |
:alt: The Dask task graph after inlining functions for optimization. |
| 140 |
|
| 141 |
Now we have a set of purely linear tasks. We'd like to have the scheduler run |
| 142 |
all of these on the same worker to reduce data serialization between workers. |
| 143 |
One option is just to merge these linear chains into one big task using the |
| 144 |
``fuse`` function: |
| 145 |
|
| 146 |
.. code-block:: python |
| 147 |
|
| 148 |
>>> from dask.optimization import fuse |
| 149 |
>>> dsk4, dependencies = fuse(dsk3) |
| 150 |
>>> results = get(dsk4, outputs) |
| 151 |
word list has 2 occurrences of apple, out of 7 words |
| 152 |
word list has 2 occurrences of orange, out of 7 words |
| 153 |
|
| 154 |
.. image:: images/optimize_dask5.svg |
| 155 |
:width: 30 % |
| 156 |
:alt: The Dask task graph after fusing tasks for optimization. |
| 157 |
|
| 158 |
|
| 159 |
Putting it all together: |
| 160 |
|
| 161 |
.. code-block:: python |
| 162 |
|
| 163 |
>>> def optimize_and_get(dsk, keys): |
| 164 |
... dsk1, deps = cull(dsk, keys) |
| 165 |
... dsk2 = inline(dsk1, dependencies=deps) |
| 166 |
... dsk3 = inline_functions(dsk2, keys, [len, str.split], |
| 167 |
... dependencies=deps) |
| 168 |
... dsk4, deps = fuse(dsk3) |
| 169 |
... return get(dsk4, keys) |
| 170 |
|
| 171 |
>>> optimize_and_get(dsk, outputs) |
| 172 |
word list has 2 occurrences of apple, out of 7 words |
| 173 |
word list has 2 occurrences of orange, out of 7 words |
| 174 |
|
| 175 |
|
| 176 |
In summary, the above operations accomplish the following: |
| 177 |
|
| 178 |
1. Removed tasks unnecessary for the desired output using ``cull`` |
| 179 |
2. Inlined constants using ``inline`` |
| 180 |
3. Inlined cheap computations using ``inline_functions``, improving parallelism |
| 181 |
4. Fused linear tasks together to ensure they run on the same worker using ``fuse`` |
| 182 |
|
| 183 |
As stated previously, these optimizations are already performed automatically |
| 184 |
in the Dask collections. Users not working with custom graphs or computations |
| 185 |
should rarely need to directly interact with them. |
| 186 |
|
| 187 |
These are just a few of the optimizations provided in ``dask.optimization``. For |
| 188 |
more information, see the API below. |
| 189 |
|
| 190 |
|
| 191 |
Rewrite Rules |
| 192 |
------------- |
| 193 |
|
| 194 |
For context based optimizations, ``dask.rewrite`` provides functionality for |
| 195 |
pattern matching and term rewriting. This is useful for replacing expensive |
| 196 |
computations with equivalent, cheaper computations. For example, Dask Array |
| 197 |
uses the rewrite functionality to replace series of array slicing operations |
| 198 |
with a more efficient single slice. |
| 199 |
|
| 200 |
The interface to the rewrite system consists of two classes: |
| 201 |
|
| 202 |
1. ``RewriteRule(lhs, rhs, vars)`` |
| 203 |
|
| 204 |
Given a left-hand-side (``lhs``), a right-hand-side (``rhs``), and a set of |
| 205 |
variables (``vars``), a rewrite rule declaratively encodes the following |
| 206 |
operation: |
| 207 |
|
| 208 |
``lhs -> rhs if task matches lhs over variables`` |
| 209 |
|
| 210 |
2. ``RuleSet(*rules)`` |
| 211 |
|
| 212 |
A collection of rewrite rules. The design of ``RuleSet`` class allows for |
| 213 |
efficient "many-to-one" pattern matching, meaning that there is minimal |
| 214 |
overhead for rewriting with multiple rules in a rule set. |
| 215 |
|
| 216 |
|
| 217 |
Example |
| 218 |
~~~~~~~ |
| 219 |
|
| 220 |
Here we create two rewrite rules expressing the following mathematical transformations: |
| 221 |
|
| 222 |
1. ``a + a -> 2*a`` |
| 223 |
2. ``a * a -> a**2`` |
| 224 |
|
| 225 |
where ``'a'`` is a variable: |
| 226 |
|
| 227 |
.. code-block:: python |
| 228 |
|
| 229 |
>>> from dask.rewrite import RewriteRule, RuleSet |
| 230 |
>>> from operator import add, mul, pow |
| 231 |
|
| 232 |
>>> variables = ('a',) |
| 233 |
|
| 234 |
>>> rule1 = RewriteRule((add, 'a', 'a'), (mul, 'a', 2), variables) |
| 235 |
|
| 236 |
>>> rule2 = RewriteRule((mul, 'a', 'a'), (pow, 'a', 2), variables) |
| 237 |
|
| 238 |
>>> rs = RuleSet(rule1, rule2) |
| 239 |
|
| 240 |
The ``RewriteRule`` objects describe the desired transformations in a |
| 241 |
declarative way, and the ``RuleSet`` builds an efficient automata for applying |
| 242 |
that transformation. Rewriting can then be done using the ``rewrite`` method: |
| 243 |
|
| 244 |
.. code-block:: python |
| 245 |
|
| 246 |
>>> rs.rewrite((add, 5, 5)) |
| 247 |
(mul, 5, 2) |
| 248 |
|
| 249 |
>>> rs.rewrite((mul, 5, 5)) |
| 250 |
(pow, 5, 2) |
| 251 |
|
| 252 |
>>> rs.rewrite((mul, (add, 3, 3), (add, 3, 3))) |
| 253 |
(pow, (mul, 3, 2), 2) |
| 254 |
|
| 255 |
The whole task is traversed by default. If you only want to apply a transform |
| 256 |
to the top-level of the task, you can pass in ``strategy='top_level'`` as shown: |
| 257 |
|
| 258 |
.. code-block:: python |
| 259 |
|
| 260 |
# Transforms whole task |
| 261 |
>>> rs.rewrite((sum, [(add, 3, 3), (mul, 3, 3)])) |
| 262 |
(sum, [(mul, 3, 2), (pow, 3, 2)]) |
| 263 |
|
| 264 |
# Only applies to top level, no transform occurs |
| 265 |
>>> rs.rewrite((sum, [(add, 3, 3), (mul, 3, 3)]), strategy='top_level') |
| 266 |
(sum, [(add, 3, 3), (mul, 3, 3)]) |
| 267 |
|
| 268 |
The rewriting system provides a powerful abstraction for transforming |
| 269 |
computations at a task level. Again, for many users, directly interacting with |
| 270 |
these transformations will be unnecessary. |
| 271 |
|
| 272 |
|
| 273 |
Keyword Arguments |
| 274 |
----------------- |
| 275 |
|
| 276 |
Some optimizations take optional keyword arguments. To pass keywords from the |
| 277 |
compute call down to the right optimization, prepend the keyword with the name |
| 278 |
of the optimization. For example, to send a ``keys=`` keyword argument to the |
| 279 |
``fuse`` optimization from a compute call, use the ``fuse_keys=`` keyword: |
| 280 |
|
| 281 |
.. code-block:: python |
| 282 |
|
| 283 |
def fuse(dsk, keys=None): |
| 284 |
... |
| 285 |
|
| 286 |
x.compute(fuse_keys=['x', 'y', 'z']) |
| 287 |
|
| 288 |
|
| 289 |
Customizing Optimization |
| 290 |
------------------------ |
| 291 |
|
| 292 |
Dask defines a default optimization strategy for each collection type (Array, |
| 293 |
Bag, DataFrame, Delayed). However, different applications may have different |
| 294 |
needs. To address this variability of needs, you can construct your own custom |
| 295 |
optimization function and use it instead of the default. An optimization |
| 296 |
function takes in a task graph and list of desired keys and returns a new |
| 297 |
task graph: |
| 298 |
|
| 299 |
.. code-block:: python |
| 300 |
|
| 301 |
def my_optimize_function(dsk, keys): |
| 302 |
new_dsk = {...} |
| 303 |
return new_dsk |
| 304 |
|
| 305 |
You can then register this optimization class against whichever collection type |
| 306 |
you prefer and it will be used instead of the default scheme: |
| 307 |
|
| 308 |
.. code-block:: python |
| 309 |
|
| 310 |
with dask.config.set(array_optimize=my_optimize_function): |
| 311 |
x, y = dask.compute(x, y) |
| 312 |
|
| 313 |
You can register separate optimization functions for different collections, or |
| 314 |
you can register ``None`` if you do not want particular types of collections to |
| 315 |
be optimized: |
| 316 |
|
| 317 |
.. code-block:: python |
| 318 |
|
| 319 |
with dask.config.set(array_optimize=my_optimize_function, |
| 320 |
dataframe_optimize=None, |
| 321 |
delayed_optimize=my_other_optimize_function): |
| 322 |
... |
| 323 |
|
| 324 |
You do not need to specify all collections. Collections will default to their |
| 325 |
standard optimization scheme (which is usually a good choice). |
| 326 |
|
| 327 |
|
| 328 |
API |
| 329 |
--- |
| 330 |
|
| 331 |
.. currentmodule:: dask.optimization |
| 332 |
|
| 333 |
**Top level optimizations** |
| 334 |
|
| 335 |
.. autosummary:: |
| 336 |
cull |
| 337 |
fuse |
| 338 |
inline |
| 339 |
inline_functions |
| 340 |
|
| 341 |
**Utility functions** |
| 342 |
|
| 343 |
.. autosummary:: |
| 344 |
functions_of |
| 345 |
|
| 346 |
**Rewrite Rules** |
| 347 |
|
| 348 |
.. currentmodule:: dask.rewrite |
| 349 |
|
| 350 |
.. autosummary:: |
| 351 |
RewriteRule |
| 352 |
RuleSet |
| 353 |
|
| 354 |
|
| 355 |
Definitions |
| 356 |
~~~~~~~~~~~ |
| 357 |
|
| 358 |
.. currentmodule:: dask.optimization |
| 359 |
|
| 360 |
.. autofunction:: cull |
| 361 |
.. autofunction:: fuse |
| 362 |
.. autofunction:: inline |
| 363 |
.. autofunction:: inline_functions |
| 364 |
|
| 365 |
.. autofunction:: functions_of |
| 366 |
|
| 367 |
.. currentmodule:: dask.rewrite |
| 368 |
|
| 369 |
.. autofunction:: RewriteRule |
| 370 |
.. autofunction:: RuleSet |