|
| 1 | +# Copyright 2026 NXP |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import torch |
| 7 | + |
| 8 | +from executorch.backends.nxp.backend.ir.converter.conversion.common import OpsList |
| 9 | +from executorch.backends.nxp.backend.ir.converter.node_converter import ( |
| 10 | + CustomDelegationOptions, |
| 11 | + is_not_qdq_node, |
| 12 | + NodeConverter, |
| 13 | +) |
| 14 | +from executorch.backends.nxp.backend.ir.converter.node_converters.shared.reduce_utils import ( |
| 15 | + convert_axes_from_attribute, |
| 16 | + get_dim_and_handle_io_formats, |
| 17 | + get_reduce_node_attrs, |
| 18 | +) |
| 19 | +from executorch.backends.nxp.backend.ir.tflite_generator.builtin_options import ( |
| 20 | + reduce_max_options, |
| 21 | +) |
| 22 | +from executorch.backends.nxp.backend.neutron_target_spec import NeutronTargetSpec |
| 23 | +from torch.fx import Node |
| 24 | +from torch.fx.passes.infra.partitioner import Partition |
| 25 | +from torch.nn import Parameter |
| 26 | + |
| 27 | + |
| 28 | +class AmaxConverter(NodeConverter): |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def supports_partitioning_result( |
| 32 | + cls, |
| 33 | + node: Node, |
| 34 | + partition_list: list[Partition], |
| 35 | + custom_delegation_options: CustomDelegationOptions, |
| 36 | + neutron_target_spec: NeutronTargetSpec, |
| 37 | + parameters_mapping: dict[str, Parameter], |
| 38 | + ) -> bool: |
| 39 | + dim, keepdim = get_reduce_node_attrs(node) |
| 40 | + input_shape = node.args[0].meta["val"].shape |
| 41 | + |
| 42 | + is_alone_in_partition = cls.is_node_alone_in_partition( |
| 43 | + node, partition_list, filter_fn=is_not_qdq_node |
| 44 | + ) |
| 45 | + |
| 46 | + if is_alone_in_partition and keepdim and all(input_shape[d] == 1 for d in dim): |
| 47 | + # The operator is a no-op, so the Neutron Converter will skip it. If it's the only node in the |
| 48 | + # partition, the graph would end up empty. |
| 49 | + return False |
| 50 | + |
| 51 | + return True |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def _is_supported_on_target( |
| 55 | + node: Node, |
| 56 | + neutron_target_spec: NeutronTargetSpec, |
| 57 | + parameters_mapping: dict[str, Parameter], |
| 58 | + custom_delegation_options: CustomDelegationOptions, |
| 59 | + ) -> bool: |
| 60 | + if not NodeConverter.uses_quantization_type_for_io( |
| 61 | + node, |
| 62 | + supported_types=[torch.int8, torch.uint8], |
| 63 | + input_indices=[0], |
| 64 | + output_indices=[0], |
| 65 | + ): |
| 66 | + return False |
| 67 | + |
| 68 | + return True |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + def _is_supported_in_IR( |
| 72 | + node: Node, |
| 73 | + parameters_mapping: dict[str, Parameter], |
| 74 | + custom_delegation_options: CustomDelegationOptions, |
| 75 | + ) -> bool: |
| 76 | + if not NodeConverter._has_shared_q_params_if_quantized(node): |
| 77 | + return False |
| 78 | + |
| 79 | + return True |
| 80 | + |
| 81 | + def convert(self, node: Node): |
| 82 | + """Convert the 'amax' operator to NeutronIR 'ReduceMax'. |
| 83 | + The ExecuTorch schema is: |
| 84 | + amax( |
| 85 | + Tensor self, |
| 86 | + int[1]? dim, |
| 87 | + bool keepdim=False, |
| 88 | + ) -> Tensor |
| 89 | + """ |
| 90 | + self.assert_convertible(node) |
| 91 | + |
| 92 | + dim, keepdim = get_reduce_node_attrs(node) |
| 93 | + |
| 94 | + t_op = self._create_tflite_op_with_io_tensors(node) |
| 95 | + t_op.builtin_options = reduce_max_options.ReduceMax(keepdim) |
| 96 | + |
| 97 | + ops = OpsList(middle_op=t_op) |
| 98 | + dim = get_dim_and_handle_io_formats(self.builder, ops, dim, keepdim) |
| 99 | + |
| 100 | + convert_axes_from_attribute(t_op, self.builder, dim) |
| 101 | + self.builder.append_operators(ops.flatten()) |
0 commit comments