Pattern: Visualize DAG during development

The example shows how to iteratively develop and visualize your deployment graph. For a runnable DAG, we will show both full and partial DAG depending on your choice of root node.

Please ensure do install dependencies in order to generate visualizations sudo apt-get install -y graphviz and pip install -U pydot.

Code

import ray
from ray import serve
from ray.serve.deployment_graph import InputNode

ray.init()


@serve.deployment
class Model:
    def __init__(self, weight):
        self.weight = weight

    def forward(self, input):
        return input + self.weight


@serve.deployment
def combine(output_1, output_2, kwargs_output=0):
    return output_1 + output_2 + kwargs_output


with InputNode() as user_input:
    m1 = Model.bind(1)
    m2 = Model.bind(2)
    m1_output = m1.forward.bind(user_input[0])
    m2_output = m2.forward.bind(user_input[1])
    dag = combine.bind(m1_output, m2_output, kwargs_output=user_input[2])

# Partial DAG visualization
graph = ray.dag.vis_utils._dag_to_dot(m1_output)
to_string = graph.to_string()
print(to_string)

# Entire DAG visualization
graph = ray.dag.vis_utils._dag_to_dot(dag)
to_string = graph.to_string()
print(to_string)

Outputs

Note

The node of user choice will become the root of the graph for both execution as well as visualization, where non-reachable nodes from root will be ignored regardless if they appeared in user code.

In the development phase, when we picked m1_output as the root, we can see a visualization of the underlying execution path that’s partial of the entire graph.

pic

Similarly, when we choose the final dag output, we will capture all nodes used in execution as they’re reachable from the root.

pic

Tip

If you run the code above within Jupyter notebook, we will automatically display it within cell. Otherwise you can either print the dot file as string and render it in graphviz tools such as https://dreampuf.github.io/GraphvizOnline, or save it as .dot file on disk with your choice of path.