-
Notifications
You must be signed in to change notification settings - Fork 752
NXP backend: Add implementation of Tanh operator converter #13510
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
robert-kalmar
merged 1 commit into
pytorch:main
from
nxp-upstream:upstream/main-nxp/EIEX-456-upstream-support-for-tanh-operator
Aug 24, 2025
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
29 changes: 29 additions & 0 deletions
29
backends/nxp/backend/ir/converter/node_converters/ops_converters/tanh_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,29 @@ | ||
| # 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.node_converter import NodeConverter | ||
| from executorch.backends.nxp.backend.ir.lib.tflite.BuiltinOperator import ( | ||
| BuiltinOperator, | ||
| ) | ||
| from torch.fx import Node | ||
| from torch.nn import Parameter | ||
|
|
||
|
|
||
| class TanhConverter(NodeConverter): | ||
|
|
||
| @staticmethod | ||
| def _is_supported_in_IR( | ||
| node: Node, | ||
| parameters_mapping: dict[str, Parameter], | ||
| ) -> bool: | ||
| return True | ||
|
|
||
| def convert(self, node: Node): | ||
| self.assert_convertible(node) | ||
|
|
||
| t_op = self._create_tflite_op_with_io_tensors(node) | ||
| t_op.opcode_index = self.builder.op_code_index_for_op_type(BuiltinOperator.TANH) | ||
|
|
||
| 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
Empty file.
Empty file.
Empty file.
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
85 changes: 85 additions & 0 deletions
85
backends/nxp/tests/ir/converter/node_converter/test_tanh_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,85 @@ | ||
| # Copyright 2025 NXP | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import unittest | ||
|
|
||
| import kgb | ||
| import numpy as np | ||
| import torch | ||
|
|
||
| from executorch.backends.nxp.nxp_backend 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.backends.nxp.tests.models import Conv2dWithActivation | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
| from parameterized import parameterized | ||
| from torch.export import ExportedProgram | ||
|
|
||
|
|
||
| class TestTanhConverter(unittest.TestCase): | ||
| __test__ = False # Prevent interfering with PyTest tests | ||
|
|
||
| @parameterized.expand( | ||
| input=[ | ||
| ( | ||
| "inplace", | ||
| True, | ||
| ), | ||
| ( | ||
| "not_inplace", | ||
| False, | ||
| ), | ||
| ] | ||
| ) | ||
| def test_conv_tanh( | ||
| self, _: str, inplace: bool, input_shape: tuple[int] = (1, 3, 112, 112) | ||
| ): | ||
| with kgb.spy_on( | ||
| EdgeProgramToIRConverter.convert_program, call_original=True | ||
| ) as converter_spy: | ||
| if inplace: | ||
| model = Conv2dWithActivation( | ||
| activation=torch.tanh_, in_channels=input_shape[1] | ||
| ) | ||
| else: | ||
| model = Conv2dWithActivation( | ||
| activation=torch.tanh, in_channels=input_shape[1] | ||
| ) | ||
|
|
||
| quantized_program = to_quantized_edge_program( | ||
| model, input_shape | ||
| ).exported_program() | ||
| tflite_flatbuffers_model, io_formats = converter_spy.calls[-1].return_value | ||
| exported_program: ExportedProgram = converter_spy.calls[-1].args[0] | ||
|
|
||
| lowered_module_graph = ( | ||
| quantized_program.graph_module.lowered_module_0.original_module.graph | ||
| ) | ||
| tanh_ops = [ | ||
| exir_ops.edge.aten.tanh.default, | ||
| exir_ops.edge.aten.tanh_.default, | ||
| ] | ||
| assert graph_contains_any_of_ops(graph=lowered_module_graph, ops=tanh_ops) | ||
|
|
||
| input_data = (np.random.random(input_shape) * 50).astype(np.int8) | ||
| convert_run_compare( | ||
| exported_program, | ||
| tfl_model=tflite_flatbuffers_model, | ||
| tflite_input_preprocess=ToChannelLastPreprocess(), | ||
| tflite_output_preprocess=ToChannelFirstPreprocess(), | ||
| input_data=input_data, | ||
| atol=1.0, | ||
| ) | ||
|
|
||
| @classmethod | ||
| def setUpClass(cls): | ||
| torch.manual_seed(23) | ||
| np.random.seed(23) |
Empty file.
Oops, something went wrong.
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.