|
| 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 warnings |
| 7 | +from typing import cast, Dict |
| 8 | + |
| 9 | +import executorch.backends.qualcomm.python.PyQnnManagerAdaptor as PyQnnManager |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +import torch |
| 13 | +from executorch.backends.qualcomm.utils.constants import ( |
| 14 | + QCOM_AXIS_ORDER, |
| 15 | + QCOM_DATA, |
| 16 | + QCOM_QUANT_ATTRS, |
| 17 | +) |
| 18 | + |
| 19 | +from .node_visitor import NodeVisitor |
| 20 | +from .node_visitor_manager import register_node_visitor |
| 21 | +from .qnn_constants import OpTopK, QNN_OP_PACKAGE_NAME_QTI_AISW |
| 22 | + |
| 23 | + |
| 24 | +@register_node_visitor |
| 25 | +class Sort(NodeVisitor): |
| 26 | + """Implement aten.sort via QNN TopK with k = dim_size. |
| 27 | +
|
| 28 | + Limitations: |
| 29 | + - Only supports sorting along the last dimension (channel dim in NHWC). |
| 30 | + Sorting on other dimensions will return None (op not delegated). |
| 31 | + - Sort indices are output as int32 by QNN. In quantized models, consuming |
| 32 | + sort indices with ops like gather (which expect int64) may cause dtype mismatches. |
| 33 | + For quantized use cases, prefer consuming only the sorted values output. |
| 34 | + """ |
| 35 | + |
| 36 | + target = ["aten.sort.default"] |
| 37 | + |
| 38 | + def __init__(self, *args) -> None: |
| 39 | + super().__init__(*args) |
| 40 | + |
| 41 | + def define_node( |
| 42 | + self, |
| 43 | + node: torch.fx.Node, |
| 44 | + nodes_to_wrappers: Dict[torch.fx.Node, PyQnnManager.TensorWrapper], |
| 45 | + ) -> PyQnnManager.PyQnnOpWrapper: |
| 46 | + |
| 47 | + input_node = self.get_node(node.args[0]) |
| 48 | + input_tensor = self.get_tensor(input_node, node) |
| 49 | + input_tensor_wrapper = self.define_tensor( |
| 50 | + input_node, |
| 51 | + node, |
| 52 | + input_tensor, |
| 53 | + PyQnnManager.Qnn_TensorType_t.QNN_TENSOR_TYPE_STATIC, |
| 54 | + nodes_to_wrappers, |
| 55 | + ) |
| 56 | + |
| 57 | + # aten.sort signature: sort(input, dim=-1, descending=False) |
| 58 | + dim = cast(int, node.args[1]) if len(node.args) > 1 else -1 |
| 59 | + descending = cast(bool, node.args[2]) if len(node.args) > 2 else False |
| 60 | + |
| 61 | + if dim < 0: |
| 62 | + dim = dim % len(input_tensor.shape) |
| 63 | + if QCOM_AXIS_ORDER in node.meta: |
| 64 | + dim = node.meta[QCOM_AXIS_ORDER].index(dim) |
| 65 | + |
| 66 | + # Sort is TopK with k = full dimension size |
| 67 | + k = input_tensor.shape[dim] |
| 68 | + |
| 69 | + if dim != len(input_tensor.shape) - 1: |
| 70 | + warnings.warn( |
| 71 | + "[QNN Delegate Op Builder]: QNN currently only supports channel as dimension for TopK/Sort.", |
| 72 | + stacklevel=1, |
| 73 | + ) |
| 74 | + return |
| 75 | + |
| 76 | + sort_input_tensors = [input_tensor_wrapper] |
| 77 | + |
| 78 | + output_val_tensor = self.get_tensor(node, node, 0) |
| 79 | + output_idx_tensor = self.get_tensor(node, node, 1).to(torch.int32) |
| 80 | + |
| 81 | + # QNN constraint, topk/sort output_0 requires having the same quant config as input |
| 82 | + node.meta[QCOM_QUANT_ATTRS] = input_node.meta.get(QCOM_QUANT_ATTRS) |
| 83 | + output_val_tensor_wrapper = self.define_tensor( |
| 84 | + node, |
| 85 | + node, |
| 86 | + output_val_tensor, |
| 87 | + PyQnnManager.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE, |
| 88 | + nodes_to_wrappers, |
| 89 | + ) |
| 90 | + |
| 91 | + # sort output_1 is index, do not quantize it. |
| 92 | + node.meta.pop(QCOM_QUANT_ATTRS, None) |
| 93 | + output_index_tensor_wrapper = self.define_tensor( |
| 94 | + node, |
| 95 | + node, |
| 96 | + output_idx_tensor, |
| 97 | + PyQnnManager.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE, |
| 98 | + nodes_to_wrappers, |
| 99 | + wrapper_idx=1, |
| 100 | + ) |
| 101 | + sort_output_tensors = [output_val_tensor_wrapper, output_index_tensor_wrapper] |
| 102 | + |
| 103 | + topk_op = PyQnnManager.PyQnnOpWrapper( |
| 104 | + node.name, |
| 105 | + QNN_OP_PACKAGE_NAME_QTI_AISW, |
| 106 | + OpTopK.op_name, |
| 107 | + ) |
| 108 | + topk_op.AddInputTensors(sort_input_tensors) |
| 109 | + topk_op.AddOutputTensors(sort_output_tensors) |
| 110 | + |
| 111 | + topk_op.AddScalarParam( |
| 112 | + OpTopK.param_k, |
| 113 | + PyQnnManager.Qnn_DataType_t.QNN_DATATYPE_UINT_32, |
| 114 | + {QCOM_DATA: np.uint32(k)}, |
| 115 | + ) |
| 116 | + |
| 117 | + # descending=True in sort corresponds to largest=True in TopK |
| 118 | + topk_op.AddScalarParam( |
| 119 | + OpTopK.param_largest, |
| 120 | + PyQnnManager.Qnn_DataType_t.QNN_DATATYPE_BOOL_8, |
| 121 | + {QCOM_DATA: descending}, |
| 122 | + ) |
| 123 | + |
| 124 | + return topk_op |
0 commit comments