|
| 1 | +# Copyright (c) Qualcomm Innovation Center, Inc. |
| 2 | +# All rights reserved |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | + |
| 8 | +import torch |
| 9 | + |
| 10 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 11 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 12 | +from executorch.exir.passes import dead_code_elimination_pass |
| 13 | + |
| 14 | + |
| 15 | +class FuseConsecutiveCast(ExportPass): |
| 16 | + """ |
| 17 | + This pass fuses consecutive cast into one or none to reduce runtime |
| 18 | + overhead. |
| 19 | + To simplify the fuse logic, we ensure each cast node's output has at most 1 cast node |
| 20 | + by cloning cast. |
| 21 | + Example: |
| 22 | + Before clone cast: |
| 23 | + relu -> cast1 ─> cast2 |
| 24 | + |──────> cast3 |
| 25 | +
|
| 26 | + After clone cast: |
| 27 | + relu ─> cast1 ──────> cast2 |
| 28 | + |───> cast4(new) ─> cast3 |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self): |
| 32 | + super().__init__() |
| 33 | + self.op_map = { |
| 34 | + exir_ops.edge.dim_order_ops._to_dim_order_copy.default, |
| 35 | + exir_ops.edge.aten._to_copy.default, |
| 36 | + } |
| 37 | + self.visited = set() |
| 38 | + self.nodes = [] |
| 39 | + |
| 40 | + def _canonicalize_cast( |
| 41 | + self, graph_module: torch.fx.GraphModule |
| 42 | + ) -> torch.fx.GraphModule: |
| 43 | + # replace all i64 cast nodes with i32 version |
| 44 | + graph = graph_module.graph |
| 45 | + for n in graph_module.graph.nodes: |
| 46 | + if n.target in self.op_map and n.meta["val"].dtype == torch.int64: |
| 47 | + users = list(n.users) |
| 48 | + for user in users: |
| 49 | + # bypass graph output node to meet original convention |
| 50 | + if user.op == "output": |
| 51 | + continue |
| 52 | + |
| 53 | + with graph.inserting_after(n): |
| 54 | + cast_node = graph.create_node( |
| 55 | + "call_function", |
| 56 | + exir_ops.edge.aten._to_copy.default, |
| 57 | + n.args, |
| 58 | + kwargs={"dtype": torch.int32}, |
| 59 | + ) |
| 60 | + cast_node.meta = n.meta |
| 61 | + cast_node.meta["val"] = cast_node.meta["val"].to(torch.int32) |
| 62 | + user.replace_input_with(n, cast_node) |
| 63 | + |
| 64 | + graph.eliminate_dead_code() |
| 65 | + |
| 66 | + # clone nodes for future fusion |
| 67 | + for n in graph_module.graph.nodes: |
| 68 | + # make sure we're handling cast node instead of convert node |
| 69 | + if n.target in self.op_map and n.kwargs.get("dtype", None) is not None: |
| 70 | + users = [user for user in list(n.users) if user.target in self.op_map] |
| 71 | + if len(users) > 1: |
| 72 | + for i in range(1, len(users)): |
| 73 | + with graph.inserting_after(n): |
| 74 | + clone_cast_node = graph.create_node( |
| 75 | + "call_function", |
| 76 | + exir_ops.edge.aten._to_copy.default, |
| 77 | + n.args, |
| 78 | + kwargs=n.kwargs, |
| 79 | + ) |
| 80 | + clone_cast_node.meta = n.meta |
| 81 | + users[i].replace_input_with(n, clone_cast_node) |
| 82 | + |
| 83 | + def _traverse(self, node): |
| 84 | + if node in self.visited or node.target not in self.op_map: |
| 85 | + return |
| 86 | + |
| 87 | + self.nodes.append(node) |
| 88 | + self.visited.add(node) |
| 89 | + next_users = [n for n in list(node.users) if n.target in self.op_map] |
| 90 | + |
| 91 | + assert ( |
| 92 | + len(next_users) <= 1 |
| 93 | + ), "Each cast node should have at most 1 cast output node after _clone_cast" |
| 94 | + if not next_users: |
| 95 | + return |
| 96 | + else: |
| 97 | + self._traverse(list(node.users)[0]) |
| 98 | + |
| 99 | + def _fuse(self, graph_module: torch.fx.GraphModule) -> torch.fx.GraphModule: |
| 100 | + for n in graph_module.graph.nodes: |
| 101 | + self._traverse(n) |
| 102 | + # TODO: how to handle following scenario (won't happen for quantized graph) |
| 103 | + # fp -> to(i32) -> to(fp) |
| 104 | + if len(self.nodes) > 1: |
| 105 | + input_node, output_node = self.nodes[0], self.nodes[-1] |
| 106 | + output_node.replace_input_with(output_node.args[0], input_node.args[0]) |
| 107 | + |
| 108 | + # clear current stack |
| 109 | + self.nodes = [] |
| 110 | + |
| 111 | + def call(self, graph_module: torch.fx.GraphModule): |
| 112 | + self._canonicalize_cast(graph_module) |
| 113 | + self._fuse(graph_module) |
| 114 | + graph_module.recompile() |
| 115 | + dead_code_elimination_pass(graph_module) |
| 116 | + return PassResult(graph_module, True) |
0 commit comments