|
| 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 | +import torch |
| 7 | +from executorch.backends.qualcomm.builders.node_visitor import q_ops |
| 8 | +from executorch.backends.qualcomm.utils.constants import QCOM_QUANT_ATTRS |
| 9 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 10 | +from torch.fx.passes.utils.source_matcher_utils import get_source_partitions |
| 11 | + |
| 12 | +from .utils import get_quant_attrs |
| 13 | + |
| 14 | + |
| 15 | +class AnnotateAdaptiveAvgPool1D(ExportPass): |
| 16 | + """ |
| 17 | + Add "quant_attrs" to graph nodes' meta from the QDQ information |
| 18 | + generated after quantization process. |
| 19 | + adaptive_avg_pool1d got decomposed to unsqueeze -> adaptive_avg_pool2d -> squeeze |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, edge_program: torch.export.ExportedProgram): |
| 23 | + super(AnnotateAdaptiveAvgPool1D, self).__init__() |
| 24 | + self.edge_program = edge_program |
| 25 | + |
| 26 | + def _annotate_adaptive_avg_pool1d(self, graph_module: torch.fx.GraphModule): |
| 27 | + partitions = get_source_partitions( |
| 28 | + graph_module.graph, [torch.ops.aten.adaptive_avg_pool1d.default] |
| 29 | + ) |
| 30 | + for src_partitions in partitions.values(): |
| 31 | + for src_partition in src_partitions: |
| 32 | + output = src_partition.output_nodes[0] |
| 33 | + if (list(output.users)[0].target) in q_ops: |
| 34 | + quant_attrs = get_quant_attrs( |
| 35 | + self.edge_program, list(output.users)[0] |
| 36 | + ) |
| 37 | + for n in src_partition.nodes: |
| 38 | + n.meta[QCOM_QUANT_ATTRS] = quant_attrs.copy() |
| 39 | + |
| 40 | + def call(self, graph_module: torch.fx.GraphModule): |
| 41 | + self._annotate_adaptive_avg_pool1d(graph_module) |
| 42 | + graph_module.recompile() |
| 43 | + return PassResult(graph_module, True) |
0 commit comments