|
| 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 | + NodeConverter, |
| 12 | +) |
| 13 | +from executorch.backends.nxp.backend.ir.converter.node_converters.shared.reduce_utils import ( |
| 14 | + convert_axes_from_attribute, |
| 15 | + get_dim_and_handle_io_formats, |
| 16 | + get_reduce_node_attrs, |
| 17 | +) |
| 18 | +from executorch.backends.nxp.backend.ir.tflite_generator.builtin_options import ( |
| 19 | + reduce_max_options, |
| 20 | +) |
| 21 | +from executorch.backends.nxp.backend.neutron_target_spec import NeutronTargetSpec |
| 22 | +from torch.fx import Node |
| 23 | +from torch.nn import Parameter |
| 24 | + |
| 25 | + |
| 26 | +class AmaxConverter(NodeConverter): |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def _is_supported_on_target( |
| 30 | + node: Node, |
| 31 | + neutron_target_spec: NeutronTargetSpec, |
| 32 | + parameters_mapping: dict[str, Parameter], |
| 33 | + custom_delegation_options: CustomDelegationOptions, |
| 34 | + ) -> bool: |
| 35 | + if not NodeConverter.uses_quantization_type_for_io( |
| 36 | + node, |
| 37 | + supported_types=[torch.int8, torch.uint8], |
| 38 | + input_indices=[0], |
| 39 | + output_indices=[0], |
| 40 | + ): |
| 41 | + return False |
| 42 | + |
| 43 | + return True |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def _is_supported_in_IR( |
| 47 | + node: Node, |
| 48 | + parameters_mapping: dict[str, Parameter], |
| 49 | + custom_delegation_options: CustomDelegationOptions, |
| 50 | + ) -> bool: |
| 51 | + if not NodeConverter._has_shared_q_params_if_quantized(node): |
| 52 | + return False |
| 53 | + |
| 54 | + return True |
| 55 | + |
| 56 | + def convert(self, node: Node): |
| 57 | + """Convert the 'amax' operator to NeutronIR 'ReduceMax'. |
| 58 | + The ExecuTorch schema is: |
| 59 | + amax( |
| 60 | + Tensor self, |
| 61 | + int[1]? dim, |
| 62 | + bool keepdim=False, |
| 63 | + ) -> Tensor |
| 64 | + """ |
| 65 | + self.assert_convertible(node) |
| 66 | + |
| 67 | + dim, keepdim = get_reduce_node_attrs(node) |
| 68 | + |
| 69 | + t_op = self._create_tflite_op_with_io_tensors(node) |
| 70 | + t_op.builtin_options = reduce_max_options.ReduceMax(keepdim) |
| 71 | + |
| 72 | + ops = OpsList(middle_op=t_op) |
| 73 | + dim = get_dim_and_handle_io_formats(self.builder, ops, dim, keepdim) |
| 74 | + |
| 75 | + convert_axes_from_attribute(t_op, self.builder, dim) |
| 76 | + self.builder.append_operators(ops.flatten()) |
0 commit comments