| 1 |
:orphan: |
| 2 |
|
| 3 |
Visualize task graphs |
| 4 |
--------------------- |
| 5 |
|
| 6 |
.. currentmodule:: dask |
| 7 |
|
| 8 |
.. autosummary:: |
| 9 |
visualize |
| 10 |
|
| 11 |
Before executing your computation you might consider visualizing the underlying task graph. |
| 12 |
By looking at the inter-connectedness of tasks |
| 13 |
you can learn more about potential bottlenecks |
| 14 |
where parallelism may not be possible, |
| 15 |
or areas where many tasks depend on each other, |
| 16 |
which may cause a great deal of communication. |
| 17 |
|
| 18 |
Visualize the low level graph |
| 19 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 20 |
|
| 21 |
The ``.visualize`` method and ``dask.visualize`` function works like |
| 22 |
the ``.compute`` method and ``dask.compute`` function, |
| 23 |
except that rather than computing the result, |
| 24 |
they produce an image of the task graph. |
| 25 |
These images are written to files, and if you are within a Jupyter notebook |
| 26 |
context they will also be displayed as cell outputs. |
| 27 |
|
| 28 |
By default the task graph is rendered from top to bottom. |
| 29 |
In the case that you prefer to visualize it from left to right, pass |
| 30 |
``rankdir="LR"`` as a keyword argument to ``.visualize``. |
| 31 |
|
| 32 |
.. code-block:: python |
| 33 |
|
| 34 |
import dask.array as da |
| 35 |
x = da.ones((15, 15), chunks=(5, 5)) |
| 36 |
|
| 37 |
y = x + x.T |
| 38 |
|
| 39 |
# y.compute() |
| 40 |
|
| 41 |
# visualize the low level Dask graph |
| 42 |
y.visualize(filename='transpose.svg') |
| 43 |
|
| 44 |
.. image:: images/transpose.svg |
| 45 |
:alt: Dask low level task graph for adding an array to its transpose |
| 46 |
|
| 47 |
It is often helpful to inspect the task graph before and after graph optimizations |
| 48 |
are applied. You can do that by setting the ``optimize_graph`` keyword. |
| 49 |
So the above example becomes: |
| 50 |
|
| 51 |
.. code-block:: python |
| 52 |
|
| 53 |
import dask.array as da |
| 54 |
x = da.ones((15, 15), chunks=(5, 5)) |
| 55 |
|
| 56 |
y = x + x.T |
| 57 |
|
| 58 |
# visualize the low level Dask graph after optimizations |
| 59 |
y.visualize(filename="transpose_opt.svg", optimize_graph=True) |
| 60 |
|
| 61 |
.. image:: images/transpose_opt.svg |
| 62 |
:alt: Dask low level task graph for adding an array to its transpose. |
| 63 |
Compared to the unoptimized graph it is simpler, as a number of |
| 64 |
the tasks have been fused together. |
| 65 |
|
| 66 |
The ``visualize`` function supports two different graph rendering engines: ``graphviz`` |
| 67 |
(the default), and ``cytoscape``. In order to change the engine that is used, pass the |
| 68 |
name of the engine for the ``engine`` argument to ``visualize``: |
| 69 |
|
| 70 |
.. code-block:: python |
| 71 |
|
| 72 |
import dask.array as da |
| 73 |
x = da.ones((15, 15), chunks=(5, 5)) |
| 74 |
|
| 75 |
y = x + x.T |
| 76 |
|
| 77 |
# visualize the low level Dask graph using cytoscape |
| 78 |
y.visualize(engine="cytoscape") |
| 79 |
|
| 80 |
.. raw:: html |
| 81 |
|
| 82 |
<iframe src="_static/transpose_cyto.html" |
| 83 |
marginwidth="0" marginheight="0" scrolling="no" |
| 84 |
width="100%" height="400" style="border:none"></iframe> |
| 85 |
|
| 86 |
|
| 87 |
You can also set the default visualization engine by setting the ``visualization.engine`` |
| 88 |
configuration option: |
| 89 |
|
| 90 |
.. code-block:: python |
| 91 |
|
| 92 |
import dask.array as da |
| 93 |
x = da.ones((15, 15), chunks=(5, 5)) |
| 94 |
|
| 95 |
y = x + x.T |
| 96 |
|
| 97 |
with dask.config.set({"visualization.engine": "cytoscape"}): |
| 98 |
y.visualize() |
| 99 |
|
| 100 |
|
| 101 |
Note that the both visualization engines require optional dependencies to be installed. |
| 102 |
The ``graphviz`` engine is powered by the `GraphViz <https://www.graphviz.org/>`_ |
| 103 |
system library. This library has a few considerations: |
| 104 |
|
| 105 |
1. You must install both the graphviz system library (with tools like apt-get, yum, or brew) |
| 106 |
*and* the graphviz Python library. |
| 107 |
If you use Conda then you need to install ``python-graphviz``, |
| 108 |
which will bring along the ``graphviz`` system library as a dependency. |
| 109 |
2. Graphviz takes a while on graphs larger than about 100 nodes. |
| 110 |
For large computations you might have to simplify your computation a bit |
| 111 |
for the visualize method to work well. |
| 112 |
|
| 113 |
The ``cytoscape`` engine uses the `Cytoscape <https://js.cytoscape.org/>`_ javascript |
| 114 |
library for rendering, and is driven on the Python side by the ``ipycytoscape`` |
| 115 |
library. Because it doesn't rely on any system libraries, this engine may be easier |
| 116 |
to install than graphviz in some deployment settings. |
| 117 |
|
| 118 |
Visualize the high level graph |
| 119 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 120 |
The low level Dask task graph can be overwhelimg, especially for large computations. |
| 121 |
A more concise alternative is to look at the Dask high level graph instead. |
| 122 |
The high level graph can be visualized using ``.dask.visualize()``. |
| 123 |
|
| 124 |
.. code-block:: python |
| 125 |
|
| 126 |
import dask.array as da |
| 127 |
x = da.ones((15, 15), chunks=(5, 5)) |
| 128 |
y = x + x.T |
| 129 |
|
| 130 |
# visualize the high level Dask graph |
| 131 |
y.dask.visualize(filename='transpose-hlg.svg') |
| 132 |
|
| 133 |
.. image:: images/transpose-hlg-hovertooltip.png |
| 134 |
:alt: Dask high level task graph for adding an array to its transpose |
| 135 |
|
| 136 |
Hovering your mouse above each high level graph label will bring up |
| 137 |
a tooltip with more detailed information about that layer. |
| 138 |
Note that if you save the graph to disk using the ``filename=`` keyword argument |
| 139 |
in ``visualize``, then the tooltips will only be preserved by the SVG image format. |
| 140 |
|
| 141 |
High level graph HTML representation |
| 142 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 143 |
Dask high level graphs also have their own HTML representation, |
| 144 |
which is useful if you like to work with Jupyter notebooks. |
| 145 |
|
| 146 |
.. code-block:: python |
| 147 |
|
| 148 |
import dask.array as da |
| 149 |
x = da.ones((15, 15), chunks=(5, 5)) |
| 150 |
y = x + x.T |
| 151 |
|
| 152 |
y.dask # shows the HTML representation in a Jupyter notebook |
| 153 |
|
| 154 |
.. image:: images/transpose-hlg-html-repr.png |
| 155 |
:alt: Dask high level graph HTML representation |
| 156 |
|
| 157 |
You can click on any of the layer names to expand or collapse more detailed |
| 158 |
information about each layer. |