-
Notifications
You must be signed in to change notification settings - Fork 1.1k
NXP backend: Enable Hardswish with new Neutron flow #21500
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
roman-janik-nxp
merged 1 commit into
pytorch:main
from
nxp-upstream:feature/nxg11066/EIEX-887-Add-hard_swish-support-using-new-Neutron-flow
Jul 31, 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
50 changes: 50 additions & 0 deletions
50
backends/nxp/backend/ir/converter/node_converters/ops_converters/hardswish_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,50 @@ | ||
| # 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 torch | ||
|
|
||
| from executorch.backends.nxp.backend.ir.converter.node_converter import ( | ||
| CustomDelegationOptions, | ||
| NodeConverter, | ||
| ) | ||
| from executorch.backends.nxp.backend.ir.tflite_generator.builtin_options import ( | ||
| hard_swish_options, | ||
| ) | ||
| from executorch.backends.nxp.backend.neutron_target_spec import NeutronTargetSpec | ||
| from torch.fx import Node | ||
| from torch.nn import Parameter | ||
|
|
||
|
|
||
| class HardswishConverter(NodeConverter): | ||
| @staticmethod | ||
| def _is_supported_on_target( | ||
| node: Node, | ||
| neutron_target_spec: NeutronTargetSpec, | ||
| parameters_mapping: dict[str, Parameter], | ||
| custom_delegation_options: CustomDelegationOptions, | ||
| ) -> bool: | ||
| supported_types = [torch.int8, torch.uint8] | ||
| if not NodeConverter.uses_quantization_type_for_io( | ||
| node, supported_types, [0], [0] | ||
| ): | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| @staticmethod | ||
| def _is_supported_in_IR( | ||
| node: Node, | ||
| parameters_mapping: dict[str, Parameter], | ||
| custom_delegation_options: CustomDelegationOptions, | ||
| ) -> bool: | ||
| return True | ||
|
|
||
| def convert(self, node: Node): | ||
| self.assert_convertible(node) | ||
|
|
||
| t_op = self._create_tflite_op_with_io_tensors(node) | ||
| t_op.builtin_options = hard_swish_options.HardSwish() | ||
|
|
||
| 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
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
152 changes: 152 additions & 0 deletions
152
backends/nxp/tests/ir/converter/node_converter/test_hardswish_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,152 @@ | ||
| # 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 | ||
|
|
||
| # noinspection PyUnusedImports | ||
| import pytest | ||
| import torch | ||
|
|
||
| from executorch.backends.nxp.tests.dataset_creator import RandomDatasetCreator | ||
| from executorch.backends.nxp.tests.graph_verifier import DetailedGraphVerifier | ||
| from executorch.backends.nxp.tests.model_output_comparator import ( | ||
| AllCloseOutputComparator, | ||
| ) | ||
| from executorch.backends.nxp.tests.models import ( | ||
| ConvHardswishModule, | ||
| HardswishModule, | ||
| LinearHardswishModule, | ||
| ) | ||
| from executorch.backends.nxp.tests.nsys_testing import lower_run_compare | ||
| from executorch.backends.nxp.tests.ops_aliases import ( | ||
| AddMM, | ||
| Convolution, | ||
| Hardswish, | ||
| PermuteCopy, | ||
| ViewCopy, | ||
| ) | ||
| from executorch.backends.nxp.tests.use_qat import * # noqa F403 | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reseed_model_per_test_run(): | ||
| torch.manual_seed(23) | ||
| np.random.seed(23) | ||
|
|
||
|
|
||
| class TestHardswishConverter: | ||
| # noinspection PyMethodMayBeStatic | ||
| def assert_delegated( | ||
| self, | ||
| model, | ||
| input_shape, | ||
| mocker, | ||
| request, | ||
| expected_delegated_ops=None, | ||
| use_qat=False, | ||
| ): | ||
| rank = len(input_shape) | ||
| graph_verifier = DetailedGraphVerifier( | ||
| mocker, | ||
| expected_delegated_ops=expected_delegated_ops | ||
| or { | ||
| Hardswish: 1, | ||
| AddMM: 1, | ||
| PermuteCopy: 1, | ||
| ViewCopy: 0 if rank == 2 else 2, | ||
| }, | ||
| expected_non_delegated_ops={}, | ||
| ) | ||
|
|
||
| # Cover also negative values to thoroughly test the operator. | ||
| dataset_creator = RandomDatasetCreator(low=-4, high=4) | ||
| comparator = AllCloseOutputComparator(atol=1) | ||
|
|
||
| lower_run_compare( | ||
| model, | ||
| input_shape, | ||
| graph_verifier, | ||
| request, | ||
| dataset_creator, | ||
| output_comparator=comparator, | ||
| remove_quant_io_ops=True, | ||
| use_qat=use_qat, | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_shape", | ||
| [ | ||
| pytest.param((1,), id="1D."), | ||
| pytest.param((7, 83), id="2D."), | ||
| pytest.param((7, 8, 12), id="3D."), | ||
| pytest.param((1, 4, 7, 8), id="4D."), | ||
|
roman-janik-nxp marked this conversation as resolved.
|
||
| pytest.param((5, 4, 7, 8), id="4D batchsize != 1."), | ||
| pytest.param((1, 4, 3, 4, 14), id="5D."), | ||
| ], | ||
| ) | ||
| def test__basic_nsys_inference(self, mocker, request, input_shape): | ||
| channels = input_shape[-1] | ||
| model = LinearHardswishModule(in_features=channels, out_features=channels) | ||
|
|
||
| self.assert_delegated(model, input_shape, mocker, request) | ||
|
|
||
| def test__basic_nsys_inference_qat(self, mocker, request): | ||
|
roman-janik-nxp marked this conversation as resolved.
|
||
| input_shape = (2, 4, 6, 7) | ||
| channels = input_shape[-1] | ||
| model = LinearHardswishModule(in_features=channels, out_features=channels) | ||
|
|
||
| self.assert_delegated(model, input_shape, mocker, request, use_qat=True) | ||
|
|
||
| def test__basic_nsys_inference_inplace(self, mocker, request, use_qat): | ||
| input_shape = (2, 4, 6, 7) | ||
| channels = input_shape[-1] | ||
| model = LinearHardswishModule( | ||
| in_features=channels, out_features=channels, inplace=True | ||
| ) | ||
|
|
||
| self.assert_delegated(model, input_shape, mocker, request, use_qat=use_qat) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_shape", | ||
| [ | ||
| pytest.param((3,), id="1D."), | ||
| pytest.param((1, 4), id="2D."), | ||
| pytest.param((4, 7, 4), id="3D."), | ||
| pytest.param((1, 6, 4, 4), id="4D."), | ||
| pytest.param((5, 4, 7, 8), id="4D batchsize != 1."), | ||
| pytest.param((2, 3, 8, 3, 11), id="5D."), | ||
| ], | ||
| ) | ||
| def test__single_hardswish(self, mocker, request, input_shape): | ||
| model = HardswishModule() | ||
| expected_delegated_ops = { | ||
| Hardswish: 1, | ||
| } | ||
|
|
||
| self.assert_delegated( | ||
| model, | ||
| input_shape, | ||
| mocker, | ||
| request, | ||
| expected_delegated_ops=expected_delegated_ops, | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_shape", | ||
| [ | ||
| pytest.param((1, 8, 4, 4), id="4D."), | ||
| ], | ||
| ) | ||
| def test__channels_first(self, mocker, request, input_shape): | ||
| channels = input_shape[1] | ||
| model = ConvHardswishModule(in_channels=channels) | ||
| expected_delegated_ops = { | ||
| Hardswish: 1, | ||
| Convolution: 1, | ||
| } | ||
|
|
||
| self.assert_delegated( | ||
| model, input_shape, mocker, request, expected_delegated_ops | ||
| ) | ||
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
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.