-
Notifications
You must be signed in to change notification settings - Fork 665
NXP backend: Resolve limitations of uncertain tensor formats #13942
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
6
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
6 commits
Select commit
Hold shift + click to select a range
d299bae
NXP backend: Store inferred node format in the `node.meta`.
MartinPavella df3a601
NXP backend: Update pass which removes GetItem nodes, to preserve the…
MartinPavella d7558e9
NXP backend: Perform node format inference before partitioning.
MartinPavella 904d36b
NXP backend: Improve `cat` delegation by using inferred node formats.
MartinPavella 8255593
NXP backend: Improve `constant_pad_nd` delegation by using inferred n…
MartinPavella 403f892
NXP backend: Do not infer format for unknown nodes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# 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 {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 {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
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,23 @@ | ||
# 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 enum import Enum | ||
|
||
# Key into the `meta` attribute of nodes, which is mapped to their inferred node format. | ||
NXP_NODE_FORMAT = "nxp_node_format" | ||
|
||
|
||
class NodeFormat(Enum): | ||
# Node's output in NCHW format | ||
CHANNELS_FIRST = 0 | ||
|
||
# Node's output format has no meaning | ||
FORMATLESS = 1 | ||
|
||
# Format has not been identified | ||
NONE = 2 | ||
|
||
def is_channels_first(self) -> bool: | ||
return self == NodeFormat.CHANNELS_FIRST |
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.
Why do we handle the aten.max.dim too? Is it a loftover from original pass?
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.
Yes, it was in the original file. I wanted to make as few changes as possible, as it is not the main focus of this PR.