|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# Copyright 2026 Arm Limited and/or its affiliates. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | +# |
| 8 | +from typing import Set, Type |
| 9 | + |
| 10 | +import torch |
| 11 | +from executorch.backends.arm._passes import ArmPass |
| 12 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 13 | +from torch._decomp import get_decompositions |
| 14 | +from torch._ops import OpOverload |
| 15 | +from torch.fx.experimental.proxy_tensor import make_fx |
| 16 | + |
| 17 | + |
| 18 | +class GetDecompositionPass(ArmPass): |
| 19 | + |
| 20 | + _passes_required_after: Set[Type[ExportPass]] = set() |
| 21 | + |
| 22 | + targeted_ops: list[OpOverload] = [] |
| 23 | + |
| 24 | + def __init__(self, tfa_pass=False, *args, **kwargs): |
| 25 | + super().__init__(tfa_pass, *args, **kwargs) |
| 26 | + |
| 27 | + self.__decomposition = None |
| 28 | + |
| 29 | + if type(self) is GetDecompositionPass: |
| 30 | + raise TypeError( |
| 31 | + "Base class GetDecompositionPass cannot be instantiated directly." |
| 32 | + ) |
| 33 | + |
| 34 | + def _skip_pass(self, input_tensors: list) -> bool: |
| 35 | + return False |
| 36 | + |
| 37 | + def call(self, graph_module: torch.fx.GraphModule) -> PassResult: # noqa: C901 |
| 38 | + modified = False |
| 39 | + for node in graph_module.graph.nodes: |
| 40 | + if ( |
| 41 | + node.op != "call_function" |
| 42 | + or node.target not in self.targeted_ops |
| 43 | + or not self.allowed_to_transform(node.meta) |
| 44 | + ): |
| 45 | + continue |
| 46 | + |
| 47 | + input_tensors = [] |
| 48 | + for arg in node.args: |
| 49 | + if hasattr(arg, "meta"): |
| 50 | + input_tensors.append(arg.meta["val"]) |
| 51 | + |
| 52 | + elif isinstance(arg, int): |
| 53 | + input_tensors.append(arg) |
| 54 | + |
| 55 | + if self._skip_pass(input_tensors): |
| 56 | + continue |
| 57 | + |
| 58 | + decomposition = ( |
| 59 | + self.__decomposition |
| 60 | + if self.__decomposition is not None |
| 61 | + else get_decompositions(self.targeted_ops) |
| 62 | + ) |
| 63 | + |
| 64 | + # refer to pytorch/test/test_decomp.py |
| 65 | + decomposed_module = make_fx( |
| 66 | + node.target, |
| 67 | + decomposition_table=decomposition, # type: ignore[arg-type] |
| 68 | + tracing_mode="fake", |
| 69 | + _allow_non_fake_inputs=False, |
| 70 | + )(*input_tensors) |
| 71 | + |
| 72 | + with graph_module.graph.inserting_before(node): |
| 73 | + name_to_input_tensor_map = {} |
| 74 | + for i, arg in enumerate(node.args): |
| 75 | + name_to_input_tensor_map[f"arg{i}_1"] = arg |
| 76 | + |
| 77 | + decomposed_node_to_subgraph_node = {} |
| 78 | + last_decomposed_node = None |
| 79 | + # Create a mapping from input nodes in decomposed module to original nodes. |
| 80 | + # In decomposed module, there are only input tensors for placeholder op. |
| 81 | + for decomposed_node in decomposed_module.graph.nodes: |
| 82 | + if decomposed_node.op == "placeholder": |
| 83 | + decomposed_node_to_subgraph_node[decomposed_node] = ( |
| 84 | + name_to_input_tensor_map[decomposed_node.name] |
| 85 | + ) |
| 86 | + |
| 87 | + if decomposed_node.op == "output": |
| 88 | + last_decomposed_node = decomposed_node.args[0] |
| 89 | + |
| 90 | + # Copy node from decompose graph module |
| 91 | + for decomposed_node in decomposed_module.graph.nodes: |
| 92 | + decomposed_node.meta["nn_module_stack"] = node.meta.get( |
| 93 | + "nn_module_stack" |
| 94 | + ) |
| 95 | + if decomposed_node.op == "placeholder": |
| 96 | + continue |
| 97 | + |
| 98 | + if ( |
| 99 | + decomposed_node.op == "output" |
| 100 | + and last_decomposed_node is not None |
| 101 | + ): |
| 102 | + for user in node.users.copy(): |
| 103 | + user.replace_input_with( |
| 104 | + node, |
| 105 | + decomposed_node_to_subgraph_node[last_decomposed_node], |
| 106 | + ) |
| 107 | + continue |
| 108 | + |
| 109 | + subgraph_node = graph_module.graph.node_copy( |
| 110 | + decomposed_node, |
| 111 | + arg_transform=lambda x: decomposed_node_to_subgraph_node[ # noqa: B023 |
| 112 | + x |
| 113 | + ], |
| 114 | + ) |
| 115 | + subgraph_node.meta["source_fn_stack"] = [ |
| 116 | + (subgraph_node, subgraph_node.target) |
| 117 | + ] |
| 118 | + decomposed_node_to_subgraph_node[decomposed_node] = subgraph_node |
| 119 | + |
| 120 | + graph_module.graph.erase_node(node) |
| 121 | + |
| 122 | + modified = True |
| 123 | + if modified: |
| 124 | + graph_module.graph.eliminate_dead_code() |
| 125 | + graph_module.recompile() |
| 126 | + return PassResult(graph_module, modified) |
0 commit comments