|
| 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 Dict |
| 8 | + |
| 9 | +import executorch.backends.qualcomm.python.PyQnnManagerAdaptor as PyQnnManager |
| 10 | + |
| 11 | +import torch |
| 12 | + |
| 13 | +from .node_visitor import NodeVisitor |
| 14 | +from .node_visitor_manager import register_node_visitor |
| 15 | +from .qnn_constants import OpIsNan, QNN_OP_PACKAGE_NAME_QTI_AISW |
| 16 | + |
| 17 | + |
| 18 | +@register_node_visitor |
| 19 | +class IsNan(NodeVisitor): |
| 20 | + target = ["aten.isnan.default"] |
| 21 | + |
| 22 | + def __init__(self, *args) -> None: |
| 23 | + super().__init__(*args) |
| 24 | + |
| 25 | + def define_node( |
| 26 | + self, |
| 27 | + node: torch.fx.Node, |
| 28 | + nodes_to_wrappers: Dict[torch.fx.Node, PyQnnManager.TensorWrapper], |
| 29 | + ) -> PyQnnManager.PyQnnOpWrapper: |
| 30 | + input_node = self.get_node(node.args[0]) |
| 31 | + input_tensor = self.get_tensor(input_node, node) |
| 32 | + |
| 33 | + if input_tensor.dtype not in [torch.float32, torch.float16]: |
| 34 | + warnings.warn( |
| 35 | + "[QNN Delegate Op Builder]: QNN IsNan only supports FP32 or FP16 inputs.", |
| 36 | + stacklevel=1, |
| 37 | + ) |
| 38 | + return None |
| 39 | + |
| 40 | + input_tensor_wrapper = self.define_tensor( |
| 41 | + input_node, |
| 42 | + node, |
| 43 | + self.get_tensor(input_node, node), |
| 44 | + PyQnnManager.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE, |
| 45 | + nodes_to_wrappers, |
| 46 | + ) |
| 47 | + input_tensors = [input_tensor_wrapper] |
| 48 | + |
| 49 | + out_tensor = self.get_tensor(node, node) |
| 50 | + output_tensor_wrapper = self.define_tensor( |
| 51 | + node, |
| 52 | + node, |
| 53 | + out_tensor, |
| 54 | + PyQnnManager.Qnn_TensorType_t.QNN_TENSOR_TYPE_NATIVE, |
| 55 | + nodes_to_wrappers, |
| 56 | + ) |
| 57 | + output_tensors = [output_tensor_wrapper] |
| 58 | + isnan_op = PyQnnManager.PyQnnOpWrapper( |
| 59 | + node.name, |
| 60 | + QNN_OP_PACKAGE_NAME_QTI_AISW, |
| 61 | + OpIsNan.op_name, |
| 62 | + ) |
| 63 | + isnan_op.AddInputTensors(input_tensors) |
| 64 | + isnan_op.AddOutputTensors(output_tensors) |
| 65 | + |
| 66 | + return isnan_op |
0 commit comments