Skip to content
Merged
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
17 changes: 11 additions & 6 deletions python/tvm/contrib/target/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def convert_attributes(cls, attrs):
"pads": attrs.get_int_tuple("padding"),
"strides": attrs.get_int_tuple("strides"),
"kernel_shape": attrs.get_int_tuple("pool_size"),
"ceil_mode": 1 if attrs.ceil_mode else 0,
}


Expand Down Expand Up @@ -330,7 +331,10 @@ def convert_attributes(cls, attrs):
after.append(axis_pads[1])
pads = before + after
pads = numpy.asarray(pads, dtype=pads[0].dtype)
return {"pads": pads, "mode": attrs.get_str("pad_mode"), "constant_value": attrs.pad_value}
return {
"pads": pads,
"mode": attrs.get_str("pad_mode"),
}

@classmethod
def convert(cls, node_entry, model_container, node_dict):
Expand All @@ -341,16 +345,17 @@ def convert(cls, node_entry, model_container, node_dict):
attrs = cls.convert_attributes(node_entry["relay_node"].attrs)

name = node_entry["name"]
data = numpy.asarray(attrs["pads"], dtype=attrs["pads"][0].dtype).astype(numpy.int64)
value = numpy.dtype(node_entry["types"][0].dtype).type(attrs["constant_value"])
pad_data = numpy.asarray(attrs["pads"], dtype=attrs["pads"][0].dtype).astype(numpy.int64)

input_names = [
node_entry["input_names"][0],
add_input(data, name, "pads", model_container),
add_input(value, name, "value", model_container),
add_input(pad_data, name, "pads", model_container),
node_entry["input_names"][1],
]

node = onnx.helper.make_node(cls.__name__, input_names, node_entry["output_names"])
node = onnx.helper.make_node(
cls.__name__, input_names, node_entry["output_names"], mode=attrs["mode"]
)
model_container.add_nodes([node])


Expand Down
17 changes: 10 additions & 7 deletions tests/python/contrib/test_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import tvm
from tvm import relay
from tvm.contrib.target.onnx import to_onnx
from tvm.relay.testing import run_infer_type


def func_to_onnx(func, name):
Expand Down Expand Up @@ -270,14 +271,16 @@ def verify_batch_norm(axis=1):


def test_pad():
"""Pad unit test."""

def verify_pad():
for dtype in ["float16", "float32"]:
dshape = (4, 10, 7, 7)
x = relay.var("x", shape=dshape, dtype=dtype)
y = relay.nn.pad(x, ((1, 1), (2, 2), (3, 3), (4, 4)))
func = relay.Function([x], y)
x_data = np.random.uniform(size=dshape).astype(dtype)
verify_results(func, [x_data], "test_pad", rtol=1e-5, atol=1e-5)
dshape = (4, 10, 7, 7)
x = relay.var("x", shape=dshape, dtype="int32")
y = relay.nn.pad(x, ((1, 1), (2, 2), (3, 3), (4, 4)))
func = relay.Function([x], y)
func = run_infer_type(func)
x_data = np.random.randint(low=-255, high=255, size=dshape).astype(np.int32)
verify_results(func, [x_data], "test_pad", rtol=1e-5, atol=1e-5)

verify_pad()

Expand Down