-
Notifications
You must be signed in to change notification settings - Fork 1.1k
NXP backend: Add pass for adding batch dimension for 3D input avgpool… #21572
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
roman-janik-nxp
merged 1 commit into
pytorch:main
from
nxp-upstream:feature/nxg11066/EIEX-984-Add-pass-for-adding-batch-dim-for-avgpool2d-and-maxpool2d-with-3D-input
Aug 13, 2026
Merged
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
116 changes: 116 additions & 0 deletions
116
backends/nxp/aten_passes/add_batch_size_for_3d_input_pool_2d_ops.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,116 @@ | ||
| # 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.edge_helper import input_rank | ||
|
|
||
| from torch.fx import GraphModule, Node | ||
| from torch.fx.passes.infra.pass_base import PassBase, PassResult | ||
|
|
||
|
|
||
| class AddBatchSizeFor3DInputPool2DOps(PassBase): | ||
| """Adds batch size dimension for aten.adaptive_avg_pool2d.default, aten.avg_pool2d.default | ||
| and aten.max_pool2d.default ops with 3D input, as the Neutron Converter is unable to convert these ops with 3D input. | ||
|
|
||
| │ | ||
| ┌──────▼──────┐ | ||
| │ reshape │ | ||
| │ (add batch) │ | ||
| └──────┬──────┘ | ||
| │ │ | ||
| ┌──────▼───────┐ ┌──────▼───────┐ | ||
| │ adaptive │ │ adaptive │ | ||
| │ _avg_pool2d/ │ replace with │ _avg_pool2d/ │ | ||
| │ avg_pool2d/ │ ──────────────► │ avg_pool2d/ │ | ||
| │ max_pool2d │ │ max_pool2d │ | ||
| │ 3D input │ │ 4D input │ | ||
| │ (C,H,W) │ │ (1,C H,W) │ | ||
| └──────┬───────┘ └──────┬───────┘ | ||
| │ │ | ||
| ┌──────▼───────┐ | ||
| │ reshape │ | ||
| │(remove batch)│ | ||
| └──────┬───────┘ | ||
| │ | ||
| """ | ||
|
|
||
| module: GraphModule | ||
|
|
||
| def _create_reshape_node(self, pool_node: Node, *args) -> Node: | ||
| reshape_node = self.module.graph.call_function( | ||
| torch.ops.aten.reshape.default, | ||
| args=args, | ||
| kwargs={}, | ||
| ) | ||
| reshape_node.meta["source_fn_stack"] = pool_node.meta.get("source_fn_stack", []) | ||
| input_node = args[0] | ||
| if input_node == pool_node: | ||
| # insert after pool_node | ||
| reshape_node.meta["val"] = input_node.meta["val"].squeeze(0) | ||
| else: | ||
| # insert before pool_node | ||
| reshape_node.meta["val"] = input_node.meta["val"].unsqueeze(0) | ||
| return reshape_node | ||
|
|
||
| def call(self, module: GraphModule) -> PassResult: | ||
| self.module = module | ||
|
|
||
| def _is_3d_pool(node_: Node) -> bool: | ||
| if node_.op != "call_function": | ||
| return False | ||
|
|
||
| if node_.target not in [ | ||
|
roman-janik-nxp marked this conversation as resolved.
|
||
| torch.ops.aten.adaptive_avg_pool2d.default, | ||
| torch.ops.aten.avg_pool2d.default, | ||
| torch.ops.aten.max_pool2d.default, | ||
| ]: | ||
| return False | ||
|
|
||
| # Check if input is 3D (C, H, W) | ||
| rank = input_rank(node_, 0) | ||
| return rank == 3 | ||
|
|
||
| made_changes = False | ||
|
|
||
| for node in module.graph.nodes: | ||
| if not _is_3d_pool(node): | ||
| continue | ||
|
|
||
| pool_node = node | ||
| input_node = pool_node.args[0] | ||
|
|
||
| # Get input shape (C, H, W) | ||
| input_shape = input_node.meta["val"].shape | ||
|
|
||
| # Get output shape (3D) before we modify metadata | ||
| output_shape_3d = pool_node.meta["val"].shape | ||
|
|
||
| # Insert reshape to add batch dimension (1, C, H, W) | ||
| with module.graph.inserting_before(pool_node): | ||
| reshape_add_batch = self._create_reshape_node( | ||
| pool_node, input_node, [1, *input_shape] | ||
| ) | ||
|
|
||
| # Update pool_node to use 4D input | ||
| pool_node.args = (reshape_add_batch, *pool_node.args[1:]) | ||
|
|
||
| # Update pool_node output metadata to 4D | ||
| pool_node.meta["val"] = pool_node.meta["val"].unsqueeze(0) | ||
|
|
||
| # Insert reshape to remove batch dimension AFTER pool_node | ||
| with module.graph.inserting_after(pool_node): | ||
|
roman-janik-nxp marked this conversation as resolved.
|
||
| reshape_remove_batch = self._create_reshape_node( | ||
| pool_node, pool_node, list(output_shape_3d) | ||
| ) | ||
|
|
||
| # Replace all uses of pool_node with reshape_remove_batch (except reshape_remove_batch itself) | ||
| pool_node.replace_all_uses_with(reshape_remove_batch) | ||
| # Restore the connection: reshape_remove_batch should use pool_node as input | ||
| reshape_remove_batch.update_arg(0, pool_node) | ||
|
|
||
| made_changes = True | ||
|
roman-janik-nxp marked this conversation as resolved.
|
||
|
|
||
| return PassResult(module, made_changes) | ||
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
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.
Please rename the file to use all lowercase characters, afaik we don't use capitalized letters in file names anywhere else in the NXP backend, plus it violates the PEP8 convetion, ie.
add_batch_size_for_3D_avgpool2D_maxpool2D.py->add_batch_size_for_3d_avgpool2d_maxpool2d.pySame goes for the test file.
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.
Great catch, thanks.
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: the current name
test_add_batch_size_for_3d_input_pool_2d_ops.pyis imo too long, too complicated and hard to understand. I suggest usingtest_add_explicit_batch_for_pools.py, and for the pass accordingly.