|
| 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 | +from executorch.backends.arm._passes import ArmPass |
| 7 | + |
| 8 | +from executorch.backends.arm._passes.arm_pass_utils import ( |
| 9 | + create_node, |
| 10 | + get_first_fake_tensor, |
| 11 | +) |
| 12 | + |
| 13 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 14 | + |
| 15 | +from executorch.exir.pass_base import PassResult |
| 16 | +from torch.fx import GraphModule, Node |
| 17 | + |
| 18 | + |
| 19 | +class BroadcastArgsPass(ArmPass): |
| 20 | + """ |
| 21 | + Pass to manually broadcast arguments by inserting repeats. |
| 22 | + This is done when more than one arg needs broadcasting. |
| 23 | + """ |
| 24 | + |
| 25 | + targeted_ops = { |
| 26 | + exir_ops.edge.aten.add.Tensor, |
| 27 | + exir_ops.edge.aten.sub.Tensor, |
| 28 | + # mul is indirectly targeting div as div is decompsed to reciprocal + mul |
| 29 | + exir_ops.edge.aten.mul.Tensor, |
| 30 | + } |
| 31 | + |
| 32 | + def call(self, graph_module: GraphModule) -> PassResult: |
| 33 | + for node in graph_module.graph.nodes: |
| 34 | + if node.op != "call_function" or node.target not in self.targeted_ops: |
| 35 | + continue |
| 36 | + |
| 37 | + output_shape = get_first_fake_tensor(node).shape |
| 38 | + nbr_of_broacasts = 0 |
| 39 | + for arg in node.args: |
| 40 | + if not isinstance(arg, Node): |
| 41 | + continue |
| 42 | + |
| 43 | + shape = get_first_fake_tensor(arg).shape |
| 44 | + if shape != output_shape: |
| 45 | + nbr_of_broacasts += 1 |
| 46 | + if nbr_of_broacasts > 1: |
| 47 | + multiples = [ |
| 48 | + int(output_shape[d] / shape[d]) |
| 49 | + for d in range(len(output_shape)) |
| 50 | + ] |
| 51 | + with graph_module.graph.inserting_before(node): |
| 52 | + repeat = create_node( |
| 53 | + graph_module.graph, |
| 54 | + exir_ops.edge.aten.repeat.default, |
| 55 | + args=(arg, multiples), |
| 56 | + kwargs={}, |
| 57 | + from_node=node, |
| 58 | + ) |
| 59 | + node.replace_input_with(arg, repeat) |
| 60 | + |
| 61 | + graph_module.recompile() |
| 62 | + graph_module = super().call(graph_module).graph_module |
| 63 | + return PassResult(graph_module, True) |
0 commit comments