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 7from typing import Dict 8 9import executorch.backends.qualcomm.python.PyQnnWrapperAdaptor as PyQnnWrapper 10 11import numpy as np 12import torch 13from executorch.backends.qualcomm.utils.constants import QCOM_DATA 14 15from .node_visitor import NodeVisitor, register_node_visitor 16from .qnn_constants import OpSpaceToDepth, QNN_OP_PACKAGE_NAME_QTI_AISW 17 18 19@register_node_visitor 20class SpaceToDepthVisitor(NodeVisitor): 21 target = ["aten.pixel_unshuffle.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 input_tensor, 36 PyQnnWrapper.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE, 37 nodes_to_wrappers, 38 is_input_tensor=True, 39 ) 40 41 output_tensor = self.get_tensor(node, node) 42 output_tensor_wrapper = self.define_tensor( 43 node, 44 output_tensor, 45 PyQnnWrapper.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE, 46 nodes_to_wrappers, 47 is_input_tensor=False, 48 ) 49 50 block_size = [] 51 for index in range(1, 3): 52 block_size.append(input_tensor.shape[index] / output_tensor.shape[index]) 53 block_size = np.array(block_size, dtype=np.uint32) 54 block_size_shape = [2] 55 56 space_to_depth_op = PyQnnWrapper.PyQnnOpWrapper( 57 node.name, 58 QNN_OP_PACKAGE_NAME_QTI_AISW, 59 OpSpaceToDepth.op_name, 60 ) 61 space_to_depth_op.AddInputTensors([input_tensor_wrapper]) 62 space_to_depth_op.AddOutputTensors([output_tensor_wrapper]) 63 space_to_depth_op.AddTensorParam( 64 OpSpaceToDepth.param_block_size, 65 PyQnnWrapper.Qnn_DataType_t.QNN_DATATYPE_UINT_32, 66 len(block_size.shape), 67 block_size_shape, 68 block_size, 69 True, 70 ) 71 space_to_depth_op.AddScalarParam( 72 OpSpaceToDepth.param_mode, 73 PyQnnWrapper.Qnn_DataType_t.QNN_DATATYPE_UINT_32, 74 {QCOM_DATA: np.uint32(OpSpaceToDepth.Mode.CRD)}, 75 ) 76 77 return space_to_depth_op 78