|
| 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 Dict |
| 7 | + |
| 8 | +import executorch.backends.qualcomm.python.PyQnnWrapperAdaptor as PyQnnWrapper |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +import torch |
| 12 | + |
| 13 | +from executorch.backends.qualcomm.utils.constants import QCOM_AXIS_ORDER |
| 14 | + |
| 15 | +from .node_visitor import NodeVisitor |
| 16 | +from .node_visitor_manager import register_node_visitor |
| 17 | +from .qnn_constants import OpStridedSlice, QNN_OP_PACKAGE_NAME_QTI_AISW |
| 18 | + |
| 19 | + |
| 20 | +@register_node_visitor |
| 21 | +class Flip(NodeVisitor): |
| 22 | + target = ["aten.flip.default"] |
| 23 | + |
| 24 | + def __init__(self, *args) -> None: |
| 25 | + super().__init__(*args) |
| 26 | + |
| 27 | + def define_node( |
| 28 | + self, |
| 29 | + node: torch.fx.Node, |
| 30 | + nodes_to_wrappers: Dict[torch.fx.Node, PyQnnWrapper.TensorWrapper], |
| 31 | + ) -> PyQnnWrapper.PyQnnOpWrapper: |
| 32 | + input_node = self.get_node(node.args[0]) |
| 33 | + input_tensor = self.get_tensor(input_node, node) |
| 34 | + tensor_type = PyQnnWrapper.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE |
| 35 | + |
| 36 | + input_tensor_wrapper = self.define_tensor( |
| 37 | + input_node, |
| 38 | + node, |
| 39 | + input_tensor, |
| 40 | + tensor_type, |
| 41 | + nodes_to_wrappers, |
| 42 | + ) |
| 43 | + |
| 44 | + output_tensor = self.get_tensor(node, node) |
| 45 | + output_tensor_wrapper = self.define_tensor( |
| 46 | + node, |
| 47 | + node, |
| 48 | + output_tensor, |
| 49 | + PyQnnWrapper.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE, |
| 50 | + nodes_to_wrappers, |
| 51 | + ) |
| 52 | + ranges = [] |
| 53 | + |
| 54 | + dims = node.args[1] |
| 55 | + if QCOM_AXIS_ORDER in node.meta: |
| 56 | + dims = [node.meta[QCOM_AXIS_ORDER].index(dim) for dim in dims] |
| 57 | + |
| 58 | + for dim, size in enumerate(output_tensor.shape): |
| 59 | + if dim in dims: |
| 60 | + ranges.extend([size - 1, -1, -1]) |
| 61 | + else: |
| 62 | + ranges.extend([0, size, 1]) |
| 63 | + |
| 64 | + range_shape = [input_tensor.dim(), 3] |
| 65 | + stride_slice_op = PyQnnWrapper.PyQnnOpWrapper( |
| 66 | + node.name, |
| 67 | + QNN_OP_PACKAGE_NAME_QTI_AISW, |
| 68 | + OpStridedSlice.op_name, |
| 69 | + ) |
| 70 | + stride_slice_op.AddInputTensors([input_tensor_wrapper]) |
| 71 | + stride_slice_op.AddOutputTensors([output_tensor_wrapper]) |
| 72 | + stride_slice_op.AddTensorParam( |
| 73 | + OpStridedSlice.param_ranges, |
| 74 | + PyQnnWrapper.Qnn_DataType_t.QNN_DATATYPE_INT_32, |
| 75 | + len(range_shape), |
| 76 | + range_shape, |
| 77 | + np.array(ranges, dtype=np.int32), |
| 78 | + True, |
| 79 | + ) |
| 80 | + |
| 81 | + return stride_slice_op |
0 commit comments