-
Notifications
You must be signed in to change notification settings - Fork 699
NXP backend: added aten.sub operator support #14514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MartinPavella
merged 10 commits into
pytorch:main
from
nxp-upstream:feature/EIEX-538-add-aten-sub-operator
Oct 3, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c60d694
NXP backend: added aten.sub operator support
novak-vaclav c7346d9
fix: fixed lint error
novak-vaclav 5ea5663
fix: applied feedback from PR
novak-vaclav 58992ef
fix: added missing license
novak-vaclav a3aeccc
fix: improved test effectiveness
novak-vaclav cc8ebaa
fix: fixed issues from PR
novak-vaclav ab21d6a
fix: fixed issues from PR
novak-vaclav 6f6ac9d
Fix type hints.
MartinPavella c588b7b
Fix sub quantization pattern.
MartinPavella 2b59bbe
Fix rebase.
MartinPavella File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
backends/nxp/backend/ir/converter/node_converters/ops_converters/sub_tensor_converter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Copyright 2025 NXP | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| from executorch.backends.nxp.backend.ir.converter.conversion.common import ( | ||
| node_uses_shape_broadcasting, | ||
| ) | ||
| from executorch.backends.nxp.backend.ir.converter.node_converter import ( | ||
| CustomDelegationOptions, | ||
| NodeConverter, | ||
| ) | ||
| from executorch.backends.nxp.backend.ir.tflite_generator.builtin_options import ( | ||
| sub_options, | ||
| ) | ||
| from executorch.backends.nxp.backend.neutron_target_spec import NeutronTargetSpec | ||
| from torch.fx import Node | ||
| from torch.nn import Parameter | ||
|
|
||
|
|
||
| class SubTensorConverter(NodeConverter): | ||
| @staticmethod | ||
| def _is_supported_on_target( | ||
| node: Node, | ||
| neutron_target_spec: NeutronTargetSpec, | ||
| parameters_mapping: dict[str, Parameter], | ||
| custom_delegation_options: CustomDelegationOptions, | ||
| ) -> bool: | ||
| if node_uses_shape_broadcasting(node): | ||
| # Shape broadcasting may require the addition of `Transpose` ops during conversion. | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| @staticmethod | ||
| def _is_supported_in_IR( | ||
| node: Node, | ||
| parameters_mapping: dict[str, Parameter], | ||
| custom_delegation_options: CustomDelegationOptions, | ||
| ) -> bool: | ||
| if len(node.args) != 2: | ||
| return False | ||
|
|
||
| # The `alpha` attribute can be represented by adding an extra `Mul` operator. | ||
| # However, this is not implemented as `alpha` is rarely used. | ||
| if hasattr(node.kwargs, "alpha"): | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| # sub.Tensor Node format: (Tensor self, Tensor other, *, Scalar alpha=1) | ||
| def convert(self, node: Node): | ||
| """Convert 'sub_tensor' operator to NeutronIR 'Sub'.""" | ||
| self.assert_convertible(node) | ||
|
|
||
| t_op = self._create_tflite_op_with_io_tensors(node) | ||
|
|
||
| t_op.builtin_options = sub_options.Sub() | ||
| self.builder.append_operators([t_op]) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
backends/nxp/tests/ir/converter/node_converter/test_add_tensor_converter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
backends/nxp/tests/ir/converter/node_converter/test_sub_tensor_converter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| # Copyright 2025 NXP | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| import numpy as np | ||
novak-vaclav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from executorch.backends.nxp.backend.edge_program_converter import ( | ||
| EdgeProgramToIRConverter, | ||
| ) | ||
| from executorch.backends.nxp.tests.executorch_pipeline import to_quantized_edge_program | ||
| from executorch.backends.nxp.tests.executors import ( | ||
| convert_run_compare, | ||
| ToChannelFirstPreprocess, | ||
| ToChannelLastPreprocess, | ||
| ) | ||
| from executorch.backends.nxp.tests.models import ( | ||
| SubTensorConvModule, | ||
| SubTensorModule, | ||
| SubTensorOneInputModule, | ||
| ) | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
| from torch.export import ExportedProgram | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reseed_model_per_test_run(): | ||
| torch.manual_seed(23) | ||
| np.random.seed(23) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_shape", | ||
| [ | ||
| pytest.param((4,), id="1D."), | ||
| pytest.param((6, 6), id="2D."), | ||
| pytest.param((1, 4, 8), id="3D."), | ||
| pytest.param((1, 4, 8, 8), id="4D."), | ||
| ], | ||
| ) | ||
| def test_sub_tensor_quant_conversion(mocker, input_shape): | ||
| model = SubTensorModule() | ||
|
|
||
| converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") | ||
|
|
||
| # Run conversion | ||
| _ = to_quantized_edge_program(model, [input_shape, input_shape]) | ||
|
|
||
| # Capture generated model | ||
| tflite_flatbuffers_model, io_formats = converter_spy.spy_return | ||
|
|
||
| # Capture converted program | ||
| exported_program: ExportedProgram = converter_spy.call_args.args[1] | ||
novak-vaclav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| input_data_1 = (np.random.random(input_shape).astype(np.float32) * 50).astype( | ||
| np.int8 | ||
| ) | ||
| input_data_2 = (np.random.random(input_shape).astype(np.float32) * 50).astype( | ||
| np.int8 | ||
| ) | ||
| input_data = {0: input_data_1, 1: input_data_2} | ||
|
|
||
| nodes = list(exported_program.graph.nodes) | ||
| assert nodes[4].target == exir_ops.edge.aten.sub.Tensor | ||
|
|
||
| convert_run_compare( | ||
| exported_program, tfl_model=tflite_flatbuffers_model, input_data=input_data | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_shape", | ||
| [ | ||
| pytest.param((4,), id="1D."), | ||
| pytest.param((6, 6), id="2D."), | ||
| pytest.param((1, 4, 8), id="3D."), | ||
| pytest.param((1, 4, 8, 8), id="4D."), | ||
| ], | ||
| ) | ||
| def test_sub_tensor_one_input_quant_conversion(mocker, input_shape): | ||
| model = SubTensorOneInputModule() | ||
|
|
||
| converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") | ||
|
|
||
| # Run conversion | ||
| _ = to_quantized_edge_program(model, input_shape) | ||
|
|
||
| # Capture generated model | ||
| tflite_flatbuffers_model, io_formats = converter_spy.spy_return | ||
|
|
||
| # Capture converted program | ||
| exported_program: ExportedProgram = converter_spy.call_args.args[1] | ||
novak-vaclav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| input_data = (np.random.random(input_shape).astype(np.float32) * 50).astype(np.int8) | ||
|
|
||
| nodes = list(exported_program.graph.nodes) | ||
| assert nodes[2].target == exir_ops.edge.aten.sub.Tensor | ||
|
|
||
| convert_run_compare( | ||
| exported_program, tfl_model=tflite_flatbuffers_model, input_data=input_data | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "x_input_shape", | ||
| [ | ||
| pytest.param((1, 4, 8, 8), id="4D."), | ||
| pytest.param((1, 4, 5, 5), id="4D, product of dims is not a multiple of 8."), | ||
| ], | ||
| ) | ||
| def test_sub_tensor_w_conv_quant_conversion(mocker, x_input_shape): | ||
| model = SubTensorConvModule() | ||
|
|
||
| converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") | ||
|
|
||
| n, c, h, w = x_input_shape | ||
| y_input_shape = (n, 8, h, w) | ||
|
|
||
| # Run conversion | ||
| _ = to_quantized_edge_program(model, [x_input_shape, y_input_shape]) | ||
|
|
||
| # Capture generated model | ||
| tflite_flatbuffers_model, io_formats = converter_spy.spy_return | ||
|
|
||
| # Capture converted program | ||
| exported_program: ExportedProgram = converter_spy.call_args.args[1] | ||
novak-vaclav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| input_data_1 = (np.random.random(x_input_shape).astype(np.float32) * 50).astype( | ||
| np.int8 | ||
| ) | ||
| input_data_2 = (np.random.random(y_input_shape).astype(np.float32) * 50).astype( | ||
| np.int8 | ||
| ) | ||
| input_data = {0: input_data_1, 1: input_data_2} | ||
|
|
||
| nodes = list(exported_program.graph.nodes) | ||
| assert nodes[15].target == exir_ops.edge.aten.sub.Tensor | ||
|
|
||
| convert_run_compare( | ||
| exported_program, | ||
| input_data=input_data, | ||
| tflite_input_preprocess=ToChannelLastPreprocess(), | ||
| tfl_model=tflite_flatbuffers_model, | ||
| tflite_output_preprocess=ToChannelFirstPreprocess(), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "x_input_shape, y_input_shape", | ||
| [ | ||
| pytest.param((1, 4, 7), (4, 7), id="3D -> 2D."), | ||
| pytest.param((1, 4, 8), (1, 4, 4, 8), id="3D -> 4D."), | ||
| pytest.param((1, 1, 4, 4, 8), (1, 4, 4, 8), id="5D -> 4D."), | ||
| pytest.param((4,), (4, 4), id="1D -> 2D."), | ||
| pytest.param((4,), (4, 4, 4), id="1D -> 3D."), | ||
| pytest.param((6, 6), (1, 8, 6, 6), id="2D -> 4D."), | ||
| pytest.param((6, 6), (6,), id="2D -> 1D."), | ||
| ], | ||
| ) | ||
| def test_sub_tensor_broadcasting_unsupported_quant_conversion( | ||
| x_input_shape, y_input_shape | ||
| ): | ||
| model = SubTensorModule() | ||
|
|
||
| # Run conversion | ||
| edge_program = to_quantized_edge_program( | ||
| model, [x_input_shape, y_input_shape] | ||
| ).exported_program() | ||
| nodes = list(edge_program.graph.nodes) | ||
|
|
||
| # Broadcast is not supported, node is not converted | ||
| assert ( | ||
| nodes[6].target == exir_ops.edge.aten.sub.Tensor | ||
| ) # Sub Tensor is not delegated. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.