-
Notifications
You must be signed in to change notification settings - Fork 1k
[EIEX-885] Add log support using new Neutron flow
#20145
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
Open
irtrukhina
wants to merge
2
commits into
pytorch:main
Choose a base branch
from
nxp-upstream:feature/EIEX-885-add-log-support-using-new-neutron-flow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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/log_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 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, | ||
| NeutronTargetSpec, | ||
| NodeConverter, | ||
| ) | ||
| from executorch.backends.nxp.backend.ir.lib.tflite.BuiltinOperator import ( | ||
| BuiltinOperator, | ||
| ) | ||
| from torch.fx import Node | ||
| from torch.nn import Parameter | ||
|
|
||
|
|
||
| class LogConverter(NodeConverter): | ||
|
|
||
| @staticmethod | ||
| def _is_supported_in_IR( | ||
| node: Node, | ||
| parameters_mapping: dict[str, Parameter], | ||
| custom_delegation_options: CustomDelegationOptions, | ||
| ) -> bool: | ||
| return True | ||
|
|
||
| @staticmethod | ||
| def _is_supported_on_target( | ||
| node: Node, | ||
| neutron_target_spec: NeutronTargetSpec, | ||
| parameters_mapping: dict[str, Parameter], | ||
| custom_delegation_options: CustomDelegationOptions, | ||
| ) -> bool: | ||
| # Requirements specified by the new Neutron flow documentation. | ||
| # Input and Output must be INT8/UINT8. | ||
| if not NodeConverter.uses_quantization_type_for_io( | ||
| node, | ||
| supported_types=[torch.int8, torch.uint8], | ||
| input_indices=[0], | ||
| output_indices=[0], | ||
| ): | ||
| return False | ||
| return True | ||
|
|
||
| def convert(self, node: Node): | ||
| """Convert the `aten.log.default` operator to Neutron IR `Log`. | ||
| The schema is: | ||
| aten::log( | ||
| Tensor self | ||
| ) -> Tensor | ||
| """ | ||
|
|
||
| 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.LOG) | ||
|
|
||
| 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
74 changes: 74 additions & 0 deletions
74
backends/nxp/tests/ir/converter/node_converter/test_log_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,74 @@ | ||
| # 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.graph_verifier import DetailedGraphVerifier | ||
| from executorch.backends.nxp.tests.nsys_testing import lower_run_compare | ||
| from executorch.backends.nxp.tests.ops_aliases import Log | ||
| from executorch.backends.nxp.tests.use_qat import * # noqa F403 | ||
| from executorch.backends.nxp.tests.dataset_creator import ( | ||
| LinearRampDatasetCreator, | ||
| RandomDatasetCreator, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reseed_model_per_test_run(): | ||
| torch.manual_seed(42) | ||
| np.random.seed(23) | ||
|
|
||
|
|
||
| class LogModule(torch.nn.Module): | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
|
|
||
| def forward(self, x): | ||
| return torch.log(x) | ||
|
|
||
|
|
||
| class TestLog: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a test using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, will add |
||
| def test__basic_nsys_inference(self, mocker): | ||
| # Use 256 elements so that, after quantization to int8, the input can | ||
| # cover the full discrete range [-128, 127]. | ||
| # The dataset is generated as a linear float ramp and later quantized, | ||
| # which effectively exercises all int8 values. | ||
| input_shape = (256,) | ||
| model = LogModule() | ||
| graph_verifier = DetailedGraphVerifier( | ||
| mocker, expected_delegated_ops={Log: 1}, expected_non_delegated_ops={} | ||
| ) | ||
| lower_run_compare( | ||
| model, | ||
| input_shape, | ||
| graph_verifier, | ||
| dataset_creator=LinearRampDatasetCreator(low=0.0, high=1.0), | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_shape", | ||
| [ | ||
| pytest.param((17, 2), id="2D"), | ||
| pytest.param((1, 3, 10), id="3D"), | ||
| pytest.param((1, 3, 16, 16), id="4D"), | ||
| ], | ||
| ) | ||
| def test__basic_nsys_inference__qat(self, mocker, input_shape, use_qat): | ||
| model = LogModule() | ||
| graph_verifier = DetailedGraphVerifier( | ||
| mocker, expected_delegated_ops={Log: 1}, expected_non_delegated_ops={} | ||
| ) | ||
| lower_run_compare( | ||
| model, | ||
| input_shape, | ||
| graph_verifier, | ||
| dataset_creator=RandomDatasetCreator(low=1.0, high=10.0), | ||
| use_qat=use_qat, | ||
| ) | ||
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.