Skip to content

Commit

Permalink
[ONNX] Support importing Conv with missing attributes (apache#7899)
Browse files Browse the repository at this point in the history
* [ONNX] Support importing Conv with missing attributes

* fix removal of attributes ONLY when they are default and for autopad

* move comment to the right place
  • Loading branch information
Matthew Brookhart authored and Trevor Morris committed May 6, 2021
1 parent aa20483 commit f15901d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
21 changes: 16 additions & 5 deletions python/tvm/relay/frontend/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ def autopad(data, strides, kernel_shape, dilations, ndim, pad_type="constant", d
),
dtype="int64",
)
shape = _op.strided_slice(shape_of(data, dtype="int64"), [2], [ndim])
# get input shape
shape = _op.strided_slice(shape_of(data, dtype="int64"), [2], [ndim])

# set up integer constants
zero = _op.const(0, dtype="int64")
Expand Down Expand Up @@ -410,12 +410,24 @@ def _impl_v1(cls, inputs, attr, params):
data = inputs[0]
input_shape = infer_shape(data)
ndim = len(input_shape)

kernel_type = infer_type(inputs[1])
kernel_shapes = [get_const_tuple(kernel_type.checked_type.shape)]
if "kernel_shape" not in attr:
attr["kernel_shape"] = kernel_shapes[0][2:]

if "auto_pad" in attr:
attr["auto_pad"] = attr["auto_pad"].decode("utf-8")
if attr["auto_pad"] in ("SAME_UPPER", "SAME_LOWER"):
# Warning: Convolution does not yet support dynamic shapes,
# one will need to run dynamic_to_static on this model after import
data = autopad(data, attr["strides"], attr["kernel_shape"], attr["dilations"], ndim)
data = autopad(
data,
attr.get("strides", [1] * (ndim - 2)),
attr["kernel_shape"],
attr.get("dilations", [1] * (ndim - 2)),
ndim,
)
elif attr["auto_pad"] == "VALID":
attr["pads"] = tuple([0 for i in range(ndim - 2)])
elif attr["auto_pad"] == "NOTSET":
Expand All @@ -424,7 +436,6 @@ def _impl_v1(cls, inputs, attr, params):
msg = 'Value {} in attribute "auto_pad" of operator Conv is invalid.'
raise tvm.error.OpAttributeInvalid(msg.format(attr["auto_pad"]))
attr.pop("auto_pad")

out = AttrCvt(
op_name=dimension_picker("conv"),
transforms={
Expand Down Expand Up @@ -469,9 +480,9 @@ def _impl_v1(cls, inputs, attr, params):
# one will need to run dynamic_to_static on this model after import
data = autopad(
data,
attr["strides"],
attr.get("strides", [1] * (ndim - 2)),
attr["kernel_shape"],
attr["dilations"],
attr.get("dilations", [1] * (ndim - 2)),
ndim,
deconv=True,
)
Expand Down
12 changes: 8 additions & 4 deletions tests/python/frontend/onnx/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -2390,16 +2390,20 @@ def verify_conv(
# groups=1
)
elif padding is None:
## autopadding with unset default attributes
kwargs = {}
if not all([s == 1 for s in strides]):
kwargs["strides"] = strides
if not all([d == 1 for d in dilations]):
kwargs["dilations"] = dilations

node = helper.make_node(
"Conv",
inputs=["x", "W"],
outputs=["y"],
kernel_shape=kernel_shape,
# Default values for other attributes:
strides=strides,
dilations=dilations,
# groups=1
auto_pad=auto_pad,
**kwargs,
)
else:
node = helper.make_node(
Expand Down

0 comments on commit f15901d

Please sign in to comment.