From 8e2d3cb1ab7681795454203478b119c968bc5309 Mon Sep 17 00:00:00 2001 From: Jonghwa Lee Date: Fri, 22 Nov 2024 13:42:18 +0900 Subject: [PATCH] [tools/model_explorer_circle] Add graph nodes in circle model (#14341) It adds input, output and operators in circle model to model explorer graph as a separted nodes. ONE-DCO-1.0-Signed-off-by: Jonghwa Lee --- .../src/circle_adapter/main.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tools/model_explorer_circle/src/circle_adapter/main.py b/tools/model_explorer_circle/src/circle_adapter/main.py index c1d7a945df9..820fefdf0a6 100644 --- a/tools/model_explorer_circle/src/circle_adapter/main.py +++ b/tools/model_explorer_circle/src/circle_adapter/main.py @@ -30,6 +30,10 @@ class CircleAdapter(Adapter): def __init__(self): super().__init__() self.model = None + self.dict_opcode_to_name = { + v: k + for k, v in circle_schema.BuiltinOperator.__dict__.items() + } def load_model(self, model_path: str) -> None: """Load the model from the given path.""" @@ -38,10 +42,40 @@ def load_model(self, model_path: str) -> None: self.model = circle_schema.ModelT.InitFromObj(model_) + def opcode_to_name(self, opcode: int) -> str: + """Convert the opcode to its name.""" + return self.dict_opcode_to_name[opcode] + + def build_graph(self, me_graph: graph_builder.Graph) -> None: + """Build the graph using the model.""" + + sub_graph = self.model.subgraphs[0] + + # Create Input nodes + input_id = len(sub_graph.operators) + me_node = graph_builder.GraphNode(id=f'{input_id}', + label="GraphInputs", + namespace="GraphInputs") + me_graph.nodes.append(me_node) + + # Create operator nodes + for idx, op in enumerate(sub_graph.operators): + name = self.opcode_to_name( + self.model.operatorCodes[op.opcodeIndex].builtinCode) + me_node = graph_builder.GraphNode(id=f'{idx}', label=name) + me_graph.nodes.append(me_node) + + # Create Output nodes + me_node = graph_builder.GraphNode(id=f'{len(me_graph.nodes)}', + label="GraphOutputs", + namespace="GraphOutputs") + me_graph.nodes.append(me_node) + def convert(self, model_path: str, settings: Dict) -> ModelExplorerGraphs: """Convert the model to a set of graphs.""" graph = graph_builder.Graph(id='main') self.load_model(model_path) + self.build_graph(graph) return {'graphs': [graph]}