Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tools/model_explorer_circle] Add graph nodes in circle model #14341

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tools/model_explorer_circle/src/circle_adapter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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]}