-
Notifications
You must be signed in to change notification settings - Fork 698
NXP backend: Resolve limitations of uncertain tensor formats. #14576
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
MartinPavella
wants to merge
7
commits into
pytorch:main
Choose a base branch
from
nxp-upstream:upstream/main-nxp/EIEX-392-resolve-limitations-of-uncertain-tensor-formats
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
7 commits
Select commit
Hold shift + click to select a range
9e78e44
NXP backend: Store inferred node format in the `node.meta`.
MartinPavella 8618fbc
NXP backend: Update pass which removes GetItem nodes, to preserve the…
MartinPavella 0288f06
NXP backend: Perform node format inference before partitioning.
MartinPavella 5c1d757
NXP backend: Improve `cat` delegation by using inferred node formats.
MartinPavella a3abfc2
NXP backend: Improve `constant_pad_nd` delegation by using inferred n…
MartinPavella aa8a8b9
NXP backend: Do not infer format for unknown nodes.
MartinPavella 768c489
NXP backend: Fix batch norm tests.
MartinPavella 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # 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 torch | ||
|
|
||
| from executorch.backends.nxp.backend.node_format_inference import ( | ||
| NodeFormat, | ||
| NXP_NODE_FORMAT, | ||
| ) | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
| from executorch.exir.pass_base import ExportPass, PassResult | ||
|
|
||
|
|
||
| class RemoveGetItemPass(ExportPass): | ||
| """ | ||
| This remove item is used to remove getitem operator for max_pool2d_with_indices.default operator, and replace it with a single operator, | ||
| that extracts the first output. More specifically, we are only getting the first output from aten::maxpool2d operator. | ||
| Before Pass: | ||
| MaxPool2d ---> GetItem[max_values, max_indexes] | ||
| After Pass: | ||
| MaxPool2d -> max_values | ||
| """ | ||
|
|
||
| def call(self, graph_module: torch.fx.GraphModule): | ||
| module = graph_module | ||
| for node in module.graph.nodes: | ||
| if node.op == "call_function": | ||
| if ( | ||
| node.target.__name__ == "aten.max_pool2d_with_indices.default" | ||
| or node.target.__name__ == "aten.max.dim" | ||
| ): | ||
| users = list(node.users.keys()) | ||
|
|
||
| if len(users) != 1: | ||
| if len(users) == 2 and node.target.__name__ == "aten.max.dim": | ||
| # Two users is allowed for max.dim. For that case, | ||
| # rather than removing the getitem node in this | ||
| # pass, we handle the getitem nodes in the op's | ||
| # visitor when serializing | ||
| continue | ||
| else: | ||
| raise AssertionError( | ||
| f"Invalid number of users for {node.target.__name__}: {len(users)}" | ||
| ) | ||
|
|
||
| getitem_node = list(node.users.keys())[0] | ||
|
|
||
| if getitem_node.target.__name__ != "getitem": | ||
| raise AssertionError( | ||
| f"Expected max node's user to be getitem, got {getitem_node.target.__name__}" | ||
| ) | ||
|
|
||
| getitem_index = getitem_node.args[1] | ||
|
|
||
| with module.graph.inserting_before(node): | ||
| if ( | ||
| node.target.__name__ | ||
| == "aten.max_pool2d_with_indices.default" | ||
| ): | ||
| if getitem_index != 0: | ||
| raise AssertionError( | ||
| f"Expected second argument of getitem node for {node.target.__name__} to be 0, got " | ||
| f"{getitem_index}. XNNPACK delegate currently only supports getting just the max " | ||
| "values from the op but not getting the corresponding indices." | ||
| ) | ||
| new_max_wd = module.graph.create_node( | ||
| "call_function", | ||
| exir_ops.edge.aten.max_pool2d.default, | ||
| args=node.args, | ||
| kwargs=node.kwargs, | ||
| ) | ||
|
|
||
| else: | ||
| if getitem_index != 0: | ||
| raise AssertionError( | ||
| f"Expected second argument of getitem node for {node.target.__name__} to be 0, got " | ||
| f"{getitem_index}. XNNPACK delegate currently only supports getting just the max " | ||
| "values or getting both the max values and their corresponding indices from the " | ||
| "op, but not getting the indices alone." | ||
| ) | ||
| new_max_wd = module.graph.create_node( | ||
| "call_function", | ||
| exir_ops.edge.aten.amax.default, | ||
| args=node.args, | ||
| kwargs=node.kwargs, | ||
| ) | ||
|
|
||
| # MODIFIED PART START | ||
| # Make sure to preserve the inferred node format. | ||
| new_max_wd.meta[NXP_NODE_FORMAT] = node.meta.get( | ||
| NXP_NODE_FORMAT, NodeFormat.NONE | ||
| ) | ||
| # MODIFIED PART END | ||
|
|
||
| getitem_node.replace_all_uses_with(new_max_wd) | ||
|
|
||
| module.graph.erase_node(getitem_node) | ||
| module.graph.erase_node(node) | ||
|
|
||
| graph_module.recompile() | ||
| # Propagate metadata and retrace module | ||
| graph_module = super().call(graph_module).graph_module | ||
|
|
||
| return PassResult(graph_module, True) |
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
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.
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.
Can you add neutron_backend build to this https://github.com/pytorch/executorch/blob/main/.ci/scripts/unittest-buck2.sh. Thats the one that was failing last time. This way we get signal on the pr itself