|
| 1 | +# Copyright 2025 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 | +from executorch.backends.nxp.backend.ir.converter.conversion.common import ( |
| 7 | + node_uses_shape_broadcasting, |
| 8 | +) |
| 9 | +from executorch.backends.nxp.backend.ir.converter.node_converter import ( |
| 10 | + CustomDelegationOptions, |
| 11 | + NodeConverter, |
| 12 | +) |
| 13 | +from executorch.backends.nxp.backend.ir.tflite_generator.builtin_options import ( |
| 14 | + sub_options, |
| 15 | +) |
| 16 | +from executorch.backends.nxp.backend.neutron_target_spec import NeutronTargetSpec |
| 17 | +from torch.fx import Node |
| 18 | +from torch.nn import Parameter |
| 19 | + |
| 20 | + |
| 21 | +class SubTensorConverter(NodeConverter): |
| 22 | + @staticmethod |
| 23 | + def _is_supported_on_target( |
| 24 | + node: Node, |
| 25 | + neutron_target_spec: NeutronTargetSpec, |
| 26 | + parameters_mapping: dict[str, Parameter], |
| 27 | + custom_delegation_options: CustomDelegationOptions, |
| 28 | + ) -> bool: |
| 29 | + if node_uses_shape_broadcasting(node): |
| 30 | + # Shape broadcasting may require the addition of `Transpose` ops during conversion. |
| 31 | + return False |
| 32 | + |
| 33 | + return True |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def _is_supported_in_IR( |
| 37 | + node: Node, |
| 38 | + parameters_mapping: dict[str, Parameter], |
| 39 | + custom_delegation_options: CustomDelegationOptions, |
| 40 | + ) -> bool: |
| 41 | + if len(node.args) != 2: |
| 42 | + return False |
| 43 | + |
| 44 | + # The `alpha` attribute can be represented by adding an extra `Mul` operator. |
| 45 | + # However, this is not implemented as `alpha` is rarely used. |
| 46 | + if hasattr(node.kwargs, "alpha"): |
| 47 | + return False |
| 48 | + |
| 49 | + return True |
| 50 | + |
| 51 | + # sub.Tensor Node format: (Tensor self, Tensor other, *, Scalar alpha=1) |
| 52 | + def convert(self, node: Node): |
| 53 | + """Convert 'sub_tensor' operator to NeutronIR 'Sub'.""" |
| 54 | + self.assert_convertible(node) |
| 55 | + |
| 56 | + t_op = self._create_tflite_op_with_io_tensors(node) |
| 57 | + |
| 58 | + t_op.builtin_options = sub_options.Sub() |
| 59 | + self.builder.append_operators([t_op]) |
0 commit comments