onnx_diagnostic.helpers.graph_helper

class onnx_diagnostic.helpers.graph_helper.GraphRendering(proto: FunctionProto | GraphProto | ModelProto)[source][source]

Helpers to renders a graph.

Parameters:

proto – model or graph to render.

classmethod build_node_edges(nodes: Sequence[NodeProto]) Set[Tuple[int, int]][source][source]

Builds the list of edges between nodes.

classmethod computation_order(nodes: Sequence[NodeProto], existing: List[str] | None = None, start: int = 1) List[int][source][source]

Returns the soonest a node can be computed, every node can assume all nodes with a lower number exists. Every node with a higher number must wait for the previous one.

Parameters:
  • nodes – list of nodes

  • existing – existing before any computation starts

  • start – lower number

Returns:

computation order

classmethod graph_positions(nodes: Sequence[NodeProto], order: List[int], existing: List[str] | None = None) List[Tuple[int, int]][source][source]

Returns positions on a plan for every node in a graph. The function minimizes the number of lines crossing each others. It goes forward, every line is optimized depending on what is below. It could be improved with more iterations.

Parameters:
Returns:

list of tuple( row, column)

property input_names: List[str]

Returns the list of input names.

property nodes: List[NodeProto]

Returns the list of nodes

property output_names: List[str]

Returns the list of output names.

property start_names: List[NodeProto]

Returns the list of known names, inputs and initializer

text_edge(grid: List[List[str]], p1: Tuple[int, int], p2: Tuple[int, int], mode: str = 'square')[source][source]

Prints inplace an edge in a grid. The text is centered.

Parameters:
  • grid – grid

  • p1 – first position

  • p2 – second position

  • mode'square' is the only supported value

classmethod text_grid(grid: List[List[str]], position: Tuple[int, int], text: str)[source][source]

Prints inplace a text in a grid. The text is centered.

Parameters:
  • grid – grid

  • position – position

  • text – text to print

classmethod text_positions(nodes: Sequence[NodeProto], positions: List[Tuple[int, int]]) List[Tuple[int, int]][source][source]

Returns positions for the nodes assuming it is rendered into text.

Parameters:
Returns:

text positions

text_rendering(prefix='') str[source][source]

Renders a model in text.

<<<

import textwrap
import onnx
import onnx.helper as oh
from onnx_diagnostic.helpers.graph_helper import GraphRendering

TFLOAT = onnx.TensorProto.FLOAT

proto = oh.make_model(
    oh.make_graph(
        [
            oh.make_node("Add", ["X", "Y"], ["xy"]),
            oh.make_node("Neg", ["Y"], ["ny"]),
            oh.make_node("Mul", ["xy", "ny"], ["a"]),
            oh.make_node("Mul", ["a", "Y"], ["Z"]),
        ],
        "-nd-",
        [
            oh.make_tensor_value_info("X", TFLOAT, ["a", "b", "c"]),
            oh.make_tensor_value_info("Y", TFLOAT, ["a", "b", "c"]),
        ],
        [oh.make_tensor_value_info("Z", TFLOAT, ["a", "b", "c"])],
    ),
    opset_imports=[oh.make_opsetid("", 18)],
    ir_version=9,
)
graph = GraphRendering(proto)
text = textwrap.dedent(graph.text_rendering()).strip("\n")
print(text)

>>>

    X              Y
    |              |
    └------┬-------┴---┬---┐
           |           |   |
          Add          |  Neg
           |           |   |
           └-------┬---┼---┘
                   |   |
                  Mul  |
                   |   |
                   └---┴---┐
                           |
                          Mul
                           |
                           └------┐
                                  |
                                  Z