|
| 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 | +import _operator |
| 8 | +from typing import Dict, Tuple |
| 9 | + |
| 10 | +import torch |
| 11 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 12 | + |
| 13 | +from .utils import copy_nn_module_stack |
| 14 | + |
| 15 | + |
| 16 | +class DecomposeWrapWithAutocast(ExportPass): |
| 17 | + """ |
| 18 | + Decompose the _higher_order_ops WrapWithAutocast |
| 19 | + """ |
| 20 | + |
| 21 | + def __init__(self) -> None: |
| 22 | + super().__init__() |
| 23 | + |
| 24 | + def _get_submod( |
| 25 | + self, gm: torch.fx.GraphModule, node: torch.fx.Node |
| 26 | + ) -> Tuple[torch.fx.GraphModule, str]: |
| 27 | + for a in node.args: |
| 28 | + if isinstance(a, torch.fx.Node) and "submod" in a.target: |
| 29 | + return getattr(gm, a.target), a.target |
| 30 | + |
| 31 | + def _replace_output( |
| 32 | + self, wwac_node: torch.fx.Node, output_node: torch.fx.Node, remap: Dict |
| 33 | + ): |
| 34 | + for user in wwac_node.users.copy(): |
| 35 | + arg_idx = 0 |
| 36 | + is_user_getitem = False |
| 37 | + |
| 38 | + if user.target == _operator.getitem: |
| 39 | + arg_idx = user.args[1] |
| 40 | + is_user_getitem = True |
| 41 | + |
| 42 | + user.replace_input_with( |
| 43 | + wwac_node, |
| 44 | + remap[output_node.args[0][arg_idx]], |
| 45 | + ) |
| 46 | + |
| 47 | + if is_user_getitem: |
| 48 | + for user_user in user.users.copy(): |
| 49 | + user_user.replace_input_with(user, user.args[0]) |
| 50 | + |
| 51 | + def _replace(self, gm: torch.fx.GraphModule) -> None: |
| 52 | + graph = gm.graph |
| 53 | + for node in graph.nodes: |
| 54 | + if isinstance(node.target, torch._higher_order_ops.wrap.WrapWithAutocast): |
| 55 | + submod, submod_name = self._get_submod(gm, node) |
| 56 | + n_args = node.args |
| 57 | + input_submod = n_args[4] |
| 58 | + decomposed_module = submod |
| 59 | + with graph.inserting_before(node): |
| 60 | + # remap is used to map original node values to new node values, |
| 61 | + # which ensures that reference to nodes are correctly updated in the new graph |
| 62 | + # remap = {"expand_1": node.args[5], "to_4": node.args[6]} |
| 63 | + remap = {n_args[i].name: n_args[i] for i in range(5, len(n_args))} |
| 64 | + |
| 65 | + for decomposed_node in decomposed_module.graph.nodes: |
| 66 | + copy_nn_module_stack(node, decomposed_node) |
| 67 | + # no need to copy existent 'output' |
| 68 | + if decomposed_node.op == "output": |
| 69 | + self._replace_output(node, decomposed_node, remap) |
| 70 | + # no need to copy existent placeholders |
| 71 | + elif decomposed_node.op == "placeholder": |
| 72 | + # replace node map from string to graph node |
| 73 | + remap[decomposed_node] = remap.pop(decomposed_node.name) |
| 74 | + else: |
| 75 | + remap[decomposed_node] = graph.node_copy( |
| 76 | + decomposed_node, |
| 77 | + arg_transform=lambda x, remap=remap: remap[x], |
| 78 | + ) |
| 79 | + |
| 80 | + graph.erase_node(node) |
| 81 | + |
| 82 | + graph.erase_node(input_submod) |
| 83 | + |
| 84 | + def call(self, graph_module: torch.fx.GraphModule) -> PassResult: |
| 85 | + self._replace(graph_module) |
| 86 | + graph_module.graph.eliminate_dead_code() |
| 87 | + graph_module.recompile() |
| 88 | + return PassResult(graph_module, True) |
0 commit comments