-
Notifications
You must be signed in to change notification settings - Fork 961
Arm backend: Remove use of tosa_dim_order #18948
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9a64c73
Arm backend: Remove use of tosa_dim_order
AdrianLundell 5ac1d3c
Merge branch 'main' into change-1241864
mcremon-meta b772b64
Arm backend: Fix remove tosa_dim_order review comments
AdrianLundell bac92e5
Merge branch 'main' of https://github.com/pytorch/executorch into HEAD
AdrianLundell 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
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
137 changes: 137 additions & 0 deletions
137
backends/arm/_passes/normalize_delegate_io_layout_pass.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,137 @@ | ||
| # Copyright 2026 Arm Limited and/or its affiliates. | ||
| # | ||
| # 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 Any, Set, Type | ||
|
|
||
| import torch | ||
| from executorch.backends.arm._passes import ArmPass | ||
| from executorch.backends.arm._passes.arm_pass_utils import ( | ||
| create_node, | ||
| get_first_fake_tensor, | ||
| is_param_node, | ||
| ) | ||
| from executorch.exir import ExportedProgram | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
| from executorch.exir.pass_base import ExportPass, PassResult | ||
|
|
||
|
|
||
| class NormalizeDelegateIOLayoutPass(ArmPass): | ||
| """Adjust delegated boundary tensor shapes and insert permutes at I/O.""" | ||
|
|
||
| _passes_required_after: Set[Type[ExportPass]] = set() | ||
|
|
||
| def __init__(self, exported_program: ExportedProgram, *args, **kwargs) -> None: | ||
| super().__init__(*args, **kwargs) | ||
| self.exported_program = exported_program | ||
|
|
||
| @staticmethod | ||
| def _inverse_permutation(perm: tuple[int, ...]) -> tuple[int, ...]: | ||
| inverse = [0] * len(perm) | ||
| for idx, axis in enumerate(perm): | ||
| inverse[axis] = idx | ||
| return tuple(inverse) | ||
|
|
||
| @staticmethod | ||
| def _permute_shape(shape: torch.Size, perm: tuple[int, ...]) -> tuple[int, ...]: | ||
| return tuple(shape[axis] for axis in perm) | ||
|
|
||
| @staticmethod | ||
| def _is_identity_dim_order(dim_order: tuple[int, ...]) -> bool: | ||
| return dim_order == tuple(range(len(dim_order))) | ||
|
|
||
| def _normalize_input_layout(self, graph_module: torch.fx.GraphModule) -> bool: | ||
| modified = False | ||
| for node in graph_module.graph.nodes: | ||
| if node.op != "placeholder" or is_param_node(self.exported_program, node): | ||
| continue | ||
|
|
||
| input_fake = get_first_fake_tensor(node) | ||
| dim_order = input_fake.dim_order() | ||
| if self._is_identity_dim_order(dim_order): | ||
| continue | ||
|
|
||
| boundary_shape = self._permute_shape(input_fake.shape, dim_order) | ||
| node.meta["val"] = input_fake.reshape(boundary_shape) | ||
|
|
||
| transpose_perm = self._inverse_permutation(dim_order) | ||
| with graph_module.graph.inserting_after(node): | ||
| permute_node = create_node( | ||
| graph_module.graph, | ||
| exir_ops.edge.aten.permute_copy.default, | ||
| args=(node, list(transpose_perm)), | ||
| from_node=node, | ||
| ) | ||
| permute_node.meta["val"] = exir_ops.edge.aten.permute_copy.default( | ||
| node.meta["val"], list(transpose_perm) | ||
| ) | ||
|
|
||
| users = [user for user in node.users if user != permute_node] | ||
| for user in users: | ||
| user.replace_input_with(node, permute_node) | ||
|
|
||
| modified = True | ||
|
|
||
| return modified | ||
|
|
||
| def _rewrite_output_arg( | ||
| self, arg: Any, graph_module: torch.fx.GraphModule | ||
| ) -> tuple[Any, bool]: | ||
| if isinstance(arg, torch.fx.Node): | ||
| output_fake = get_first_fake_tensor(arg) | ||
| dim_order = output_fake.dim_order() | ||
|
Contributor
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. can't this change during the lowering?
Collaborator
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. It can, so it is important that this is the first pass run during the lowering! |
||
| if self._is_identity_dim_order(dim_order): | ||
| return arg, False | ||
|
|
||
| with graph_module.graph.inserting_after(arg): | ||
| permute_node = create_node( | ||
| graph_module.graph, | ||
| exir_ops.edge.aten.permute_copy.default, | ||
| args=(arg, list(dim_order)), | ||
| from_node=arg, | ||
| ) | ||
| permute_node.meta["val"] = exir_ops.edge.aten.permute_copy.default( | ||
| output_fake, list(dim_order) | ||
| ) | ||
|
|
||
| return permute_node, True | ||
|
|
||
| if isinstance(arg, tuple): | ||
| modified = False | ||
| rewritten = [] | ||
| for item in arg: | ||
| new_item, item_modified = self._rewrite_output_arg(item, graph_module) | ||
| rewritten.append(new_item) | ||
| modified = modified or item_modified | ||
| return tuple(rewritten), modified | ||
|
|
||
| if isinstance(arg, list): | ||
| modified = False | ||
| rewritten = [] | ||
| for item in arg: | ||
| new_item, item_modified = self._rewrite_output_arg(item, graph_module) | ||
| rewritten.append(new_item) | ||
| modified = modified or item_modified | ||
| return rewritten, modified | ||
|
|
||
| return arg, False | ||
|
|
||
| def _normalize_output_layout(self, graph_module: torch.fx.GraphModule) -> bool: | ||
| output_node = graph_module.graph.output_node() | ||
| rewritten_outputs, modified = self._rewrite_output_arg( | ||
| output_node.args[0], graph_module | ||
| ) | ||
| if modified: | ||
| output_node.args = (rewritten_outputs,) | ||
| return modified | ||
|
|
||
| def call(self, graph_module: torch.fx.GraphModule) -> PassResult: | ||
| modified = self._normalize_input_layout(graph_module) | ||
| modified = self._normalize_output_layout(graph_module) or modified | ||
|
|
||
| if modified: | ||
| graph_module.recompile() | ||
| graph_module = super().call(graph_module).graph_module | ||
|
|
||
| return PassResult(graph_module, modified) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Maybe a comment about that this needs to be first:ish and why?