-
Notifications
You must be signed in to change notification settings - Fork 859
NXP backend: Add support for aten.neg.default.
#17279
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 1 commit into
pytorch:main
from
nxp-upstream:nxg01483/EIEX-706-add-support-for-aten.neg-to-nxp-backend
Feb 12, 2026
Merged
Changes from all commits
Commits
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
70 changes: 70 additions & 0 deletions
70
backends/nxp/backend/ir/converter/node_converters/ops_converters/neg_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,70 @@ | ||
| # Copyright 2026 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 | ||
|
|
||
| from executorch.backends.nxp.backend import edge_helper | ||
| 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.ir.tflite_generator.tflite_model import ( | ||
| Quantization, | ||
| Scale, | ||
| ZeroPoint, | ||
| ) | ||
| from torch.fx import Node | ||
| from torch.nn import Parameter | ||
|
|
||
|
|
||
| class NegConverter(NodeConverter): | ||
|
|
||
| @staticmethod | ||
| def _is_supported_in_IR( | ||
| node: Node, | ||
| parameters_mapping: dict[str, Parameter], | ||
| custom_delegation_options: CustomDelegationOptions, | ||
| ) -> bool: | ||
| if len(node.args) != 1: | ||
| # Should never happen | ||
| return False | ||
|
|
||
| # The conversion code below expects a per tensor quantized operator. | ||
| scale, zp = edge_helper.get_quantization_parameters_for(node.args[0]) | ||
| match scale, zp: | ||
| case [float(), int()]: | ||
| pass # Atomic quantization parameters -> per tensor quantization. | ||
| case _: | ||
| return False # Everything else is unexpected. | ||
|
|
||
| return True | ||
|
|
||
| def convert(self, node: Node): | ||
| """Convert 'aten.neg.default' operator to NeutronIR 0 - 'Sub'. | ||
|
|
||
| The ExecuTorch schema is 'aten::neg(Tensor self) -> Tensor' | ||
| """ | ||
| self.assert_convertible(node) | ||
|
|
||
| t_op = self._create_tflite_op_with_io_tensors(node) | ||
|
|
||
| x = t_op.tmp_inputs[0] | ||
|
|
||
| # Extract the zero_point, to use as the first input of the `Sub`. | ||
| scale = x.quantization.scale.vector | ||
| zp = x.quantization.zero_point.vector | ||
| zero_tensor = self.builder.create_tensor_for_data(np.array(zp, "int8"), "zero") | ||
| zero_tensor.quantization = Quantization( | ||
| scale=Scale(list(scale)), zero_point=ZeroPoint(list(zp)) | ||
| ) | ||
|
|
||
| # Assign the NeutronIR operator its builtin options and inputs. | ||
| t_op.builtin_options = sub_options.Sub() | ||
| t_op.tmp_inputs = [zero_tensor, x] | ||
|
|
||
| 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
123 changes: 123 additions & 0 deletions
123
backends/nxp/tests/ir/converter/node_converter/test_neg_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,123 @@ | ||
| # Copyright 2026 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 | ||
| 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, | ||
| graph_contains_any_of_ops, | ||
| ToChannelFirstPreprocess, | ||
| ToChannelLastPreprocess, | ||
| ) | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reseed_model_per_test_run(): | ||
| torch.manual_seed(42) | ||
| np.random.seed(23) | ||
|
|
||
|
|
||
| # noinspection PyProtectedMember | ||
| ExecutorchDelegateCall = torch._higher_order_ops.executorch_call_delegate | ||
| Neg = exir_ops.edge.aten.neg.default | ||
|
|
||
|
|
||
| class NegModule(torch.nn.Module): | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
|
|
||
| # noinspection PyMethodMayBeStatic | ||
| def forward(self, x): | ||
| return -x | ||
|
|
||
|
|
||
| class ConvNegModule(torch.nn.Module): | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self.conv = torch.nn.Conv2d(3, 3, 1) | ||
|
|
||
| # noinspection PyMethodMayBeStatic | ||
| def forward(self, x): | ||
| x = self.conv(x) | ||
| return -x | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_shape", | ||
| [ | ||
| pytest.param((8,), id="1D"), | ||
| pytest.param((4, 2), id="2D"), | ||
| pytest.param((1, 2, 3), id="3D"), | ||
| pytest.param((1, 2, 3, 4), id="4D"), | ||
| ], | ||
| ) | ||
| def test_convert_neg(mocker, input_shape): | ||
| model = NegModule() | ||
|
|
||
| converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") | ||
| delegated_ep = to_quantized_edge_program(model, input_shape).exported_program() | ||
|
|
||
| # Make sure the `neg` was delegated. | ||
| assert graph_contains_any_of_ops(delegated_ep.graph, [ExecutorchDelegateCall]) | ||
| assert not graph_contains_any_of_ops(delegated_ep.graph, [Neg]) | ||
|
|
||
| # Verify correct behavior of the converted NeutronIR model. | ||
| intermediate_ep = converter_spy.call_args.args[1] | ||
| neutron_ir_model, _ = converter_spy.spy_return | ||
|
|
||
| input_data = ( | ||
| np.random.random(input_shape).astype(np.float32) * 256.0 - 128.0 | ||
| ).astype(np.int8) | ||
|
|
||
| # Make sure the tested program contains the `neg`. | ||
| assert graph_contains_any_of_ops(intermediate_ep.graph, [Neg]) | ||
|
|
||
| convert_run_compare( | ||
| intermediate_ep, | ||
| tfl_model=neutron_ir_model, | ||
| input_data=input_data, | ||
| ) | ||
|
|
||
|
|
||
| def test_convert_neg__channels_last(mocker): | ||
| model = ConvNegModule() | ||
| input_shape = (1, 3, 4, 5) | ||
|
|
||
| converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") | ||
| delegated_ep = to_quantized_edge_program( | ||
| model, input_shape, use_neutron_for_format_conversion=False | ||
| ).exported_program() | ||
|
|
||
| # Make sure the `neg` was delegated. | ||
| assert graph_contains_any_of_ops(delegated_ep.graph, [ExecutorchDelegateCall]) | ||
| assert not graph_contains_any_of_ops(delegated_ep.graph, [Neg]) | ||
|
|
||
| # Verify correct behavior of the converted NeutronIR model. | ||
| intermediate_ep = converter_spy.call_args.args[1] | ||
| neutron_ir_model, _ = converter_spy.spy_return | ||
|
|
||
| input_data = ( | ||
| np.random.random(input_shape).astype(np.float32) * 256.0 - 128.0 | ||
| ).astype(np.int8) | ||
|
|
||
| # Make sure the tested program contains the `neg`. | ||
| assert graph_contains_any_of_ops(intermediate_ep.graph, [Neg]) | ||
|
|
||
| convert_run_compare( | ||
| intermediate_ep, | ||
| tfl_model=neutron_ir_model, | ||
| input_data=input_data, | ||
| tflite_input_preprocess=ToChannelLastPreprocess(), | ||
| tflite_output_preprocess=ToChannelFirstPreprocess(), | ||
| ) |
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.