-
Notifications
You must be signed in to change notification settings - Fork 1.1k
NXP backend: added support for aten.pad with mode reflect
#21515
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
novak-vaclav
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
nxp-upstream:feature/EIEX-888-add-pad-support
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
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
120 changes: 120 additions & 0 deletions
120
backends/nxp/backend/ir/converter/node_converters/ops_converters/pad_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,120 @@ | ||
| # 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. | ||
|
|
||
| from typing import Collection | ||
|
|
||
| import numpy as np | ||
| import torch | ||
| from executorch.backends.nxp.backend.custom_delegation_options import ( | ||
| CustomDelegationOptions, | ||
| ) | ||
|
|
||
| from executorch.backends.nxp.backend.ir.converter.conversion.translator import ( | ||
| apply_permutation_to, | ||
| create_channels_first_to_channels_last_permutation, | ||
| ) | ||
| from executorch.backends.nxp.backend.ir.converter.node_converter import NodeConverter | ||
| from executorch.backends.nxp.backend.ir.lib.tflite.MirrorPadMode import MirrorPadMode | ||
| from executorch.backends.nxp.backend.ir.tflite_generator import tflite_model | ||
| from executorch.backends.nxp.backend.ir.tflite_generator.builtin_options import ( | ||
| mirror_pad_options, | ||
| ) | ||
| from executorch.backends.nxp.backend.neutron_target_spec import NeutronTargetSpec | ||
| from torch.fx import Node | ||
| from torch.nn import Parameter | ||
|
|
||
|
|
||
| class PadConverter(NodeConverter): | ||
| """Convert `aten.pad.default` to a TFLite padding operator. | ||
|
|
||
| The ExecuTorch schema is: | ||
| aten::pad( | ||
| Tensor self, | ||
| SymInt[] pad, | ||
| str mode="constant", | ||
| float? value=None | ||
| ) -> Tensor | ||
|
|
||
| Only mode "reflect" is handled in this converter. | ||
| Mode "constant" is decomposed to `aten.constant_pad_nd` and handled in its own converter. | ||
| Modes "replicate"/"circular" are not handled for now, such conversion to TFLite is not trivial. | ||
| """ | ||
|
|
||
| @staticmethod | ||
| def _get_mode(node: Node) -> str: | ||
| return node.args[2] if len(node.args) > 2 else "constant" | ||
|
|
||
| @staticmethod | ||
| def _is_supported_in_IR( | ||
| node: Node, | ||
| parameters_mapping: dict[str, Parameter], | ||
| custom_delegation_options: CustomDelegationOptions, | ||
| ) -> bool: | ||
| mode = PadConverter._get_mode(node) | ||
| # `constant` mode is decomposed to `aten.constant_pad_nd`. | ||
| # Conversion of `replicate`/`circular` Torch padding to TFLite | ||
| # is more complicated and is skipped for now. | ||
| if mode != "reflect": | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| @staticmethod | ||
| def _is_supported_on_target( | ||
| node: Node, | ||
| neutron_target_spec: NeutronTargetSpec, | ||
| parameters_mapping: dict[str, Parameter], | ||
| custom_delegation_options: CustomDelegationOptions, | ||
| ) -> bool: | ||
| 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 | ||
|
|
||
| @staticmethod | ||
| def _convert_paddings_to_tflite( | ||
| paddings: Collection[int], input_tensor: tflite_model.Tensor | ||
| ) -> list[int]: | ||
| # Group `padding` by two elements per list. | ||
| paddings_grouped = np.array(paddings).reshape(-1, 2) | ||
|
|
||
| # In TFLite, `padding` order is reversed. | ||
| paddings_reversed = list(reversed(paddings_grouped)) | ||
|
|
||
| # Add complementary zero pairs to `padding` to match input tensor rank. | ||
| zero_pair_compl = [[0, 0]] * (input_tensor.rank - len(paddings_reversed)) | ||
| padding_tfl = zero_pair_compl + paddings_reversed | ||
|
|
||
| if input_tensor.tensor_format.is_channels_last(): | ||
| # Permute padding to match tensor format. | ||
| to_tflite_perm = create_channels_first_to_channels_last_permutation( | ||
| input_tensor.rank | ||
| ) | ||
| padding_tfl = apply_permutation_to(padding_tfl, to_tflite_perm) | ||
|
|
||
| return padding_tfl | ||
|
|
||
| def convert(self, node: Node): | ||
| """Convert `aten.pad.default` to a TFLite padding operator.""" | ||
| self.assert_convertible(node) | ||
|
|
||
| t_op = self._create_tflite_op_with_io_tensors(node) | ||
| x = t_op.tmp_inputs[0] | ||
|
|
||
| paddings = self._convert_paddings_to_tflite(node.args[1], x) | ||
|
|
||
| paddings_tensor = self.builder.create_tensor_for_data( | ||
| np.asarray(paddings, "int32"), "paddings" | ||
| ) | ||
|
|
||
| t_op.builtin_options = mirror_pad_options.MirrorPad(MirrorPadMode.REFLECT) | ||
| t_op.tmp_inputs = [x, paddings_tensor] | ||
|
|
||
| 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
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.