-
Notifications
You must be signed in to change notification settings - Fork 657
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
Add support for Torch conv aliases #2011
Open
alealv
wants to merge
3
commits into
apple:main
Choose a base branch
from
alealv:add_conv1d_op
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
3 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -217,7 +217,10 @@ def get_bindings(alist) -> List[Any]: | |
|
||
for i in alist: | ||
if isinstance(i, str): | ||
results.append(context[i]) | ||
try: | ||
results.append(context[i]) | ||
except ValueError: | ||
results.append(None) | ||
elif isinstance(i, (list, tuple)) and all(isinstance(j, int) for j in i): | ||
results.append(mb.const(val=i)) | ||
elif isinstance(i, (list, tuple)): | ||
|
@@ -962,7 +965,8 @@ def linear(context, node): | |
context.add(res, torch_name=node.name) | ||
|
||
|
||
@register_torch_op(torch_alias=["conv2d", "convolution"]) | ||
# NOTE: This function is also an alias of: ["conv_transpose1d", "conv_transpose2d", "conv_transpose3d"] but we lack tests for those | ||
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. Unfortunately, I fail to add test for |
||
@register_torch_op(torch_alias=["convolution", "conv1d", "conv2d", "conv3d"]) | ||
def _convolution(context, node): | ||
inputs = _get_inputs(context, node) | ||
|
||
|
@@ -980,11 +984,25 @@ def _convolution(context, node): | |
# we require a (2 * n)-tuple, where n is the number of spatial dimensions, start and end for each spatial dimension | ||
pad = inputs[4].val | ||
|
||
if len(weight.shape) in (3, 4): | ||
# 1D and 2D: Need to explicitly state L-R, T-B pad | ||
if type(pad) == str: | ||
if pad == "same": | ||
pad = 1 | ||
elif pad == "valid": | ||
pad = 0 | ||
else: | ||
raise ValueError(f"Unkown padding string value: '{pad}'") | ||
|
||
if len(weight.shape) == 3: | ||
# 1D padding: needs explicitly state L-R for x dim | ||
pad = _np.repeat(pad, 2) | ||
elif len(weight.shape) == 4: | ||
# 2D padding: needs explicitly state L-R for x,y dims | ||
if type(pad) == int: | ||
pad = _np.repeat(pad, 4) | ||
elif len(pad) == 2: | ||
pad = _np.repeat(pad, 2) | ||
elif len(weight.shape) == 5: | ||
# 3D: Need to explicitly state F-Bk, L-R, T-B pad | ||
# 3D padding: needs explicitly state L-R for x,y,z dims | ||
if type(pad) == int: | ||
pad = _np.repeat(pad, 6) | ||
elif len(pad) == 3: | ||
|
@@ -1000,6 +1018,11 @@ def _convolution(context, node): | |
transposed = inputs[6].val | ||
out_pad = inputs[7].val | ||
group = inputs[8] | ||
elif len(inputs) == 8: | ||
transposed = True | ||
out_pad = inputs[5].val | ||
dilations = inputs[7] | ||
group = inputs[6] | ||
elif len(inputs) == 7: | ||
transposed = False | ||
group = inputs[6] | ||
|
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 was the magic trick to continue when
bias
is None, because is an optional parameter.