|
| 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 typing import cast, Set, Type |
| 7 | + |
| 8 | +import torch |
| 9 | +from executorch.backends.arm._passes.arm_pass import ArmPass |
| 10 | +from executorch.backends.arm._passes.arm_pass_utils import create_node |
| 11 | +from executorch.backends.arm._passes.decompose_round_pass import DecomposeRoundPass |
| 12 | +from executorch.backends.arm.constants import DEQUANT_PER_TENSOR_OP, QUANT_PER_TENSOR_OP |
| 13 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 14 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 15 | + |
| 16 | + |
| 17 | +class DecomposeQuantNodesPass(ArmPass): |
| 18 | + """Decomposes quantization nodes into more primitive operations by rewriting the graph |
| 19 | + using the two formulas: |
| 20 | +
|
| 21 | + quantized value = clamp(round(fp32_value / scale) + zero point, qmin, qmax) |
| 22 | +
|
| 23 | + fp32_value = (quantized value - zp) * scale |
| 24 | +
|
| 25 | + For quantization nodes, the pass replaces them with: |
| 26 | +
|
| 27 | + 1. Multiplying the input by the inverse of the scale factor. |
| 28 | + 2. Rounding the result. |
| 29 | + 3. Adding the zero point. |
| 30 | + 4. Clamping the result to [qmin, qmax]. |
| 31 | + 5. Casting to the target data type. |
| 32 | +
|
| 33 | + For dequantization nodes, the pass replaces them with: |
| 34 | +
|
| 35 | + 1. Casting the input to int32. |
| 36 | + 2. Subtracting the zero point. |
| 37 | + 3. Casting to float32. |
| 38 | + 4. Multiplying by the scale factor. |
| 39 | +
|
| 40 | + """ |
| 41 | + |
| 42 | + _passes_required_after: Set[Type[ExportPass]] = {DecomposeRoundPass} |
| 43 | + |
| 44 | + def call(self, graph_module: torch.fx.GraphModule): |
| 45 | + modified = False |
| 46 | + for node in list(graph_module.graph.nodes): |
| 47 | + if node.op != "call_function" or node.target not in ( |
| 48 | + QUANT_PER_TENSOR_OP, |
| 49 | + DEQUANT_PER_TENSOR_OP, |
| 50 | + ): |
| 51 | + continue |
| 52 | + if node.target == DEQUANT_PER_TENSOR_OP and all( |
| 53 | + user.target == QUANT_PER_TENSOR_OP for user in node.users |
| 54 | + ): |
| 55 | + continue |
| 56 | + elif ( |
| 57 | + node.target == QUANT_PER_TENSOR_OP |
| 58 | + and node.all_input_nodes[0].target == DEQUANT_PER_TENSOR_OP |
| 59 | + ): |
| 60 | + continue |
| 61 | + modified = True |
| 62 | + args = node.args |
| 63 | + input_rank = args[0].meta["val"].ndim |
| 64 | + x, scale, zero_point, qmin, qmax, dtype = args |
| 65 | + # Instead of dividing by scale in quantization, we multiply by 1/scale |
| 66 | + # when quantizing. |
| 67 | + scale = cast(float, scale) |
| 68 | + scale = scale if node.target == DEQUANT_PER_TENSOR_OP else 1.0 / scale |
| 69 | + with graph_module.graph.inserting_before(node): |
| 70 | + scale_const = create_node( |
| 71 | + graph_module.graph, |
| 72 | + exir_ops.edge.aten.full.default, |
| 73 | + args=((1,) * input_rank, scale), |
| 74 | + kwargs={"dtype": torch.float32}, |
| 75 | + ) |
| 76 | + zp_const = create_node( |
| 77 | + graph_module.graph, |
| 78 | + exir_ops.edge.aten.full.default, |
| 79 | + args=((1,) * input_rank, zero_point), |
| 80 | + kwargs={ |
| 81 | + "dtype": ( |
| 82 | + torch.float32 |
| 83 | + if node.target == QUANT_PER_TENSOR_OP |
| 84 | + else torch.int32 |
| 85 | + ) |
| 86 | + }, |
| 87 | + ) |
| 88 | + if node.target == QUANT_PER_TENSOR_OP: |
| 89 | + # TODO MLETORCH-1587: Decompose quantization nodes using more integer arithmetic |
| 90 | + scaled = create_node( |
| 91 | + graph_module.graph, |
| 92 | + exir_ops.edge.aten.mul.Tensor, |
| 93 | + args=(x, scale_const), |
| 94 | + from_node=node, |
| 95 | + ) |
| 96 | + rounded = create_node( |
| 97 | + graph_module.graph, |
| 98 | + exir_ops.edge.aten.round.default, |
| 99 | + args=(scaled,), |
| 100 | + from_node=node, |
| 101 | + ) |
| 102 | + shifted = create_node( |
| 103 | + graph_module.graph, |
| 104 | + exir_ops.edge.aten.add.Tensor, |
| 105 | + args=(rounded, zp_const), |
| 106 | + from_node=node, |
| 107 | + ) |
| 108 | + clamped = create_node( |
| 109 | + graph_module.graph, |
| 110 | + exir_ops.edge.aten.clamp.default, |
| 111 | + args=(shifted, float(qmin), float(qmax)), |
| 112 | + from_node=node, |
| 113 | + ) |
| 114 | + quantized = create_node( |
| 115 | + graph_module.graph, |
| 116 | + exir_ops.edge.dim_order_ops._to_dim_order_copy.default, |
| 117 | + args=(clamped,), |
| 118 | + kwargs={"dtype": dtype}, |
| 119 | + from_node=node, |
| 120 | + ) |
| 121 | + output = quantized |
| 122 | + else: |
| 123 | + input_casted_to_zp_dtype = create_node( |
| 124 | + graph_module.graph, |
| 125 | + exir_ops.edge.dim_order_ops._to_dim_order_copy.default, |
| 126 | + args=(x,), |
| 127 | + kwargs={"dtype": torch.int32}, |
| 128 | + from_node=node, |
| 129 | + ) |
| 130 | + shifted = create_node( |
| 131 | + graph_module.graph, |
| 132 | + exir_ops.edge.aten.sub.Tensor, |
| 133 | + args=(input_casted_to_zp_dtype, zp_const), |
| 134 | + from_node=node, |
| 135 | + ) |
| 136 | + casted_to_float = create_node( |
| 137 | + graph_module.graph, |
| 138 | + exir_ops.edge.dim_order_ops._to_dim_order_copy.default, |
| 139 | + args=(shifted,), |
| 140 | + kwargs={"dtype": torch.float32}, |
| 141 | + from_node=node, |
| 142 | + ) |
| 143 | + dequantized = create_node( |
| 144 | + graph_module.graph, |
| 145 | + exir_ops.edge.aten.mul.Tensor, |
| 146 | + args=(casted_to_float, scale_const), |
| 147 | + from_node=node, |
| 148 | + ) |
| 149 | + output = dequantized |
| 150 | + node.replace_all_uses_with(output) |
| 151 | + graph_module.graph.erase_node(node) |
| 152 | + if modified: |
| 153 | + graph_module.graph.eliminate_dead_code() |
| 154 | + graph_module.recompile() |
| 155 | + graph_module = super().call(graph_module).graph_module |
| 156 | + return PassResult(graph_module, modified=modified) |
0 commit comments