-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Normalize single element convolution arg lists #22778
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
giuliocorradi
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
giuliocorradi:vulkan-conv-valid-padding
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.
+143
−0
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # 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. | ||
|
|
||
| # pyre-strict | ||
|
|
||
| from typing import Dict, List, Tuple | ||
|
|
||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
| from executorch.exir.pass_base import ExportPass, NodeMetadata, ProxyValue | ||
|
|
||
| from torch.fx.node import Argument | ||
|
|
||
|
|
||
| class NormalizeConvolutionArgs(ExportPass): | ||
| """ | ||
| Broadcasts single element stride/padding/dilation/output_padding lists of a | ||
| convolution to the number of spatial dimensions. | ||
|
|
||
| ATen allows these arguments to be given as a single value that applies to | ||
| every spatial dimension, and torch.nn does exactly that for | ||
| `padding="valid"`, which exports as `padding=[0]` rather than `[0, 0]`. | ||
| The Vulkan convolution reads them as fixed width vectors | ||
| (`make_ivec2_from_list` -> `make_ivec2`, which requires exactly 2 elements), | ||
| so a 2D convolution written that way aborts at the first inference: | ||
|
|
||
| make_ivec2 ... (ints.size() == 2) is false! | ||
|
|
||
| Normalizing here keeps the graph ATen compliant and leaves the runtime | ||
| unchanged. | ||
| """ | ||
|
|
||
| # arg index -> name, for the list arguments of aten.convolution | ||
| _list_arg_indices: Dict[int, str] = { | ||
| 3: "stride", | ||
| 4: "padding", | ||
| 5: "dilation", | ||
| 7: "output_padding", | ||
| } | ||
|
|
||
| def call_operator( | ||
| self, | ||
| op, # pyre-ignore | ||
| args: Tuple[Argument, ...], | ||
| kwargs: Dict[str, Argument], | ||
| meta: NodeMetadata, | ||
| ) -> ProxyValue: | ||
| if op != exir_ops.edge.aten.convolution.default: | ||
| return super().call_operator(op, args, kwargs, meta) | ||
|
|
||
| # weight is (out_channels, in_channels / groups, *kernel_size) | ||
| weight = args[1] | ||
| # pyre-ignore[16] | ||
| spatial_dims = len(weight.node.meta["val"].shape) - 2 | ||
| if spatial_dims < 2: | ||
| return super().call_operator(op, args, kwargs, meta) | ||
|
|
||
| new_args: List[Argument] = list(args) | ||
| modified = False | ||
| for idx in self._list_arg_indices: | ||
| if idx >= len(new_args): | ||
| continue | ||
| value = new_args[idx] | ||
| if isinstance(value, (list, tuple)) and len(value) == 1: | ||
| new_args[idx] = [value[0]] * spatial_dims | ||
| modified = True | ||
|
|
||
| if not modified: | ||
| return super().call_operator(op, args, kwargs, meta) | ||
|
|
||
| return super().call_operator(op, tuple(new_args), kwargs, meta) |
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.
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.
This lengthy docstring repeats the implementation rationale, runtime assertion, and downstream model context rather than concisely describing the behavior under test; the assertions below already make the 2-D broadcast and 1-D no-op expectations clear. Keeping the same explanation in both the pass and its test increases maintenance burden, so reduce this to a short behavioral description.
AGENTS.md reference: AGENTS.md:L46-L50
Useful? React with 👍 / 👎.