|
| 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 torch |
| 8 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 9 | +from executorch.exir.dialects.edge._ops import EdgeOpOverload |
| 10 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 11 | + |
| 12 | +from .utils import copy_meta, get_const_node |
| 13 | + |
| 14 | + |
| 15 | +class DecomposeAddmm(ExportPass): |
| 16 | + """ |
| 17 | + Decompose addmm into mm + add (with optional mul for non-unit alpha/beta). |
| 18 | + addmm(bias, input, mat2, beta=1, alpha=1) = beta * bias + alpha * (input @ mat2) |
| 19 | +
|
| 20 | + For the common case (alpha=1, beta=1): addmm(bias, input, mat2) = mm(input, mat2) + bias |
| 21 | +
|
| 22 | + Note: This pass serves as a fallback for standalone addmm nodes that are NOT |
| 23 | + handled by the ExecuTorch-provided pass AddmmToLinearTransform. |
| 24 | + Any remaining addmm nodes (e.g., with non-transposed mat2) are decomposed here into mm + add. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self): |
| 28 | + super().__init__() |
| 29 | + self.addmm_targets = { |
| 30 | + torch.ops.aten.addmm.default, |
| 31 | + exir_ops.edge.aten.addmm.default, |
| 32 | + } |
| 33 | + |
| 34 | + def call(self, graph_module: torch.fx.GraphModule): |
| 35 | + graph = graph_module.graph |
| 36 | + |
| 37 | + for node in list(graph.nodes): |
| 38 | + if node.op == "call_function" and node.target in self.addmm_targets: |
| 39 | + is_edge = isinstance(node.target, EdgeOpOverload) |
| 40 | + bias_node = node.args[0] |
| 41 | + input_node = node.args[1] |
| 42 | + mat2_node = node.args[2] |
| 43 | + # kwargs beta and alpha default to 1 |
| 44 | + beta = node.kwargs.get("beta", 1) |
| 45 | + alpha = node.kwargs.get("alpha", 1) |
| 46 | + |
| 47 | + mm_op = ( |
| 48 | + exir_ops.edge.aten.mm.default |
| 49 | + if is_edge |
| 50 | + else torch.ops.aten.mm.default |
| 51 | + ) |
| 52 | + add_op = ( |
| 53 | + exir_ops.edge.aten.add.Tensor |
| 54 | + if is_edge |
| 55 | + else torch.ops.aten.add.Tensor |
| 56 | + ) |
| 57 | + mul_op = ( |
| 58 | + exir_ops.edge.aten.mul.Tensor |
| 59 | + if is_edge |
| 60 | + else torch.ops.aten.mul.Tensor |
| 61 | + ) |
| 62 | + |
| 63 | + meta = node.meta |
| 64 | + |
| 65 | + with graph.inserting_before(node): |
| 66 | + # mm_result = input @ mat2 |
| 67 | + mm_node = graph.create_node( |
| 68 | + "call_function", mm_op, (input_node, mat2_node) |
| 69 | + ) |
| 70 | + mm_node.meta = copy_meta(meta) |
| 71 | + |
| 72 | + if alpha != 1: |
| 73 | + alpha_node = get_const_node( |
| 74 | + graph, |
| 75 | + graph_module, |
| 76 | + f"{node.name}_alpha", |
| 77 | + alpha, |
| 78 | + mm_node, |
| 79 | + ) |
| 80 | + mm_scaled = graph.create_node( |
| 81 | + "call_function", mul_op, (mm_node, alpha_node) |
| 82 | + ) |
| 83 | + mm_scaled.meta = copy_meta(meta) |
| 84 | + mm_result = mm_scaled |
| 85 | + else: |
| 86 | + mm_result = mm_node |
| 87 | + |
| 88 | + if beta != 1: |
| 89 | + beta_const = get_const_node( |
| 90 | + graph, |
| 91 | + graph_module, |
| 92 | + f"{node.name}_beta", |
| 93 | + beta, |
| 94 | + bias_node, |
| 95 | + ) |
| 96 | + bias_scaled = graph.create_node( |
| 97 | + "call_function", mul_op, (bias_node, beta_const) |
| 98 | + ) |
| 99 | + bias_scaled.meta = copy_meta(meta) |
| 100 | + bias_result = bias_scaled |
| 101 | + else: |
| 102 | + bias_result = bias_node |
| 103 | + |
| 104 | + # result = mm_result + bias |
| 105 | + add_node = graph.create_node( |
| 106 | + "call_function", add_op, (mm_result, bias_result) |
| 107 | + ) |
| 108 | + add_node.meta = copy_meta(meta) |
| 109 | + |
| 110 | + for user in node.users.copy(): |
| 111 | + user.replace_input_with(node, add_node) |
| 112 | + |
| 113 | + graph.eliminate_dead_code() |
| 114 | + graph_module.recompile() |
| 115 | + return PassResult(graph_module, True) |
0 commit comments