Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions coremltools/converters/mil/frontend/torch/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +220 to +223
Copy link
Contributor Author

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.

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)):
Expand Down Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I fail to add test for conv_transposeXd because it asks for output_size as an input in the computational graph which I don't know how to solve. Though, the function should support the operation.

@register_torch_op(torch_alias=["convolution", "conv1d", "conv2d", "conv3d"])
def _convolution(context, node):
inputs = _get_inputs(context, node)

Expand All @@ -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:
Expand All @@ -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]
Expand Down
Loading