|
| 1 | +# Copyright 2025 Arm Limited and/or its affiliates. |
| 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 torch |
| 8 | +from executorch.backends.arm._passes.arm_pass_utils import ( |
| 9 | + create_node, |
| 10 | + get_first_fake_tensor, |
| 11 | + insert_q_dq_pair, |
| 12 | +) |
| 13 | +from executorch.backends.arm.tosa_quant_utils import dq_op, q_op |
| 14 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 15 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 16 | +from torch.fx import Node |
| 17 | + |
| 18 | + |
| 19 | +class ConvertMmToBmmPass(ExportPass): |
| 20 | + """ |
| 21 | + This pass converts a MM node to a BMM one and turns input and output tensors |
| 22 | + from rank 2 to rank 3. The TOSA specification requires rank 3. The graph is |
| 23 | + modified to do the following: |
| 24 | + 1) Unsqueeze input tensors to rank 3. |
| 25 | + 2) Convert MM node to BMM. |
| 26 | + 3) Squeeze output tensor to rank 2. |
| 27 | + """ |
| 28 | + |
| 29 | + def call(self, graph_module: torch.fx.GraphModule): |
| 30 | + modified_graph = False |
| 31 | + graph = graph_module.graph |
| 32 | + node_list = graph.find_nodes( |
| 33 | + op="call_function", target=exir_ops.edge.aten.mm.default |
| 34 | + ) |
| 35 | + for node in node_list: |
| 36 | + # Unsqueeze input tensors to rank 3 |
| 37 | + for input_node in node.args: |
| 38 | + if not isinstance(input_node, Node): |
| 39 | + continue |
| 40 | + |
| 41 | + shape = get_first_fake_tensor(input_node).shape |
| 42 | + rank = len(shape) |
| 43 | + if rank != 2: |
| 44 | + raise RuntimeError(f"Input tensor has rank {rank}, must be 2") |
| 45 | + |
| 46 | + with graph.inserting_before(node): |
| 47 | + unsqueeze_before = create_node( |
| 48 | + graph, exir_ops.edge.aten.unsqueeze_copy.default |
| 49 | + ) |
| 50 | + unsqueeze_before.args = ( |
| 51 | + input_node, # Input is node's original input |
| 52 | + 0, |
| 53 | + ) |
| 54 | + node.replace_input_with(input_node, unsqueeze_before) |
| 55 | + |
| 56 | + # If Quantized we must insert unsqueeze --> q --> dq --> node |
| 57 | + if input_node.target == dq_op: |
| 58 | + q_params = input_node.args[1:] |
| 59 | + insert_q_dq_pair(graph, unsqueeze_before, q_params) |
| 60 | + |
| 61 | + # Replace mm node with bmm |
| 62 | + with graph.inserting_before(node): |
| 63 | + bmm_node = create_node( |
| 64 | + graph, |
| 65 | + exir_ops.edge.aten.bmm.default, |
| 66 | + ) |
| 67 | + bmm_node.args = node.args |
| 68 | + node.replace_all_uses_with(bmm_node) |
| 69 | + graph.erase_node(node) |
| 70 | + |
| 71 | + # Unsqueeze output tensor to rank 3 |
| 72 | + with graph.inserting_after(bmm_node): |
| 73 | + squeeze_after = create_node( |
| 74 | + graph, |
| 75 | + exir_ops.edge.aten.squeeze_copy.dims, |
| 76 | + ) |
| 77 | + squeeze_after.args = ( |
| 78 | + bmm_node, |
| 79 | + [0], |
| 80 | + ) |
| 81 | + original_users = [ |
| 82 | + user for user in bmm_node.users if user != squeeze_after |
| 83 | + ] |
| 84 | + for user in original_users: |
| 85 | + user.replace_input_with(bmm_node, squeeze_after) |
| 86 | + |
| 87 | + # If quantized, insert mm --> q --> dq --> squeeze |
| 88 | + if all(original_user.target == q_op for original_user in original_users): |
| 89 | + q_params = original_users[0].args[1:] |
| 90 | + insert_q_dq_pair(graph, bmm_node, q_params) |
| 91 | + |
| 92 | + modified_graph = True |
| 93 | + |
| 94 | + if modified_graph: |
| 95 | + graph_module.recompile() |
| 96 | + graph_module = super().call(graph_module).graph_module |
| 97 | + |
| 98 | + return PassResult(graph_module, modified_graph) |
0 commit comments