(
ep: ExportedProgram,
graph_module: torch.fx.GraphModule,
patterns: List[torch.fx.GraphModule],
create_replacement_func: CreateReplacementFn,
)
| 96 | |
| 97 | |
| 98 | def create_replacement_for_pattern( |
| 99 | ep: ExportedProgram, |
| 100 | graph_module: torch.fx.GraphModule, |
| 101 | patterns: List[torch.fx.GraphModule], |
| 102 | create_replacement_func: CreateReplacementFn, |
| 103 | ) -> int: |
| 104 | total_replaced = 0 |
| 105 | |
| 106 | for pattern in patterns: |
| 107 | sm = SubgraphMatcher(pattern.graph, ignore_literals=True) |
| 108 | matches = list(sm.match(graph_module.graph)) |
| 109 | |
| 110 | for partition_to_replace in matches: |
| 111 | pattern = create_pattern_match_from_internal_match(partition_to_replace) |
| 112 | create_replacement_func(ep, graph_module, pattern) |
| 113 | total_replaced += 1 |
| 114 | # Remove dead code so they won't be matched again |
| 115 | graph_module.graph.eliminate_dead_code() |
| 116 | |
| 117 | return total_replaced |
| 118 | |
| 119 | |
| 120 | def replace_all_fusable_subgraphs( |
no test coverage detected