|
1 | | -# Copyright 2025 NXP |
| 1 | +# Copyright 2025-2026 NXP |
2 | 2 | # |
3 | 3 | # This source code is licensed under the BSD-style license found in the |
4 | 4 | # LICENSE file in the root directory of this source tree. |
5 | 5 |
|
6 | 6 | from executorch.backends.nxp.backend.ir.converter.node_converter import ( |
7 | 7 | CustomDelegationOptions, |
| 8 | + is_not_qdq_node, |
8 | 9 | NodeConverter, |
| 10 | + Partition, |
9 | 11 | ) |
10 | 12 | from executorch.backends.nxp.backend.ir.lib.tflite.BuiltinOperator import ( |
11 | 13 | BuiltinOperator, |
12 | 14 | ) |
| 15 | +from executorch.backends.nxp.backend.neutron_operator_support import ( |
| 16 | + activation_supported_on_target, |
| 17 | +) |
| 18 | +from executorch.backends.nxp.backend.neutron_target_spec import NeutronTargetSpec |
13 | 19 | from torch.fx import Node |
14 | 20 | from torch.nn import Parameter |
15 | 21 |
|
16 | 22 |
|
17 | 23 | class HardTanhConverter(NodeConverter): |
18 | 24 |
|
19 | 25 | # Maps possible input parameters of HardTanh to equivalent ReLU-based operators supported by TFLite. |
20 | | - supported_modes_map = { |
| 26 | + SUPPORTED_MODES_MAP = { |
21 | 27 | (0.0, 6.0): BuiltinOperator.RELU6, |
22 | 28 | (-1.0, 1.0): BuiltinOperator.RELU_N1_TO_1, |
23 | 29 | (0.0, 1.0): BuiltinOperator.RELU_0_TO_1, |
24 | 30 | (0.0, float("inf")): BuiltinOperator.RELU, |
25 | 31 | } |
26 | 32 |
|
| 33 | + # Maps possible modes of HardTanh to equivalent ReLU bounds. |
| 34 | + SUPPORTED_BOUNDS_MAP = { |
| 35 | + "ReluN1To1": (-1.0, 1.0), |
| 36 | + "Relu0To1": (0.0, 1.0), |
| 37 | + "Relu6": (0.0, 6.0), |
| 38 | + "Relu": (0.0, float("inf")), |
| 39 | + } |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def _get_hardtanh_bounds(node: Node) -> tuple[float, float]: |
| 43 | + args = node.args |
| 44 | + |
| 45 | + match len(args): |
| 46 | + case 1: |
| 47 | + min_val = -1 |
| 48 | + max_val = 1 |
| 49 | + |
| 50 | + case 2: |
| 51 | + min_val = args[1] |
| 52 | + max_val = 1 |
| 53 | + |
| 54 | + case 3: |
| 55 | + min_val = args[1] |
| 56 | + max_val = args[2] |
| 57 | + |
| 58 | + case _: |
| 59 | + # should not occur |
| 60 | + raise ValueError( |
| 61 | + f"Unexpected number of arguments for HardTanh node: {len(args)}" |
| 62 | + ) |
| 63 | + |
| 64 | + return min_val, max_val |
| 65 | + |
27 | 66 | @staticmethod |
28 | 67 | def _is_supported_in_IR( |
29 | 68 | node: Node, |
30 | 69 | parameters_mapping: dict[str, Parameter], |
31 | 70 | custom_delegation_options: CustomDelegationOptions, |
32 | 71 | ) -> bool: |
33 | | - _, min_value, max_value = node.args |
34 | | - return (min_value, max_value) in HardTanhConverter.supported_modes_map.keys() |
| 72 | + bounds = HardTanhConverter._get_hardtanh_bounds(node) |
| 73 | + return bounds in HardTanhConverter.SUPPORTED_MODES_MAP |
| 74 | + |
| 75 | + @classmethod |
| 76 | + def supports_partitioning_result( |
| 77 | + cls, |
| 78 | + node: Node, |
| 79 | + partition_list: list[Partition], |
| 80 | + custom_delegation_options: CustomDelegationOptions, |
| 81 | + neutron_target_spec: NeutronTargetSpec, |
| 82 | + parameters_mapping: dict[str, Parameter], |
| 83 | + ) -> bool: |
| 84 | + bounds = HardTanhConverter._get_hardtanh_bounds(node) |
| 85 | + |
| 86 | + # Neutron cannot delegate a partition where ReLU or ReLU6 is the only operator |
| 87 | + # and at the same time the node does not satisfy delegation requirements. |
| 88 | + # In contrast, ReLUN1To1 and ReLU0To1 are supported and delegated successfuly. |
| 89 | + if bounds in [ |
| 90 | + cls.SUPPORTED_BOUNDS_MAP["Relu"], |
| 91 | + cls.SUPPORTED_BOUNDS_MAP["Relu6"], |
| 92 | + ]: |
| 93 | + is_alone_in_partition = cls.is_node_alone_in_partition( |
| 94 | + node, partition_list, filter_fn=is_not_qdq_node |
| 95 | + ) |
| 96 | + if is_alone_in_partition: |
| 97 | + return activation_supported_on_target(node, neutron_target_spec) |
| 98 | + |
| 99 | + return True |
35 | 100 |
|
36 | 101 | def convert(self, node: Node): |
37 | | - """Convert 'aten::hardtanh' to it's supported ReLU equivalent.""" |
| 102 | + """Convert 'aten::hardtanh' to its supported ReLU equivalent.""" |
38 | 103 | self.assert_convertible(node) |
39 | 104 |
|
40 | 105 | t_op = self._create_tflite_op_with_io_tensors(node) |
41 | 106 |
|
42 | | - _, min_value, max_value = node.args |
| 107 | + bounds = HardTanhConverter._get_hardtanh_bounds(node) |
43 | 108 |
|
44 | | - op = self.supported_modes_map[(min_value, max_value)] |
| 109 | + op = self.SUPPORTED_MODES_MAP[bounds] |
45 | 110 | t_op.opcode_index = self.builder.op_code_index_for_op_type(op) |
46 | 111 |
|
47 | 112 | self.builder.append_operators([t_op]) |
0 commit comments