|
| 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 | +from typing import cast, Dict |
| 7 | + |
| 8 | +import executorch.backends.qualcomm.python.PyQnnWrapperAdaptor as PyQnnWrapper |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +import torch |
| 12 | +from executorch.backends.qualcomm.utils.constants import QCOM_DATA |
| 13 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 14 | + |
| 15 | +from .node_visitor import NodeVisitor, register_node_visitor |
| 16 | +from .qnn_constants import OpCast, OpGatherElements, QNN_OP_PACKAGE_NAME_QTI_AISW |
| 17 | + |
| 18 | + |
| 19 | +@register_node_visitor |
| 20 | +class Gather(NodeVisitor): |
| 21 | + target = ["aten.gather.default"] |
| 22 | + |
| 23 | + def __init__(self, *args) -> None: |
| 24 | + super().__init__(*args) |
| 25 | + |
| 26 | + def define_node( |
| 27 | + self, |
| 28 | + node: torch.fx.Node, |
| 29 | + nodes_to_wrappers: Dict[torch.fx.Node, PyQnnWrapper.TensorWrapper], |
| 30 | + ) -> PyQnnWrapper.PyQnnOpWrapper: |
| 31 | + input_node = node.args[0] |
| 32 | + input_tensor = self.get_tensor(input_node, node) |
| 33 | + input_tensor_wrapper = self.define_tensor( |
| 34 | + input_node, |
| 35 | + node, |
| 36 | + input_tensor, |
| 37 | + PyQnnWrapper.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE, |
| 38 | + nodes_to_wrappers, |
| 39 | + ) |
| 40 | + |
| 41 | + dim = cast(int, node.args[1]) |
| 42 | + |
| 43 | + indices_node = node.args[2] |
| 44 | + indices_tensor = self.get_tensor(indices_node, node) |
| 45 | + indices_tensor_wrapper = self.define_tensor( |
| 46 | + indices_node, |
| 47 | + node, |
| 48 | + indices_tensor, |
| 49 | + PyQnnWrapper.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE, |
| 50 | + nodes_to_wrappers, |
| 51 | + ) |
| 52 | + |
| 53 | + cast_node = self.edge_program.graph.create_node( |
| 54 | + "call_function", |
| 55 | + exir_ops.edge.aten._to_copy.default, |
| 56 | + (indices_node,), |
| 57 | + {"dtype": torch.int32}, |
| 58 | + ) |
| 59 | + cast_node.meta["val"] = indices_node.meta["val"].to(torch.int32) |
| 60 | + cast_tensor = self.get_tensor(cast_node, node) |
| 61 | + cast_tensor_wrapper = self.define_tensor( |
| 62 | + cast_node, |
| 63 | + node, |
| 64 | + cast_tensor, |
| 65 | + PyQnnWrapper.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE, |
| 66 | + nodes_to_wrappers, |
| 67 | + ) |
| 68 | + # graph is not allowed to be modified in partition stage |
| 69 | + # erase it here to prevent lowering failure |
| 70 | + self.edge_program.graph.erase_node(cast_node) |
| 71 | + cast_op = PyQnnWrapper.PyQnnOpWrapper( |
| 72 | + f"{node.name}_cast_i64_to_i32", QNN_OP_PACKAGE_NAME_QTI_AISW, OpCast.op_name |
| 73 | + ) |
| 74 | + cast_op.AddInputTensors([indices_tensor_wrapper]) |
| 75 | + cast_op.AddOutputTensors([cast_tensor_wrapper]) |
| 76 | + |
| 77 | + gather_input_tensors = [input_tensor_wrapper, cast_tensor_wrapper] |
| 78 | + output_tensor = self.get_tensor(node, node) |
| 79 | + output_tensor_wrapper = self.define_tensor( |
| 80 | + node, |
| 81 | + node, |
| 82 | + output_tensor, |
| 83 | + PyQnnWrapper.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE, |
| 84 | + nodes_to_wrappers, |
| 85 | + ) |
| 86 | + gather_output_tensors = [output_tensor_wrapper] |
| 87 | + |
| 88 | + gather_op = PyQnnWrapper.PyQnnOpWrapper( |
| 89 | + node.name, |
| 90 | + QNN_OP_PACKAGE_NAME_QTI_AISW, |
| 91 | + OpGatherElements.op_name, |
| 92 | + ) |
| 93 | + gather_op.AddInputTensors(gather_input_tensors) |
| 94 | + gather_op.AddOutputTensors(gather_output_tensors) |
| 95 | + gather_op.AddScalarParam( |
| 96 | + OpGatherElements.param_axis, |
| 97 | + PyQnnWrapper.Qnn_DataType_t.QNN_DATATYPE_UINT_32, |
| 98 | + {QCOM_DATA: np.uint32(dim)}, |
| 99 | + ) |
| 100 | + |
| 101 | + return [cast_op, gather_op] |
0 commit comments