|
| 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 | + |
| 7 | +import logging |
| 8 | + |
| 9 | +import executorch.backends.cortex_m.ops.operators # noqa: F401 |
| 10 | + |
| 11 | +import torch |
| 12 | +from executorch.backends.arm._passes.quant_args import QuantArgs |
| 13 | + |
| 14 | +from executorch.backends.cortex_m.passes.passes_utils import quantize_val |
| 15 | + |
| 16 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 17 | +from executorch.exir.pass_base import ExportPass |
| 18 | +from torch.fx import GraphModule, Node |
| 19 | +from torch.fx.passes.infra.pass_manager import PassResult |
| 20 | + |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +class DecomposeHardswishPass(ExportPass): |
| 25 | + """ |
| 26 | + Decomposes hardswish like |
| 27 | +
|
| 28 | + hardswish(x) = x * (clamp(x, -3, 3) + 3)/6 |
| 29 | +
|
| 30 | + where the add and division is implemented by modifying the quantization parameters similar |
| 31 | + to hardsigmoid in the activation_fusion_pass. Note that this pass assumes |
| 32 | + that the output range of the preceding op is already clamped to [-3, inf] during |
| 33 | + quantization by the clamp_hardswish_pass, removing the need for the negative clamp. |
| 34 | + """ |
| 35 | + |
| 36 | + TARGETS = { |
| 37 | + exir_ops.edge.aten.hardswish.default, |
| 38 | + } |
| 39 | + |
| 40 | + FUSE_OPS = { |
| 41 | + exir_ops.edge.aten.linear.default, |
| 42 | + exir_ops.edge.aten.convolution.default, |
| 43 | + } |
| 44 | + |
| 45 | + def call(self, graph_module: GraphModule) -> PassResult: |
| 46 | + modified = False |
| 47 | + nodes_to_erase: list[Node] = [] |
| 48 | + |
| 49 | + for node in list(graph_module.graph.nodes): |
| 50 | + if node.op != "call_function" or node.target not in self.TARGETS: |
| 51 | + continue |
| 52 | + |
| 53 | + input_node = node.args[0] |
| 54 | + if ( |
| 55 | + input_node.op != "call_function" |
| 56 | + or input_node.target not in self.FUSE_OPS |
| 57 | + ): |
| 58 | + logger.warning( |
| 59 | + f"Cannot fuse activation {node.name} as input node {input_node.name} is not a supported fused activation op." |
| 60 | + ) |
| 61 | + continue |
| 62 | + if len(input_node.users.values()) > 1: |
| 63 | + logger.warning( |
| 64 | + f"Cannot fuse activation {node.name} as input node {input_node.name} has multiple users." |
| 65 | + ) |
| 66 | + continue |
| 67 | + |
| 68 | + input_quant_dict = input_node.meta.get("output_qparams", [None])[ |
| 69 | + 0 |
| 70 | + ]._asdict() |
| 71 | + scale = input_quant_dict["scale"] |
| 72 | + zero_point = input_quant_dict["zp"] |
| 73 | + qmin = input_quant_dict["qmin"] |
| 74 | + qmax = input_quant_dict["qmax"] |
| 75 | + |
| 76 | + # Create min node |
| 77 | + with graph_module.graph.inserting_after(input_node): |
| 78 | + clamp_node = graph_module.graph.create_node( |
| 79 | + "call_function", |
| 80 | + target=exir_ops.edge.aten.minimum.default, |
| 81 | + args=( |
| 82 | + input_node, |
| 83 | + torch.tensor( |
| 84 | + quantize_val(3, scale, zero_point, qmin, qmax), |
| 85 | + dtype=torch.int8, |
| 86 | + ), |
| 87 | + ), |
| 88 | + kwargs={}, |
| 89 | + ) |
| 90 | + clamp_node.meta = input_node.meta.copy() |
| 91 | + |
| 92 | + # Create mul node |
| 93 | + with graph_module.graph.inserting_after(clamp_node): |
| 94 | + mul_node = graph_module.graph.create_node( |
| 95 | + "call_function", |
| 96 | + target=exir_ops.edge.aten.mul.Tensor, |
| 97 | + args=(input_node, clamp_node), |
| 98 | + kwargs={}, |
| 99 | + ) |
| 100 | + mul_node.meta = node.meta.copy() |
| 101 | + |
| 102 | + mul_quant_dict = node.meta["input_qparams"][0]._asdict() |
| 103 | + |
| 104 | + mul_quant_dict_shifted = mul_quant_dict.copy() |
| 105 | + mul_quant_dict_shifted["zp"] = mul_quant_dict_shifted["zp"] - round( |
| 106 | + 3 / (mul_quant_dict_shifted["scale"]) |
| 107 | + ) |
| 108 | + |
| 109 | + output_quant_dict = node.meta["output_qparams"][0]._asdict() |
| 110 | + output_quant_dict["scale"] = output_quant_dict["scale"] * 6 |
| 111 | + |
| 112 | + node.meta["input_qparams"][0] = QuantArgs(**mul_quant_dict) |
| 113 | + mul_node.meta["input_qparams"][1] = QuantArgs(**mul_quant_dict_shifted) |
| 114 | + mul_node.meta["output_qparams"][0] = QuantArgs(**output_quant_dict) |
| 115 | + |
| 116 | + node.replace_all_uses_with(mul_node) |
| 117 | + nodes_to_erase.append(node) |
| 118 | + modified = True |
| 119 | + |
| 120 | + for node in nodes_to_erase: |
| 121 | + graph_module.graph.erase_node(node) |
| 122 | + |
| 123 | + if modified: |
| 124 | + graph_module.graph.eliminate_dead_code() |
| 125 | + graph_module.recompile() |
| 126 | + |
| 127 | + return PassResult(graph_module, modified) |
0 commit comments