|
| 1 | +# Copyright 2025 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import torch |
| 7 | +from executorch.backends.arm._passes.arm_pass_utils import ( |
| 8 | + get_constant_placeholder_kind, |
| 9 | + get_param_tensor, |
| 10 | + is_param_node, |
| 11 | +) |
| 12 | +from executorch.backends.transforms.utils import ( |
| 13 | + create_constant_placeholder, |
| 14 | + delete_constant_placeholder, |
| 15 | +) |
| 16 | +from executorch.exir import ExportedProgram |
| 17 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 18 | + |
| 19 | + |
| 20 | +class FuseEqualPlaceholdersPass(ExportPass): |
| 21 | + """ |
| 22 | + This pass optimizes memory usage by finding constant placeholders |
| 23 | + pointing to identical tensors and fusing them to one single placeholder |
| 24 | + with multiple users. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, exported_program: ExportedProgram): |
| 28 | + self.exported_program = exported_program |
| 29 | + super().__init__() |
| 30 | + |
| 31 | + def call(self, graph_module: torch.fx.GraphModule) -> PassResult: |
| 32 | + modified = False |
| 33 | + const_placeholder_nodes = [] |
| 34 | + for node in graph_module.graph.nodes: |
| 35 | + if is_param_node(self.exported_program, node): |
| 36 | + const_placeholder_nodes.append(node) |
| 37 | + |
| 38 | + while const_placeholder_nodes: |
| 39 | + |
| 40 | + # Find equal tensors |
| 41 | + node1 = const_placeholder_nodes.pop() |
| 42 | + eq_nodes = [node1] |
| 43 | + tensor1 = get_param_tensor(self.exported_program, node1) |
| 44 | + if tensor1 is None: |
| 45 | + continue |
| 46 | + |
| 47 | + for node2 in const_placeholder_nodes: |
| 48 | + tensor2 = get_param_tensor(self.exported_program, node2) |
| 49 | + if tensor2 is None: |
| 50 | + continue |
| 51 | + |
| 52 | + if torch.equal(tensor1, tensor2): |
| 53 | + eq_nodes.append(node2) |
| 54 | + |
| 55 | + if len(eq_nodes) > 1: |
| 56 | + common_name = node1.name + "_common" |
| 57 | + common_kind = get_constant_placeholder_kind( |
| 58 | + self.exported_program, node1 |
| 59 | + ) |
| 60 | + common_persisten_buffer = True |
| 61 | + |
| 62 | + with graph_module.graph.inserting_before(node1): |
| 63 | + common_node = create_constant_placeholder( |
| 64 | + self.exported_program, |
| 65 | + graph_module.graph, |
| 66 | + common_name, |
| 67 | + common_kind, |
| 68 | + tensor1, |
| 69 | + common_persisten_buffer, |
| 70 | + ) |
| 71 | + |
| 72 | + for eq_node in eq_nodes: |
| 73 | + eq_node.replace_all_uses_with(common_node) |
| 74 | + delete_constant_placeholder(self.exported_program, eq_node) |
| 75 | + if eq_node != node1: |
| 76 | + const_placeholder_nodes.remove(eq_node) |
| 77 | + |
| 78 | + modified = True |
| 79 | + |
| 80 | + if modified: |
| 81 | + graph_module.recompile() |
| 82 | + graph_module = super().call(graph_module).graph_module |
| 83 | + return PassResult(graph_module=graph_module, modified=modified) |
0 commit comments