MCPcopy Create free account
hub / github.com/pytorch/executorch / split_graph

Method split_graph

backends/qualcomm/tests/utils.py:864–920  ·  view source on GitHub ↗
(self, division: int)

Source from the content-addressed store, hash-verified

862 return adb
863
864 def split_graph(self, division: int):
865 class SplitGraph(ExportPass):
866 """
867 Split graph based on number of nodes.
868 """
869
870 def __init__(self, division):
871 super().__init__()
872 self.division = division
873
874 def _is_legit_node(self, node):
875 # skip dq_ops for frozen_params
876 return node.op == "call_function" and node.target not in dq_ops
877
878 def _insert_clone(
879 self, graph_module: torch.fx.GraphModule
880 ) -> torch.fx.GraphModule:
881 # Count the total of nodes in the graph
882 num_graph_nodes = 0
883 for node in graph_module.graph.nodes:
884 num_graph_nodes += 1 if node.op == "call_function" else 0
885
886 # Compute how many nodes in one share
887 shares = num_graph_nodes // self.division
888
889 # Insert clone op to split model based on the shares
890 num_graph_nodes = 0
891 for node in graph_module.graph.nodes:
892 if not self._is_legit_node(node):
893 continue
894
895 num_graph_nodes += 1
896 if num_graph_nodes % shares != 0:
897 continue
898
899 with graph_module.graph.inserting_after(node):
900 users = list(node.users.keys())
901 inserted_node = graph_module.graph.create_node(
902 "call_function",
903 exir_ops.edge.dim_order_ops._clone_dim_order.default,
904 (node,),
905 )
906 inserted_node.meta["val"] = node.meta["val"]
907 if "quant_attrs" in node.meta:
908 inserted_node.meta["quant_attrs"] = node.meta["quant_attrs"]
909 for user in users:
910 user.replace_input_with(node, inserted_node)
911
912 def call(self, graph_module: torch.fx.GraphModule):
913 self._insert_clone(graph_module)
914 graph_module.recompile()
915 return PassResult(graph_module, True)
916
917 return SplitGraph, {
918 QCOM_PASS_ACTIVATE_KEY: True,
919 QCOM_PASS_ARGS_KWARGS_DEFAULTS_KEY: {"division": division},
920 }
921

Calls

no outgoing calls