|
| 1 | +# Copyright 2024 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 copy |
| 8 | + |
| 9 | +from typing import Callable, cast, Iterable |
| 10 | + |
| 11 | +from executorch.backends.arm.tosa_quant_utils import QuantArgs |
| 12 | + |
| 13 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 14 | + |
| 15 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 16 | +from torch.fx import GraphModule, Node |
| 17 | + |
| 18 | + |
| 19 | +def get_input_qparams(node: Node) -> dict[int, QuantArgs]: |
| 20 | + """ |
| 21 | + Get the input quantization parameters from a node, set by the 'FoldAndAnnotateQParamsPass'. |
| 22 | + Raises a ValueError if the node doesn't have any parameters set. |
| 23 | + """ |
| 24 | + if "input_qparams" not in node.meta.keys(): |
| 25 | + raise ValueError(f"No input quantization parameter found in node {node}") |
| 26 | + input_qparams = cast(dict[int, QuantArgs], node.meta["input_qparams"]) |
| 27 | + if len(input_qparams) == 0: |
| 28 | + raise ValueError(f"No input quantization parameter found in node {node}") |
| 29 | + return input_qparams |
| 30 | + |
| 31 | + |
| 32 | +def get_output_qparams(node: Node) -> dict[int, QuantArgs]: |
| 33 | + """ |
| 34 | + Get the output quantization parameters from a node, set by the 'FoldAndAnnotateQParamsPass'. |
| 35 | + Raises a ValueError if the node doesn't have any parameters set. |
| 36 | + """ |
| 37 | + if "output_qparams" not in node.meta.keys(): |
| 38 | + raise ValueError(f"No output quantization parameter found in node {node}") |
| 39 | + input_qparams = cast(dict[int, QuantArgs], node.meta["output_qparams"]) |
| 40 | + if len(input_qparams) == 0: |
| 41 | + raise ValueError(f"No output quantization parameter found in node {node}") |
| 42 | + return input_qparams |
| 43 | + |
| 44 | + |
| 45 | +class FoldAndAnnotateQParamsPass(ExportPass): |
| 46 | + """ |
| 47 | + A pass that walks the graph and removes any DQ and Q nodes before and after the target |
| 48 | + node in the supplied list of operators. |
| 49 | + The quantization parameters from the DQ/Q nodes are stored as meta values to be |
| 50 | + accessible for later lowering and serialization passes. |
| 51 | + The assumption is that the quantization annotatation adds DQ nodes for all tensor |
| 52 | + inputs to the target one Q node to the output. |
| 53 | +
|
| 54 | + Example ('executorch_exir_dialects_edge__ops_' prefix removed from operators for readability): |
| 55 | +
|
| 56 | + x_q: "i8[5]" = quantized_decomposed_quantize_per_tensor_default(x, 0.05487706884741783, -128, -128, 127, torch.int8) |
| 57 | +
|
| 58 | + x_dq: "f32[5]" = quantized_decomposed_dequantize_per_tensor_default(x_q, 0.05487706884741783, -128, -128, 127, torch.int8) |
| 59 | + aten_add_tensor: "f32[5]" = ops_aten_add_Tensor(x_dq, x_dq) |
| 60 | + aten_add_tensor_q: "i8[5]" = quantized_decomposed_quantize_per_tensor_default(aten_add_tensor, 0.05487706884741783, -128, -128, 127, torch.int8) |
| 61 | +
|
| 62 | + output_dq: "f32[5]" = quantized_decomposed_dequantize_per_tensor_default(aten_add_tensor_q, 0.05487706884741783, -128, -128, 127, torch.int8) |
| 63 | +
|
| 64 | + Becomes: |
| 65 | + x_q: "i8[5]" = quantized_decomposed_quantize_per_tensor_default(x, 0.05487706884741783, -128, -128, 127, torch.int8) |
| 66 | +
|
| 67 | + aten_add_tensor: "i8[5]" = aten_add_Tensor(x_q, x_q) |
| 68 | +
|
| 69 | + output_dq: "f32[5]" = quantized_decomposed_dequantize_per_tensor_default(aten_add_tensor_q, 0.05487706884741783, -128, -128, 127, torch.int8) |
| 70 | +
|
| 71 | + The quantization parameters for x_dq and aten_add_tensor_q are store in meta for the aten_add_tensor node. |
| 72 | +
|
| 73 | + """ |
| 74 | + |
| 75 | + def __init__(self, targeted_ops: Iterable[Callable]): |
| 76 | + super().__init__() |
| 77 | + self.targeted_ops = targeted_ops |
| 78 | + |
| 79 | + def call(self, graph_module: GraphModule) -> PassResult: |
| 80 | + q_op = exir_ops.edge.quantized_decomposed.quantize_per_tensor.default |
| 81 | + dq_op = exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default |
| 82 | + |
| 83 | + # Loop over the graph nodes and find any node in the 'targeted_ops' list. |
| 84 | + for n in graph_module.graph.nodes: |
| 85 | + n = cast(Node, n) |
| 86 | + if n.op != "call_function" or n.target not in self.targeted_ops: |
| 87 | + continue |
| 88 | + |
| 89 | + # Make sure we haven't already set qparams meta information on the node |
| 90 | + assert "input_qparams" not in n.meta.keys() |
| 91 | + assert "output_qparams" not in n.meta.keys() |
| 92 | + |
| 93 | + # for the inputs and outputs search the graph for quantization info and |
| 94 | + # store the information in a dict with order of the _tensor_ inputs as key, |
| 95 | + # ignoring any other arguments to the target node. |
| 96 | + n.meta["input_qparams"] = {} |
| 97 | + n.meta["output_qparams"] = {} |
| 98 | + for i, arg in enumerate(n.args): |
| 99 | + if not isinstance(arg, Node): |
| 100 | + continue |
| 101 | + if arg.target != dq_op: |
| 102 | + continue |
| 103 | + |
| 104 | + # arg.target for argument i is a dequant node, extract the information |
| 105 | + n.meta["input_qparams"][i] = QuantArgs.from_operator( |
| 106 | + arg.target, arg.args |
| 107 | + ) |
| 108 | + |
| 109 | + # arg.args[0] is the tensor input, replace the input usage |
| 110 | + n.replace_input_with(arg, arg.args[0]) |
| 111 | + graph_module.graph.erase_node(arg) |
| 112 | + |
| 113 | + # Copy the users, since we are modifying it. |
| 114 | + users_copy = copy.copy(n.users) |
| 115 | + for i, user in enumerate(users_copy): |
| 116 | + if user.target != q_op: |
| 117 | + continue |
| 118 | + |
| 119 | + # quantization node found here, store the quantization parameters in meta value |
| 120 | + n.meta["output_qparams"][i] = QuantArgs.from_operator( |
| 121 | + user.target, user.args |
| 122 | + ) |
| 123 | + |
| 124 | + user.replace_all_uses_with(n) |
| 125 | + graph_module.graph.erase_node(user) |
| 126 | + |
| 127 | + # retrace the graph to update the fake tensor types |
| 128 | + graph_module = super().call(graph_module).graph_module |
| 129 | + |
| 130 | + graph_module.recompile() |
| 131 | + return PassResult(graph_module, True) |
0 commit comments